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(); }
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; }
@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(); } }
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(); } }
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()); } }
/** * 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))); }
/** * 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; }
private void tabBackward() { final KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); manager.focusPreviousComponent(); SwingUtilities.invokeLater( new Runnable() { public void run() { if (manager.getFocusOwner() instanceof JScrollBar) manager.focusPreviousComponent(); } }); }
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)); } }
/* * 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); }
public void removeNotify() { final KeyboardFocusManager keyboardFocusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); //noinspection HardCodedStringLiteral keyboardFocusManager.removePropertyChangeListener("permanentFocusOwner", myEditorRemover); //noinspection HardCodedStringLiteral keyboardFocusManager.removePropertyChangeListener("focusOwner", myEditorRemover); super.removeNotify(); if (myBusyIcon != null) { remove(myBusyIcon); Disposer.dispose(myBusyIcon); myBusyIcon = null; } }
/* * The method maps the list of the active windows to the window's AppContext, * then the method registers ActiveWindowListener, GuiDisposedListener listeners; * it executes the initilialization only once per AppContext. */ @SuppressWarnings("unchecked") private static void initActiveWindowsTracking(Window w) { AppContext appContext = AppContext.getAppContext(); synchronized (appContext) { List<WWindowPeer> l = (List<WWindowPeer>) appContext.get(ACTIVE_WINDOWS_KEY); if (l == null) { l = new LinkedList<WWindowPeer>(); appContext.put(ACTIVE_WINDOWS_KEY, l); appContext.addPropertyChangeListener(AppContext.GUI_DISPOSED, guiDisposedListener); KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); kfm.addPropertyChangeListener("activeWindow", activeWindowListener); } } }
public boolean editCellAt(final int row, final int column, final EventObject e) { if (cellEditor != null && !cellEditor.stopCellEditing()) { return false; } if (row < 0 || row >= getRowCount() || column < 0 || column >= getColumnCount()) { return false; } if (!isCellEditable(row, column)) { return false; } if (myEditorRemover == null) { final KeyboardFocusManager keyboardFocusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); myEditorRemover = new MyCellEditorRemover(); //noinspection HardCodedStringLiteral keyboardFocusManager.addPropertyChangeListener("focusOwner", myEditorRemover); //noinspection HardCodedStringLiteral keyboardFocusManager.addPropertyChangeListener("permanentFocusOwner", myEditorRemover); } final TableCellEditor editor = getCellEditor(row, column); if (editor != null && editor.isCellEditable(e)) { editorComp = prepareEditor(editor, row, column); // ((JComponent)editorComp).setBorder(null); if (editorComp == null) { removeEditor(); return false; } editorComp.setBounds(getCellRect(row, column, false)); add(editorComp); editorComp.validate(); IdeFocusManager.findInstanceByComponent(this).requestFocus(editorComp, false); setCellEditor(editor); setEditingRow(row); setEditingColumn(column); editor.addCellEditorListener(this); if (isTypeAhead) { JTableCellEditorHelper.typeAhead(this, e, row, column); } 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; }
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; }
public void propertyChange(PropertyChangeEvent e) { boolean isDisposed = (Boolean) e.getNewValue(); if (isDisposed != true) { if (log.isLoggable(PlatformLogger.Level.FINE)) { log.fine(" Assertion (newValue != true) failed for AppContext.GUI_DISPOSED "); } } AppContext appContext = AppContext.getAppContext(); synchronized (appContext) { appContext.remove(ACTIVE_WINDOWS_KEY); appContext.removePropertyChangeListener(AppContext.GUI_DISPOSED, this); KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); kfm.removePropertyChangeListener("activeWindow", activeWindowListener); } }
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); }
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; } }; }
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; }
/** * 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); }
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); } } }
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); } }
@Nullable private static Editor getEditorFromFocus() { final Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); if (c instanceof EditorComponentImpl) { return ((EditorComponentImpl) c).getEditor(); } return null; }
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(); }
/* * 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(); }
public void setVisible(boolean b) { KeyboardFocusManager keyboardFocusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); if (b) { keyboardFocusManager.addKeyEventDispatcher(keyManager); } else { keyboardFocusManager.removeKeyEventDispatcher(keyManager); } super.setVisible(b); Window owner = getOwner(); if (owner != null) { owner.requestFocus(); if (lastFocusOwner != null) { lastFocusOwner.requestFocusInWindow(); } } }
/** * 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); } }
/** Creates a new <code>CollapsiblePaneGroup</code>. */ public CollapsiblePaneGroup() { final KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); _listener = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { Component focusOwner = focusManager.getFocusOwner(); if (focusOwner != null) { for (CollapsiblePane pane : _panes) { if (SwingUtilities.isDescendingFrom(focusOwner, pane)) { pane.setEmphasized(true); break; } } } } }; focusManager.addPropertyChangeListener( "focusOwner", new WeakPropertyChangeListener(_listener, focusManager)); }
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; }