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

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