001: package com.opensymphony.workflow.designer.swing;
002:
003: import java.awt.*;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006: import java.io.File;
007: import javax.swing.*;
008: import javax.swing.event.EventListenerList;
009:
010: import com.opensymphony.workflow.designer.Utils;
011:
012: public class FileField extends JPanel {
013: private final JTextField field = new JTextField();
014: private final FixedButton button = new FixedButton(field);
015: private String suffix;
016: private String description;
017: private final int type;
018: private boolean isSave;
019: private boolean buttonEnabled;
020: private File file;
021: protected EventListenerList listenerList = new EventListenerList();
022:
023: /**
024: * Only one <code>ActionEvent</code> is needed per field instance since the event's only state is the source property.
025: * The source of events generated is always "this".
026: */
027: protected transient ActionEvent actionEvent;
028:
029: public void actionPerformed(ActionEvent e) {
030: }
031:
032: public FileField(int type, boolean save, String suffix,
033: String description) {
034: super (new BorderLayout());
035: this .type = type;
036: this .isSave = save;
037: this .suffix = suffix;
038: this .description = description;
039: buttonEnabled = true;
040: add(field, BorderLayout.CENTER);
041: add(button, BorderLayout.EAST);
042: button.addActionListener(new ActionListener() {
043: public void actionPerformed(ActionEvent e) {
044: File file = Utils.promptUserForFile(FileField.this ,
045: FileField.this .type, FileField.this .isSave,
046: FileField.this .suffix,
047: FileField.this .description);
048: if (file != null) {
049: field.setText(file.getAbsolutePath());
050: FileField.this .file = file;
051: fireStateChanged();
052: }
053: }
054: });
055: }
056:
057: /**
058: * Adds an <code>ActionListener</code> to the field.
059: *
060: * @param l the <code>ActionListener</code> to be added
061: */
062: public void addActionListener(ActionListener l) {
063: listenerList.add(ActionListener.class, l);
064: }
065:
066: /**
067: * Notifies all listeners that have registered interest for
068: * notification on this event type. The event instance
069: * is lazily created.
070: *
071: * @see EventListenerList
072: */
073: protected void fireStateChanged() {
074: // Guaranteed to return a non-null array
075: Object[] listeners = listenerList.getListenerList();
076: // Process the listeners last to first, notifying
077: // those that are interested in this event
078: for (int i = listeners.length - 2; i >= 0; i -= 2) {
079: if (listeners[i] == ActionListener.class) {
080: // Lazily create the event:
081: if (actionEvent == null)
082: actionEvent = new ActionEvent(this ,
083: ActionEvent.ACTION_PERFORMED,
084: "fileselected");
085: ((ActionListener) listeners[i + 1])
086: .actionPerformed(actionEvent);
087: }
088: }
089: }
090:
091: /**
092: * Removes an <code>ActionListener</code> from the field.
093: *
094: * @param l the listener to be removed
095: */
096: public void removeActionListener(ActionListener l) {
097: listenerList.remove(ActionListener.class, l);
098: }
099:
100: /**
101: * Returns an array of all the <code>ActionListener</code>s added
102: * to this FileField with addActionListener().
103: *
104: * @return all of the <code>ActionListener</code>s added or an empty
105: * array if no listeners have been added
106: */
107: public ActionListener[] getActionListeners() {
108: return (ActionListener[]) (listenerList
109: .getListeners(ActionListener.class));
110: }
111:
112: public void setButtonIcon(Icon icon) {
113: button.setIcon(icon);
114: }
115:
116: public void setButtonEnabled(boolean flag) {
117: buttonEnabled = flag;
118: setEnabled(isEnabled());
119: }
120:
121: public void setEnabled(boolean enabled) {
122: super .setEnabled(enabled);
123: button.setEnabled(enabled && buttonEnabled);
124: field.setEnabled(enabled);
125: }
126:
127: public FixedButton getButton() {
128: return button;
129: }
130:
131: public JTextField getTextField() {
132: return field;
133: }
134:
135: public String getText() {
136: return field.getText().trim();
137: }
138:
139: public void setText(String s) {
140: field.setText(s);
141: }
142:
143: public File getFile() {
144: return file;
145: }
146:
147: public void setTextFieldPreferredWidth(int i) {
148: Dimension dimension = field.getPreferredSize();
149: FontMetrics fontmetrics = field.getFontMetrics(field.getFont());
150: dimension.width = fontmetrics.charWidth('a') * i;
151: field.setPreferredSize(dimension);
152: }
153:
154: public boolean isEditable() {
155: return field.isEditable();
156: }
157:
158: public void setEditable(boolean flag) {
159: field.setEditable(flag);
160: }
161: }
|