📄 defaultkeyboardfocusmanager.java
字号:
/* DefaultKeyboardFocusManager.java -- Copyright (C) 2002, 2004 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.awt;import java.awt.event.ActionEvent;import java.awt.event.FocusEvent;import java.awt.event.KeyEvent;import java.awt.event.WindowEvent;import java.util.Iterator;import java.util.LinkedList;import java.util.Set;import java.util.SortedSet;import java.util.TreeSet;// FIXME: finish documentationpublic class DefaultKeyboardFocusManager extends KeyboardFocusManager{ /** * This class models a request to delay the dispatch of events that * arrive after a certain time, until a certain component becomes * the focus owner. */ private class EventDelayRequest implements Comparable { /** A {@link java.util.List} of {@link java.awt.event.KeyEvent}s that are being delayed, pending this request's {@link Component} receiving the keyboard focus. */ private LinkedList enqueuedKeyEvents = new LinkedList (); /** An event timestamp. All events that arrive after this time should be queued in the {@link #enqueuedKeyEvents} {@link java.util.List}. */ public long timestamp; /** When this {@link Component} becomes focused, all events between this EventDelayRequest and the next one in will be dispatched from {@link #enqueuedKeyEvents}. */ public Component focusedComp; /** * Construct a new EventDelayRequest. * * @param timestamp events that arrive after this time will be * delayed * @param focusedComp the Component that needs to receive focus * before events are dispatched */ public EventDelayRequest (long timestamp, Component focusedComp) { this.timestamp = timestamp; this.focusedComp = focusedComp; } public int compareTo (Object o) { if (!(o instanceof EventDelayRequest)) throw new ClassCastException (); EventDelayRequest request = (EventDelayRequest) o; if (request.timestamp < timestamp) return -1; else if (request.timestamp == timestamp) return 0; else return 1; } public boolean equals (Object o) { if (!(o instanceof EventDelayRequest) || o == null) return false; EventDelayRequest request = (EventDelayRequest) o; return (request.timestamp == timestamp && request.focusedComp == focusedComp); } public void enqueueEvent (KeyEvent e) { KeyEvent last = (KeyEvent) enqueuedKeyEvents.getLast (); if (last != null && e.getWhen () < last.getWhen ()) throw new RuntimeException ("KeyEvents enqueued out-of-order"); if (e.getWhen () <= timestamp) throw new RuntimeException ("KeyEvents enqueued before starting timestamp"); enqueuedKeyEvents.add (e); } public void dispatchEvents () { int size = enqueuedKeyEvents.size (); for (int i = 0; i < size; i++) { KeyEvent e = (KeyEvent) enqueuedKeyEvents.remove (0); dispatchKeyEvent (e); } } public void discardEvents () { enqueuedKeyEvents.clear (); } } /** * This flag indicates for which focus traversal key release event we * possibly wait, before letting any more KEY_TYPED events through. */ private AWTKeyStroke waitForKeyStroke = null; /** The {@link java.util.SortedSet} of current * {@link EventDelayRequest}s. */ private SortedSet delayRequests = new TreeSet (); public DefaultKeyboardFocusManager () { } public boolean dispatchEvent (AWTEvent e) { if (e instanceof WindowEvent) { Window target = (Window) e.getSource (); if (e.id == WindowEvent.WINDOW_ACTIVATED) setGlobalActiveWindow (target); else if (e.id == WindowEvent.WINDOW_GAINED_FOCUS) setGlobalFocusedWindow (target); else if (e.id != WindowEvent.WINDOW_LOST_FOCUS && e.id != WindowEvent.WINDOW_DEACTIVATED) return false; redispatchEvent(target, e); return true; } else if (e instanceof FocusEvent) { Component target = (Component) e.getSource (); if (e.id == FocusEvent.FOCUS_GAINED) { if (! (target instanceof Window)) { if (((FocusEvent) e).isTemporary ()) setGlobalFocusOwner (target); else setGlobalPermanentFocusOwner (target); } // Keep track of this window's focus owner. // Find the target Component's top-level ancestor. target // may be a window. Container parent = target.getParent (); while (parent != null && !(parent instanceof Window)) parent = parent.getParent (); // If the parent is null and target is not a window, then target is an // unanchored component and so we don't want to set the focus owner. if (! (parent == null && ! (target instanceof Window))) { Window toplevel = parent == null ? (Window) target : (Window) parent; Component focusOwner = getFocusOwner (); if (focusOwner != null && ! (focusOwner instanceof Window)) toplevel.setFocusOwner (focusOwner); } } else if (e.id == FocusEvent.FOCUS_LOST) { if (((FocusEvent) e).isTemporary ()) setGlobalFocusOwner (null); else setGlobalPermanentFocusOwner (null); } redispatchEvent(target, e); return true; } else if (e instanceof KeyEvent) { // Loop through all registered KeyEventDispatchers, giving // each a chance to handle this event. Iterator i = getKeyEventDispatchers().iterator(); while (i.hasNext ()) { KeyEventDispatcher dispatcher = (KeyEventDispatcher) i.next (); if (dispatcher.dispatchKeyEvent ((KeyEvent) e)) return true; } // processKeyEvent checks if this event represents a focus // traversal key stroke. Component focusOwner = getGlobalPermanentFocusOwner (); if (focusOwner != null) processKeyEvent (focusOwner, (KeyEvent) e); if (e.isConsumed ()) return true; if (enqueueKeyEvent ((KeyEvent) e)) // This event was enqueued for dispatch at a later time. return true; else // This event wasn't handled by any of the registered // KeyEventDispatchers, and wasn't enqueued for dispatch // later, so send it to the default dispatcher. return dispatchKeyEvent ((KeyEvent) e); } return false; } private boolean enqueueKeyEvent (KeyEvent e) { Iterator i = delayRequests.iterator (); boolean oneEnqueued = false; while (i.hasNext ()) { EventDelayRequest request = (EventDelayRequest) i.next (); if (e.getWhen () > request.timestamp) { request.enqueueEvent (e); oneEnqueued = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -