Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

InputTextFramePasteAction.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.components.inputfields.SwingInputField;
  25. import com.dmdirc.addons.ui_swing.dialogs.paste.PasteDialogFactory;
  26. import com.dmdirc.interfaces.WindowModel;
  27. import java.awt.Toolkit;
  28. import java.awt.Window;
  29. import java.awt.datatransfer.Clipboard;
  30. import java.awt.datatransfer.DataFlavor;
  31. import java.awt.datatransfer.UnsupportedFlavorException;
  32. import java.awt.event.ActionEvent;
  33. import java.io.IOException;
  34. import javax.swing.AbstractAction;
  35. import org.slf4j.Logger;
  36. import org.slf4j.LoggerFactory;
  37. import static com.dmdirc.util.LogUtils.USER_ERROR;
  38. /**
  39. * Paste action for input frames.
  40. */
  41. public final class InputTextFramePasteAction extends AbstractAction {
  42. private static final Logger LOG = LoggerFactory.getLogger(InputTextFramePasteAction.class);
  43. /** A version number for this class. */
  44. private static final long serialVersionUID = 1;
  45. /** Clipboard to paste from. */
  46. private final Clipboard clipboard;
  47. /** Text component to be acted upon. */
  48. private final InputTextFrame inputFrame;
  49. /** Event bus to post events to. */
  50. private final DMDircMBassador eventBus;
  51. /** Swing input field. */
  52. private final SwingInputField inputField;
  53. /** Frame container. */
  54. private final WindowModel container;
  55. /** Paste dialog factory. */
  56. private final PasteDialogFactory pasteDialogFactory;
  57. /** Window to parent the dialog on. */
  58. private final Window window;
  59. /**
  60. * Instantiates a new paste action.
  61. *
  62. * @param clipboard Clipboard to paste from
  63. * @param inputFrame Component to be acted upon
  64. */
  65. public InputTextFramePasteAction(final InputTextFrame inputFrame,
  66. final SwingInputField inputField,
  67. final WindowModel container,
  68. final Clipboard clipboard,
  69. final DMDircMBassador eventBus,
  70. final PasteDialogFactory pasteDialogFactory,
  71. final Window window) {
  72. super("Paste");
  73. this.clipboard = clipboard;
  74. this.inputFrame = inputFrame;
  75. this.eventBus = eventBus;
  76. this.inputField = inputField;
  77. this.container = container;
  78. this.pasteDialogFactory = pasteDialogFactory;
  79. this.window = window;
  80. }
  81. @Override
  82. public void actionPerformed(final ActionEvent e) {
  83. //wrap in try and catch for when clipboard doesn't exist
  84. //only seems to happen on linux and I couldnt see an obvious JVM bug
  85. try {
  86. if (!clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
  87. return;
  88. }
  89. } catch (final IllegalStateException ex) {
  90. LOG.info(USER_ERROR, "Unable to paste from clipboard.", ex);
  91. return;
  92. }
  93. try {
  94. //get the contents of the input field and combine it with the
  95. //clipboard
  96. doPaste((String) Toolkit.getDefaultToolkit()
  97. .getSystemClipboard().getData(DataFlavor.stringFlavor));
  98. } catch (final IOException ex) {
  99. LOG.info(USER_ERROR, "Unable to get clipboard contents: {}",
  100. ex.getMessage(), ex);
  101. } catch (final UnsupportedFlavorException ex) {
  102. LOG.info(USER_ERROR, "Unsupported clipboard type: {}",
  103. ex.getMessage(), ex);
  104. }
  105. }
  106. /**
  107. * Pastes the specified content into the input area.
  108. *
  109. * @param clipboard The contents of the clipboard to be pasted
  110. *
  111. * @since 0.6.3m1
  112. */
  113. public void doPaste(final String clipboard) {
  114. final String inputFieldText = inputField.getText();
  115. //Get the text that would result from the paste (inputfield
  116. //- selection + clipboard)
  117. final String text = inputFieldText.substring(0, inputField.getSelectionStart())
  118. + clipboard + inputFieldText.substring(inputField.getSelectionEnd());
  119. final String[] clipboardLines = getSplitLine(text);
  120. //check theres something to paste
  121. if (clipboardLines.length > 1) {
  122. //Clear the input field
  123. inputField.setText("");
  124. final Integer pasteTrigger = container.getConfigManager().
  125. getOptionInt("ui", "pasteProtectionLimit", false);
  126. //check whether the number of lines is over the limit
  127. if (pasteTrigger != null && container.getInputModel().get().getNumLines(text) >
  128. pasteTrigger) {
  129. //show the multi line paste dialog
  130. pasteDialogFactory.getPasteDialog(inputFrame, text, window).displayOrRequestFocus();
  131. } else {
  132. //send the lines
  133. for (final String clipboardLine : clipboardLines) {
  134. inputFrame.getContainer().getInputModel()
  135. .ifPresent(im -> im.sendLine(clipboardLine));
  136. }
  137. }
  138. } else {
  139. //put clipboard text in input field
  140. inputField.replaceSelection(clipboard);
  141. }
  142. }
  143. /**
  144. * Splits the line on all line endings.
  145. *
  146. * @param line Line that will be split
  147. *
  148. * @return Split line array
  149. */
  150. private String[] getSplitLine(final String line) {
  151. return line.replace("\r\n", "\n").replace('\r', '\n').split("\n");
  152. }
  153. @Override
  154. @SuppressWarnings("PMD.AvoidCatchingNPE")
  155. public boolean isEnabled() {
  156. try {
  157. return clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor);
  158. } catch (NullPointerException | IllegalStateException ex) { //https://bugs.openjdk.java.net/browse/JDK-7000965
  159. return false;
  160. }
  161. }
  162. }