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.7KB

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