blob: 151dc7c4cf0e5192ff63a44a229799d54f186f9a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.util.swing;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import net.java.sip.communicator.util.swing.event.*;
/**
* The <tt>SIPCommTextField</tt> is a <tt>JTextField</tt> that offers the
* possibility to specify a default (tip) text that explains what is the
* required data.
* @author Yana Stamcheva
*/
public class SIPCommTextField
extends JTextField
implements MouseListener,
FocusListener,
KeyListener,
DocumentListener
{
/**
* Serial version UID.
*/
private static final long serialVersionUID = 0L;
/**
* The default text.
*/
private String defaultText;
/**
* A list of all listeners registered for text field change events.
*/
private Collection<TextFieldChangeListener> changeListeners
= new LinkedList<TextFieldChangeListener>();
/**
* Indicates if the default text is currently visible.
*/
private boolean isDefaultTextVisible;
/**
* The color of the foreground.
*/
private Color foregroundColor = Color.BLACK;
/**
* The foreground color of the default text.
*/
private Color defaultTextColor = Color.GRAY;
/**
* Creates an instance of <tt>SIPCommTextField</tt> by specifying the text
* we would like to show by default in it.
* @param text the text we would like to enter by default
*/
public SIPCommTextField(String text)
{
super(text);
if (text != null && text.length() > 0)
{
this.defaultText = text;
isDefaultTextVisible = true;
}
this.setFont(getFont().deriveFont(10f));
this.setForeground(defaultTextColor);
this.addMouseListener(this);
this.addFocusListener(this);
this.addKeyListener(this);
this.getDocument().addDocumentListener(this);
}
/**
* Indicates that the mouse button was pressed on this component. Hides
* the default text when user clicks on the text field.
* @param e the <tt>MouseEvent</tt> that notified us
*/
public void mousePressed(MouseEvent e)
{
if (getText() == null)
{
clearDefaultText();
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
/**
* Selects the user text when this text field gains the focus.
* @param e the <tt>FocusEvent</tt> that notified us
*/
public void focusGained(FocusEvent e)
{
clearDefaultText();
}
/**
* Sets the default text when the field looses focus.
* @param e the <tt>FocusEvent</tt> that notified us
*/
public void focusLost(FocusEvent e)
{
if (getText() == null || getText().length() == 0)
{
setDefaultText();
}
}
/**
* Returns the text contained in this field.
* @return the text contained in this field
*/
public String getText()
{
if (!super.getText().equals(defaultText))
return super.getText();
return null;
}
/**
* Sets the text of this text field.
* @param text the text to show in this text field
*/
public void setText(String text)
{
if ((text == null || text.length() == 0) && !isFocusOwner())
setDefaultText();
else
{
this.setForeground(foregroundColor);
super.setText(text);
}
}
/**
* Sets the default text.
*/
private void setDefaultText()
{
super.setText(defaultText);
this.setForeground(defaultTextColor);
}
/**
* Clears the default text.
*/
private void clearDefaultText()
{
if (super.getText().equals(defaultText))
{
super.setText("");
this.setForeground(foregroundColor);
}
}
/**
* Clears the default text when a key pressed event is received.
* @param e the <tt>KeyEvent</tt> that notified us
*/
public void keyPressed(KeyEvent e)
{
clearDefaultText();
}
/**
* Clears the default text when a key typed event is received.
* @param e the <tt>KeyEvent</tt> that notified us
*/
public void keyTyped(KeyEvent e)
{
clearDefaultText();
}
public void keyReleased(KeyEvent e){}
/**
* Adds the given <tt>TextFieldChangeListener</tt> to the list of listeners
* notified on changes of the text contained in this field.
* @param l the <tt>TextFieldChangeListener</tt> to add
*/
public void addTextChangeListener(TextFieldChangeListener l)
{
synchronized (changeListeners)
{
changeListeners.add(l);
}
}
/**
* Removes the given <tt>TextFieldChangeListener</tt> from the list of
* listeners notified on changes of the text contained in this field.
* @param l the <tt>TextFieldChangeListener</tt> to add
*/
public void removeTextChangeListener(TextFieldChangeListener l)
{
synchronized (changeListeners)
{
changeListeners.remove(l);
}
}
public void changedUpdate(DocumentEvent e) {}
/**
* Handles the change when a char has been inserted in the field.
* @param e the <tt>DocumentEvent</tt> that notified us
*/
public void insertUpdate(DocumentEvent e)
{
if(!super.getText().equals(defaultText))
fireTextFieldChangeListener(0);
else
isDefaultTextVisible = true;
}
/**
* Handles the change when a char has been removed from the field.
* @param e the <tt>DocumentEvent</tt> that notified us
*/
public void removeUpdate(DocumentEvent e)
{
if (!isDefaultTextVisible)
fireTextFieldChangeListener(1);
else
isDefaultTextVisible = false;
}
/**
* Sets the foreground color.
*
* @param c the color to set for the text field foreground
*/
public void setForegroundColor(Color c)
{
foregroundColor = c;
}
/**
* Sets the foreground color of the default text shown in this text field.
*
* @param c the color to set
*/
public void setDefaultTextColor(Color c)
{
defaultTextColor = c;
if (isDefaultTextVisible)
setForeground(defaultTextColor);
}
/**
* Notifies all registered <tt>TextFieldChangeListener</tt>s that a change
* has occurred in the text contained in this field.
* @param eventType the type of the event to transfer
*/
private void fireTextFieldChangeListener(int eventType)
{
for (TextFieldChangeListener l : changeListeners)
switch (eventType)
{
case 0: l.textInserted(); break;
case 1: l.textRemoved(); break;
}
}
}
|