|
@@ -0,0 +1,133 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
|
|
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.inputfields;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.addons.ui_swing.components.colours.ColourPickerDialog;
|
|
26
|
+import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
|
|
27
|
+import com.dmdirc.config.IdentityManager;
|
|
28
|
+import com.dmdirc.ui.interfaces.InputField;
|
|
29
|
+import com.dmdirc.util.validators.Validator;
|
|
30
|
+
|
|
31
|
+import java.awt.event.ActionEvent;
|
|
32
|
+import java.awt.event.ActionListener;
|
|
33
|
+
|
|
34
|
+import javax.swing.JTextField;
|
|
35
|
+import javax.swing.text.BadLocationException;
|
|
36
|
+
|
|
37
|
+/**
|
|
38
|
+ * Extended ValidatingTextField that adds Inputfield support.
|
|
39
|
+ */
|
|
40
|
+public class ValidatingTextFieldInputField extends ValidatingJTextField
|
|
41
|
+ implements InputField {
|
|
42
|
+
|
|
43
|
+ /**
|
|
44
|
+ * A version number for this class. It should be changed whenever the class
|
|
45
|
+ * structure is changed (or anything else that would prevent serialized
|
|
46
|
+ * objects being unserialized with the new class).
|
|
47
|
+ */
|
|
48
|
+ private static final long serialVersionUID = 2;
|
|
49
|
+
|
|
50
|
+ /** Colour picker. */
|
|
51
|
+ protected ColourPickerDialog colourPicker;
|
|
52
|
+
|
|
53
|
+ /**
|
|
54
|
+ * Creates a new text field with the specified validator.
|
|
55
|
+ *
|
|
56
|
+ * @param validator Validator for this textfield
|
|
57
|
+ */
|
|
58
|
+ public ValidatingTextFieldInputField(final Validator<String> validator) {
|
|
59
|
+ super(validator);
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ /**
|
|
63
|
+ * Creates a new text field with the specified validator.
|
|
64
|
+ *
|
|
65
|
+ * @param validator Validator for this textfield
|
|
66
|
+ * @param textField Textfield to use as a base
|
|
67
|
+ */
|
|
68
|
+ public ValidatingTextFieldInputField(final JTextField textField,
|
|
69
|
+ final Validator<String> validator) {
|
|
70
|
+ super(textField, validator);
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ /** {@inheritDoc} */
|
|
74
|
+ @Override
|
|
75
|
+ public void addActionListener(final ActionListener listener) {
|
|
76
|
+ // Ignore request - we don't handle returns for text areas
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ /** {@inheritDoc} */
|
|
80
|
+ @Override
|
|
81
|
+ public void removeActionListener(final ActionListener listener) {
|
|
82
|
+ // Ignore request - we don't handle returns for text areas
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ /** {@inheritDoc} */
|
|
86
|
+ @Override
|
|
87
|
+ public void showColourPicker(final boolean irc, final boolean hex) {
|
|
88
|
+ if (IdentityManager.getGlobalConfig().getOptionBool("general",
|
|
89
|
+ "showcolourdialog")) {
|
|
90
|
+ colourPicker = new ColourPickerDialog(irc, hex);
|
|
91
|
+ colourPicker.addActionListener(new ActionListener() {
|
|
92
|
+
|
|
93
|
+ @Override
|
|
94
|
+ public void actionPerformed(final ActionEvent actionEvent) {
|
|
95
|
+ try {
|
|
96
|
+ getDocument().insertString(getCaretPosition(),
|
|
97
|
+ actionEvent.getActionCommand(), null);
|
|
98
|
+ } catch (BadLocationException ex) {
|
|
99
|
+ //Ignore, wont happen
|
|
100
|
+ }
|
|
101
|
+ colourPicker.dispose();
|
|
102
|
+ colourPicker = null;
|
|
103
|
+ }
|
|
104
|
+ });
|
|
105
|
+ colourPicker.display();
|
|
106
|
+ colourPicker.setLocation((int) getLocationOnScreen().getX(),
|
|
107
|
+ (int) getLocationOnScreen().getY() -
|
|
108
|
+ colourPicker.getHeight());
|
|
109
|
+ }
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ /** {@inheritDoc} */
|
|
113
|
+ @Override
|
|
114
|
+ public void hideColourPicker() {
|
|
115
|
+ if (colourPicker != null) {
|
|
116
|
+ colourPicker.dispose();
|
|
117
|
+ colourPicker = null;
|
|
118
|
+ }
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+ /** {@inheritDoc} */
|
|
122
|
+ @Override
|
|
123
|
+ public int getCaretPosition() {
|
|
124
|
+ return getTextField().getCaretPosition();
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ /** {@inheritDoc} */
|
|
128
|
+ @Override
|
|
129
|
+ public void setCaretPosition(int position) {
|
|
130
|
+ getTextField().setCaretPosition(position);
|
|
131
|
+ }
|
|
132
|
+
|
|
133
|
+}
|