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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2006-2015 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.addons.ui_swing.actions.CopyAction;
  24. import com.dmdirc.addons.ui_swing.actions.CutAction;
  25. import com.dmdirc.addons.ui_swing.events.SwingWindowEvent;
  26. import com.dmdirc.addons.ui_swing.events.ClientKeyPressedEvent;
  27. import com.dmdirc.interfaces.EventBus;
  28. import java.awt.AWTEvent;
  29. import java.awt.Component;
  30. import java.awt.EventQueue;
  31. import java.awt.Point;
  32. import java.awt.datatransfer.Clipboard;
  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. /** Event bus to dispatch events to. */
  49. private final EventBus eventBus;
  50. /** Clipboard to copy and paste from. */
  51. private final Clipboard clipboard;
  52. @Inject
  53. public DMDircEventQueue(final EventBus eventBus, final Clipboard clipboard) {
  54. this.eventBus = eventBus;
  55. this.clipboard = clipboard;
  56. }
  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. eventBus.publish(new SwingWindowEvent((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. eventBus.publishAsync(new ClientKeyPressedEvent(
  93. KeyStroke.getKeyStroke(ke.getKeyChar(), ke.getModifiers())));
  94. }
  95. /**
  96. * Handles mouse events.
  97. *
  98. * @param me Mouse event
  99. */
  100. private void handleMouseEvent(final MouseEvent me) {
  101. if (!me.isPopupTrigger() || me.getComponent() == null) {
  102. return;
  103. }
  104. final Component comp = SwingUtilities.getDeepestComponentAt(
  105. me.getComponent(), me.getX(), me.getY());
  106. if (!(comp instanceof JTextComponent) || MenuSelectionManager
  107. .defaultManager().getSelectedPath().length > 0) {
  108. return;
  109. }
  110. final JTextComponent tc = (JTextComponent) comp;
  111. final JPopupMenu menu = new JPopupMenu();
  112. menu.add(new CutAction(tc));
  113. menu.add(new CopyAction(tc));
  114. menu.add(new PasteAction(clipboard, tc));
  115. final Point pt = SwingUtilities.convertPoint(me.getComponent(),
  116. me.getPoint(), tc);
  117. menu.show(tc, pt.x, pt.y);
  118. }
  119. @Override
  120. public void pop() { //NOPMD
  121. super.pop();
  122. }
  123. }