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.

PasteDialog.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.dialogs.paste;
  23. import com.dmdirc.addons.ui_swing.UIUtilities;
  24. import com.dmdirc.addons.ui_swing.components.frames.InputTextFrame;
  25. import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
  26. import com.dmdirc.addons.ui_swing.components.SwingInputHandler;
  27. import com.dmdirc.addons.ui_swing.components.TextAreaInputField;
  28. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  29. import java.awt.Window;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import java.awt.event.KeyEvent;
  33. import java.awt.event.KeyListener;
  34. import javax.swing.AbstractAction;
  35. import javax.swing.JButton;
  36. import javax.swing.JComponent;
  37. import javax.swing.JScrollPane;
  38. import javax.swing.KeyStroke;
  39. import javax.swing.SwingUtilities;
  40. import javax.swing.WindowConstants;
  41. import net.miginfocom.swing.MigLayout;
  42. /**
  43. * Allows the user to confirm and modify a multi-line paste.
  44. *
  45. * @author Greboid
  46. */
  47. public final class PasteDialog extends StandardDialog implements ActionListener,
  48. KeyListener {
  49. /**
  50. * A version number for this class. It should be changed whenever the class
  51. * structure is changed (or anything else that would prevent serialized
  52. * objects being unserialized with the new class).
  53. */
  54. private static final long serialVersionUID = 4;
  55. /** Number of lines Label. */
  56. private TextLabel infoLabel;
  57. /** Text area scrollpane. */
  58. private JScrollPane scrollPane;
  59. /** Text area. */
  60. private TextAreaInputField textField;
  61. /** Parent frame. */
  62. private final InputTextFrame parent;
  63. /** Edit button. */
  64. private JButton editButton;
  65. /** Parent window. */
  66. private Window parentWindow;
  67. /**
  68. * Creates a new instance of PreferencesDialog.
  69. *
  70. * @param newParent The frame that owns this dialog
  71. * @param text text to show in the paste dialog
  72. * @param parentWindow Parent window
  73. */
  74. public PasteDialog(final InputTextFrame newParent, final String text,
  75. final Window parentWindow) {
  76. super(parentWindow, ModalityType.MODELESS);
  77. this.parent = newParent;
  78. this.parentWindow = parentWindow;
  79. initComponents(text);
  80. initListeners();
  81. setFocusTraversalPolicy(new PasteDialogFocusTraversalPolicy(
  82. getCancelButton(), editButton, getOkButton()));
  83. setFocusable(true);
  84. getOkButton().requestFocusInWindow();
  85. getOkButton().setSelected(true);
  86. }
  87. /**
  88. * Initialises GUI components.
  89. *
  90. * @param text text to show in the dialog
  91. */
  92. private void initComponents(final String text) {
  93. scrollPane = new JScrollPane();
  94. textField = new TextAreaInputField(text);
  95. editButton = new JButton("Edit");
  96. infoLabel = new TextLabel();
  97. UIUtilities.addUndoManager(textField);
  98. orderButtons(new JButton(), new JButton());
  99. getOkButton().setText("Send");
  100. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  101. setTitle("Multi-line paste");
  102. setResizable(false);
  103. infoLabel.setText("This will be sent as "
  104. + parent.getContainer().getNumLines(textField.getText())
  105. + " lines. Are you sure you want to continue?");
  106. textField.setColumns(50);
  107. textField.setRows(10);
  108. new SwingInputHandler(textField, parent.getCommandParser(), parent)
  109. .setTypes(false, false, true, false);
  110. scrollPane.setViewportView(textField);
  111. scrollPane.setVisible(false);
  112. getContentPane().setLayout(new MigLayout("fill, hidemode 3"));
  113. getContentPane().add(infoLabel, "wrap, growx, pushx, span 3");
  114. getContentPane().add(scrollPane, "wrap, grow, push, span 3");
  115. getContentPane().add(getLeftButton(), "right, sg button");
  116. getContentPane().add(editButton, "right, sg button");
  117. getContentPane().add(getRightButton(), "right, sg button");
  118. }
  119. /**
  120. * Initialises listeners for this dialog.
  121. */
  122. private void initListeners() {
  123. getOkButton().addActionListener(this);
  124. getCancelButton().addActionListener(this);
  125. editButton.addActionListener(this);
  126. textField.addKeyListener(this);
  127. getRootPane().getActionMap().put("rightArrowAction",
  128. new AbstractAction("rightArrowAction") {
  129. private static final long serialVersionUID = 1;
  130. /** {@inheritDoc} */
  131. @Override
  132. public void actionPerformed(final ActionEvent evt) {
  133. final JButton button = (JButton) getFocusTraversalPolicy().
  134. getComponentAfter(PasteDialog.this, getFocusOwner());
  135. button.requestFocusInWindow();
  136. button.setSelected(true);
  137. }
  138. });
  139. getRootPane().getActionMap().put("leftArrowAction",
  140. new AbstractAction("leftArrowAction") {
  141. private static final long serialVersionUID = 1;
  142. /** {@inheritDoc} */
  143. @Override
  144. public void actionPerformed(final ActionEvent evt) {
  145. final JButton button = (JButton) getFocusTraversalPolicy().
  146. getComponentBefore(PasteDialog.this, getFocusOwner());
  147. button.requestFocusInWindow();
  148. button.setSelected(true);
  149. }
  150. });
  151. getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
  152. KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "rightArrowAction");
  153. getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
  154. KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "leftArrowAction");
  155. }
  156. /**
  157. * Handles the actions for the dialog.
  158. *
  159. * @param actionEvent Action event
  160. */
  161. @Override
  162. public void actionPerformed(final ActionEvent actionEvent) {
  163. if (getOkButton().equals(actionEvent.getSource())) {
  164. if (!textField.getText().isEmpty()) {
  165. final String[] lines = textField.getText().split("(\n|\r\n|\r)",
  166. Integer.MAX_VALUE);
  167. for (String line : lines) {
  168. if (!line.isEmpty()) {
  169. parent.getContainer().sendLine(line);
  170. parent.getInputHandler().addToBuffer(line);
  171. }
  172. }
  173. }
  174. dispose();
  175. } else if (editButton.equals(actionEvent.getSource())) {
  176. editButton.setEnabled(false);
  177. setResizable(true);
  178. scrollPane.setVisible(true);
  179. infoLabel.setText("This will be sent as "
  180. + parent.getContainer().getNumLines(textField.getText())
  181. + " lines.");
  182. setResizable(true);
  183. pack();
  184. SwingUtilities.invokeLater(new Runnable(){
  185. @Override
  186. public void run() {
  187. setLocationRelativeTo(parentWindow);
  188. }
  189. });
  190. } else if (getCancelButton().equals(actionEvent.getSource())) {
  191. dispose();
  192. }
  193. }
  194. /**
  195. * {@inheritDoc}
  196. *
  197. * @param e Key event
  198. */
  199. @Override
  200. public void keyTyped(final KeyEvent e) {
  201. infoLabel.setText("This will be sent as "
  202. + parent.getContainer().getNumLines(textField.getText())
  203. + " lines.");
  204. }
  205. /**
  206. * {@inheritDoc}
  207. *
  208. * @param e Key event
  209. */
  210. @Override
  211. public void keyPressed(final KeyEvent e) {
  212. //Ignore.
  213. }
  214. /**
  215. * {@inheritDoc}
  216. *
  217. * @param e Key event
  218. */
  219. @Override
  220. public void keyReleased(final KeyEvent e) {
  221. //Ignore.
  222. }
  223. }