42 |
import java.awt.FontMetrics; |
import java.awt.FontMetrics; |
43 |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
44 |
import java.awt.event.ActionListener; |
import java.awt.event.ActionListener; |
45 |
|
import java.beans.PropertyChangeEvent; |
46 |
|
import java.beans.PropertyChangeListener; |
47 |
|
|
48 |
import javax.accessibility.AccessibleStateSet; |
import javax.accessibility.AccessibleStateSet; |
49 |
import javax.swing.text.Document; |
import javax.swing.text.Document; |
86 |
|
|
87 |
private int align; |
private int align; |
88 |
|
|
89 |
|
private int scrollOffset; |
90 |
|
|
91 |
|
/** @since 1.3 */ |
92 |
|
private Action action; |
93 |
|
|
94 |
|
/** @since 1.3 */ |
95 |
|
private String actionCommand; |
96 |
|
|
97 |
|
private PropertyChangeListener actionPropertyChangeListener; |
98 |
|
|
99 |
/** |
/** |
100 |
* Creates a new instance of <code>JTextField</code>. |
* Creates a new instance of <code>JTextField</code>. |
101 |
*/ |
*/ |
150 |
*/ |
*/ |
151 |
public JTextField(Document doc, String text, int columns) |
public JTextField(Document doc, String text, int columns) |
152 |
{ |
{ |
153 |
|
if (columns < 0) |
154 |
|
throw new IllegalArgumentException(); |
155 |
|
|
156 |
|
this.columns = columns; |
157 |
|
|
158 |
setDocument(doc == null ? createDefaultModel() : doc); |
setDocument(doc == null ? createDefaultModel() : doc); |
159 |
setText(text); |
|
160 |
setColumns(columns); |
if (text != null) |
161 |
|
setText(text); |
162 |
} |
} |
163 |
|
|
164 |
/** |
/** |
254 |
|
|
255 |
public void setHorizontalAlignment(int newAlign) |
public void setHorizontalAlignment(int newAlign) |
256 |
{ |
{ |
257 |
|
if (align == newAlign) |
258 |
|
return; |
259 |
|
|
260 |
int oldAlign = align; |
int oldAlign = align; |
261 |
align = newAlign; |
align = newAlign; |
262 |
|
firePropertyChange("horizontalAlignment", oldAlign, newAlign); |
263 |
invalidate(); |
invalidate(); |
264 |
repaint(); |
repaint(); |
|
firePropertyChange("horizontalAlignment", oldAlign, newAlign); |
|
265 |
} |
} |
266 |
|
|
267 |
public void setFont(Font newFont) |
public void setFont(Font newFont) |
288 |
|
|
289 |
return size; |
return size; |
290 |
} |
} |
291 |
|
|
292 |
|
/** |
293 |
|
* Returns the scroll offset in pixels. |
294 |
|
* |
295 |
|
* @return the scroll offset |
296 |
|
*/ |
297 |
|
public int getScrollOffset() |
298 |
|
{ |
299 |
|
return scrollOffset; |
300 |
|
} |
301 |
|
|
302 |
|
/** |
303 |
|
* Sets the scroll offset in pixels. |
304 |
|
* |
305 |
|
* @param offset the scroll offset |
306 |
|
*/ |
307 |
|
public void setScrollOffset(int offset) |
308 |
|
{ |
309 |
|
scrollOffset = offset; |
310 |
|
} |
311 |
|
|
312 |
|
public void postActionEvent() |
313 |
|
{ |
314 |
|
ActionEvent event = new ActionEvent(this, 0, actionCommand); |
315 |
|
ActionListener[] listeners = getActionListeners(); |
316 |
|
|
317 |
|
for (int index = 0; index < listeners.length; ++index) |
318 |
|
listeners[index].actionPerformed(event); |
319 |
|
} |
320 |
|
|
321 |
|
/** |
322 |
|
* @since 1.3 |
323 |
|
*/ |
324 |
|
public Action getAction() |
325 |
|
{ |
326 |
|
return action; |
327 |
|
} |
328 |
|
|
329 |
|
/** |
330 |
|
* @since 1.3 |
331 |
|
*/ |
332 |
|
public void setAction(Action newAction) |
333 |
|
{ |
334 |
|
if (action == newAction) |
335 |
|
return; |
336 |
|
|
337 |
|
if (action != null) |
338 |
|
{ |
339 |
|
removeActionListener(action); |
340 |
|
action.removePropertyChangeListener(actionPropertyChangeListener); |
341 |
|
actionPropertyChangeListener = null; |
342 |
|
} |
343 |
|
|
344 |
|
Action oldAction = action; |
345 |
|
action = newAction; |
346 |
|
|
347 |
|
if (action != null) |
348 |
|
{ |
349 |
|
addActionListener(action); |
350 |
|
actionPropertyChangeListener = |
351 |
|
createActionPropertyChangeListener(action); |
352 |
|
action.addPropertyChangeListener(actionPropertyChangeListener); |
353 |
|
} |
354 |
|
|
355 |
|
firePropertyChange("horizontalAlignment", oldAction, newAction); |
356 |
|
} |
357 |
|
|
358 |
|
/** |
359 |
|
* @since 1.3 |
360 |
|
*/ |
361 |
|
public String getActionCommand() |
362 |
|
{ |
363 |
|
return actionCommand; |
364 |
|
} |
365 |
|
|
366 |
|
/** |
367 |
|
* @since 1.3 |
368 |
|
*/ |
369 |
|
public void setActionCommand(String command) |
370 |
|
{ |
371 |
|
this.actionCommand = command; |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
|
* @since 1.3 |
376 |
|
*/ |
377 |
|
protected PropertyChangeListener createActionPropertyChangeListener(Action action) |
378 |
|
{ |
379 |
|
return new PropertyChangeListener() |
380 |
|
{ |
381 |
|
public void propertyChange(PropertyChangeEvent event) |
382 |
|
{ |
383 |
|
// Update properties "action" and "horizontalAlignment". |
384 |
|
String name = event.getPropertyName(); |
385 |
|
|
386 |
|
if (name.equals("enabled")) |
387 |
|
{ |
388 |
|
boolean enabled = ((Boolean) event.getNewValue()).booleanValue(); |
389 |
|
JTextField.this.setEnabled(enabled); |
390 |
|
} |
391 |
|
else if (name.equals(Action.SHORT_DESCRIPTION)) |
392 |
|
{ |
393 |
|
JTextField.this.setToolTipText((String) event.getNewValue()); |
394 |
|
} |
395 |
|
} |
396 |
|
}; |
397 |
|
} |
398 |
|
|
399 |
|
/** |
400 |
|
* @since 1.3 |
401 |
|
*/ |
402 |
|
protected void configurePropertiesFromAction(Action action) |
403 |
|
{ |
404 |
|
if (action != null) |
405 |
|
{ |
406 |
|
setEnabled(action.isEnabled()); |
407 |
|
setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION)); |
408 |
|
} |
409 |
|
else |
410 |
|
{ |
411 |
|
setEnabled(true); |
412 |
|
setToolTipText(null); |
413 |
|
} |
414 |
|
} |
415 |
|
|
416 |
|
protected int getColumnWidth() |
417 |
|
{ |
418 |
|
FontMetrics metrics = getToolkit().getFontMetrics(getFont()); |
419 |
|
return metrics.charWidth('m'); |
420 |
|
} |
421 |
} |
} |