You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DMDircEventQueue.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (c) 2006-2010 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. import com.dmdirc.actions.ActionManager;
  24. import com.dmdirc.actions.CoreActionType;
  25. import com.dmdirc.addons.ui_swing.actions.CopyAction;
  26. import com.dmdirc.addons.ui_swing.actions.CutAction;
  27. import com.dmdirc.addons.ui_swing.actions.PasteAction;
  28. import com.dmdirc.config.IdentityManager;
  29. import com.dmdirc.interfaces.ConfigChangeListener;
  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. import java.awt.event.WindowEvent;
  38. import javax.swing.JPopupMenu;
  39. import javax.swing.KeyStroke;
  40. import javax.swing.MenuSelectionManager;
  41. import javax.swing.SwingUtilities;
  42. import javax.swing.text.JTextComponent;
  43. /**
  44. * Custom event queue to add common functionality to certain components and
  45. * monitor the EDT for long running tasks. The monitoring code was taken from
  46. * TracingEventQueue by Kirill Grouchnikov (http://today.java.net/lpt/a/433).
  47. */
  48. public final class DMDircEventQueue extends EventQueue implements
  49. ConfigChangeListener {
  50. /** Swing Controller. */
  51. private SwingController controller;
  52. /** Tracing thread. */
  53. private TracingEventQueueThread tracingThread;
  54. /**
  55. * Instantiates the DMDircEventQueue.
  56. *
  57. * @param controller Swing controller
  58. */
  59. public DMDircEventQueue(final SwingController controller) {
  60. super();
  61. this.controller = controller;
  62. checkTracing();
  63. IdentityManager.getGlobalConfig().addChangeListener(
  64. controller.getDomain(), "debugEDT", this);
  65. }
  66. /** {@inheritDoc} */
  67. @Override
  68. protected void dispatchEvent(final AWTEvent event) {
  69. if (tracingThread == null) {
  70. super.dispatchEvent(event);
  71. } else {
  72. this.tracingThread.eventDispatched(event);
  73. super.dispatchEvent(event);
  74. this.tracingThread.eventProcessed(event);
  75. }
  76. if (event instanceof MouseEvent) {
  77. handleMouseEvent((MouseEvent) event);
  78. } else if (event instanceof KeyEvent) {
  79. handleKeyEvent((KeyEvent) event);
  80. } else if (event instanceof WindowEvent) {
  81. handleWindowEvent((WindowEvent) event);
  82. }
  83. }
  84. private void checkTracing() {
  85. final boolean tracing = IdentityManager.getGlobalConfig().
  86. getOptionBool(controller.getDomain(), "debugEDT");
  87. if (tracing) {
  88. this.tracingThread = new TracingEventQueueThread(100);
  89. this.tracingThread.start();
  90. } else {
  91. tracingThread = null;
  92. }
  93. }
  94. /**
  95. * Handles key events.
  96. *
  97. * @param ke Key event
  98. */
  99. private void handleKeyEvent(final KeyEvent ke) {
  100. switch (ke.getKeyChar()) {
  101. case KeyEvent.VK_F1:
  102. //Fallthrough
  103. case KeyEvent.VK_F2:
  104. //Fallthrough
  105. case KeyEvent.VK_F3:
  106. //Fallthrough
  107. case KeyEvent.VK_F4:
  108. //Fallthrough
  109. case KeyEvent.VK_F5:
  110. //Fallthrough
  111. case KeyEvent.VK_F6:
  112. //Fallthrough
  113. case KeyEvent.VK_F7:
  114. //Fallthrough
  115. case KeyEvent.VK_F8:
  116. //Fallthrough
  117. case KeyEvent.VK_F9:
  118. //Fallthrough
  119. case KeyEvent.VK_F10:
  120. //Fallthrough
  121. case KeyEvent.VK_F11:
  122. //Fallthrough
  123. case KeyEvent.VK_F12:
  124. //Fallthrough
  125. case KeyEvent.VK_F13:
  126. //Fallthrough
  127. case KeyEvent.VK_F14:
  128. //Fallthrough
  129. case KeyEvent.VK_F15:
  130. //Fallthrough
  131. case KeyEvent.VK_F16:
  132. //Fallthrough
  133. case KeyEvent.VK_F17:
  134. //Fallthrough
  135. case KeyEvent.VK_F18:
  136. //Fallthrough
  137. case KeyEvent.VK_F19:
  138. //Fallthrough
  139. case KeyEvent.VK_F20:
  140. //Fallthrough
  141. case KeyEvent.VK_F21:
  142. //Fallthrough
  143. case KeyEvent.VK_F22:
  144. //Fallthrough
  145. case KeyEvent.VK_F23:
  146. //Fallthrough
  147. case KeyEvent.VK_F24:
  148. ActionManager.processEvent(CoreActionType.CLIENT_KEY_PRESSED,
  149. null, KeyStroke.getKeyStroke(ke.getKeyChar(),
  150. ke.getModifiers()));
  151. break;
  152. default:
  153. if (ke.getModifiers() != 0) {
  154. ActionManager.processEvent(CoreActionType.CLIENT_KEY_PRESSED,
  155. null, KeyStroke.getKeyStroke(ke.getKeyChar(),
  156. ke.getModifiers()));
  157. }
  158. break;
  159. }
  160. }
  161. /**
  162. * Handles mouse events.
  163. *
  164. * @param me Mouse event
  165. */
  166. private void handleMouseEvent(final MouseEvent me) {
  167. if (!me.isPopupTrigger()) {
  168. return;
  169. }
  170. if (me.getComponent() == null) {
  171. return;
  172. }
  173. final Component comp = SwingUtilities.getDeepestComponentAt(
  174. me.getComponent(), me.getX(), me.getY());
  175. if (!(comp instanceof JTextComponent)) {
  176. return;
  177. }
  178. if (MenuSelectionManager.defaultManager().getSelectedPath().length > 0) {
  179. return;
  180. }
  181. final JTextComponent tc = (JTextComponent) comp;
  182. final JPopupMenu menu = new JPopupMenu();
  183. menu.add(new CutAction(tc));
  184. menu.add(new CopyAction(tc));
  185. menu.add(new PasteAction(tc));
  186. final Point pt = SwingUtilities.convertPoint(me.getComponent(),
  187. me.getPoint(), tc);
  188. menu.show(tc, pt.x, pt.y);
  189. }
  190. /**
  191. * Handles window events
  192. *
  193. * @param windowEvent Window event
  194. */
  195. private void handleWindowEvent(final WindowEvent we) {
  196. if (we.getSource() instanceof Window) {
  197. if (controller.hasMainFrame()) {
  198. if (we.getID() == WindowEvent.WINDOW_OPENED) {
  199. controller.addTopLevelWindow((Window) we.getSource());
  200. } else if (we.getID() == WindowEvent.WINDOW_CLOSED) {
  201. controller.delTopLevelWindow((Window) we.getSource());
  202. }
  203. }
  204. }
  205. }
  206. /** {@inheritDoc} */
  207. @Override
  208. public void configChanged(final String domain, final String key) {
  209. checkTracing();
  210. }
  211. }