KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > swing > FileField


1 package com.opensymphony.workflow.designer.swing;
2
3 import java.awt.*;
4 import java.awt.event.ActionEvent JavaDoc;
5 import java.awt.event.ActionListener JavaDoc;
6 import java.io.File JavaDoc;
7 import javax.swing.*;
8 import javax.swing.event.EventListenerList JavaDoc;
9
10 import com.opensymphony.workflow.designer.Utils;
11
12 public class FileField extends JPanel
13 {
14   private final JTextField field = new JTextField();
15   private final FixedButton button = new FixedButton(field);
16   private String JavaDoc suffix;
17   private String JavaDoc description;
18   private final int type;
19   private boolean isSave;
20   private boolean buttonEnabled;
21   private File JavaDoc file;
22   protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
23
24   /**
25    * Only one <code>ActionEvent</code> is needed per field instance since the event's only state is the source property.
26    * The source of events generated is always "this".
27    */

28   protected transient ActionEvent JavaDoc actionEvent;
29
30   public void actionPerformed(ActionEvent JavaDoc e)
31   {
32   }
33
34   public FileField(int type, boolean save, String JavaDoc suffix, String JavaDoc description)
35   {
36     super(new BorderLayout());
37     this.type = type;
38     this.isSave = save;
39     this.suffix = suffix;
40     this.description = description;
41     buttonEnabled = true;
42     add(field, BorderLayout.CENTER);
43     add(button, BorderLayout.EAST);
44     button.addActionListener(new ActionListener JavaDoc()
45     {
46       public void actionPerformed(ActionEvent JavaDoc e)
47       {
48         File JavaDoc file = Utils.promptUserForFile(FileField.this, FileField.this.type, FileField.this.isSave,
49                                             FileField.this.suffix, FileField.this.description);
50         if(file != null)
51         {
52           field.setText(file.getAbsolutePath());
53           FileField.this.file = file;
54           fireStateChanged();
55         }
56       }
57     });
58   }
59
60   /**
61    * Adds an <code>ActionListener</code> to the field.
62    *
63    * @param l the <code>ActionListener</code> to be added
64    */

65   public void addActionListener(ActionListener JavaDoc l)
66   {
67     listenerList.add(ActionListener JavaDoc.class, l);
68   }
69
70   /**
71    * Notifies all listeners that have registered interest for
72    * notification on this event type. The event instance
73    * is lazily created.
74    *
75    * @see EventListenerList
76    */

77   protected void fireStateChanged()
78   {
79     // Guaranteed to return a non-null array
80
Object JavaDoc[] listeners = listenerList.getListenerList();
81     // Process the listeners last to first, notifying
82
// those that are interested in this event
83
for(int i = listeners.length - 2; i >= 0; i -= 2)
84     {
85       if(listeners[i] == ActionListener JavaDoc.class)
86       {
87         // Lazily create the event:
88
if(actionEvent == null)
89           actionEvent = new ActionEvent JavaDoc(this, ActionEvent.ACTION_PERFORMED, "fileselected");
90         ((ActionListener JavaDoc)listeners[i + 1]).actionPerformed(actionEvent);
91       }
92     }
93   }
94
95   /**
96    * Removes an <code>ActionListener</code> from the field.
97    *
98    * @param l the listener to be removed
99    */

100   public void removeActionListener(ActionListener JavaDoc l)
101   {
102     listenerList.remove(ActionListener JavaDoc.class, l);
103   }
104
105   /**
106    * Returns an array of all the <code>ActionListener</code>s added
107    * to this FileField with addActionListener().
108    *
109    * @return all of the <code>ActionListener</code>s added or an empty
110    * array if no listeners have been added
111    */

112   public ActionListener JavaDoc[] getActionListeners()
113   {
114     return (ActionListener JavaDoc[])(listenerList.getListeners(ActionListener JavaDoc.class));
115   }
116
117   public void setButtonIcon(Icon icon)
118   {
119     button.setIcon(icon);
120   }
121
122   public void setButtonEnabled(boolean flag)
123   {
124     buttonEnabled = flag;
125     setEnabled(isEnabled());
126   }
127
128   public void setEnabled(boolean enabled)
129   {
130     super.setEnabled(enabled);
131     button.setEnabled(enabled && buttonEnabled);
132     field.setEnabled(enabled);
133   }
134
135   public FixedButton getButton()
136   {
137     return button;
138   }
139
140   public JTextField getTextField()
141   {
142     return field;
143   }
144
145   public String JavaDoc getText()
146   {
147     return field.getText().trim();
148   }
149
150   public void setText(String JavaDoc s)
151   {
152     field.setText(s);
153   }
154
155   public File JavaDoc getFile()
156   {
157     return file;
158   }
159
160   public void setTextFieldPreferredWidth(int i)
161   {
162     Dimension dimension = field.getPreferredSize();
163     FontMetrics fontmetrics = field.getFontMetrics(field.getFont());
164     dimension.width = fontmetrics.charWidth('a') * i;
165     field.setPreferredSize(dimension);
166   }
167
168   public boolean isEditable()
169   {
170     return field.isEditable();
171   }
172
173   public void setEditable(boolean flag)
174   {
175     field.setEditable(flag);
176   }
177 }
178
Popular Tags