Browse Source

DI the clipboard (or a fake if null.

Change-Id: I80f964a049442f6844be240af8394c4018afc282
Reviewed-on: http://gerrit.dmdirc.com/3389
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
changes/89/3389/3
Greg Holmes 10 years ago
parent
commit
f3137b57d8

+ 6
- 2
src/com/dmdirc/addons/ui_swing/DMDircEventQueue.java View File

@@ -34,6 +34,7 @@ import java.awt.AWTEvent;
34 34
 import java.awt.Component;
35 35
 import java.awt.EventQueue;
36 36
 import java.awt.Point;
37
+import java.awt.datatransfer.Clipboard;
37 38
 import java.awt.event.KeyEvent;
38 39
 import java.awt.event.MouseEvent;
39 40
 import java.awt.event.WindowEvent;
@@ -54,10 +55,13 @@ public class DMDircEventQueue extends EventQueue {
54 55
 
55 56
     /** Event bus to despatch events to. */
56 57
     private final EventBus eventBus;
58
+    /** Clipboard to copy and paste from. */
59
+    private final Clipboard clipboard;
57 60
 
58 61
     @Inject
59
-    public DMDircEventQueue(final EventBus eventBus) {
62
+    public DMDircEventQueue(final EventBus eventBus, final Clipboard clipboard) {
60 63
         this.eventBus = eventBus;
64
+        this.clipboard = clipboard;
61 65
     }
62 66
 
63 67
     @Override
@@ -124,7 +128,7 @@ public class DMDircEventQueue extends EventQueue {
124 128
         final JPopupMenu menu = new JPopupMenu();
125 129
         menu.add(new CutAction(tc));
126 130
         menu.add(new CopyAction(tc));
127
-        menu.add(new PasteAction(tc));
131
+        menu.add(new PasteAction(clipboard, tc));
128 132
 
129 133
         final Point pt = SwingUtilities.convertPoint(me.getComponent(),
130 134
                 me.getPoint(), tc);

+ 64
- 0
src/com/dmdirc/addons/ui_swing/NoopClipboard.java View File

@@ -0,0 +1,64 @@
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;
24
+
25
+import java.awt.datatransfer.Clipboard;
26
+import java.awt.datatransfer.ClipboardOwner;
27
+import java.awt.datatransfer.DataFlavor;
28
+import java.awt.datatransfer.Transferable;
29
+import java.awt.datatransfer.UnsupportedFlavorException;
30
+
31
+import javax.inject.Inject;
32
+
33
+/**
34
+ * System clipboard implementation that noops all operations.  This is used when no system clipboard
35
+ * can be found.
36
+ */
37
+public class NoopClipboard extends Clipboard {
38
+
39
+    @Inject
40
+    public NoopClipboard() {
41
+        super("Noop Clipboard");
42
+    }
43
+
44
+    @Override
45
+    public Transferable getContents(final Object requestor) {
46
+        return null;
47
+    }
48
+
49
+    @Override
50
+    public void setContents(final Transferable contents, final ClipboardOwner owner) {
51
+        //NOOP
52
+    }
53
+
54
+    @Override
55
+    public boolean isDataFlavorAvailable(final DataFlavor flavor) {
56
+        return false;
57
+    }
58
+
59
+    @Override
60
+    public Object getData(final DataFlavor flavor) {
61
+        return new UnsupportedFlavorException(flavor);
62
+    }
63
+
64
+}

+ 8
- 7
src/com/dmdirc/addons/ui_swing/actions/ChannelCopyAction.java View File

@@ -22,30 +22,34 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.actions;
24 24
 
25
-import java.awt.Toolkit;
25
+import java.awt.datatransfer.Clipboard;
26 26
 import java.awt.datatransfer.StringSelection;
27 27
 import java.awt.event.ActionEvent;
28 28
 
29 29
 import javax.swing.AbstractAction;
30 30
 
31 31
 /**
32
- * channel copy action.
32
+ * Channel copy action.
33 33
  */
34 34
 public final class ChannelCopyAction extends AbstractAction {
35 35
 
36 36
     /** A version number for this class. */
37 37
     private static final long serialVersionUID = 1;
38
+    /** Clipboard to copy from. */
39
+    private final Clipboard clipboard;
38 40
     /** channel to be copied. */
39 41
     private final String channel;
40 42
 
41 43
     /**
42 44
      * Instantiates a new channel copy action.
43 45
      *
46
+     * @param clipboard Clipboard to copy from
44 47
      * @param channel channel to be copied
45 48
      */
46
-    public ChannelCopyAction(final String channel) {
49
+    public ChannelCopyAction(final Clipboard clipboard, final String channel) {
47 50
         super("Copy channel");
48 51
 
52
+        this.clipboard = clipboard;
49 53
         this.channel = channel;
50 54
     }
51 55
 
@@ -56,10 +60,7 @@ public final class ChannelCopyAction extends AbstractAction {
56 60
      */
57 61
     @Override
58 62
     public void actionPerformed(final ActionEvent e) {
59
-        if (Toolkit.getDefaultToolkit().getSystemClipboard() != null) {
60
-            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
61
-                    new StringSelection(channel), null);
62
-        }
63
+        clipboard.setContents(new StringSelection(channel), null);
63 64
     }
64 65
 
65 66
 }

+ 1
- 5
src/com/dmdirc/addons/ui_swing/actions/CopyAction.java View File

@@ -33,11 +33,7 @@ import javax.swing.text.JTextComponent;
33 33
  */
34 34
 public final class CopyAction extends AbstractAction {
35 35
 
36
-    /**
37
-     * A version number for this class. It should be changed whenever the class structure is changed
38
-     * (or anything else that would prevent serialized objects being unserialized with the new
39
-     * class).
40
-     */
36
+    /** A version number for this class. */
41 37
     private static final long serialVersionUID = 1;
42 38
     /** Text component to be acted upon. */
43 39
     private final JTextComponent comp;

+ 1
- 5
src/com/dmdirc/addons/ui_swing/actions/CutAction.java View File

@@ -33,11 +33,7 @@ import javax.swing.text.JTextComponent;
33 33
  */
34 34
 public final class CutAction extends AbstractAction {
35 35
 
36
-    /**
37
-     * A version number for this class. It should be changed whenever the class structure is changed
38
-     * (or anything else that would prevent serialized objects being unserialized with the new
39
-     * class).
40
-     */
36
+    /** A version number for this class. */
41 37
     private static final long serialVersionUID = 1;
42 38
     /** Text component to be acted upon. */
43 39
     private final JTextComponent comp;

+ 8
- 11
src/com/dmdirc/addons/ui_swing/actions/HyperlinkCopyAction.java View File

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.actions;
24 24
 
25
-import java.awt.Toolkit;
25
+import java.awt.datatransfer.Clipboard;
26 26
 import java.awt.datatransfer.StringSelection;
27 27
 import java.awt.event.ActionEvent;
28 28
 
@@ -33,23 +33,23 @@ import javax.swing.AbstractAction;
33 33
  */
34 34
 public final class HyperlinkCopyAction extends AbstractAction {
35 35
 
36
-    /**
37
-     * A version number for this class. It should be changed whenever the class structure is changed
38
-     * (or anything else that would prevent serialized objects being unserialized with the new
39
-     * class).
40
-     */
36
+    /** A version number for this class. */
41 37
     private static final long serialVersionUID = 1;
38
+    /** Clipboard to copy from. */
39
+    private final Clipboard clipboard;
42 40
     /** hyperlink to be copied. */
43 41
     private final String hyperlink;
44 42
 
45 43
     /**
46 44
      * Instantiates a new hyperlink copy action.
47 45
      *
46
+     * @param clipboard Clipboard to copy from
48 47
      * @param hyperlink hyperlink to be copied
49 48
      */
50
-    public HyperlinkCopyAction(final String hyperlink) {
49
+    public HyperlinkCopyAction(final Clipboard clipboard, final String hyperlink) {
51 50
         super("Copy hyperlink");
52 51
 
52
+        this.clipboard = clipboard;
53 53
         this.hyperlink = hyperlink;
54 54
     }
55 55
 
@@ -60,10 +60,7 @@ public final class HyperlinkCopyAction extends AbstractAction {
60 60
      */
61 61
     @Override
62 62
     public void actionPerformed(final ActionEvent e) {
63
-        if (Toolkit.getDefaultToolkit().getSystemClipboard() != null) {
64
-            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
65
-                    new StringSelection(hyperlink), null);
66
-        }
63
+        clipboard.setContents(new StringSelection(hyperlink), null);
67 64
     }
68 65
 
69 66
 }

+ 7
- 8
src/com/dmdirc/addons/ui_swing/actions/InputTextFramePasteAction.java View File

@@ -24,7 +24,7 @@ package com.dmdirc.addons.ui_swing.actions;
24 24
 
25 25
 import com.dmdirc.addons.ui_swing.components.frames.InputTextFrame;
26 26
 
27
-import java.awt.Toolkit;
27
+import java.awt.datatransfer.Clipboard;
28 28
 import java.awt.datatransfer.DataFlavor;
29 29
 import java.awt.event.ActionEvent;
30 30
 
@@ -37,17 +37,21 @@ public final class InputTextFramePasteAction extends AbstractAction {
37 37
 
38 38
     /** A version number for this class. */
39 39
     private static final long serialVersionUID = 1;
40
+    /** Clipboard to paste from. */
41
+    private final Clipboard clipboard;
40 42
     /** Text component to be acted upon. */
41 43
     private final InputTextFrame inputFrame;
42 44
 
43 45
     /**
44 46
      * Instantiates a new paste action.
45 47
      *
48
+     * @param clipboard Clipboard to paste from
46 49
      * @param inputFrame Component to be acted upon
47 50
      */
48
-    public InputTextFramePasteAction(final InputTextFrame inputFrame) {
51
+    public InputTextFramePasteAction(final Clipboard clipboard, final InputTextFrame inputFrame) {
49 52
         super("Paste");
50 53
 
54
+        this.clipboard = clipboard;
51 55
         this.inputFrame = inputFrame;
52 56
     }
53 57
 
@@ -64,12 +68,7 @@ public final class InputTextFramePasteAction extends AbstractAction {
64 68
     @Override
65 69
     public boolean isEnabled() {
66 70
         try {
67
-            if (Toolkit.getDefaultToolkit().getSystemClipboard() != null) {
68
-                return Toolkit.getDefaultToolkit().getSystemClipboard().
69
-                        isDataFlavorAvailable(DataFlavor.stringFlavor);
70
-            } else {
71
-                return false;
72
-            }
71
+            return clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor);
73 72
         } catch (IllegalStateException ex) {
74 73
             return false;
75 74
         }

+ 9
- 12
src/com/dmdirc/addons/ui_swing/actions/NicknameCopyAction.java View File

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.actions;
24 24
 
25
-import java.awt.Toolkit;
25
+import java.awt.datatransfer.Clipboard;
26 26
 import java.awt.datatransfer.StringSelection;
27 27
 import java.awt.event.ActionEvent;
28 28
 
@@ -33,23 +33,23 @@ import javax.swing.AbstractAction;
33 33
  */
34 34
 public final class NicknameCopyAction extends AbstractAction {
35 35
 
36
-    /**
37
-     * A version number for this class. It should be changed whenever the class structure is changed
38
-     * (or anything else that would prevent serialized objects being unserialized with the new
39
-     * class).
40
-     */
36
+    /** A version number for this class. */
41 37
     private static final long serialVersionUID = 1;
38
+    /** Clipboard to copy from. */
39
+    private final Clipboard clipboard;
42 40
     /** Nickname to be copied. */
43 41
     private final String nickname;
44 42
 
45 43
     /**
46 44
      * Instantiates a new nickname copy action.
47 45
      *
48
-     * @param nickname Nickname to be copied
46
+     * @param clipboard Clipboard to copy from
47
+     * @param nickname  Nickname to be copied
49 48
      */
50
-    public NicknameCopyAction(final String nickname) {
49
+    public NicknameCopyAction(final Clipboard clipboard, final String nickname) {
51 50
         super("Copy Nickname");
52 51
 
52
+        this.clipboard = clipboard;
53 53
         this.nickname = nickname;
54 54
     }
55 55
 
@@ -60,10 +60,7 @@ public final class NicknameCopyAction extends AbstractAction {
60 60
      */
61 61
     @Override
62 62
     public void actionPerformed(final ActionEvent e) {
63
-        if (Toolkit.getDefaultToolkit().getSystemClipboard() != null) {
64
-            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
65
-                    new StringSelection(nickname), null);
66
-        }
63
+        clipboard.setContents(new StringSelection(nickname), null);
67 64
     }
68 65
 
69 66
 }

+ 9
- 7
src/com/dmdirc/addons/ui_swing/actions/PasteAction.java View File

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.actions;
24 24
 
25
-import java.awt.Toolkit;
25
+import java.awt.datatransfer.Clipboard;
26 26
 import java.awt.datatransfer.DataFlavor;
27 27
 import java.awt.datatransfer.Transferable;
28 28
 import java.awt.event.ActionEvent;
@@ -37,17 +37,21 @@ public final class PasteAction extends AbstractAction {
37 37
 
38 38
     /** A version number for this class. */
39 39
     private static final long serialVersionUID = 1;
40
+    /** Clipboard to paste with. */
41
+    private final Clipboard clipboard;
40 42
     /** Text component to be acted upon. */
41 43
     private final JTextComponent comp;
42 44
 
43 45
     /**
44 46
      * Instantiates a new paste action.
45 47
      *
46
-     * @param comp Component to be acted upon
48
+     * @param clipboard Clipboard to paste with
49
+     * @param comp      Component to be acted upon
47 50
      */
48
-    public PasteAction(final JTextComponent comp) {
51
+    public PasteAction(final Clipboard clipboard, final JTextComponent comp) {
49 52
         super("Paste");
50 53
 
54
+        this.clipboard = clipboard;
51 55
         this.comp = comp;
52 56
     }
53 57
 
@@ -63,10 +67,8 @@ public final class PasteAction extends AbstractAction {
63 67
 
64 68
     @Override
65 69
     public boolean isEnabled() {
66
-        if (comp.isEditable() && comp.isEnabled() && Toolkit.getDefaultToolkit().
67
-                getSystemClipboard() != null) {
68
-            final Transferable contents = Toolkit.getDefaultToolkit().
69
-                    getSystemClipboard().getContents(this);
70
+        if (comp.isEditable() && comp.isEnabled()) {
71
+            final Transferable contents = clipboard.getContents(this);
70 72
             return contents.isDataFlavorSupported(DataFlavor.stringFlavor);
71 73
         } else {
72 74
             return false;

+ 9
- 9
src/com/dmdirc/addons/ui_swing/actions/ReplacePasteAction.java View File

@@ -25,7 +25,7 @@ package com.dmdirc.addons.ui_swing.actions;
25 25
 import com.dmdirc.logger.ErrorLevel;
26 26
 import com.dmdirc.logger.Logger;
27 27
 
28
-import java.awt.Toolkit;
28
+import java.awt.datatransfer.Clipboard;
29 29
 import java.awt.datatransfer.DataFlavor;
30 30
 import java.awt.datatransfer.UnsupportedFlavorException;
31 31
 import java.awt.event.ActionEvent;
@@ -41,6 +41,8 @@ public final class ReplacePasteAction extends AbstractAction {
41 41
 
42 42
     /** A version number for this class. */
43 43
     private static final long serialVersionUID = 1;
44
+    /** Clipboard to handle pasting. */
45
+    private final Clipboard clipboard;
44 46
     /** Regex to match for replacement. */
45 47
     private final String replacementRegex;
46 48
     /** Replacement string. */
@@ -49,13 +51,15 @@ public final class ReplacePasteAction extends AbstractAction {
49 51
     /**
50 52
      * Creates a new instance of regex replacement paste action.
51 53
      *
54
+     * @param clipboard Clipboard to handle pasting
52 55
      * @param replacementRegex  Regex to match for replacement
53 56
      * @param replacementString Replacement string
54 57
      */
55
-    public ReplacePasteAction(final String replacementRegex,
58
+    public ReplacePasteAction(final Clipboard clipboard, final String replacementRegex,
56 59
             final String replacementString) {
57 60
         super("NoSpacesPasteAction");
58 61
 
62
+        this.clipboard = clipboard;
59 63
         this.replacementRegex = replacementRegex;
60 64
         this.replacementString = replacementString;
61 65
     }
@@ -67,12 +71,8 @@ public final class ReplacePasteAction extends AbstractAction {
67 71
      */
68 72
     @Override
69 73
     public void actionPerformed(final ActionEvent e) {
70
-        if (Toolkit.getDefaultToolkit().getSystemClipboard() == null) {
71
-            return;
72
-        }
73 74
         if (!(e.getSource() instanceof JTextComponent)
74
-                || !Toolkit.getDefaultToolkit().getSystemClipboard().
75
-                isDataFlavorAvailable(DataFlavor.stringFlavor)) {
75
+                || !clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
76 76
             return;
77 77
         }
78 78
 
@@ -80,8 +80,8 @@ public final class ReplacePasteAction extends AbstractAction {
80 80
             //Get clipboard clipboard contents
81 81
             //Replace spaces with nothing
82 82
             //Replace the current selection with the contents of the clipboard
83
-            ((JTextComponent) e.getSource()).replaceSelection(((String) Toolkit.getDefaultToolkit()
84
-                    .getSystemClipboard().getData(DataFlavor.stringFlavor))
83
+            ((JTextComponent) e.getSource()).replaceSelection(
84
+                    ((String) clipboard.getData(DataFlavor.stringFlavor))
85 85
                     .replaceAll(replacementRegex, replacementString));
86 86
         } catch (IOException ex) {
87 87
             Logger.userError(ErrorLevel.LOW, "Unable to get clipboard "

+ 15
- 9
src/com/dmdirc/addons/ui_swing/components/TopicBar.java View File

@@ -45,6 +45,7 @@ import com.dmdirc.util.annotations.factory.Factory;
45 45
 import com.dmdirc.util.annotations.factory.Unbound;
46 46
 
47 47
 import java.awt.Color;
48
+import java.awt.datatransfer.Clipboard;
48 49
 import java.awt.event.ActionEvent;
49 50
 import java.awt.event.ActionListener;
50 51
 import java.awt.event.MouseEvent;
@@ -91,6 +92,8 @@ public class TopicBar extends JComponent implements ActionListener, ConfigChange
91 92
     private final int topicLengthMax;
92 93
     /** The config domain to read settings from. */
93 94
     private final String domain;
95
+    /** Clipboard to copy and paste from. */
96
+    private final Clipboard clipboard;
94 97
     /** Empty Attribute set. */
95 98
     private SimpleAttributeSet as;
96 99
     /** Foreground Colour. */
@@ -109,14 +112,15 @@ public class TopicBar extends JComponent implements ActionListener, ConfigChange
109 112
     /**
110 113
      * Creates a new instance of {@link TopicBar}.
111 114
      *
112
-     * @param parentWindow    The window that ultimately contains this topic bar.
113
-     * @param globalConfig    The config provider to read settings from.
114
-     * @param domain          The domain that settings are stored in.
115
-     * @param colourManager   The colour manager to use for colour input.
116
-     * @param pluginManager   The plugin manager to use for plugin information.
117
-     * @param channel         The channel that this topic bar is for.
118
-     * @param window          The window this topic bar is for.
119
-     * @param iconManager     The icon manager to use for this bar's icons.
115
+     * @param parentWindow  The window that ultimately contains this topic bar.
116
+     * @param globalConfig  The config provider to read settings from.
117
+     * @param domain        The domain that settings are stored in.
118
+     * @param colourManager The colour manager to use for colour input.
119
+     * @param pluginManager The plugin manager to use for plugin information.
120
+     * @param clipboard     The clipboard to copy and paste from
121
+     * @param channel       The channel that this topic bar is for.
122
+     * @param window        The window this topic bar is for.
123
+     * @param iconManager   The icon manager to use for this bar's icons.
120 124
      */
121 125
     public TopicBar(
122 126
             final MainFrame parentWindow,
@@ -124,6 +128,7 @@ public class TopicBar extends JComponent implements ActionListener, ConfigChange
124 128
             @SuppressWarnings("qualifiers") @PluginDomain(SwingController.class) final String domain,
125 129
             final ColourManager colourManager,
126 130
             final PluginManager pluginManager,
131
+            final Clipboard clipboard,
127 132
             @Unbound final Channel channel,
128 133
             @Unbound final ChannelFrame window,
129 134
             @Unbound final IconManager iconManager) {
@@ -131,6 +136,7 @@ public class TopicBar extends JComponent implements ActionListener, ConfigChange
131 136
         this.domain = domain;
132 137
         this.colourManager = colourManager;
133 138
         this.window = window;
139
+        this.clipboard = clipboard;
134 140
         topicText = new TextPaneInputField(parentWindow, globalConfig, colourManager, iconManager);
135 141
         topicLengthMax = channel.getMaxTopicLength();
136 142
         updateOptions();
@@ -140,7 +146,7 @@ public class TopicBar extends JComponent implements ActionListener, ConfigChange
140 146
                 new NewlinesDocumentFilter());
141 147
 
142 148
         topicText.getActionMap().put("paste-from-clipboard",
143
-                new ReplacePasteAction("(\r\n|\n|\r)", " "));
149
+                new ReplacePasteAction(clipboard, "(\r\n|\n|\r)", " "));
144 150
         topicEdit = new ImageButton<>("edit",
145 151
                 iconManager.getIcon("edit-inactive"),
146 152
                 iconManager.getIcon("edit"));

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

@@ -45,6 +45,7 @@ import com.dmdirc.ui.messages.ColourManager;
45 45
 import java.awt.BorderLayout;
46 46
 import java.awt.Point;
47 47
 import java.awt.Toolkit;
48
+import java.awt.datatransfer.Clipboard;
48 49
 import java.awt.datatransfer.DataFlavor;
49 50
 import java.awt.datatransfer.UnsupportedFlavorException;
50 51
 import java.awt.event.KeyEvent;
@@ -91,6 +92,8 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow,
91 92
     private final PluginManager pluginManager;
92 93
     /** Paste dialog factory. */
93 94
     private final PasteDialogFactory pasteDialogFactory;
95
+    /** Clipboard to use for copying and pasting. */
96
+    private final Clipboard clipboard;
94 97
 
95 98
     /**
96 99
      * Creates a new instance of InputFrame.
@@ -110,6 +113,7 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow,
110 113
         this.mainFrame = deps.mainFrame;
111 114
         this.pluginManager = deps.pluginManager;
112 115
         this.pasteDialogFactory = deps.pasteDialog;
116
+        this.clipboard = deps.clipboard;
113 117
 
114 118
         initComponents(inputFieldProvider);
115 119
 
@@ -180,7 +184,7 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow,
180 184
 
181 185
         inputFieldPopup.add(new CutAction(getInputField().getTextField()));
182 186
         inputFieldPopup.add(new CopyAction(getInputField().getTextField()));
183
-        inputFieldPopup.add(new InputTextFramePasteAction(this));
187
+        inputFieldPopup.add(new InputTextFramePasteAction(clipboard, this));
184 188
         inputFieldPopup.setOpaque(true);
185 189
         inputFieldPopup.setLightWeightPopupEnabled(true);
186 190
 
@@ -195,7 +199,7 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow,
195 199
         UIUtilities.addUndoManager(getInputField().getTextField());
196 200
 
197 201
         getInputField().getActionMap().put("paste",
198
-                new InputTextFramePasteAction(this));
202
+                new InputTextFramePasteAction(clipboard, this));
199 203
         getInputField().getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke(
200 204
                 "shift INSERT"), "paste");
201 205
         getInputField().getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke(
@@ -307,12 +311,8 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow,
307 311
 
308 312
     /** Checks and pastes text. */
309 313
     public void doPaste() {
310
-        if (Toolkit.getDefaultToolkit().getSystemClipboard() == null) {
311
-            return;
312
-        }
313 314
         try {
314
-            if (!Toolkit.getDefaultToolkit().getSystemClipboard().
315
-                    isDataFlavorAvailable(DataFlavor.stringFlavor)) {
315
+            if (!clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
316 316
                 return;
317 317
             }
318 318
         } catch (final IllegalStateException ex) {

+ 11
- 4
src/com/dmdirc/addons/ui_swing/components/frames/TextFrame.java View File

@@ -66,6 +66,7 @@ import com.dmdirc.ui.messages.ColourManager;
66 66
 import com.google.common.eventbus.EventBus;
67 67
 
68 68
 import java.awt.Point;
69
+import java.awt.datatransfer.Clipboard;
69 70
 import java.awt.event.KeyEvent;
70 71
 import java.awt.event.MouseEvent;
71 72
 
@@ -104,6 +105,8 @@ public abstract class TextFrame extends JPanel implements Window,
104 105
     private final PopupManager popupManager;
105 106
     /** Bus to despatch events on. */
106 107
     private final EventBus eventBus;
108
+    /** Clipboard to copy and paste from. */
109
+    private final Clipboard clipboard;
107 110
     /** Boolean to determine if this frame should be popped out of main client. */
108 111
     private boolean popout;
109 112
     /** DesktopWindowFrame to use for this TextFrame if it is to be popped out of the client. */
@@ -130,6 +133,7 @@ public abstract class TextFrame extends JPanel implements Window,
130 133
         this.iconManager = deps.iconManager;
131 134
         this.eventBus = deps.eventBus;
132 135
         this.commandParser = commandParser;
136
+        this.clipboard = deps.clipboard;
133 137
 
134 138
         final AggregateConfigProvider config = owner.getConfigManager();
135 139
 
@@ -370,7 +374,7 @@ public abstract class TextFrame extends JPanel implements Window,
370 374
         switch (type.getType()) {
371 375
             case CHANNEL:
372 376
                 popupMenu = getPopupMenu(getChannelPopupType(), arguments);
373
-                popupMenu.add(new ChannelCopyAction(type.getValue()));
377
+                popupMenu.add(new ChannelCopyAction(clipboard, type.getValue()));
374 378
                 if (popupMenu.getComponentCount() > 1) {
375 379
                     popupMenu.addSeparator();
376 380
                 }
@@ -378,7 +382,7 @@ public abstract class TextFrame extends JPanel implements Window,
378 382
                 break;
379 383
             case HYPERLINK:
380 384
                 popupMenu = getPopupMenu(getHyperlinkPopupType(), arguments);
381
-                popupMenu.add(new HyperlinkCopyAction(type.getValue()));
385
+                popupMenu.add(new HyperlinkCopyAction(clipboard, type.getValue()));
382 386
                 if (popupMenu.getComponentCount() > 1) {
383 387
                     popupMenu.addSeparator();
384 388
                 }
@@ -390,7 +394,7 @@ public abstract class TextFrame extends JPanel implements Window,
390 394
                     popupMenu.addSeparator();
391 395
                 }
392 396
 
393
-                popupMenu.add(new NicknameCopyAction(type.getValue()));
397
+                popupMenu.add(new NicknameCopyAction(clipboard, type.getValue()));
394 398
                 break;
395 399
             default:
396 400
                 popupMenu = getPopupMenu(null, arguments);
@@ -540,6 +544,7 @@ public abstract class TextFrame extends JPanel implements Window,
540 544
         final PluginManager pluginManager;
541 545
         final IconManager iconManager;
542 546
         final ActiveFrameManager activeFrameManager;
547
+        final Clipboard clipboard;
543 548
 
544 549
         @Inject
545 550
         public TextFrameDependencies(
@@ -552,7 +557,8 @@ public abstract class TextFrame extends JPanel implements Window,
552 557
                 final PluginManager pluginManager,
553 558
                 @GlobalConfig final IconManager iconManager,
554 559
                 @GlobalConfig final AggregateConfigProvider globalConfig,
555
-                final ActiveFrameManager activeFrameManager) {
560
+                final ActiveFrameManager activeFrameManager,
561
+                final Clipboard clipboard) {
556 562
             this.textPaneFactory = textPaneFactory;
557 563
             this.controller = controller;
558 564
             this.mainFrame = mainFrame;
@@ -563,6 +569,7 @@ public abstract class TextFrame extends JPanel implements Window,
563 569
             this.pluginManager = pluginManager;
564 570
             this.iconManager = iconManager;
565 571
             this.activeFrameManager = activeFrameManager;
572
+            this.clipboard = clipboard;
566 573
         }
567 574
 
568 575
     }

+ 8
- 2
src/com/dmdirc/addons/ui_swing/dialogs/channelsetting/ChannelSettingsDialog.java View File

@@ -36,6 +36,7 @@ import com.dmdirc.interfaces.config.IdentityFactory;
36 36
 import com.dmdirc.interfaces.ui.InputWindow;
37 37
 import com.dmdirc.plugins.ServiceManager;
38 38
 
39
+import java.awt.datatransfer.Clipboard;
39 40
 import java.awt.event.ActionEvent;
40 41
 import java.awt.event.ActionListener;
41 42
 
@@ -79,6 +80,8 @@ public class ChannelSettingsDialog extends StandardDialog implements ActionListe
79 80
     private final PreferencesManager preferencesManager;
80 81
     /** Preferences setting component factory. */
81 82
     private final PrefsComponentFactory compFactory;
83
+    /** Clipboard to copy and paste from. */
84
+    private final Clipboard clipboard;
82 85
 
83 86
     /**
84 87
      * Creates a new instance of ChannelSettingsDialog.
@@ -91,6 +94,7 @@ public class ChannelSettingsDialog extends StandardDialog implements ActionListe
91 94
      * @param compFactory        Preferences setting component factory
92 95
      * @param channel            The channel object that we're editing settings for
93 96
      * @param parentWindow       Parent window
97
+     * @param clipboard          Clipboard to copy and paste from
94 98
      */
95 99
     public ChannelSettingsDialog(
96 100
             final IdentityFactory identityFactory,
@@ -100,7 +104,8 @@ public class ChannelSettingsDialog extends StandardDialog implements ActionListe
100 104
             final PreferencesManager preferencesManager,
101 105
             final PrefsComponentFactory compFactory,
102 106
             final Channel channel,
103
-            final MainFrame parentWindow) {
107
+            final MainFrame parentWindow,
108
+            final Clipboard clipboard) {
104 109
         super(parentWindow, ModalityType.MODELESS);
105 110
 
106 111
         this.userConfig = checkNotNull(userConfig);
@@ -108,6 +113,7 @@ public class ChannelSettingsDialog extends StandardDialog implements ActionListe
108 113
         this.preferencesManager = checkNotNull(preferencesManager);
109 114
         this.compFactory = checkNotNull(compFactory);
110 115
         this.channel = checkNotNull(channel);
116
+        this.clipboard = clipboard;
111 117
 
112 118
         this.identity = identityFactory.createChannelConfig(channel.getConnection().getNetwork(),
113 119
                 channel.getChannelInfo().getName());
@@ -148,7 +154,7 @@ public class ChannelSettingsDialog extends StandardDialog implements ActionListe
148 154
     /** Initialises the Topic tab. */
149 155
     private void initTopicTab() {
150 156
         topicModesPane = new TopicPane(channel, channel.getIconManager(), serviceManager,
151
-                this, channelWindow);
157
+                this, channelWindow, clipboard);
152 158
         tabbedPane.addTab("Topic", topicModesPane);
153 159
     }
154 160
 

+ 7
- 4
src/com/dmdirc/addons/ui_swing/dialogs/channelsetting/TopicDisplayPane.java View File

@@ -36,6 +36,7 @@ import com.dmdirc.plugins.ServiceManager;
36 36
 import com.dmdirc.ui.IconManager;
37 37
 
38 38
 import java.awt.Color;
39
+import java.awt.datatransfer.Clipboard;
39 40
 import java.awt.event.KeyEvent;
40 41
 import java.util.Date;
41 42
 
@@ -63,6 +64,8 @@ public class TopicDisplayPane extends JPanel implements DocumentListener {
63 64
     private final InputWindow channelWindow;
64 65
     /** the maximum length allowed for a topic. */
65 66
     private final int topicLengthMax;
67
+    /** Clipboard to copy and paste from. */
68
+    private final Clipboard clipboard;
66 69
     /** label showing the number of characters left in a topic. */
67 70
     private JLabel topicLengthLabel;
68 71
     /** Topic text entry text area. */
@@ -79,12 +82,12 @@ public class TopicDisplayPane extends JPanel implements DocumentListener {
79 82
      * @param serviceManager Service manager
80 83
      * @param parent         Parent channel settings dialog
81 84
      * @param channelWindow  Channel window
85
+     * @param clipboard      Clipboard to copy and paste
82 86
      */
83 87
     public TopicDisplayPane(final Channel channel, final IconManager iconManager,
84 88
             final ServiceManager serviceManager, final ChannelSettingsDialog parent,
85
-            final InputWindow channelWindow) {
86
-        super();
87
-
89
+            final InputWindow channelWindow, final Clipboard clipboard) {
90
+        this.clipboard = clipboard;
88 91
         this.channel = channel;
89 92
         this.parent = parent;
90 93
         topicLengthMax = channel.getConnection().getParser().getMaxTopicLength();
@@ -115,7 +118,7 @@ public class TopicDisplayPane extends JPanel implements DocumentListener {
115 118
         handler.setTabCompleter(channel.getTabCompleter());
116 119
 
117 120
         topicText.getActionMap().put("paste-from-clipboard",
118
-                new ReplacePasteAction("(\r\n|\n|\r)", " "));
121
+                new ReplacePasteAction(clipboard, "(\r\n|\n|\r)", " "));
119 122
         topicText.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
120 123
                 0), new TopicEnterAction(parent));
121 124
         topicText.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,

+ 8
- 7
src/com/dmdirc/addons/ui_swing/dialogs/channelsetting/TopicPane.java View File

@@ -28,6 +28,7 @@ import com.dmdirc.interfaces.ui.InputWindow;
28 28
 import com.dmdirc.plugins.ServiceManager;
29 29
 import com.dmdirc.ui.IconManager;
30 30
 
31
+import java.awt.datatransfer.Clipboard;
31 32
 import java.awt.event.ActionEvent;
32 33
 import java.awt.event.ActionListener;
33 34
 
@@ -38,11 +39,7 @@ import net.miginfocom.swing.MigLayout;
38 39
 /** Topic panel. */
39 40
 public class TopicPane extends JPanel implements ActionListener {
40 41
 
41
-    /**
42
-     * A version number for this class. It should be changed whenever the class structure is changed
43
-     * (or anything else that would prevent serialized objects being unserialized with the new
44
-     * class).
45
-     */
42
+    /** A version number for this class. */
46 43
     private static final long serialVersionUID = 2;
47 44
     /** Parent channel. */
48 45
     private final Channel channel;
@@ -50,6 +47,8 @@ public class TopicPane extends JPanel implements ActionListener {
50 47
     private final InputWindow channelWindow;
51 48
     /** Parent dialog. */
52 49
     private final ChannelSettingsDialog parent;
50
+    /** Clipboard to copy and paste with. */
51
+    private final Clipboard clipboard;
53 52
     /** Topic history panel. */
54 53
     private TopicHistoryPane topicHistoryPane;
55 54
     /** Topic display pane. */
@@ -63,16 +62,18 @@ public class TopicPane extends JPanel implements ActionListener {
63 62
      * @param serviceManager Service manager
64 63
      * @param parent         Parent dialog
65 64
      * @param channelWindow  Channel window
65
+     * @param clipboard      Clipboard to copy and paste with
66 66
      */
67 67
     public TopicPane(final Channel channel, final IconManager iconManager,
68 68
             final ServiceManager serviceManager, final ChannelSettingsDialog parent,
69
-            final InputWindow channelWindow) {
69
+            final InputWindow channelWindow, final Clipboard clipboard) {
70 70
         super();
71 71
 
72 72
         this.setOpaque(UIUtilities.getTabbedPaneOpaque());
73 73
         this.channel = channel;
74 74
         this.parent = parent;
75 75
         this.channelWindow = channelWindow;
76
+        this.clipboard = clipboard;
76 77
 
77 78
         update(iconManager, serviceManager);
78 79
     }
@@ -94,7 +95,7 @@ public class TopicPane extends JPanel implements ActionListener {
94 95
     private void initTopicsPanel(final IconManager iconManager,
95 96
             final ServiceManager serviceManager) {
96 97
         topicDisplayPane = new TopicDisplayPane(channel, iconManager, serviceManager, parent,
97
-                channelWindow);
98
+                channelWindow, clipboard);
98 99
         topicHistoryPane = new TopicHistoryPane(channel);
99 100
     }
100 101
 

+ 5
- 2
src/com/dmdirc/addons/ui_swing/injection/DialogModule.java View File

@@ -48,6 +48,8 @@ import com.dmdirc.interfaces.config.ConfigProvider;
48 48
 import com.dmdirc.interfaces.config.IdentityFactory;
49 49
 import com.dmdirc.plugins.ServiceManager;
50 50
 
51
+import java.awt.datatransfer.Clipboard;
52
+
51 53
 import javax.inject.Provider;
52 54
 import javax.inject.Qualifier;
53 55
 import javax.inject.Singleton;
@@ -147,13 +149,14 @@ public class DialogModule {
147 149
             final ServiceManager serviceManager,
148 150
             final PreferencesManager preferencesManager,
149 151
             final PrefsComponentFactory compFactory,
150
-            final MainFrame parentWindow) {
152
+            final MainFrame parentWindow,
153
+            final Clipboard clipboard) {
151 154
         return new KeyedDialogProvider<Channel, ChannelSettingsDialog>() {
152 155
             @Override
153 156
             protected ChannelSettingsDialog getInstance(final Channel key) {
154 157
                 return new ChannelSettingsDialog(identityFactory, windowFactory,
155 158
                         userConfig, serviceManager, preferencesManager,
156
-                        compFactory, key, parentWindow);
159
+                        compFactory, key, parentWindow, clipboard);
157 160
             }
158 161
         };
159 162
     }

+ 13
- 0
src/com/dmdirc/addons/ui_swing/injection/SwingModule.java View File

@@ -29,6 +29,7 @@ import com.dmdirc.ServerManager;
29 29
 import com.dmdirc.actions.ActionManager;
30 30
 import com.dmdirc.addons.ui_swing.Apple;
31 31
 import com.dmdirc.addons.ui_swing.MainFrame;
32
+import com.dmdirc.addons.ui_swing.NoopClipboard;
32 33
 import com.dmdirc.addons.ui_swing.QuitWorker;
33 34
 import com.dmdirc.addons.ui_swing.SwingController;
34 35
 import com.dmdirc.addons.ui_swing.SwingManager;
@@ -66,6 +67,8 @@ import com.dmdirc.util.URLBuilder;
66 67
 
67 68
 import com.google.common.eventbus.EventBus;
68 69
 
70
+import java.awt.Toolkit;
71
+import java.awt.datatransfer.Clipboard;
69 72
 import java.util.concurrent.Callable;
70 73
 
71 74
 import javax.inject.Provider;
@@ -111,6 +114,16 @@ public class SwingModule {
111 114
         return controller;
112 115
     }
113 116
 
117
+    @Provides
118
+    @Singleton
119
+    public Clipboard getClipboard() {
120
+        if (Toolkit.getDefaultToolkit().getSystemClipboard() == null) {
121
+            return new NoopClipboard();
122
+        } else {
123
+            return Toolkit.getDefaultToolkit().getSystemClipboard();
124
+        }
125
+    }
126
+
114 127
     @Provides
115 128
     @Singleton
116 129
     public MainFrame getMainFrame(

+ 8
- 6
src/com/dmdirc/addons/ui_swing/textpane/TextPane.java View File

@@ -38,7 +38,7 @@ import com.dmdirc.util.annotations.factory.Unbound;
38 38
 
39 39
 import java.awt.Color;
40 40
 import java.awt.Point;
41
-import java.awt.Toolkit;
41
+import java.awt.datatransfer.Clipboard;
42 42
 import java.awt.datatransfer.StringSelection;
43 43
 import java.awt.event.AdjustmentEvent;
44 44
 import java.awt.event.AdjustmentListener;
@@ -85,6 +85,8 @@ public final class TextPane extends JComponent implements MouseWheelListener,
85 85
     private final BackgroundPainter backgroundPainter;
86 86
     /** The domain to read configuration from. */
87 87
     private final String configDomain;
88
+    /** Clipboard to handle copy and paste cations. */
89
+    private final Clipboard clipboard;
88 90
     /** Last seen line. */
89 91
     private int lastSeenLine = 0;
90 92
     /** Show new line notifications. */
@@ -95,15 +97,17 @@ public final class TextPane extends JComponent implements MouseWheelListener,
95 97
      *
96 98
      * @param configDomain The domain to read configuration from.
97 99
      * @param urlBuilder   The builder to use to construct URLs for resources.
100
+     * @param clipboard     The clipboard to handle copy and paste actions
98 101
      * @param frame        Parent Frame
99 102
      */
100 103
     public TextPane(
101 104
             @SuppressWarnings("qualifiers") @PluginDomain(SwingController.class) final String configDomain,
102
-            final URLBuilder urlBuilder,
105
+            final URLBuilder urlBuilder, final Clipboard clipboard,
103 106
             @Unbound final TextFrame frame) {
104 107
         super();
105 108
         this.frame = frame;
106 109
         this.configDomain = configDomain;
110
+        this.clipboard = clipboard;
107 111
 
108 112
         setUI(new TextPaneUI());
109 113
         document = frame.getContainer().getDocument();
@@ -426,10 +430,8 @@ public final class TextPane extends JComponent implements MouseWheelListener,
426 430
      * @param copyControlCharacters Should we copy control codes, or strip them?
427 431
      */
428 432
     public void copy(final boolean copyControlCharacters) {
429
-        if (getSelectedText() != null && !getSelectedText().isEmpty()
430
-                && Toolkit.getDefaultToolkit().getSystemClipboard() != null) {
431
-            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
432
-                    new StringSelection(getSelectedText(copyControlCharacters)), null);
433
+        if (getSelectedText() != null && !getSelectedText().isEmpty()) {
434
+            clipboard.setContents(new StringSelection(getSelectedText(copyControlCharacters)), null);
433 435
         }
434 436
     }
435 437
 

Loading…
Cancel
Save