Browse Source

fixed the console errors being generated by the swinginputfield

git-svn-id: http://svn.dmdirc.com/trunk@3565 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Gregory Holmes 16 years ago
parent
commit
96f0b68034

+ 2
- 4
src/com/dmdirc/ui/swing/components/SwingInputField.java View File

@@ -31,16 +31,16 @@ import java.awt.event.ActionEvent;
31 31
 import java.awt.event.ActionListener;
32 32
 import java.awt.event.KeyListener;
33 33
 
34
+import javax.swing.JComponent;
34 35
 import javax.swing.JLabel;
35 36
 import javax.swing.JTextField;
36 37
 import javax.swing.event.DocumentEvent;
37 38
 import javax.swing.event.DocumentListener;
38 39
 import javax.swing.text.BadLocationException;
39
-import javax.swing.text.JTextComponent;
40 40
 import net.miginfocom.swing.MigLayout;
41 41
 
42 42
 /** Swing input field. */
43
-public class SwingInputField extends JTextComponent implements InputField,
43
+public class SwingInputField extends JComponent implements InputField,
44 44
         DocumentListener {
45 45
 
46 46
     /**
@@ -204,7 +204,6 @@ public class SwingInputField extends JTextComponent implements InputField,
204 204
      * 
205 205
      * @param clipboard Text to replace selection with
206 206
      */
207
-    @Override
208 207
     public void replaceSelection(String clipboard) {
209 208
         textField.replaceSelection(clipboard);
210 209
     }
@@ -214,7 +213,6 @@ public class SwingInputField extends JTextComponent implements InputField,
214 213
      * 
215 214
      * @param optionColour Colour for the caret
216 215
      */
217
-    @Override
218 216
     public void setCaretColor(Color optionColour) {
219 217
         textField.setCaretColor(optionColour);
220 218
     }

+ 40
- 8
src/com/dmdirc/ui/swing/components/SwingInputHandler.java View File

@@ -57,7 +57,15 @@ public class SwingInputHandler extends InputHandler implements KeyListener {
57 57
     /** {@inheritDoc} */
58 58
     @Override
59 59
     protected void addUpHandler() {
60
-        ((JTextComponent) target).getActionMap().put("upArrow", new AbstractAction() {
60
+        final JTextComponent localTarget;
61
+        if (target instanceof JTextComponent) {
62
+            localTarget = (JTextComponent) target;
63
+        } else if (target instanceof SwingInputField) {
64
+            localTarget = ((SwingInputField) target).getTextField();
65
+        } else {
66
+            throw new RuntimeException("Unsupported component: " + target.getClass());
67
+        }
68
+        localTarget.getActionMap().put("upArrow", new AbstractAction() {
61 69
 
62 70
             /**
63 71
              * A version number for this class. It should be changed whenever the class
@@ -72,14 +80,22 @@ public class SwingInputHandler extends InputHandler implements KeyListener {
72 80
                 doBufferUp();
73 81
             }
74 82
         });
75
-        ((JTextComponent) target).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
83
+        localTarget.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
76 84
                 put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "upArrow");
77 85
     }
78 86
 
79 87
     /** {@inheritDoc} */
80 88
     @Override
81 89
     protected void addDownHandler() {
82
-        ((JTextComponent) target).getActionMap().put("downArrow", new AbstractAction() {
90
+        final JTextComponent localTarget;
91
+        if (target instanceof JTextComponent) {
92
+            localTarget = (JTextComponent) target;
93
+        } else if (target instanceof SwingInputField) {
94
+            localTarget = ((SwingInputField) target).getTextField();
95
+        } else {
96
+            throw new RuntimeException("Unsupported component: " + target.getClass());
97
+        }
98
+        localTarget.getActionMap().put("downArrow", new AbstractAction() {
83 99
 
84 100
             /**
85 101
              * A version number for this class. It should be changed whenever the class
@@ -94,14 +110,22 @@ public class SwingInputHandler extends InputHandler implements KeyListener {
94 110
                 doBufferDown();
95 111
             }
96 112
         });
97
-        ((JTextComponent) target).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
113
+        localTarget.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
98 114
                 put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "downArrow");
99 115
     }
100 116
 
101 117
     /** {@inheritDoc} */
102 118
     @Override
103 119
     protected void addTabHandler() {
104
-        ((JTextComponent) target).getActionMap().put("tabPressed", new AbstractAction() {
120
+        final JTextComponent localTarget;
121
+        if (target instanceof JTextComponent) {
122
+            localTarget = (JTextComponent) target;
123
+        } else if (target instanceof SwingInputField) {
124
+            localTarget = ((SwingInputField) target).getTextField();
125
+        } else {
126
+            throw new RuntimeException("Unsupported component: " + target.getClass());
127
+        }
128
+        localTarget.getActionMap().put("tabPressed", new AbstractAction() {
105 129
 
106 130
             /**
107 131
              * A version number for this class. It should be changed whenever the class
@@ -116,14 +140,22 @@ public class SwingInputHandler extends InputHandler implements KeyListener {
116 140
                 doTabCompletion();
117 141
             }
118 142
         });
119
-        ((JTextComponent) target).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
143
+        localTarget.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
120 144
                 put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "tabPressed");
121 145
     }
122 146
 
123 147
     /** {@inheritDoc} */
124 148
     @Override
125 149
     protected void addEnterHandler() {
126
-        ((JTextComponent) target).getActionMap().put("enterButton", new AbstractAction() {
150
+        final JTextComponent localTarget;
151
+        if (target instanceof JTextComponent) {
152
+            localTarget = (JTextComponent) target;
153
+        } else if (target instanceof SwingInputField) {
154
+            localTarget = ((SwingInputField) target).getTextField();
155
+        } else {
156
+            throw new RuntimeException("Unsupported component: " + target.getClass());
157
+        }
158
+        localTarget.getActionMap().put("enterButton", new AbstractAction() {
127 159
 
128 160
             /**
129 161
              * A version number for this class. It should be changed whenever the class
@@ -138,7 +170,7 @@ public class SwingInputHandler extends InputHandler implements KeyListener {
138 170
                 enterPressed(target.getText());
139 171
             }
140 172
         });
141
-        ((JTextComponent) target).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
173
+        localTarget.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
142 174
                 put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enterButton");
143 175
     }
144 176
 

Loading…
Cancel
Save