File Source: DMDircEventQueue.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.DMDircEventQueue__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22 package com.dmdirc.addons.ui_swing;
23
24 import com.dmdirc.actions.ActionManager;
25 import com.dmdirc.actions.CoreActionType;
26 import com.dmdirc.addons.ui_swing.actions.CopyAction;
27 import com.dmdirc.addons.ui_swing.actions.CutAction;
28 import com.dmdirc.addons.ui_swing.actions.PasteAction;
29
30 import java.awt.AWTEvent;
31 import java.awt.Component;
32 import java.awt.EventQueue;
33 import java.awt.Point;
34 import java.awt.Window;
35 import java.awt.event.KeyEvent;
36 import java.awt.event.MouseEvent;
37
38 import java.awt.event.WindowEvent;
39 import javax.swing.JPopupMenu;
40 import javax.swing.KeyStroke;
41 import javax.swing.MenuSelectionManager;
42 import javax.swing.SwingUtilities;
43 import javax.swing.text.JTextComponent;
44
45 /**
46 * Custom event queue to add commong functionality to certain components.
47 */
48 public final class DMDircEventQueue extends EventQueue {
49
50 /** Swing Controller. */
51 private SwingController controller;
52
53 /**
54 * Instantiates the DMDircEventQueue.
55 *
56 * @param controller Swing controller
57 */
58 public DMDircEventQueue(final SwingController controller) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.DMDircEventQueue(SwingController)
*
* Postconditions:
* this.controller == controller
* init'ed(this.controller)
*/
59 super();
60
61 this.controller = controller;
62 }
63
64 /** {@inheritDoc} */
65 @Override
66 protected void dispatchEvent(final AWTEvent event) {
/*
P/P * Method: void dispatchEvent(AWTEvent)
*
* Preconditions:
* (soft) this.controller != null
* (soft) this.controller.mainFrameCreated != null
* (soft) this.controller.windows != null
*/
67 super.dispatchEvent(event);
68
69 if (event instanceof MouseEvent) {
70 handleMouseEvent((MouseEvent) event);
71 } else if (event instanceof KeyEvent) {
72 handleKeyEvent((KeyEvent) event);
73 } else if (event instanceof WindowEvent) {
74 handleWindowEvent((WindowEvent) event);
75 }
76 }
77
78 /**
79 * Handles key events.
80 *
81 * @param ke Key event
82 */
83 private void handleKeyEvent(final KeyEvent ke) {
/*
P/P * Method: void handleKeyEvent(KeyEvent)
*
* Preconditions:
* ke != null
*
* Presumptions:
* init'ed(com.dmdirc.actions.CoreActionType.CLIENT_KEY_PRESSED)
*
* Test Vectors:
* java.awt.event.KeyEvent:getKeyChar(...)@84: {112..123, 61_440..61_451}, {0..111, 124..61_439, 61_452..216-1}
* java.awt.event.KeyEvent:getModifiers(...)@137: {0}, {-231..-1, 1..232-1}
*/
84 switch (ke.getKeyChar()) {
85 case KeyEvent.VK_F1:
86 //Fallthrough
87 case KeyEvent.VK_F2:
88 //Fallthrough
89 case KeyEvent.VK_F3:
90 //Fallthrough
91 case KeyEvent.VK_F4:
92 //Fallthrough
93 case KeyEvent.VK_F5:
94 //Fallthrough
95 case KeyEvent.VK_F6:
96 //Fallthrough
97 case KeyEvent.VK_F7:
98 //Fallthrough
99 case KeyEvent.VK_F8:
100 //Fallthrough
101 case KeyEvent.VK_F9:
102 //Fallthrough
103 case KeyEvent.VK_F10:
104 //Fallthrough
105 case KeyEvent.VK_F11:
106 //Fallthrough
107 case KeyEvent.VK_F12:
108 //Fallthrough
109 case KeyEvent.VK_F13:
110 //Fallthrough
111 case KeyEvent.VK_F14:
112 //Fallthrough
113 case KeyEvent.VK_F15:
114 //Fallthrough
115 case KeyEvent.VK_F16:
116 //Fallthrough
117 case KeyEvent.VK_F17:
118 //Fallthrough
119 case KeyEvent.VK_F18:
120 //Fallthrough
121 case KeyEvent.VK_F19:
122 //Fallthrough
123 case KeyEvent.VK_F20:
124 //Fallthrough
125 case KeyEvent.VK_F21:
126 //Fallthrough
127 case KeyEvent.VK_F22:
128 //Fallthrough
129 case KeyEvent.VK_F23:
130 //Fallthrough
131 case KeyEvent.VK_F24:
132 ActionManager.processEvent(CoreActionType.CLIENT_KEY_PRESSED,
133 null, KeyStroke.getKeyStroke(ke.getKeyChar(),
134 ke.getModifiers()));
135 break;
136 default:
137 if (ke.getModifiers() != 0) {
138 ActionManager.processEvent(CoreActionType.CLIENT_KEY_PRESSED,
139 null, KeyStroke.getKeyStroke(ke.getKeyChar(),
140 ke.getModifiers()));
141 }
142 break;
143 }
144 }
145
146 /**
147 * Handles mouse events.
148 *
149 * @param me Mouse event
150 */
151 private void handleMouseEvent(final MouseEvent me) {
/*
P/P * Method: void handleMouseEvent(MouseEvent)
*
* Preconditions:
* me != null
*
* Presumptions:
* javax.swing.MenuSelectionManager:defaultManager(...)@167 != null
* javax.swing.MenuSelectionManager:getSelectedPath(...)@167 != null
* javax.swing.SwingUtilities:convertPoint(...)@177 != null
*
* Test Vectors:
* java.awt.event.MouseEvent:getComponent(...)@156: Inverse{null}, Addr_Set{null}
* java.awt.event.MouseEvent:isPopupTrigger(...)@152: {1}, {0}
* javax.swing.MenuSelectionManager:getSelectedPath(...).length@167: {0}, {1..+Inf}
*/
152 if (!me.isPopupTrigger()) {
153 return;
154 }
155
156 if (me.getComponent() == null) {
157 return;
158 }
159
160 final Component comp = SwingUtilities.getDeepestComponentAt(
161 me.getComponent(), me.getX(), me.getY());
162
163 if (!(comp instanceof JTextComponent)) {
164 return;
165 }
166
167 if (MenuSelectionManager.defaultManager().getSelectedPath().length > 0) {
168 return;
169 }
170
171 final JTextComponent tc = (JTextComponent) comp;
172 final JPopupMenu menu = new JPopupMenu();
173 menu.add(new CutAction(tc));
174 menu.add(new CopyAction(tc));
175 menu.add(new PasteAction(tc));
176
177 final Point pt = SwingUtilities.convertPoint(me.getComponent(),
178 me.getPoint(), tc);
179 menu.show(tc, pt.x, pt.y);
180 }
181
182 /**
183 * Handles window events
184 *
185 * @param windowEvent Window event
186 */
187 private void handleWindowEvent(final WindowEvent we) {
/*
P/P * Method: void handleWindowEvent(WindowEvent)
*
* Preconditions:
* we != null
* (soft) this.controller != null
* (soft) this.controller.mainFrameCreated != null
* (soft) this.controller.windows != null
*
* Test Vectors:
* java.awt.event.WindowEvent:getID(...)@190: {-231..199, 201..232-1}, {200}
* java.awt.event.WindowEvent:getID(...)@192: {-231..201, 203..232-1}, {202}
* java.util.concurrent.atomic.AtomicBoolean:get(...)@114: {0}, {1}
*/
188 if (we.getSource() instanceof Window) {
189 if (controller.hasMainFrame()) {
190 if (we.getID() == WindowEvent.WINDOW_OPENED) {
191 controller.addTopLevelWindow((Window) we.getSource());
192 } else if (we.getID() == WindowEvent.WINDOW_CLOSED) {
193 controller.delTopLevelWindow((Window) we.getSource());
194 }
195 }
196 }
197 }
198 }
SofCheck Inspector Build Version : 2.17854
| DMDircEventQueue.java |
2009-Jun-25 01:54:24 |
| DMDircEventQueue.class |
2009-Sep-02 17:04:15 |