jkfeventqueue.java
来自「主要是对串口驱动的的一些控制源码!!! 在下载javacomm20-win32」· Java 代码 · 共 268 行
JAVA
268 行
/**
* $Log: JKFEventQueue.java,v $
* Revision 1.6 2003/02/01 16:00:05 willaxt
* removed access to static methods with this reference
*
* Revision 1.5 2003/02/01 00:26:57 mwulff
* changed method dispatchEvent
*
* Revision 1.4 2003/01/30 14:41:18 mwulff
* no message
*
* Revision 1.3 2003/01/17 15:56:38 mwulff
* when searching for a parent JFrame or JInternalFrame now is checked
* if the parent is null, otherwise we'll get a NullPointerException, if for example
* the parent is a JOptionPane
*
* Revision 1.2 2003/01/16 15:46:14 mwulff
* added support for consuming events
* implemented method invokeAndWait()
*
* Revision 1.1 2003/01/16 13:10:54 mwulff
* initial version
*
* Revision 1.1 2002/12/31 12:18:41 mwulff
* no message
*
*/
package de.fhm.jkf.gui;
import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.event.InvocationEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.AbstractButton;
import javax.swing.DefaultFocusManager;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
/**
* <br><br><center><table border="1" width="80%"><hr>
* <strong><a href="http://jkf.sourceforge.net">The JKF Project</a></strong>
* <p>
* Copyright (C) 2002 by Marten Wulff
* <p>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* <p>
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* <p>
* You should have received a copy of the <a href="http://www.gnu.org/copyleft/lesser.html">
* GNU Lesser General Public License</a> along with this library; if not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
* <hr></table></center>
*
* @author marten wulff
* @version 1.0
*/
public class JKFEventQueue extends EventQueue {
private static Hashtable blockedFrames = new Hashtable();
/**
* Constructor for JKFEventQueue.
*/
public JKFEventQueue() {
super();
}
public static void unlock(Component componentToUnlock) {
blockedFrames.remove(componentToUnlock);
}
public static void invokeAndWait(Runnable t)
throws InterruptedException, InvocationTargetException {
if (JKFEventQueue.isDispatchThread()) {
throw new java.lang.Error(
"Cannot call invokeAndWait from the event dispatcher thread");
}
Object lock = new Object();
InvocationEvent event =
new JKFInvocationEvent(Toolkit.getDefaultToolkit(), t, lock, true);
synchronized (lock) {
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);
lock.wait();
}
Exception eventException = event.getException();
if (eventException != null) {
throw new InvocationTargetException(eventException);
}
}
public static void invokeLater(Runnable t) {
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(
new JKFInvocationEvent(Toolkit.getDefaultToolkit(), t));
}
public static void lock(Component restrictedComponent) {
Vector helperVector = new Vector();
if (restrictedComponent != null) {
helperVector.addElement(restrictedComponent);
extractAllComponents(
((Container) restrictedComponent).getComponents(),
helperVector);
}
if (helperVector.size() > 0) {
blockedFrames.put(
restrictedComponent,
helperVector.toArray(new Component[0]));
}
}
private static void extractAllComponents(
Component[] array,
Vector helperVector) {
for (int i = 0; i < array.length; i++) {
if (array[i] != null) {
helperVector.addElement(array[i]);
if (((Container) array[i]).getComponentCount() != 0) {
extractAllComponents(
((Container) array[i]).getComponents(),
helperVector);
}
}
}
}
private void adjustFocusCapabilities(boolean blocked) {
Iterator it = JKFEventQueue.blockedFrames.entrySet().iterator();
Component[] tmp;
while (it.hasNext()) {
tmp = (Component[]) it.next();
if (blocked) {
for (int i = 0; i < tmp.length; i++) {
if (tmp[i] instanceof JComponent) {
((JComponent) tmp[i]).setRequestFocusEnabled(false);
}
// removes the focus indicator from all components that are capable
// of painting their focus
if (tmp[i] instanceof AbstractButton) {
((AbstractButton) tmp[i]).setFocusPainted(false);
}
}
} else {
for (int k = 0; k < tmp.length; k++) {
if (tmp[k] instanceof JComponent) {
((JComponent) tmp[k]).setRequestFocusEnabled(true);
}
if (tmp[k] instanceof AbstractButton) {
((AbstractButton) tmp[k]).setFocusPainted(true);
}
}
}
}
}
private Component getSource(AWTEvent event) {
Component source = null;
if ((event instanceof MouseEvent)
&& (event.getID() != MouseEvent.MOUSE_DRAGGED)
&& (event.getID() != MouseEvent.MOUSE_ENTERED)
&& (event.getID() != MouseEvent.MOUSE_EXITED)
&& (event.getID() != MouseEvent.MOUSE_MOVED)
&& (event.getID() != MouseEvent.MOUSE_RELEASED)) {
MouseEvent mouseEvent = (MouseEvent) event;
source =
SwingUtilities.getDeepestComponentAt(
mouseEvent.getComponent(),
mouseEvent.getX(),
mouseEvent.getY());
} else if (
event instanceof KeyEvent
&& event.getSource() instanceof Component) {
source =
KeyboardFocusManager
.getCurrentKeyboardFocusManager()
.getFocusOwner();
}
return source;
}
private boolean isSourceLocked(Component source) {
boolean blocked = false;
Iterator it = JKFEventQueue.blockedFrames.values().iterator();
Component[] tmp;
while (it.hasNext() && !blocked) {
tmp = (Component[]) it.next();
if ((tmp != null) && (source != null)) {
int i = 0;
while ((i < tmp.length) && (tmp[i].equals(source) == false)) {
i++;
}
blocked = i < tmp.length;
}
}
return blocked;
}
protected void dispatchEvent(AWTEvent event) {
boolean blocked = false;
blocked = isSourceLocked(getSource(event));
if (blocked
&& (event.getID() == MouseEvent.MOUSE_CLICKED
|| event.getID() == MouseEvent.MOUSE_PRESSED)) {
Toolkit.getDefaultToolkit().beep();
} else if (
blocked
&& event instanceof KeyEvent
&& event.getSource() instanceof Component) {
DefaultFocusManager dfm = new DefaultFocusManager();
dfm.getCurrentManager();
Component currentFocusOwner = getSource(event);
Component originalFocusOwner = currentFocusOwner;
boolean focusNotFound = true;
do {
currentFocusOwner =
dfm.getComponentAfter(
currentFocusOwner.getParent(),
currentFocusOwner);
if (currentFocusOwner instanceof JComponent) {
focusNotFound =
((JComponent) currentFocusOwner)
.isRequestFocusEnabled()
== false;
}
} while (
focusNotFound && !currentFocusOwner.equals(originalFocusOwner));
currentFocusOwner.requestFocusInWindow();
} else {
super.dispatchEvent(event);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?