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

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