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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.WritableFrameContainer;
  24. import com.dmdirc.addons.ui_swing.SwingController;
  25. import com.dmdirc.commandparser.PopupManager;
  26. import com.dmdirc.commandparser.PopupMenu;
  27. import com.dmdirc.commandparser.PopupMenuItem;
  28. import com.dmdirc.commandparser.PopupType;
  29. import com.dmdirc.config.ConfigManager;
  30. import com.dmdirc.interfaces.AwayStateListener;
  31. import com.dmdirc.logger.ErrorLevel;
  32. import com.dmdirc.logger.Logger;
  33. import com.dmdirc.ui.input.InputHandler;
  34. import com.dmdirc.ui.interfaces.InputWindow;
  35. import com.dmdirc.addons.ui_swing.UIUtilities;
  36. import com.dmdirc.addons.ui_swing.actions.CopyAction;
  37. import com.dmdirc.addons.ui_swing.actions.CutAction;
  38. import com.dmdirc.addons.ui_swing.actions.InputTextFramePasteAction;
  39. import com.dmdirc.addons.ui_swing.dialogs.paste.PasteDialog;
  40. import com.dmdirc.addons.ui_swing.actions.CommandAction;
  41. import com.dmdirc.addons.ui_swing.components.SwingInputField;
  42. import java.awt.BorderLayout;
  43. import java.awt.Point;
  44. import java.awt.Toolkit;
  45. import java.awt.datatransfer.DataFlavor;
  46. import java.awt.datatransfer.UnsupportedFlavorException;
  47. import java.awt.event.MouseEvent;
  48. import java.io.IOException;
  49. import javax.swing.JComponent;
  50. import javax.swing.JLabel;
  51. import javax.swing.JMenu;
  52. import javax.swing.JMenuItem;
  53. import javax.swing.JPanel;
  54. import javax.swing.JPopupMenu;
  55. import javax.swing.JSeparator;
  56. import javax.swing.KeyStroke;
  57. import javax.swing.event.InternalFrameEvent;
  58. import net.miginfocom.layout.PlatformDefaults;
  59. /**
  60. * Frame with an input field.
  61. */
  62. public abstract class InputTextFrame extends TextFrame implements InputWindow,
  63. AwayStateListener {
  64. /**
  65. * A version number for this class. It should be changed whenever the class
  66. * structure is changed (or anything else that would prevent serialized
  67. * objects being unserialized with the new class).
  68. */
  69. private static final long serialVersionUID = 2;
  70. /** Input field panel. */
  71. protected JPanel inputPanel;
  72. /** Away label. */
  73. protected JLabel awayLabel;
  74. /** The InputHandler for our input field. */
  75. private InputHandler inputHandler;
  76. /** Frame input field. */
  77. private SwingInputField inputField;
  78. /** Popupmenu for this frame. */
  79. private JPopupMenu inputFieldPopup;
  80. /** Nick popup menu. */
  81. protected JPopupMenu nickPopup;
  82. /**
  83. * Creates a new instance of InputFrame.
  84. *
  85. * @param owner WritableFrameContainer owning this frame.
  86. * @param controller Swing controller
  87. */
  88. public InputTextFrame(final WritableFrameContainer owner, final SwingController controller) {
  89. super(owner, controller);
  90. initComponents();
  91. final ConfigManager config = owner.getConfigManager();
  92. getInputField().setBackground(config.getOptionColour(
  93. "ui", "inputbackgroundcolour",
  94. "ui", "backgroundcolour"));
  95. getInputField().setForeground(config.getOptionColour(
  96. "ui", "inputforegroundcolour",
  97. "ui", "foregroundcolour"));
  98. getInputField().setCaretColor(config.getOptionColour(
  99. "ui", "inputforegroundcolour",
  100. "ui", "foregroundcolour"));
  101. config.addChangeListener("ui", "inputforegroundcolour", this);
  102. config.addChangeListener("ui", "inputbackgroundcolour", this);
  103. if (getContainer().getServer() != null) {
  104. getContainer().getServer().addAwayStateListener(this);
  105. }
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. public void open() {
  110. UIUtilities.invokeLater(new Runnable() {
  111. /** {@inheritDoc} */
  112. @Override
  113. public void run() {
  114. InputTextFrame.super.open();
  115. if (getConfigManager().getOptionBool("ui", "awayindicator") && getContainer().
  116. getServer() != null) {
  117. awayLabel.setVisible(getContainer().getServer().isAway());
  118. }
  119. inputField.requestFocusInWindow();
  120. }
  121. });
  122. }
  123. /**
  124. * Initialises the components for this frame.
  125. */
  126. private void initComponents() {
  127. setInputField(new SwingInputField(getController().getMainFrame()));
  128. getInputField().addKeyListener(this);
  129. getInputField().addMouseListener(this);
  130. initPopupMenu();
  131. nickPopup = new JPopupMenu();
  132. awayLabel = new JLabel();
  133. awayLabel.setText("(away)");
  134. awayLabel.setVisible(false);
  135. inputPanel = new JPanel(new BorderLayout(
  136. (int) PlatformDefaults.getUnitValueX("related").getValue(),
  137. (int) PlatformDefaults.getUnitValueX("related").getValue()));
  138. inputPanel.add(awayLabel, BorderLayout.LINE_START);
  139. inputPanel.add(inputField, BorderLayout.CENTER);
  140. initInputField();
  141. }
  142. /** Initialises the popupmenu. */
  143. private void initPopupMenu() {
  144. inputFieldPopup = new JPopupMenu();
  145. inputFieldPopup.add(new CutAction(getInputField().getTextField()));
  146. inputFieldPopup.add(new CopyAction(getInputField().getTextField()));
  147. inputFieldPopup.add(new InputTextFramePasteAction(this));
  148. inputFieldPopup.setOpaque(true);
  149. inputFieldPopup.setLightWeightPopupEnabled(true);
  150. }
  151. /**
  152. * Initialises the input field.
  153. */
  154. private void initInputField() {
  155. UIUtilities.addUndoManager(getInputField().getTextField());
  156. getInputField().getActionMap().put("paste",
  157. new InputTextFramePasteAction(this));
  158. getInputField().getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke("shift INSERT"),
  159. "paste");
  160. getInputField().getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ctrl V"),
  161. "paste");
  162. }
  163. /**
  164. * Returns the container associated with this frame.
  165. *
  166. * @return This frame's container.
  167. */
  168. @Override
  169. public WritableFrameContainer getContainer() {
  170. return (WritableFrameContainer) super.getContainer();
  171. }
  172. /**
  173. * Returns the input handler associated with this frame.
  174. *
  175. * @return Input handlers for this frame
  176. */
  177. @Override
  178. public final InputHandler getInputHandler() {
  179. return inputHandler;
  180. }
  181. /**
  182. * Sets the input handler for this frame.
  183. *
  184. * @param newInputHandler input handler to set for this frame
  185. */
  186. public final void setInputHandler(final InputHandler newInputHandler) {
  187. this.inputHandler = newInputHandler;
  188. inputHandler.addValidationListener(inputField);
  189. }
  190. /**
  191. * Returns the input field for this frame.
  192. *
  193. * @return SwingInputField input field for the frame.
  194. */
  195. public final SwingInputField getInputField() {
  196. return inputField;
  197. }
  198. /**
  199. * Sets the frames input field.
  200. *
  201. * @param newInputField new input field to use
  202. */
  203. protected final void setInputField(final SwingInputField newInputField) {
  204. this.inputField = newInputField;
  205. }
  206. /**
  207. * Returns the away label for this server connection.
  208. *
  209. * @return JLabel away label
  210. */
  211. public JLabel getAwayLabel() {
  212. return awayLabel;
  213. }
  214. /**
  215. * Sets the away indicator on or off.
  216. *
  217. * @param awayState away state
  218. */
  219. @Override
  220. public void setAwayIndicator(final boolean awayState) {
  221. final boolean awayIndicator = getConfigManager().
  222. getOptionBool("ui", "awayindicator");
  223. if (awayIndicator || !awayState) {
  224. if (awayState) {
  225. inputPanel.add(awayLabel, BorderLayout.LINE_START);
  226. awayLabel.setVisible(true);
  227. } else {
  228. awayLabel.setVisible(false);
  229. }
  230. }
  231. }
  232. /**
  233. * Checks for url's, channels and nicknames. {@inheritDoc}
  234. */
  235. @Override
  236. public void mouseClicked(final MouseEvent mouseEvent) {
  237. if (mouseEvent.getSource() == getTextPane()) {
  238. processMouseEvent(mouseEvent);
  239. }
  240. super.mouseClicked(mouseEvent);
  241. }
  242. /**
  243. * Not needed for this class. {@inheritDoc}
  244. */
  245. @Override
  246. public void mousePressed(final MouseEvent mouseEvent) {
  247. processMouseEvent(mouseEvent);
  248. super.mousePressed(mouseEvent);
  249. }
  250. /**
  251. * Not needed for this class. {@inheritDoc}
  252. */
  253. @Override
  254. public void mouseReleased(final MouseEvent mouseEvent) {
  255. processMouseEvent(mouseEvent);
  256. super.mouseReleased(mouseEvent);
  257. }
  258. /**
  259. * Processes every mouse button event to check for a popup trigger.
  260. *
  261. * @param e mouse event
  262. */
  263. @Override
  264. public void processMouseEvent(final MouseEvent e) {
  265. if (e.isPopupTrigger() && e.getSource() == getInputField()) {
  266. final Point point = getInputField().getMousePosition();
  267. if (point != null) {
  268. initPopupMenu();
  269. inputFieldPopup.show(this, (int) point.getX(),
  270. (int) point.getY() + getTextPane().getHeight() +
  271. (int) PlatformDefaults.getUnitValueX("related").getValue());
  272. }
  273. }
  274. super.processMouseEvent(e);
  275. }
  276. /** Checks and pastes text. */
  277. public void doPaste() {
  278. String clipboard = null;
  279. try {
  280. if (!Toolkit.getDefaultToolkit().getSystemClipboard().
  281. isDataFlavorAvailable(DataFlavor.stringFlavor)) {
  282. return;
  283. }
  284. } catch (IllegalStateException ex) {
  285. Logger.userError(ErrorLevel.LOW, "Unable to paste from clipboard.");
  286. return;
  287. }
  288. try {
  289. //get the contents of the input field and combine it with the clipboard
  290. clipboard = (String) Toolkit.getDefaultToolkit().
  291. getSystemClipboard().getData(DataFlavor.stringFlavor);
  292. doPaste(clipboard);
  293. } catch (IOException ex) {
  294. Logger.userError(ErrorLevel.LOW, "Unable to get clipboard contents: " +
  295. ex.getMessage());
  296. } catch (UnsupportedFlavorException ex) {
  297. Logger.userError(ErrorLevel.LOW, "Unsupported clipboard type", ex);
  298. }
  299. }
  300. /**
  301. * Pastes the specified content into the input area.
  302. *
  303. * @param clipboard The contents of the clipboard to be pasted
  304. * @since 0.6.3m1
  305. */
  306. protected void doPaste(final String clipboard) {
  307. String[] clipboardLines;
  308. //check theres something to paste
  309. if (clipboard != null && (clipboardLines = getSplitLine(clipboard)).length > 1) {
  310. final int caretPosition = getInputField().getCaretPosition();
  311. final String inputFieldText = getInputField().getText();
  312. final String text = inputFieldText.substring(0, caretPosition) + clipboard + inputFieldText.substring(caretPosition);
  313. //check the limit
  314. final Integer pasteTrigger = getConfigManager().getOptionInt("ui",
  315. "pasteProtectionLimit");
  316. //check whether the number of lines is over the limit
  317. if (pasteTrigger != null && getContainer().getNumLines(text) > pasteTrigger) {
  318. //show the multi line paste dialog
  319. new PasteDialog(this, text, getController().getMainFrame()).display();
  320. inputField.setText("");
  321. } else {
  322. //send the lines
  323. for (String clipboardLine : clipboardLines) {
  324. getContainer().sendLine(clipboardLine);
  325. }
  326. }
  327. } else {
  328. inputField.replaceSelection(clipboard);
  329. }
  330. }
  331. /**
  332. * Splits the line on all line endings.
  333. *
  334. * @param line Line that will be split
  335. *
  336. * @return Split line array
  337. */
  338. private String[] getSplitLine(final String line) {
  339. return line.replace("\r\n", "\n").replace('\r', '\n').split("\n");
  340. }
  341. /** {@inheritDoc} */
  342. @Override
  343. public void configChanged(final String domain, final String key) {
  344. super.configChanged(domain, key);
  345. if ("ui".equals(domain) && getInputField() != null &&
  346. getConfigManager() != null) {
  347. if ("inputbackgroundcolour".equals(key) ||
  348. "backgroundcolour".equals(key)) {
  349. getInputField().setBackground(getConfigManager().getOptionColour(
  350. "ui", "inputbackgroundcolour",
  351. "ui", "backgroundcolour"));
  352. } else if ("inputforegroundcolour".equals(key) ||
  353. "foregroundcolour".equals(key)) {
  354. getInputField().setForeground(getConfigManager().getOptionColour(
  355. "ui", "inputforegroundcolour",
  356. "ui", "foregroundcolour"));
  357. getInputField().setCaretColor(getConfigManager().getOptionColour(
  358. "ui", "inputforegroundcolour",
  359. "ui", "foregroundcolour"));
  360. }
  361. }
  362. }
  363. /**
  364. * Popuplates the nicklist popup.
  365. *
  366. * @param nickname Nickname for the popup
  367. */
  368. protected final void popuplateNicklistPopup(final String nickname) {
  369. final PopupMenu popups = PopupManager.getMenu(PopupType.CHAN_NICK,
  370. getConfigManager());
  371. nickPopup = (JPopupMenu) populatePopupMenu(new JPopupMenu(), popups,
  372. nickname);
  373. }
  374. /**
  375. * Populates the specified popupmenu
  376. *
  377. * @param menu Menu component
  378. * @param popup Popup to get info from
  379. * @param arguments Arguments for the command
  380. *
  381. * @return Populated popup
  382. */
  383. private JComponent populatePopupMenu(final JComponent menu,
  384. final PopupMenu popup, final Object... arguments) {
  385. for (PopupMenuItem menuItem : popup.getItems()) {
  386. if (menuItem.isDivider()) {
  387. menu.add(new JSeparator());
  388. } else if (menuItem.isSubMenu()) {
  389. menu.add(populatePopupMenu(new JMenu(menuItem.getName()),
  390. menuItem.getSubMenu(), arguments));
  391. } else {
  392. menu.add(new JMenuItem(new CommandAction(getCommandParser(),
  393. this, menuItem.getName(), menuItem.getCommand(arguments))));
  394. }
  395. }
  396. return menu;
  397. }
  398. /** Request input field focus. */
  399. public void requestInputFieldFocus() {
  400. if (inputField != null) {
  401. inputField.requestFocusInWindow();
  402. }
  403. }
  404. /** {@inheritDoc} */
  405. @Override
  406. public void onAway(final String reason) {
  407. setAwayIndicator(true);
  408. }
  409. /** {@inheritDoc} */
  410. @Override
  411. public void onBack() {
  412. setAwayIndicator(false);
  413. }
  414. /** {@inheritDoc} */
  415. @Override
  416. public void close() {
  417. super.close();
  418. if (getContainer() != null && getContainer().getServer() != null) {
  419. getContainer().getServer().removeAwayStateListener(this);
  420. }
  421. }
  422. /**
  423. * Activates the input field on frame focus. {@inheritDoc}
  424. *
  425. * @param event Internal frame event
  426. */
  427. @Override
  428. public void internalFrameActivated(final InternalFrameEvent event) {
  429. super.internalFrameActivated(event);
  430. getInputField().requestFocusInWindow();
  431. }
  432. }