예제 #1
0
  void test3(Window owner, Window child1, Window child2) {
    System.out.println("* * * STAGE 3 * * *\nWidow owner: " + owner);

    owner.setFocusableWindowState(true);
    owner.setVisible(true);

    child1.setFocusableWindowState(false);
    child1.setVisible(true);

    child2.setFocusableWindowState(true);
    child2.add(button);
    child2.setVisible(true);

    Util.waitTillShown(child2);

    Util.clickOnComp(button, robot);
    System.err.println(
        "focus owner: " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
    if (button != KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
      throw new RuntimeException("Test failed.");
    }
    child1.dispose();
    child2.dispose();
    owner.dispose();
  }
예제 #2
0
 @Override
 public void actionPerformed(ActionEvent evt) {
   switch (command) {
     case TAB_OUT_FORWARD:
       KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
       break;
     case TAB_OUT_BACK:
       KeyboardFocusManager.getCurrentKeyboardFocusManager().focusPreviousComponent();
       break;
     case EDIT_PLUGIN:
       int[] rows = table.getSelectedRows();
       Object[] state = new Object[rows.length];
       for (int i = 0; i < rows.length; i++) {
         state[i] = pluginModel.getValueAt(rows[i], 0);
       }
       for (int i = 0; i < rows.length; i++) {
         pluginModel.setValueAt(state[i].equals(Boolean.FALSE), rows[i], 0);
       }
       break;
     case CLOSE_PLUGIN_MANAGER:
       window.ok();
       break;
     default:
       throw new InternalError();
   }
 }
예제 #3
0
  /**
   * Creates a swing applet instance.
   *
   * <p>This constructor sets the component's locale property to the value returned by <code>
   * JComponent.getDefaultLocale</code>.
   *
   * @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true.
   * @see java.awt.GraphicsEnvironment#isHeadless
   * @see JComponent#getDefaultLocale
   */
  public JApplet() throws HeadlessException {
    super();
    // Check the timerQ and restart if necessary.
    TimerQueue q = TimerQueue.sharedInstance();
    if (q != null) {
      synchronized (q) {
        if (!q.running) q.start();
      }
    }

    /* Workaround for bug 4155072.  The shared double buffer image
     * may hang on to a reference to this applet; unfortunately
     * Image.getGraphics() will continue to call JApplet.getForeground()
     * and getBackground() even after this applet has been destroyed.
     * So we ensure that these properties are non-null here.
     */
    setForeground(Color.black);
    setBackground(Color.white);

    setLocale(JComponent.getDefaultLocale());
    setLayout(new BorderLayout());
    setRootPane(createRootPane());
    setRootPaneCheckingEnabled(true);

    // This code should be changed after the RFE 4719336 is resolved
    // to not make the applet a FocusCycleRoot, but set it's
    // FocusTraversalPolicy only.
    setFocusCycleRoot(true);
    setFocusTraversalPolicy(
        KeyboardFocusManager.getCurrentKeyboardFocusManager().getDefaultFocusTraversalPolicy());

    enableEvents(AWTEvent.KEY_EVENT_MASK);
  }
예제 #4
0
  public CommonDialog() {
    super(GuiUtils.getActiveFrame());
    lastFocusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    setResizable(false);
    setModal(true);

    keyManager =
        new DefaultKeyboardFocusManager() {
          public boolean dispatchKeyEvent(KeyEvent e) {
            if (e.getID() == KeyEvent.KEY_PRESSED) {
              int keyCode = e.getKeyCode();
              Object source = e.getSource();
              if (keyCode == KeyEvent.VK_ESCAPE || keyCode == KeyEvent.VK_ENTER) {
                if (source instanceof JTextArea && keyCode == KeyEvent.VK_ENTER) {
                  return false;
                }
                JComboBox comboBox =
                    source instanceof Component ? getParentComboBox((Component) source) : null;
                if (comboBox != null && comboBox.isPopupVisible()) {
                  return false;
                } else if (isActive()) {
                  if (keyCode == KeyEvent.VK_ESCAPE) {
                    onCancel();
                    return true;
                  } else {
                    onOk();
                  }
                }
              }
            }
            return false;
          }
        };
  }
예제 #5
0
파일: JAWTDemo.java 프로젝트: Noviv/lwjgl3
  public static void main(String[] args) {
    if (Platform.get() != Platform.WINDOWS)
      throw new UnsupportedOperationException("This demo can only run on Windows.");

    LWJGLCanvas canvas = new LWJGLCanvas();
    canvas.setSize(640, 480);

    JFrame frame = new JFrame("JAWT Demo");

    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.addWindowListener(
        new WindowAdapter() {
          @Override
          public void windowClosed(WindowEvent e) {
            canvas.destroy();
          }
        });

    KeyboardFocusManager.getCurrentKeyboardFocusManager()
        .addKeyEventDispatcher(
            e -> {
              if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                frame.dispose();
                return true;
              }

              return false;
            });

    frame.setLayout(new BorderLayout());
    frame.add(canvas, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);
  }
예제 #6
0
 public void actionPerformed(ActionEvent e) {
   KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
   Container container = kfm.getCurrentFocusCycleRoot();
   FocusTraversalPolicy policy = container.getFocusTraversalPolicy();
   if (null == policy) policy = kfm.getDefaultFocusTraversalPolicy();
   Component next =
       moveDown
           ? policy.getComponentAfter(container, PaletteGroupHeader.this)
           : policy.getComponentBefore(container, PaletteGroupHeader.this);
   if (null != next && next instanceof PaletteComponentList) {
     final PaletteComponentList list = (PaletteComponentList) next;
     if (list.getModel().getSize() != 0) {
       list.takeFocusFrom(PaletteGroupHeader.this, list == myComponentList ? 0 : -1);
       return;
     } else {
       next =
           moveDown
               ? policy.getComponentAfter(container, next)
               : policy.getComponentBefore(container, next);
     }
   }
   if (null != next && next instanceof PaletteGroupHeader) {
     next.requestFocus();
   }
 }
  private boolean paintPlaceholderText(Graphics2D g) {
    CharSequence hintText = myEditor.getPlaceholder();
    EditorComponentImpl editorComponent = myEditor.getContentComponent();
    if (myDocument.getTextLength() > 0
        || hintText == null
        || hintText.length() == 0
        || KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == editorComponent
            && !myEditor.getShowPlaceholderWhenFocused()) {
      return false;
    }

    hintText =
        SwingUtilities.layoutCompoundLabel(
            g.getFontMetrics(),
            hintText.toString(),
            null,
            0,
            0,
            0,
            0,
            editorComponent.getBounds(),
            new Rectangle(),
            new Rectangle(),
            0);
    g.setColor(myEditor.getFoldingModel().getPlaceholderAttributes().getForegroundColor());
    g.setFont(myEditor.getColorsScheme().getFont(EditorFontType.PLAIN));
    g.drawString(hintText.toString(), 0, myView.getAscent());
    return true;
  }
  public static boolean isFocused(@Nullable Component[] components) {
    if (components == null) return false;

    Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();

    if (owner == null) return false;

    Window wnd;
    if (owner instanceof Window) {
      wnd = (Window) owner;
    } else {
      wnd = SwingUtilities.getWindowAncestor(owner);
    }

    for (Component each : components) {
      if (each != null && SwingUtilities.isDescendingFrom(owner, each)) {
        Window eachWindow =
            each instanceof Window ? (Window) each : SwingUtilities.getWindowAncestor(each);
        if (eachWindow == wnd) {
          return true;
        }
      }
    }

    return false;
  }
 static boolean shouldOpenDialog(Collection hints) {
   if (hints.contains(DiffTool.HINT_SHOW_MODAL_DIALOG)) return true;
   if (hints.contains(DiffTool.HINT_SHOW_NOT_MODAL_DIALOG)) return true;
   if (hints.contains(DiffTool.HINT_SHOW_FRAME)) return false;
   return KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow()
       instanceof JDialog;
 }
  static void subscribeTo(NavBarPanel panel) {
    if (panel.getClientProperty(LISTENER) != null) {
      unsubscribeFrom(panel);
    }

    final NavBarListener listener = new NavBarListener(panel);
    final Project project = panel.getProject();
    panel.putClientProperty(LISTENER, listener);
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(listener);
    FileStatusManager.getInstance(project).addFileStatusListener(listener);
    PsiManager.getInstance(project).addPsiTreeChangeListener(listener);
    WolfTheProblemSolver.getInstance(project).addProblemListener(listener);
    ActionManager.getInstance().addAnActionListener(listener);

    final MessageBusConnection connection = project.getMessageBus().connect();
    connection.subscribe(ProjectTopics.PROJECT_ROOTS, listener);
    connection.subscribe(NavBarModelListener.NAV_BAR, listener);
    connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, listener);
    panel.putClientProperty(BUS, connection);
    panel.addKeyListener(listener);

    if (panel.isInFloatingMode()) {
      final Window window = SwingUtilities.windowForComponent(panel);
      if (window != null) {
        window.addWindowFocusListener(listener);
      }
    }
  }
    public void actionPerformed(ActionEvent e) {
      int selIndexBefore = getSelectedIndex();
      myDefaultAction.actionPerformed(e);
      int selIndexCurrent = getSelectedIndex();
      if (selIndexBefore != selIndexCurrent) {
        return;
      }
      if (myFocusNext && selIndexCurrent == 0) {
        return;
      }

      KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
      Container container = kfm.getCurrentFocusCycleRoot();
      FocusTraversalPolicy policy = container.getFocusTraversalPolicy();
      if (policy == null) {
        policy = kfm.getDefaultFocusTraversalPolicy();
      }
      Component next =
          myFocusNext
              ? policy.getComponentAfter(container, PaletteItemsComponent.this)
              : policy.getComponentBefore(container, PaletteItemsComponent.this);
      if (next instanceof PaletteGroupComponent) {
        clearSelection();
        next.requestFocus();
        ((PaletteGroupComponent) next).scrollRectToVisible(next.getBounds());
      }
    }
  public boolean editCellAt(int index, EventObject e) {
    if (editor != null && !editor.stopCellEditing()) return false;

    if (index < 0 || index >= getModel().getSize()) return false;

    if (!isCellEditable(index)) return false;

    if (editorRemover == null) {
      KeyboardFocusManager fm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
      editorRemover = new CellEditorRemover(fm);
      fm.addPropertyChangeListener("permanentFocusOwner", editorRemover); // NOI18N
    }

    if (editor != null && editor.isCellEditable(e)) {
      editorComp = prepareEditor(index);
      if (editorComp == null) {
        removeEditor();
        return false;
      }
      editorComp.setBounds(getCellBounds(index, index));
      add(editorComp);
      editorComp.validate();

      editingIndex = index;
      editor.addCellEditorListener(this);

      return true;
    }
    return false;
  }
예제 #13
0
 @Nullable
 private static Editor getEditorFromFocus() {
   final Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
   if (c instanceof EditorComponentImpl) {
     return ((EditorComponentImpl) c).getEditor();
   }
   return null;
 }
예제 #14
0
  private void clearAWTFocus() {
    // This is needed so that the last focused component AWT remembers is NOT our Canvas
    WindowsDisplay.this.parent.setFocusable(false);
    WindowsDisplay.this.parent.setFocusable(true);

    // Clear AWT focus owner
    KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
  }
 static {
   if (System.getProperty("is.popup.test") != null
       || (ApplicationManagerEx.getApplicationEx() != null
           && ApplicationManagerEx.getApplicationEx().isUnitTestMode())) {
     Toolkit.getDefaultToolkit().addAWTEventListener(ourInstance, MouseEvent.MOUSE_PRESSED);
     KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(ourInstance);
   }
 }
예제 #16
0
  /**
   * Returns whether the given component is "actively" shown in screen, that is, it or any of its
   * ancestors is focused.
   *
   * @param aComponent the component to determine whether it is actively shown on screen, may be
   *     <code>null</code>.
   * @return <code>true</code> if the given component is actively shown, <code>false</code>
   *     otherwise.
   */
  public static final boolean isActivelyShown(final Component aComponent) {
    final KeyboardFocusManager kbdFocusManager =
        KeyboardFocusManager.getCurrentKeyboardFocusManager();
    final Window owner = kbdFocusManager.getFocusedWindow();

    return ((aComponent != null)
        && (owner != null)
        && ((owner == aComponent) || owner.isAncestorOf(aComponent)));
  }
예제 #17
0
  /*
   * Requests platform to set native focus on a frame/dialog.
   * In case of a simple window, triggers appropriate java focus change.
   */
  public boolean requestWindowFocus(CausedFocusEvent.Cause cause) {
    if (focusLog.isLoggable(PlatformLogger.FINE)) {
      focusLog.fine("requesting native focus to " + this);
    }

    if (!focusAllowedFor()) {
      focusLog.fine("focus is not allowed");
      return false;
    }

    if (platformWindow.rejectFocusRequest(cause)) {
      return false;
    }

    Window currentActive = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();

    Window opposite = LWKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow();

    // Make the owner active window.
    if (isSimpleWindow()) {
      LWWindowPeer owner = getOwnerFrameDialog(this);

      // If owner is not natively active, request native
      // activation on it w/o sending events up to java.
      if (owner != null && !owner.platformWindow.isActive()) {
        if (focusLog.isLoggable(PlatformLogger.FINE)) {
          focusLog.fine("requesting native focus to the owner " + owner);
        }
        LWWindowPeer currentActivePeer =
            (currentActive != null ? (LWWindowPeer) currentActive.getPeer() : null);

        // Ensure the opposite is natively active and suppress sending events.
        if (currentActivePeer != null && currentActivePeer.platformWindow.isActive()) {
          if (focusLog.isLoggable(PlatformLogger.FINE)) {
            focusLog.fine("the opposite is " + currentActivePeer);
          }
          currentActivePeer.skipNextFocusChange = true;
        }
        owner.skipNextFocusChange = true;

        owner.platformWindow.requestWindowFocus();
      }

      // DKFM will synthesize all the focus/activation events correctly.
      changeFocusedWindow(true, opposite);
      return true;

      // In case the toplevel is active but not focused, change focus directly,
      // as requesting native focus on it will not have effect.
    } else if (getTarget() == currentActive && !getTarget().hasFocus()) {

      changeFocusedWindow(true, opposite);
      return true;
    }

    return platformWindow.requestWindowFocus();
  }
예제 #18
0
 /**
  * Because there may be many AppContexts, and we can't be sure where this EmbeddedFrame is first
  * created or shown, we can't automatically determine the correct KeyboardFocusManager to attach
  * to as KeyEventDispatcher. Those who want to use the functionality of traversing out of the
  * EmbeddedFrame must call this method on the Applet's AppContext. After that, all the changes can
  * be handled automatically, including possible replacement of KeyboardFocusManager.
  */
 public void registerListeners() {
   if (appletKFM != null) {
     removeTraversingOutListeners(appletKFM);
   }
   appletKFM = KeyboardFocusManager.getCurrentKeyboardFocusManager();
   if (isVisible()) {
     addTraversingOutListeners(appletKFM);
   }
 }
예제 #19
0
 /**
  * Tries to find the current focused window.
  *
  * @return the current focused window, or <code>null</code> if no such window could be found.
  */
 public static final Window getCurrentWindow() {
   Window owner;
   final KeyboardFocusManager kbdFocusManager =
       KeyboardFocusManager.getCurrentKeyboardFocusManager();
   owner = kbdFocusManager.getFocusedWindow();
   if (owner == null) {
     owner = kbdFocusManager.getActiveWindow();
   }
   return owner;
 }
예제 #20
0
  public boolean isActive() {
    if (isUnitTestMode()) return true;

    if (myActive == null) {
      Window active = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
      return active != null;
    }

    return myActive;
  }
  @Nullable
  private Editor validateCurrentEditor() {
    Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    if (focusOwner instanceof JComponent) {
      final JComponent jComponent = (JComponent) focusOwner;
      if (jComponent.getClientProperty("AuxEditorComponent") != null)
        return null; // Hack for EditorSearchComponent
    }

    return myEditor;
  }
예제 #22
0
 public void resetDefaultFocusTraversalKeys() {
   KeyboardFocusManager m = KeyboardFocusManager.getCurrentKeyboardFocusManager();
   for (Integer each :
       Arrays.asList(
           KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
           KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
           KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS,
           KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS)) {
     setFocusTraversalKeys(each, m.getDefaultFocusTraversalKeys(each));
   }
 }
예제 #23
0
 public void actionPerformed(ActionEvent evt) {
   Component focusOwner =
       KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
   if (focusOwner != null) {
     if (focusOwner instanceof EditableComponent) {
       ((EditableComponent) focusOwner).duplicate();
     } else {
       focusOwner.getToolkit().beep();
     }
   }
 }
  private void tabBackward() {
    final KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    manager.focusPreviousComponent();

    SwingUtilities.invokeLater(
        new Runnable() {
          public void run() {
            if (manager.getFocusOwner() instanceof JScrollBar) manager.focusPreviousComponent();
          }
        });
  }
예제 #25
0
    @Override
    public Dimension getPreferredSize() {
      final Component focusOwner =
          KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
      Point p = null;
      if (focusOwner != null && focusOwner.isShowing()) {
        p = focusOwner.getLocationOnScreen();
      }

      return computeNotBiggerDimension(super.getPreferredSize().getSize(), p);
    }
예제 #26
0
 @FeatureEntryPoint(JHotDrawFeatures.BASIC_EDITING)
 public void actionPerformed(ActionEvent evt) {
   Component focusOwner =
       KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
   if (focusOwner != null && focusOwner instanceof JComponent) {
     JComponent component = (JComponent) focusOwner;
     component
         .getTransferHandler()
         .exportToClipboard(
             component, component.getToolkit().getSystemClipboard(), TransferHandler.MOVE);
   }
 }
 @Override
 public void actionPerformed(ActionEvent evt) {
   JComponent c = target;
   if (c == null
       && (KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner()
           instanceof JComponent)) {
     c =
         (JComponent)
             KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
   }
   if (c != null && c.isEnabled()) {
     if (c instanceof EditableComponent) {
       ((EditableComponent) c).clearSelection();
     } else if (c instanceof JTextComponent) {
       JTextComponent tc = ((JTextComponent) c);
       tc.select(tc.getSelectionStart(), tc.getSelectionStart());
     } else {
       c.getToolkit().beep();
     }
   }
 }
예제 #28
0
  private void processDrag(final MouseEvent e) {
    if (myDragCancelled) return;
    if (!isDraggingNow()) {
      if (myPressedPoint == null) return;
      if (isWithinDeadZone(e)) return;

      myDragPane = findLayeredPane(e);
      if (myDragPane == null) return;
      final BufferedImage image =
          new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
      paint(image.getGraphics());
      myDragButtonImage =
          new JLabel(new ImageIcon(image)) {

            public String toString() {
              return "Image for: " + StripeButton.this.toString();
            }
          };
      myDragPane.add(myDragButtonImage, JLayeredPane.POPUP_LAYER);
      myDragButtonImage.setSize(myDragButtonImage.getPreferredSize());
      setVisible(false);
      myPane.startDrag();
      myDragKeyEventDispatcher = new DragKeyEventDispatcher();
      KeyboardFocusManager.getCurrentKeyboardFocusManager()
          .addKeyEventDispatcher(myDragKeyEventDispatcher);
    }
    if (!isDraggingNow()) return;

    Point xy = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), myDragPane);
    if (myPressedPoint != null) {
      xy.x -= myPressedPoint.x;
      xy.y -= myPressedPoint.y;
    }
    myDragButtonImage.setLocation(xy);

    SwingUtilities.convertPointToScreen(xy, myDragPane);

    final Stripe stripe =
        myPane.getStripeFor(new Rectangle(xy, myDragButtonImage.getSize()), (Stripe) getParent());
    if (stripe == null) {
      if (myLastStripe != null) {
        myLastStripe.resetDrop();
      }
    } else {
      if (myLastStripe != null && myLastStripe != stripe) {
        myLastStripe.resetDrop();
      }
      stripe.processDropButton(this, myDragButtonImage, xy);
    }

    myLastStripe = stripe;
  }
  /*
   *  Create using the specified focus policy
   */
  public TabFocusHandler(JTabbedPane tabbedPane, int focusPolicy) {
    if (focusPolicy != RESET_FOCUS && focusPolicy != RETAIN_FOCUS)
      throw new IllegalArgumentException("Invalid focus policy");

    this.tabbedPane = tabbedPane;
    this.focusPolicy = focusPolicy;

    //  Add listeners to manage a tab change

    tabbedPane.addChangeListener(this);
    KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    focusManager.addPropertyChangeListener("permanentFocusOwner", this);
  }
예제 #30
0
  private static Component findFocusOwner(Component c) {
    Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();

    // verify focusOwner is a descendant of c
    for (Component temp = focusOwner;
        temp != null;
        temp = (temp instanceof Window) ? null : temp.getParent()) {
      if (temp == c) {
        return focusOwner;
      }
    }

    return null;
  }