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

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