Browse Source

Move doPaste method out of InputTextFrame.

pull/176/head
Greg Holmes 9 years ago
parent
commit
9649bcc92b

+ 9
- 99
ui_swing/src/com/dmdirc/addons/ui_swing/components/frames/InputTextFrame.java View File

@@ -33,12 +33,9 @@ import com.dmdirc.addons.ui_swing.components.AwayLabel;
33 33
 import com.dmdirc.addons.ui_swing.components.TypingLabel;
34 34
 import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputField;
35 35
 import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputHandler;
36
-import com.dmdirc.addons.ui_swing.dialogs.paste.PasteDialogFactory;
37 36
 import com.dmdirc.config.ConfigBinding;
38
-import com.dmdirc.events.UserErrorEvent;
39 37
 import com.dmdirc.interfaces.CommandController;
40 38
 import com.dmdirc.interfaces.ui.InputWindow;
41
-import com.dmdirc.logger.ErrorLevel;
42 39
 import com.dmdirc.plugins.PluginManager;
43 40
 import com.dmdirc.ui.input.InputHandler;
44 41
 import com.dmdirc.ui.input.TabCompleterUtils;
@@ -46,15 +43,9 @@ import com.dmdirc.ui.input.TabCompleterUtils;
46 43
 import java.awt.BorderLayout;
47 44
 import java.awt.Color;
48 45
 import java.awt.Point;
49
-import java.awt.Toolkit;
50
-import java.awt.Window;
51
-import java.awt.datatransfer.Clipboard;
52
-import java.awt.datatransfer.DataFlavor;
53
-import java.awt.datatransfer.UnsupportedFlavorException;
54 46
 import java.awt.event.KeyEvent;
55 47
 import java.awt.event.MouseEvent;
56 48
 import java.awt.event.MouseListener;
57
-import java.io.IOException;
58 49
 
59 50
 import javax.inject.Provider;
60 51
 import javax.swing.JPanel;
@@ -70,6 +61,8 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow, M
70 61
 
71 62
     /** Serial version UID. */
72 63
     private static final long serialVersionUID = 3;
64
+    /** Factory to create an {@link InputTextFramePasteAction}. */
65
+    private final InputTextFramePasteActionFactory inputTextFramePasteActionFactory;
73 66
     /** Input field panel. */
74 67
     protected JPanel inputPanel;
75 68
     /** The InputHandler for our input field. */
@@ -84,14 +77,8 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow, M
84 77
     private AwayLabel awayLabel;
85 78
     /** Typing indicator label. */
86 79
     private TypingLabel typingLabel;
87
-    /** Main frame. */
88
-    private final Provider<Window> parentWindow;
89 80
     /** Plugin Manager. */
90 81
     private final PluginManager pluginManager;
91
-    /** Paste dialog factory. */
92
-    private final PasteDialogFactory pasteDialogFactory;
93
-    /** Clipboard to use for copying and pasting. */
94
-    private final Clipboard clipboard;
95 82
     /** The controller to use to retrieve command information. */
96 83
     private final CommandController commandController;
97 84
     /** The bus to dispatch input events on. */
@@ -107,15 +94,14 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow, M
107 94
     protected InputTextFrame(
108 95
             final TextFrameDependencies deps,
109 96
             final Provider<SwingInputField> inputFieldProvider,
97
+            final InputTextFramePasteActionFactory inputTextFramePasteActionFactory,
110 98
             final FrameContainer owner) {
111 99
         super(owner, owner.getCommandParser(), deps);
112 100
 
113
-        parentWindow = deps.mainWindow;
114 101
         pluginManager = deps.pluginManager;
115
-        pasteDialogFactory = deps.pasteDialog;
116
-        clipboard = deps.clipboard;
117 102
         commandController = deps.commandController;
118 103
         eventBus = deps.eventBus;
104
+        this.inputTextFramePasteActionFactory = inputTextFramePasteActionFactory;
119 105
 
120 106
         initComponents(inputFieldProvider, deps.tabCompleterUtils);
121 107
 
@@ -165,7 +151,8 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow, M
165 151
 
166 152
         inputFieldPopup.add(new CutAction(getInputField().getTextField()));
167 153
         inputFieldPopup.add(new CopyAction(getInputField().getTextField()));
168
-        inputFieldPopup.add(new InputTextFramePasteAction(clipboard, this));
154
+        inputFieldPopup.add(inputTextFramePasteActionFactory.getInputTextFramePasteAction(this,
155
+                inputField, getContainer()));
169 156
         inputFieldPopup.setOpaque(true);
170 157
         inputFieldPopup.setLightWeightPopupEnabled(true);
171 158
 
@@ -181,12 +168,11 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow, M
181 168
     private void initInputField() {
182 169
         UIUtilities.addUndoManager(eventBus, getInputField().getTextField());
183 170
 
184
-        getInputField().getActionMap().put("paste",
185
-                new InputTextFramePasteAction(clipboard, this));
171
+        getInputField().getActionMap().put("paste", inputTextFramePasteActionFactory
172
+                .getInputTextFramePasteAction(this, inputField, getContainer()));
186 173
         getInputField().getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke(
187 174
                 "shift INSERT"), "paste");
188
-        getInputField().getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke(
189
-                "ctrl V"), "paste");
175
+        getInputField().getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ctrl V"), "paste");
190 176
     }
191 177
 
192 178
     /**
@@ -250,82 +236,6 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow, M
250 236
         }
251 237
     }
252 238
 
253
-    /** Checks and pastes text. */
254
-    public void doPaste() {
255
-        try {
256
-            if (!clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
257
-                return;
258
-            }
259
-        } catch (final IllegalStateException ex) {
260
-            eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, ex,
261
-                    "Unable to past from clipboard.", ""));
262
-            return;
263
-        }
264
-
265
-        try {
266
-            //get the contents of the input field and combine it with the
267
-            //clipboard
268
-            doPaste((String) Toolkit.getDefaultToolkit()
269
-                    .getSystemClipboard().getData(DataFlavor.stringFlavor));
270
-        } catch (final IOException ex) {
271
-            eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, ex,
272
-                    "Unable to get clipboard contents: " + ex.getMessage(), ""));
273
-        } catch (final UnsupportedFlavorException ex) {
274
-            eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, ex,
275
-                    "Unsupported clipboard type", ""));
276
-        }
277
-    }
278
-
279
-    /**
280
-     * Pastes the specified content into the input area.
281
-     *
282
-     * @param clipboard The contents of the clipboard to be pasted
283
-     *
284
-     * @since 0.6.3m1
285
-     */
286
-    protected void doPaste(final String clipboard) {
287
-        final String inputFieldText = getInputField().getText();
288
-        //Get the text that would result from the paste (inputfield
289
-        //- selection + clipboard)
290
-        final String text = inputFieldText.substring(0, getInputField().
291
-                getSelectionStart()) + clipboard + inputFieldText.substring(
292
-                        getInputField().getSelectionEnd());
293
-        final String[] clipboardLines = getSplitLine(text);
294
-        //check theres something to paste
295
-        if (clipboardLines.length > 1) {
296
-            //Clear the input field
297
-            inputField.setText("");
298
-            final Integer pasteTrigger = getContainer().getConfigManager().
299
-                    getOptionInt("ui", "pasteProtectionLimit", false);
300
-            //check whether the number of lines is over the limit
301
-            if (pasteTrigger != null && getContainer().getNumLines(text)
302
-                    > pasteTrigger) {
303
-                //show the multi line paste dialog
304
-                pasteDialogFactory.getPasteDialog(this, text, parentWindow.get()).
305
-                        displayOrRequestFocus();
306
-            } else {
307
-                //send the lines
308
-                for (final String clipboardLine : clipboardLines) {
309
-                    getContainer().sendLine(clipboardLine);
310
-                }
311
-            }
312
-        } else {
313
-            //put clipboard text in input field
314
-            inputField.replaceSelection(clipboard);
315
-        }
316
-    }
317
-
318
-    /**
319
-     * Splits the line on all line endings.
320
-     *
321
-     * @param line Line that will be split
322
-     *
323
-     * @return Split line array
324
-     */
325
-    private String[] getSplitLine(final String line) {
326
-        return line.replace("\r\n", "\n").replace('\r', '\n').split("\n");
327
-    }
328
-
329 239
     @ConfigBinding(domain="ui", key="inputbackgroundcolour",
330 240
             fallbacks = {"ui", "backgroundcolour"}, invocation = EDTInvocation.class)
331 241
     public void handleInputBackgroundColour(final String value) {

+ 103
- 2
ui_swing/src/com/dmdirc/addons/ui_swing/components/frames/InputTextFramePasteAction.java View File

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

+ 65
- 0
ui_swing/src/com/dmdirc/addons/ui_swing/components/frames/InputTextFramePasteActionFactory.java View File

@@ -0,0 +1,65 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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
+
23
+package com.dmdirc.addons.ui_swing.components.frames;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.FrameContainer;
27
+import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputField;
28
+import com.dmdirc.addons.ui_swing.dialogs.paste.PasteDialogFactory;
29
+import com.dmdirc.addons.ui_swing.injection.MainWindow;
30
+
31
+import java.awt.Window;
32
+import java.awt.datatransfer.Clipboard;
33
+
34
+import javax.inject.Inject;
35
+
36
+/**
37
+ * Factory to create an {@link InputTextFramePasteAction}.
38
+ */
39
+public class InputTextFramePasteActionFactory {
40
+
41
+    private final Clipboard clipboard;
42
+    private final DMDircMBassador eventBus;
43
+    private final PasteDialogFactory pasteDialogFactory;
44
+    private final Window window;
45
+
46
+    @Inject
47
+    public InputTextFramePasteActionFactory(
48
+            final Clipboard clipboard,
49
+            final DMDircMBassador eventBus,
50
+            final PasteDialogFactory pasteDialogFactory,
51
+            @MainWindow final Window window) {
52
+        this.clipboard = clipboard;
53
+        this.eventBus = eventBus;
54
+        this.pasteDialogFactory = pasteDialogFactory;
55
+        this.window = window;
56
+    }
57
+
58
+    public InputTextFramePasteAction getInputTextFramePasteAction(
59
+            final InputTextFrame inputFrame,
60
+            final SwingInputField inputField,
61
+            final FrameContainer container) {
62
+        return new InputTextFramePasteAction(inputFrame, inputField, container, clipboard,
63
+                eventBus, pasteDialogFactory, window);
64
+    }
65
+}

+ 3
- 3
ui_swing/src/com/dmdirc/addons/ui_swing/dialogs/paste/PasteDialogFactory.java View File

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.dialogs.paste;
24 24
 
25
-import com.dmdirc.ClientModule;
25
+import com.dmdirc.ClientModule.GlobalConfig;
26 26
 import com.dmdirc.DMDircMBassador;
27 27
 import com.dmdirc.addons.ui_swing.components.frames.InputTextFrame;
28 28
 import com.dmdirc.interfaces.CommandController;
@@ -53,8 +53,8 @@ public class PasteDialogFactory {
53 53
 
54 54
     @Inject
55 55
     public PasteDialogFactory(
56
-            @ClientModule.GlobalConfig final IconManager iconManager,
57
-            @ClientModule.GlobalConfig final AggregateConfigProvider config,
56
+            @GlobalConfig final IconManager iconManager,
57
+            @GlobalConfig final AggregateConfigProvider config,
58 58
             final PluginManager pluginManager,
59 59
             final CommandController commandController,
60 60
             final DMDircMBassador eventBus,

Loading…
Cancel
Save