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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  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 java.awt.AWTEvent;
  29. import java.awt.Component;
  30. import java.awt.EventQueue;
  31. import java.awt.Point;
  32. import java.awt.Window;
  33. import java.awt.event.KeyEvent;
  34. import java.awt.event.MouseEvent;
  35. import java.awt.event.WindowEvent;
  36. import javax.inject.Inject;
  37. import javax.inject.Singleton;
  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.
  45. */
  46. @Singleton
  47. public class DMDircEventQueue extends EventQueue {
  48. /** Swing Controller. */
  49. private final SwingController controller;
  50. /**
  51. * Instantiates the DMDircEventQueue.
  52. *
  53. * @param controller Swing controller
  54. */
  55. @Inject
  56. public DMDircEventQueue(final SwingController controller) {
  57. super();
  58. this.controller = controller;
  59. }
  60. /** {@inheritDoc} */
  61. @Override
  62. protected void dispatchEvent(final AWTEvent event) {
  63. preDispatchEvent(event);
  64. super.dispatchEvent(event);
  65. postDispatchEvent(event);
  66. if (event instanceof MouseEvent) {
  67. handleMouseEvent((MouseEvent) event);
  68. } else if (event instanceof KeyEvent) {
  69. handleKeyEvent((KeyEvent) event);
  70. } else if (event instanceof WindowEvent) {
  71. handleWindowEvent((WindowEvent) event);
  72. }
  73. }
  74. /**
  75. * Triggered before the event is dispatched to AWT.
  76. *
  77. * @param event Event about to be dispatched
  78. */
  79. protected void preDispatchEvent(final AWTEvent event) {
  80. //Do nothing
  81. }
  82. /**
  83. * Called just after an event is dispatched to AWT.
  84. *
  85. * @param event Event that has been dispatched
  86. */
  87. protected void postDispatchEvent(final AWTEvent event) {
  88. //Do nothing
  89. }
  90. /**
  91. * Handles key events.
  92. *
  93. * @param ke Key event
  94. */
  95. private void handleKeyEvent(final KeyEvent ke) {
  96. ActionManager.getActionManager().triggerEvent(
  97. CoreActionType.CLIENT_KEY_PRESSED, null,
  98. KeyStroke.getKeyStroke(ke.getKeyChar(), ke.getModifiers()));
  99. }
  100. /**
  101. * Handles mouse events.
  102. *
  103. * @param me Mouse event
  104. */
  105. private void handleMouseEvent(final MouseEvent me) {
  106. if (!me.isPopupTrigger() || me.getComponent() == null) {
  107. return;
  108. }
  109. final Component comp = SwingUtilities.getDeepestComponentAt(
  110. me.getComponent(), me.getX(), me.getY());
  111. if (!(comp instanceof JTextComponent) || MenuSelectionManager
  112. .defaultManager().getSelectedPath().length > 0) {
  113. return;
  114. }
  115. final JTextComponent tc = (JTextComponent) comp;
  116. final JPopupMenu menu = new JPopupMenu();
  117. menu.add(new CutAction(tc));
  118. menu.add(new CopyAction(tc));
  119. menu.add(new PasteAction(tc));
  120. final Point pt = SwingUtilities.convertPoint(me.getComponent(),
  121. me.getPoint(), tc);
  122. menu.show(tc, pt.x, pt.y);
  123. }
  124. /**
  125. * Handles window events.
  126. *
  127. * @param we Window event
  128. */
  129. private void handleWindowEvent(final WindowEvent we) {
  130. if ((we.getSource() instanceof Window) && controller.hasMainFrame()) {
  131. if (we.getID() == WindowEvent.WINDOW_OPENED) {
  132. controller.addTopLevelWindow((Window) we.getSource());
  133. } else if (we.getID() == WindowEvent.WINDOW_CLOSED) {
  134. controller.delTopLevelWindow((Window) we.getSource());
  135. }
  136. }
  137. }
  138. /** {@inheritDoc} */
  139. @Override
  140. public void pop() { //NOPMD
  141. super.pop();
  142. }
  143. }