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.

InputTextFrame.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.components.frames;
  18. import com.dmdirc.addons.ui_swing.EDTInvocation;
  19. import com.dmdirc.addons.ui_swing.UIUtilities;
  20. import com.dmdirc.addons.ui_swing.actions.CopyAction;
  21. import com.dmdirc.addons.ui_swing.actions.CutAction;
  22. import com.dmdirc.addons.ui_swing.actions.InputFieldCopyAction;
  23. import com.dmdirc.addons.ui_swing.components.AwayLabel;
  24. import com.dmdirc.addons.ui_swing.components.TypingLabel;
  25. import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputField;
  26. import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputHandler;
  27. import com.dmdirc.config.ConfigBinding;
  28. import com.dmdirc.interfaces.CommandController;
  29. import com.dmdirc.events.eventbus.EventBus;
  30. import com.dmdirc.interfaces.WindowModel;
  31. import com.dmdirc.plugins.ServiceManager;
  32. import com.dmdirc.ui.input.InputHandler;
  33. import com.dmdirc.ui.input.TabCompleterUtils;
  34. import java.awt.BorderLayout;
  35. import java.awt.Color;
  36. import java.awt.Point;
  37. import java.awt.event.KeyEvent;
  38. import java.awt.event.MouseEvent;
  39. import java.awt.event.MouseListener;
  40. import javax.inject.Provider;
  41. import javax.swing.JPanel;
  42. import javax.swing.JPopupMenu;
  43. import javax.swing.KeyStroke;
  44. import net.miginfocom.layout.PlatformDefaults;
  45. /**
  46. * Frame with an input field.
  47. */
  48. public abstract class InputTextFrame extends TextFrame implements MouseListener {
  49. /** Serial version UID. */
  50. private static final long serialVersionUID = 3;
  51. /** Factory to create an {@link InputTextFramePasteAction}. */
  52. private final InputTextFramePasteActionFactory inputTextFramePasteActionFactory;
  53. /** Input field panel. */
  54. protected JPanel inputPanel;
  55. /** The InputHandler for our input field. */
  56. private InputHandler inputHandler;
  57. /** Frame input field. */
  58. private SwingInputField inputField;
  59. /** Popupmenu for this frame. */
  60. private JPopupMenu inputFieldPopup;
  61. /** Nick popup menu. */
  62. protected JPopupMenu nickPopup;
  63. /** Away label. */
  64. private AwayLabel awayLabel;
  65. /** Typing indicator label. */
  66. private TypingLabel typingLabel;
  67. /** Plugin Manager. */
  68. private final ServiceManager serviceManager;
  69. /** The controller to use to retrieve command information. */
  70. private final CommandController commandController;
  71. /** The bus to dispatch input events on. */
  72. private final EventBus eventBus;
  73. /**
  74. * Creates a new instance of InputFrame.
  75. *
  76. * @param deps The dependencies required by text frames.
  77. * @param inputFieldProvider The provider to use to create a new input field.
  78. * @param owner WritableFrameContainer owning this frame.
  79. */
  80. protected InputTextFrame(
  81. final TextFrameDependencies deps,
  82. final Provider<SwingInputField> inputFieldProvider,
  83. final InputTextFramePasteActionFactory inputTextFramePasteActionFactory,
  84. final WindowModel owner) {
  85. super(owner, owner.getInputModel().get().getCommandParser(), deps);
  86. serviceManager = deps.serviceManager;
  87. commandController = deps.commandController;
  88. eventBus = deps.eventBus;
  89. this.inputTextFramePasteActionFactory = inputTextFramePasteActionFactory;
  90. initComponents(inputFieldProvider, deps.tabCompleterUtils);
  91. getInputField().getTextField().getInputMap().put(
  92. KeyStroke.getKeyStroke(KeyEvent.VK_C, UIUtilities.getCtrlMask()), "textpaneCopy");
  93. getInputField().getTextField().getInputMap().put(KeyStroke.getKeyStroke(
  94. KeyEvent.VK_C, UIUtilities.getCtrlMask()
  95. | KeyEvent.SHIFT_DOWN_MASK), "textpaneCopy");
  96. getInputField().getTextField().getActionMap().put("textpaneCopy",
  97. new InputFieldCopyAction(getTextPane(), getInputField().getTextField()));
  98. }
  99. /**
  100. * Initialises the instance, adding any required listeners.
  101. */
  102. @Override
  103. public void init() {
  104. getContainer().getConfigManager().getBinder().bind(this, InputTextFrame.class);
  105. getInputField().addMouseListener(this);
  106. eventBus.subscribe(awayLabel);
  107. eventBus.subscribe(typingLabel);
  108. super.init();
  109. }
  110. /**
  111. * Initialises the components for this frame.
  112. *
  113. * @param inputFieldProvider The provider to use to create a new input field.
  114. */
  115. private void initComponents(final Provider<SwingInputField> inputFieldProvider,
  116. final TabCompleterUtils tabCompleterUtils) {
  117. inputField = inputFieldProvider.get();
  118. inputHandler = new SwingInputHandler(serviceManager, inputField, commandController,
  119. getContainer().getInputModel().get().getCommandParser(), getContainer(),
  120. tabCompleterUtils, eventBus);
  121. inputHandler.addValidationListener(inputField);
  122. inputHandler.setTabCompleter(frameParent.getInputModel().get().getTabCompleter());
  123. initPopupMenu();
  124. nickPopup = new JPopupMenu();
  125. inputPanel = new JPanel(new BorderLayout(
  126. (int) PlatformDefaults.getUnitValueX("related").getValue(),
  127. (int) PlatformDefaults.getUnitValueX("related").getValue()));
  128. inputPanel.add(awayLabel, BorderLayout.LINE_START);
  129. inputPanel.add(inputField, BorderLayout.CENTER);
  130. inputPanel.add(typingLabel, BorderLayout.LINE_END);
  131. initInputField();
  132. }
  133. /** Initialises the popupmenu. */
  134. private void initPopupMenu() {
  135. inputFieldPopup = new JPopupMenu();
  136. inputFieldPopup.add(new CutAction(getInputField().getTextField()));
  137. inputFieldPopup.add(new CopyAction(getInputField().getTextField()));
  138. inputFieldPopup.add(inputTextFramePasteActionFactory.getInputTextFramePasteAction(this,
  139. inputField, getContainer()));
  140. inputFieldPopup.setOpaque(true);
  141. inputFieldPopup.setLightWeightPopupEnabled(true);
  142. awayLabel = new AwayLabel(getContainer());
  143. typingLabel = new TypingLabel(getContainer());
  144. }
  145. /**
  146. * Initialises the input field.
  147. */
  148. private void initInputField() {
  149. UIUtilities.addUndoManager(getInputField().getTextField());
  150. getInputField().getActionMap().put("paste", inputTextFramePasteActionFactory
  151. .getInputTextFramePasteAction(this, inputField, getContainer()));
  152. getInputField().getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke(
  153. "shift INSERT"), "paste");
  154. getInputField().getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ctrl V"), "paste");
  155. }
  156. /**
  157. * Returns the input handler associated with this frame.
  158. *
  159. * @return Input handlers for this frame
  160. */
  161. public final InputHandler getInputHandler() {
  162. return inputHandler;
  163. }
  164. /**
  165. * Returns the input field for this frame.
  166. *
  167. * @return SwingInputField input field for the frame.
  168. */
  169. public final SwingInputField getInputField() {
  170. return inputField;
  171. }
  172. @Override
  173. public void mouseClicked(final MouseEvent mouseEvent) {
  174. if (mouseEvent.getSource() == getTextPane()) {
  175. processMouseEvent(mouseEvent);
  176. }
  177. }
  178. @Override
  179. public void mousePressed(final MouseEvent mouseEvent) {
  180. processMouseEvent(mouseEvent);
  181. }
  182. @Override
  183. public void mouseReleased(final MouseEvent mouseEvent) {
  184. processMouseEvent(mouseEvent);
  185. }
  186. @Override
  187. public void mouseExited(final MouseEvent mouseEvent) {
  188. //Ignore
  189. }
  190. @Override
  191. public void mouseEntered(final MouseEvent mouseEvent) {
  192. //Ignore
  193. }
  194. @Override
  195. public void processMouseEvent(final MouseEvent e) {
  196. if (e.isPopupTrigger() && e.getSource() == getInputField()) {
  197. final Point point = getInputField().getMousePosition();
  198. if (point != null) {
  199. initPopupMenu();
  200. inputFieldPopup.show(this, (int) point.getX(),
  201. (int) point.getY() + getTextPane().getHeight()
  202. + (int) PlatformDefaults.
  203. getUnitValueX("related").getValue());
  204. }
  205. }
  206. }
  207. @ConfigBinding(domain="ui", key="inputbackgroundcolour",
  208. fallbacks = {"ui", "backgroundcolour"}, invocation = EDTInvocation.class)
  209. public void handleInputBackgroundColour(final String value) {
  210. if (getInputField() == null || UIUtilities.isGTKUI()) {
  211. return;
  212. }
  213. getInputField().setBackground(UIUtilities.convertColour(
  214. colourManager.getColourFromString(value, null)));
  215. }
  216. @ConfigBinding(domain = "ui", key="inputforegroundcolour", fallbacks = {"ui",
  217. "foregroundcolour"}, invocation = EDTInvocation.class)
  218. public void handleInputForegroundColour(final String value) {
  219. if (getInputField() == null || UIUtilities.isGTKUI()) {
  220. return;
  221. }
  222. final Color colour = UIUtilities.convertColour(colourManager.getColourFromString(value, null));
  223. getInputField().setForeground(colour);
  224. getInputField().setCaretColor(colour);
  225. }
  226. @Override
  227. public void activateFrame() {
  228. super.activateFrame();
  229. inputField.requestFocusInWindow();
  230. }
  231. @Override
  232. public void dispose() {
  233. getInputField().removeMouseListener(this);
  234. eventBus.unsubscribe(awayLabel);
  235. eventBus.unsubscribe(typingLabel);
  236. getContainer().getConfigManager().getBinder().unbind(this);
  237. super.dispose();
  238. }
  239. }