package javax.swing;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.accessibility.AccessibleStateSet;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;
import javax.swing.text.TextAction;
public class JTextField extends JTextComponent
implements SwingConstants
{
protected class AccessibleJTextField extends AccessibleJTextComponent
{
private static final long serialVersionUID = 8255147276740453036L;
protected AccessibleJTextField()
{
}
public AccessibleStateSet getAccessibleStateSet()
{
return null;
}
}
private static final long serialVersionUID = 353853209832607592L;
private static final Action[] actions;
public static final String notifyAction = "notify-field-accept";
static
{
actions = new Action[1];
actions[0] = new TextAction(notifyAction)
{
public void actionPerformed(ActionEvent event)
{
JTextField textField = (JTextField) event.getSource();
textField.fireActionPerformed();
}
};
}
private int columns;
private int align;
private int scrollOffset;
private Action action;
private String actionCommand;
private PropertyChangeListener actionPropertyChangeListener;
public JTextField()
{
this(null, null, 0);
}
public JTextField(String text)
{
this(null, text, 0);
}
public JTextField(int columns)
{
this(null, null, columns);
}
public JTextField(String text, int columns)
{
this(null, text, columns);
}
public JTextField(Document doc, String text, int columns)
{
if (columns < 0)
throw new IllegalArgumentException();
this.columns = columns;
setDocument(doc == null ? createDefaultModel() : doc);
if (text != null)
setText(text);
}
protected Document createDefaultModel()
{
return new PlainDocument();
}
public String getUIClassID()
{
return "TextFieldUI";
}
public void addActionListener(ActionListener listener)
{
listenerList.add(ActionListener.class, listener);
}
public void removeActionListener(ActionListener listener)
{
listenerList.remove(ActionListener.class, listener);
}
public ActionListener[] getActionListeners()
{
return (ActionListener[]) getListeners(ActionListener.class);
}
protected void fireActionPerformed()
{
ActionEvent event = new ActionEvent(this, 0, notifyAction);
ActionListener[] listeners = getActionListeners();
for (int index = 0; index < listeners.length; ++index)
listeners[index].actionPerformed(event);
}
public int getColumns()
{
return columns;
}
public void setColumns(int columns)
{
if (columns < 0)
throw new IllegalArgumentException();
this.columns = columns;
invalidate();
repaint();
}
public int getHorizontalAlignment()
{
return align;
}
public void setHorizontalAlignment(int newAlign)
{
if (align == newAlign)
return;
int oldAlign = align;
align = newAlign;
firePropertyChange("horizontalAlignment", oldAlign, newAlign);
invalidate();
repaint();
}
public void setFont(Font newFont)
{
super.setFont(newFont);
revalidate();
}
public Dimension getPreferredSize()
{
Dimension size = super.getPreferredSize();
if (columns != 0)
size.width = columns * getColumnWidth();
return size;
}
public int getScrollOffset()
{
return scrollOffset;
}
public void setScrollOffset(int offset)
{
scrollOffset = offset;
}
public Action[] getActions()
{
return TextAction.augmentList(super.getActions(), actions);
}
public void postActionEvent()
{
String command = actionCommand != null ? actionCommand : getText();
ActionEvent event = new ActionEvent(this, 0, command);
ActionListener[] listeners = getActionListeners();
for (int index = 0; index < listeners.length; ++index)
listeners[index].actionPerformed(event);
}
public Action getAction()
{
return action;
}
public void setAction(Action newAction)
{
if (action == newAction)
return;
if (action != null)
{
removeActionListener(action);
action.removePropertyChangeListener(actionPropertyChangeListener);
actionPropertyChangeListener = null;
}
Action oldAction = action;
action = newAction;
if (action != null)
{
addActionListener(action);
actionPropertyChangeListener =
createActionPropertyChangeListener(action);
action.addPropertyChangeListener(actionPropertyChangeListener);
}
firePropertyChange("horizontalAlignment", oldAction, newAction);
}
public void setActionCommand(String command)
{
actionCommand = command;
}
protected PropertyChangeListener createActionPropertyChangeListener(Action action)
{
return new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent event)
{
String name = event.getPropertyName();
if (name.equals("enabled"))
{
boolean enabled = ((Boolean) event.getNewValue()).booleanValue();
JTextField.this.setEnabled(enabled);
}
else if (name.equals(Action.SHORT_DESCRIPTION))
{
JTextField.this.setToolTipText((String) event.getNewValue());
}
}
};
}
protected void configurePropertiesFromAction(Action action)
{
if (action != null)
{
setEnabled(action.isEnabled());
setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
}
else
{
setEnabled(true);
setToolTipText(null);
}
}
protected int getColumnWidth()
{
FontMetrics metrics = getToolkit().getFontMetrics(getFont());
return metrics.charWidth('m');
}
}