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

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