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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing;
  18. import com.dmdirc.addons.ui_swing.actions.CopyAction;
  19. import com.dmdirc.addons.ui_swing.actions.CutAction;
  20. import com.dmdirc.addons.ui_swing.events.SwingWindowEvent;
  21. import com.dmdirc.addons.ui_swing.events.ClientKeyPressedEvent;
  22. import com.dmdirc.events.eventbus.EventBus;
  23. import java.awt.AWTEvent;
  24. import java.awt.Component;
  25. import java.awt.EventQueue;
  26. import java.awt.Point;
  27. import java.awt.datatransfer.Clipboard;
  28. import java.awt.event.KeyEvent;
  29. import java.awt.event.MouseEvent;
  30. import java.awt.event.WindowEvent;
  31. import javax.inject.Inject;
  32. import javax.inject.Singleton;
  33. import javax.swing.JPopupMenu;
  34. import javax.swing.KeyStroke;
  35. import javax.swing.MenuSelectionManager;
  36. import javax.swing.SwingUtilities;
  37. import javax.swing.text.JTextComponent;
  38. /**
  39. * Custom event queue to add common functionality to certain components.
  40. */
  41. @Singleton
  42. public class DMDircEventQueue extends EventQueue {
  43. /** Event bus to dispatch events to. */
  44. private final EventBus eventBus;
  45. /** Clipboard to copy and paste from. */
  46. private final Clipboard clipboard;
  47. @Inject
  48. public DMDircEventQueue(final EventBus eventBus, final Clipboard clipboard) {
  49. this.eventBus = eventBus;
  50. this.clipboard = clipboard;
  51. }
  52. @Override
  53. protected void dispatchEvent(final AWTEvent event) {
  54. preDispatchEvent(event);
  55. super.dispatchEvent(event);
  56. postDispatchEvent(event);
  57. if (event instanceof MouseEvent) {
  58. handleMouseEvent((MouseEvent) event);
  59. } else if (event instanceof KeyEvent) {
  60. handleKeyEvent((KeyEvent) event);
  61. } else if (event instanceof WindowEvent) {
  62. eventBus.publish(new SwingWindowEvent((WindowEvent) event));
  63. }
  64. }
  65. /**
  66. * Triggered before the event is dispatched to AWT.
  67. *
  68. * @param event Event about to be dispatched
  69. */
  70. protected void preDispatchEvent(final AWTEvent event) {
  71. //Do nothing
  72. }
  73. /**
  74. * Called just after an event is dispatched to AWT.
  75. *
  76. * @param event Event that has been dispatched
  77. */
  78. protected void postDispatchEvent(final AWTEvent event) {
  79. //Do nothing
  80. }
  81. /**
  82. * Handles key events.
  83. *
  84. * @param ke Key event
  85. */
  86. private void handleKeyEvent(final KeyEvent ke) {
  87. eventBus.publishAsync(new ClientKeyPressedEvent(
  88. KeyStroke.getKeyStroke(ke.getKeyChar(), ke.getModifiers())));
  89. }
  90. /**
  91. * Handles mouse events.
  92. *
  93. * @param me Mouse event
  94. */
  95. private void handleMouseEvent(final MouseEvent me) {
  96. if (!me.isPopupTrigger() || me.getComponent() == null) {
  97. return;
  98. }
  99. final Component comp = SwingUtilities.getDeepestComponentAt(
  100. me.getComponent(), me.getX(), me.getY());
  101. if (!(comp instanceof JTextComponent) || MenuSelectionManager
  102. .defaultManager().getSelectedPath().length > 0) {
  103. return;
  104. }
  105. final JTextComponent tc = (JTextComponent) comp;
  106. final JPopupMenu menu = new JPopupMenu();
  107. menu.add(new CutAction(tc));
  108. menu.add(new CopyAction(tc));
  109. menu.add(new PasteAction(clipboard, tc));
  110. final Point pt = SwingUtilities.convertPoint(me.getComponent(),
  111. me.getPoint(), tc);
  112. menu.show(tc, pt.x, pt.y);
  113. }
  114. @Override
  115. public void pop() { //NOPMD
  116. super.pop();
  117. }
  118. }