Browse Source

Fixes issue 3890

Rework selection behaviour in the inputfield and the textpane.

Change-Id: Ie6cec6c1885124ba3e96b549fdfb86fcf76edcf5
Reviewed-on: http://gerrit.dmdirc.com/1013
Reviewed-by: Chris Smith <chris@dmdirc.com>
Automatic-Compile: Gregory Holmes <greg@dmdirc.com>
tags/0.6.4
Greboid 14 years ago
parent
commit
184d8017b1

src/com/dmdirc/addons/ui_swing/actions/TextPaneCopyAction.java → src/com/dmdirc/addons/ui_swing/actions/InputFieldCopyAction.java View File

@@ -27,46 +27,51 @@ import com.dmdirc.addons.ui_swing.textpane.TextPane;
27 27
 import java.awt.event.ActionEvent;
28 28
 
29 29
 import javax.swing.AbstractAction;
30
+import javax.swing.JTextField;
30 31
 
31 32
 /**
32 33
  * Textpane Copy action.
33 34
  */
34
-public final class TextPaneCopyAction extends AbstractAction { 
35
-    
35
+public final class InputFieldCopyAction extends AbstractAction {
36
+
36 37
     /**
37 38
      * A version number for this class. It should be changed whenever the class
38 39
      * structure is changed (or anything else that would prevent serialized
39 40
      * objects being unserialized with the new class).
40 41
      */
41 42
     private static final long serialVersionUID = 1;
42
-    
43
+
43 44
     /** Text component to be acted upon. */
44
-    private final TextPane comp; 
45
- 
46
-    /** 
45
+    private final TextPane textPane;
46
+    /** Input field to be acted upon. */
47
+    private final JTextField inputField;
48
+
49
+    /**
47 50
      * Instantiates a new copy action.
48
-     * 
49
-     * @param comp TextPane to be acted upon
51
+     *
52
+     * @param textPane TextPane to be acted upon
53
+     * @param inputField Inputfield to be acted upon
50 54
      */
51
-    public TextPaneCopyAction(final TextPane comp) { 
52
-        super("Copy"); 
53
-        
54
-        this.comp = comp; 
55
-    } 
56
- 
57
-    /** 
55
+    public InputFieldCopyAction(final TextPane textPane,
56
+            final JTextField inputField) {
57
+        super("Copy");
58
+
59
+        this.textPane = textPane;
60
+        this.inputField = inputField;
61
+    }
62
+
63
+    /**
58 64
      * {@inheritDoc}
59
-     * 
65
+     *
60 66
      * @param e Action event
61 67
      */
62 68
     @Override
63
-    public void actionPerformed(final ActionEvent e) { 
64
-        comp.copy(); 
65
-    } 
66
- 
67
-    /** {@inheritDoc} */
68
-    @Override
69
-    public boolean isEnabled() { 
70
-        return comp.isEnabled() && comp.hasSelectedRange();
71
-    } 
72
-} 
69
+    public void actionPerformed(final ActionEvent e) {
70
+        final String inputSelected = inputField.getSelectedText();
71
+        if (inputSelected != null && !inputSelected.isEmpty()) {
72
+            inputField.copy();
73
+        } else if (textPane.hasSelectedRange()) {
74
+            textPane.copy();
75
+        }
76
+    }
77
+}

+ 10
- 0
src/com/dmdirc/addons/ui_swing/components/SwingSearchBar.java View File

@@ -51,6 +51,7 @@ import javax.swing.JButton;
51 51
 import javax.swing.JCheckBox;
52 52
 import javax.swing.JComponent;
53 53
 import javax.swing.JPanel;
54
+import javax.swing.JTextField;
54 55
 import javax.swing.KeyStroke;
55 56
 import javax.swing.SwingUtilities;
56 57
 import javax.swing.event.DocumentEvent;
@@ -291,6 +292,15 @@ public final class SwingSearchBar extends JPanel implements ActionListener,
291 292
         searchBox.checkError();
292 293
     }
293 294
 
295
+    /**
296
+     * Returns the textfield used in this search bar.
297
+     *
298
+     * @return Search textfield
299
+     */
300
+    public JTextField getTextField() {
301
+        return searchBox.getTextField();
302
+    }
303
+
294 304
     /**
295 305
      * {@inheritDoc}.
296 306
      *

+ 0
- 2
src/com/dmdirc/addons/ui_swing/components/frames/CustomFrame.java View File

@@ -53,8 +53,6 @@ public class CustomFrame extends TextFrame {
53 53
         super(owner, controller);
54 54
 
55 55
         initComponents();
56
-
57
-        addKeyListener(this);
58 56
     }
59 57
 
60 58
     /**

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

@@ -40,6 +40,7 @@ import com.dmdirc.addons.ui_swing.actions.CutAction;
40 40
 import com.dmdirc.addons.ui_swing.actions.InputTextFramePasteAction;
41 41
 import com.dmdirc.addons.ui_swing.dialogs.paste.PasteDialog;
42 42
 import com.dmdirc.addons.ui_swing.actions.CommandAction;
43
+import com.dmdirc.addons.ui_swing.actions.InputFieldCopyAction;
43 44
 import com.dmdirc.addons.ui_swing.components.SwingInputField;
44 45
 
45 46
 import java.awt.BorderLayout;
@@ -47,6 +48,7 @@ import java.awt.Point;
47 48
 import java.awt.Toolkit;
48 49
 import java.awt.datatransfer.DataFlavor;
49 50
 import java.awt.datatransfer.UnsupportedFlavorException;
51
+import java.awt.event.KeyEvent;
50 52
 import java.awt.event.MouseEvent;
51 53
 import java.io.IOException;
52 54
 
@@ -120,6 +122,12 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow,
120 122
         if (getContainer().getServer() != null) {
121 123
             getContainer().getServer().addAwayStateListener(this);
122 124
         }
125
+
126
+        getInputField().getTextField().getInputMap().put(KeyStroke.getKeyStroke(
127
+                KeyEvent.VK_C, UIUtilities.getCtrlMask()), "textpaneCopy");
128
+        getInputField().getTextField().getActionMap().put("textpaneCopy",
129
+                new InputFieldCopyAction(getTextPane(),
130
+                getInputField().getTextField()));
123 131
     }
124 132
 
125 133
     /** {@inheritDoc} */
@@ -147,7 +155,6 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow,
147 155
     private void initComponents() {
148 156
         setInputField(new SwingInputField(getController().getMainFrame()));
149 157
 
150
-        getInputField().addKeyListener(this);
151 158
         getInputField().addMouseListener(this);
152 159
 
153 160
         initPopupMenu();

+ 10
- 53
src/com/dmdirc/addons/ui_swing/components/frames/TextFrame.java View File

@@ -30,14 +30,15 @@ import com.dmdirc.addons.ui_swing.UIUtilities;
30 30
 import com.dmdirc.addons.ui_swing.actions.ChannelCopyAction;
31 31
 import com.dmdirc.addons.ui_swing.actions.CommandAction;
32 32
 import com.dmdirc.addons.ui_swing.actions.HyperlinkCopyAction;
33
+import com.dmdirc.addons.ui_swing.actions.InputFieldCopyAction;
33 34
 import com.dmdirc.addons.ui_swing.actions.NicknameCopyAction;
34 35
 import com.dmdirc.addons.ui_swing.actions.SearchAction;
35
-import com.dmdirc.addons.ui_swing.actions.TextPaneCopyAction;
36 36
 import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
37 37
 import com.dmdirc.addons.ui_swing.components.SwingSearchBar;
38 38
 import com.dmdirc.addons.ui_swing.textpane.ClickType;
39 39
 import com.dmdirc.addons.ui_swing.textpane.LineInfo;
40 40
 import com.dmdirc.addons.ui_swing.textpane.TextPane;
41
+import com.dmdirc.addons.ui_swing.textpane.TextPaneCopyAction;
41 42
 import com.dmdirc.addons.ui_swing.textpane.TextPanePageDownAction;
42 43
 import com.dmdirc.addons.ui_swing.textpane.TextPanePageUpAction;
43 44
 import com.dmdirc.addons.ui_swing.textpane.TextPaneHomeAction;
@@ -65,7 +66,6 @@ import java.awt.Container;
65 66
 import java.awt.Dimension;
66 67
 import java.awt.Point;
67 68
 import java.awt.event.KeyEvent;
68
-import java.awt.event.KeyListener;
69 69
 import java.awt.event.MouseEvent;
70 70
 import java.awt.event.MouseListener;
71 71
 import java.beans.PropertyChangeEvent;
@@ -104,8 +104,8 @@ import net.miginfocom.swing.MigLayout;
104 104
  * Implements a generic (internal) frame.
105 105
  */
106 106
 public abstract class TextFrame extends JInternalFrame implements Window,
107
-        PropertyChangeListener, InternalFrameListener,
108
-        MouseListener, KeyListener, ConfigChangeListener, FrameInfoListener {
107
+        PropertyChangeListener, InternalFrameListener, MouseListener,
108
+        ConfigChangeListener, FrameInfoListener {
109 109
 
110 110
     /** Logger to use. */
111 111
     private static final java.util.logging.Logger LOGGER =
@@ -126,8 +126,6 @@ public abstract class TextFrame extends JInternalFrame implements Window,
126 126
     private StringTranscoder transcoder;
127 127
     /** Frame buffer size. */
128 128
     private int frameBufferSize;
129
-    /** Quick copy? */
130
-    private boolean quickCopy;
131 129
     /** Are we closing? */
132 130
     private boolean closing = false;
133 131
     /** Input window for popup commands. */
@@ -164,7 +162,6 @@ public abstract class TextFrame extends JInternalFrame implements Window,
164 162
 
165 163
         final ConfigManager config = owner.getConfigManager();
166 164
         frameBufferSize = config.getOptionInt("ui", "frameBufferSize");
167
-        quickCopy = config.getOptionBool("ui", "quickCopy");
168 165
 
169 166
         setFrameIcon(IconManager.getIconManager().getIcon(owner.getIcon()));
170 167
 
@@ -206,7 +203,6 @@ public abstract class TextFrame extends JInternalFrame implements Window,
206 203
 
207 204
         config.addChangeListener("ui", "foregroundcolour", this);
208 205
         config.addChangeListener("ui", "backgroundcolour", this);
209
-        config.addChangeListener("ui", "quickCopy", this);
210 206
         config.addChangeListener("ui", "frameBufferSize", this);
211 207
 
212 208
         addPropertyChangeListener("maximum", this);
@@ -519,11 +515,9 @@ public abstract class TextFrame extends JInternalFrame implements Window,
519 515
         setTextPane(new TextPane(this));
520 516
 
521 517
         getTextPane().addMouseListener(this);
522
-        getTextPane().addKeyListener(this);
523 518
 
524 519
         searchBar = new SwingSearchBar(this, controller.getMainFrame());
525 520
         searchBar.setVisible(false);
526
-        searchBar.addKeyListener(this);
527 521
 
528 522
         getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
529 523
                 put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0),
@@ -548,6 +542,12 @@ public abstract class TextFrame extends JInternalFrame implements Window,
548 542
                 put(KeyStroke.getKeyStroke(KeyEvent.VK_END,
549 543
                 UIUtilities.getCtrlDownMask()), "endAction");
550 544
 
545
+        getSearchBar().getTextField().getInputMap().put(KeyStroke.getKeyStroke(
546
+                KeyEvent.VK_C, UIUtilities.getCtrlMask()), "textpaneCopy");
547
+        getSearchBar().getTextField().getActionMap().put("textpaneCopy",
548
+                new InputFieldCopyAction(getTextPane(),
549
+                getSearchBar().getTextField()));
550
+
551 551
         getActionMap().put("pageUpAction",
552 552
                 new TextPanePageUpAction(getTextPane()));
553 553
         getActionMap().put("pageDownAction",
@@ -856,11 +856,6 @@ public abstract class TextFrame extends JInternalFrame implements Window,
856 856
      */
857 857
     @Override
858 858
     public void mouseReleased(final MouseEvent mouseEvent) {
859
-        if (quickCopy && mouseEvent.getSource() == getTextPane()) {
860
-            getTextPane().copy();
861
-            getTextPane().clearSelection();
862
-        }
863
-
864 859
         processMouseClickEvent(mouseEvent, MouseClickType.RELEASED);
865 860
     }
866 861
 
@@ -1121,41 +1116,6 @@ public abstract class TextFrame extends JInternalFrame implements Window,
1121 1116
         return menu;
1122 1117
     }
1123 1118
 
1124
-    /** 
1125
-     * {@inheritDoc}
1126
-     * 
1127
-     * @param event Key event
1128
-     */
1129
-    @Override
1130
-    public void keyTyped(final KeyEvent event) {
1131
-        //Ignore.
1132
-        }
1133
-
1134
-    /** 
1135
-     * {@inheritDoc}
1136
-     * 
1137
-     * @param event Key event
1138
-     */
1139
-    @Override
1140
-    public void keyPressed(final KeyEvent event) {
1141
-        if (!quickCopy && (event.getModifiers() & UIUtilities.getCtrlMask()) !=
1142
-                0 &&
1143
-                event.getKeyCode() == KeyEvent.VK_C) {
1144
-            getTextPane().copy();
1145
-        }
1146
-
1147
-    }
1148
-
1149
-    /** 
1150
-     * {@inheritDoc}
1151
-     * 
1152
-     * @param event Key event
1153
-     */
1154
-    @Override
1155
-    public void keyReleased(final KeyEvent event) {
1156
-        //Ignore.
1157
-        }
1158
-
1159 1119
     /**
1160 1120
      * Gets the search bar.
1161 1121
      *
@@ -1183,9 +1143,6 @@ public abstract class TextFrame extends JInternalFrame implements Window,
1183 1143
             } else if ("frameBufferSize".equals(key)) {
1184 1144
                 frameBufferSize = getContainer().getConfigManager().
1185 1145
                         getOptionInt("ui", "frameBufferSize");
1186
-            } else if ("quickCopy".equals(key)) {
1187
-                quickCopy = getContainer().getConfigManager().
1188
-                        getOptionBool("ui", "quickCopy");
1189 1146
             }
1190 1147
         }
1191 1148
     }

+ 23
- 4
src/com/dmdirc/addons/ui_swing/textpane/TextPaneCanvas.java View File

@@ -99,6 +99,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
99 99
     private String domain;
100 100
     /** Background image option. */
101 101
     private BackgroundOption backgroundOption;
102
+    /** Quick copy? */
103
+    private boolean quickCopy;
102 104
 
103 105
     /**
104 106
      * Creates a new text pane canvas.
@@ -124,6 +126,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
124 126
         addComponentListener(this);
125 127
         manager.addChangeListener(domain, "textpanebackground", this);
126 128
         manager.addChangeListener(domain, "textpanebackgroundoption", this);
129
+        manager.addChangeListener("ui", "quickCopy", this);
127 130
 
128 131
         updateCachedSettings();
129 132
     }
@@ -181,6 +184,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
181 184
         } catch (IllegalArgumentException ex) {
182 185
             backgroundOption = BackgroundOption.CENTER;
183 186
         }
187
+        quickCopy = manager.getOptionBool("ui", "quickCopy");
184 188
     }
185 189
 
186 190
     private void paintOntoGraphics(final Graphics2D g) {
@@ -464,15 +468,19 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
464 468
                 selection.setEndLine(lineInfo.getLine());
465 469
                 selection.setStartPos(start);
466 470
                 selection.setEndPos(end);
467
-                textPane.copy();
468
-                textPane.clearSelection();
471
+                if (quickCopy) {
472
+                    textPane.copy();
473
+                    clearSelection();
474
+                }
469 475
             } else if (e.getClickCount() == 3) {
470 476
                 selection.setStartLine(lineInfo.getLine());
471 477
                 selection.setEndLine(lineInfo.getLine());
472 478
                 selection.setStartPos(0);
473 479
                 selection.setEndPos(clickedText.length());
474
-                textPane.copy();
475
-                textPane.clearSelection();
480
+                if (quickCopy) {
481
+                    textPane.copy();
482
+                    clearSelection();
483
+                }
476 484
             }
477 485
         }
478 486
 
@@ -632,6 +640,17 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
632 640
      */
633 641
     @Override
634 642
     public void mouseReleased(final MouseEvent e) {
643
+        if (quickCopy) {
644
+            textPane.copy();
645
+            SwingUtilities.invokeLater(new Runnable() {
646
+
647
+                /** {@inheritDoc} */
648
+                @Override
649
+                public void run() {
650
+                    clearSelection();
651
+                }
652
+            });
653
+        }
635 654
         if (e.getButton() == MouseEvent.BUTTON1) {
636 655
             highlightEvent(MouseEventType.RELEASE, e);
637 656
         }

Loading…
Cancel
Save