您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

InputTextFrame.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.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().getCommandParser(), getContainer(), tabCompleterUtils, eventBus);
  126. inputHandler.addValidationListener(inputField);
  127. inputHandler.setTabCompleter(frameParent.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(eventBus, 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. @Override
  167. public final InputHandler getInputHandler() {
  168. return inputHandler;
  169. }
  170. /**
  171. * Returns the input field for this frame.
  172. *
  173. * @return SwingInputField input field for the frame.
  174. */
  175. public final SwingInputField getInputField() {
  176. return inputField;
  177. }
  178. @Override
  179. public void mouseClicked(final MouseEvent mouseEvent) {
  180. if (mouseEvent.getSource() == getTextPane()) {
  181. processMouseEvent(mouseEvent);
  182. }
  183. }
  184. @Override
  185. public void mousePressed(final MouseEvent mouseEvent) {
  186. processMouseEvent(mouseEvent);
  187. }
  188. @Override
  189. public void mouseReleased(final MouseEvent mouseEvent) {
  190. processMouseEvent(mouseEvent);
  191. }
  192. @Override
  193. public void mouseExited(final MouseEvent mouseEvent) {
  194. //Ignore
  195. }
  196. @Override
  197. public void mouseEntered(final MouseEvent mouseEvent) {
  198. //Ignore
  199. }
  200. @Override
  201. public void processMouseEvent(final MouseEvent e) {
  202. if (e.isPopupTrigger() && e.getSource() == getInputField()) {
  203. final Point point = getInputField().getMousePosition();
  204. if (point != null) {
  205. initPopupMenu();
  206. inputFieldPopup.show(this, (int) point.getX(),
  207. (int) point.getY() + getTextPane().getHeight()
  208. + (int) PlatformDefaults.
  209. getUnitValueX("related").getValue());
  210. }
  211. }
  212. }
  213. @ConfigBinding(domain="ui", key="inputbackgroundcolour",
  214. fallbacks = {"ui", "backgroundcolour"}, invocation = EDTInvocation.class)
  215. public void handleInputBackgroundColour(final String value) {
  216. if (getInputField() == null || UIUtilities.isGTKUI()) {
  217. return;
  218. }
  219. getInputField().setBackground(UIUtilities.convertColour(
  220. colourManager.getColourFromString(value, null)));
  221. }
  222. @ConfigBinding(domain = "ui", key="inputforegroundcolour", fallbacks = {"ui",
  223. "foregroundcolour"}, invocation = EDTInvocation.class)
  224. public void handleInputForegroundColour(final String value) {
  225. if (getInputField() == null || UIUtilities.isGTKUI()) {
  226. return;
  227. }
  228. final Color colour = UIUtilities.convertColour(colourManager.getColourFromString(value, null));
  229. getInputField().setForeground(colour);
  230. getInputField().setCaretColor(colour);
  231. }
  232. @Override
  233. public void activateFrame() {
  234. super.activateFrame();
  235. inputField.requestFocusInWindow();
  236. }
  237. @Override
  238. public void dispose() {
  239. getInputField().removeMouseListener(this);
  240. eventBus.unsubscribe(awayLabel);
  241. eventBus.unsubscribe(typingLabel);
  242. getContainer().getConfigManager().getBinder().unbind(this);
  243. super.dispose();
  244. }
  245. }