KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > gui > URIChooser


1 /*
2
3    Copyright 2000-2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.util.gui;
19
20 import java.awt.FlowLayout JavaDoc;
21 import java.awt.GridBagConstraints JavaDoc;
22 import java.awt.GridBagLayout JavaDoc;
23 import java.awt.Insets JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31
32 import javax.swing.AbstractAction JavaDoc;
33 import javax.swing.Action JavaDoc;
34 import javax.swing.BorderFactory JavaDoc;
35 import javax.swing.JButton JavaDoc;
36 import javax.swing.JDialog JavaDoc;
37 import javax.swing.JFileChooser JavaDoc;
38 import javax.swing.JFrame JavaDoc;
39 import javax.swing.JLabel JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41 import javax.swing.JTextField JavaDoc;
42 import javax.swing.event.DocumentEvent JavaDoc;
43 import javax.swing.event.DocumentListener JavaDoc;
44 import javax.swing.filechooser.FileFilter JavaDoc;
45
46 import org.apache.batik.util.gui.resource.ActionMap;
47 import org.apache.batik.util.gui.resource.ButtonFactory;
48 import org.apache.batik.util.gui.resource.MissingListenerException;
49 import org.apache.batik.util.gui.resource.ResourceManager;
50
51 /**
52  * This class is a dialog used to enter an URI or to choose a local file.
53  *
54  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
55  * @version $Id: URIChooser.java,v 1.7 2005/03/27 08:58:37 cam Exp $
56  */

57 public class URIChooser extends JDialog JavaDoc implements ActionMap {
58
59     /**
60      * The return value if 'OK' is chosen.
61      */

62     public final static int OK_OPTION = 0;
63
64     /**
65      * The return value if 'Cancel' is chosen.
66      */

67     public final static int CANCEL_OPTION = 1;
68
69     /**
70      * The resource file name
71      */

72     protected final static String JavaDoc RESOURCES =
73         "org.apache.batik.util.gui.resources.URIChooserMessages";
74
75     /**
76      * The resource bundle
77      */

78     protected static ResourceBundle JavaDoc bundle;
79
80     /**
81      * The resource manager
82      */

83     protected static ResourceManager resources;
84     static {
85         bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault());
86         resources = new ResourceManager(bundle);
87     }
88     
89     /**
90      * The button factory
91      */

92     protected ButtonFactory buttonFactory;
93
94     /**
95      * The text field
96      */

97     protected JTextField JavaDoc textField;
98
99     /**
100      * The OK button
101      */

102     protected JButton JavaDoc okButton;
103     
104     /**
105      * The Clear button
106      */

107     protected JButton JavaDoc clearButton;
108
109     /**
110      * The current path.
111      */

112     protected String JavaDoc currentPath = ".";
113
114     /**
115      * The file filter.
116      */

117     protected FileFilter JavaDoc fileFilter;
118
119     /**
120      * The last return code.
121      */

122     protected int returnCode;
123
124     /**
125      * The last chosen path.
126      */

127     protected String JavaDoc chosenPath;
128
129     /**
130      * Creates a new URIChooser.
131      * @param d the parent dialog
132      */

133     public URIChooser(JDialog JavaDoc d) {
134         super(d);
135         initialize();
136     }
137
138     /**
139      * Creates a new URIChooser.
140      * @param f the parent frame
141      */

142     public URIChooser(JFrame JavaDoc f) {
143         super(f);
144         initialize();
145     }
146
147     /**
148      * Shows the dialog.
149      * @return OK_OPTION or CANCEL_OPTION.
150      */

151     public int showDialog() {
152         pack();
153         show();
154         return returnCode;
155     }
156
157     /**
158      * Returns the text entered by the user.
159      */

160     public String JavaDoc getText() {
161         return chosenPath;
162     }
163
164     /**
165      * Sets the file filter to use with the file selector.
166      */

167     public void setFileFilter(FileFilter JavaDoc ff) {
168         fileFilter = ff;
169     }
170
171     /**
172      * Initializes the dialog
173      */

174     protected void initialize() {
175         setModal(true);
176
177         listeners.put("BrowseButtonAction", new BrowseButtonAction());
178         listeners.put("OKButtonAction", new OKButtonAction());
179         listeners.put("CancelButtonAction", new CancelButtonAction());
180         listeners.put("ClearButtonAction", new ClearButtonAction());
181
182         setTitle(resources.getString("Dialog.title"));
183         buttonFactory = new ButtonFactory(bundle, this);
184         
185         getContentPane().add("North", createURISelectionPanel());
186         getContentPane().add("South", createButtonsPanel());
187     }
188
189     /**
190      * Creates the URI selection panel
191      */

192     protected JPanel JavaDoc createURISelectionPanel() {
193         JPanel JavaDoc p = new JPanel JavaDoc(new GridBagLayout JavaDoc());
194         p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
195             
196         ExtendedGridBagConstraints constraints;
197         constraints = new ExtendedGridBagConstraints();
198
199         constraints.insets = new Insets JavaDoc(5, 5, 5, 5);
200         constraints.weightx = 0;
201         constraints.weighty = 0;
202         constraints.fill = GridBagConstraints.HORIZONTAL;
203         constraints.setGridBounds(0, 0, 2, 1);
204         p.add(new JLabel JavaDoc(resources.getString("Dialog.label")), constraints);
205
206         textField = new JTextField JavaDoc(30);
207         textField.getDocument().addDocumentListener(new DocumentAdapter());
208         constraints.weightx = 1.0;
209         constraints.weighty = 0;
210         constraints.fill = GridBagConstraints.HORIZONTAL;
211         constraints.setGridBounds(0, 1, 1, 1);
212         p.add(textField, constraints);
213         
214         constraints.weightx = 0;
215         constraints.weighty = 0;
216         constraints.fill = GridBagConstraints.NONE;
217         constraints.setGridBounds(1, 1, 1, 1);
218         p.add(buttonFactory.createJButton("BrowseButton"), constraints);
219         
220         return p;
221     }
222
223     /**
224      * Creates the buttons panel
225      */

226     protected JPanel JavaDoc createButtonsPanel() {
227         JPanel JavaDoc p = new JPanel JavaDoc(new FlowLayout JavaDoc());
228
229         p.add(okButton = buttonFactory.createJButton("OKButton"));
230         p.add(buttonFactory.createJButton("CancelButton"));
231         p.add(clearButton = buttonFactory.createJButton("ClearButton"));
232             
233         okButton.setEnabled(false);
234         clearButton.setEnabled(false);
235         
236         return p;
237     }
238
239     /**
240      * To update the state of the OK button
241      */

242     protected void updateOKButtonAction() {
243         okButton.setEnabled(!textField.getText().equals(""));
244     }
245
246     /**
247      * To update the state of the Clear button
248      */

249     protected void updateClearButtonAction() {
250         clearButton.setEnabled(!textField.getText().equals(""));
251     }
252
253     /**
254      * To listen to the document changes
255      */

256     protected class DocumentAdapter implements DocumentListener JavaDoc {
257         public void changedUpdate(DocumentEvent JavaDoc e) {
258             updateOKButtonAction();
259             updateClearButtonAction();
260         }
261             
262         public void insertUpdate(DocumentEvent JavaDoc e) {
263             updateOKButtonAction();
264             updateClearButtonAction();
265         }
266
267         public void removeUpdate(DocumentEvent JavaDoc e) {
268             updateOKButtonAction();
269             updateClearButtonAction();
270         }
271     }
272
273     /**
274      * The action associated with the 'browse' button
275      */

276     protected class BrowseButtonAction extends AbstractAction JavaDoc {
277         public void actionPerformed(ActionEvent JavaDoc e) {
278             JFileChooser JavaDoc fileChooser = new JFileChooser JavaDoc(currentPath);
279             fileChooser.setFileHidingEnabled(false);
280             fileChooser.setFileSelectionMode
281                 (JFileChooser.FILES_AND_DIRECTORIES);
282             if (fileFilter != null) {
283                 fileChooser.setFileFilter(fileFilter);
284             }
285             
286             int choice = fileChooser.showOpenDialog(URIChooser.this);
287             if (choice == JFileChooser.APPROVE_OPTION) {
288                 File JavaDoc f = fileChooser.getSelectedFile();
289                 try {
290                     textField.setText(currentPath = f.getCanonicalPath());
291                 } catch (IOException JavaDoc ex) {
292                 }
293             }
294         }
295     }
296
297     /**
298      * The action associated with the 'OK' button of the URI chooser
299      */

300     protected class OKButtonAction extends AbstractAction JavaDoc {
301         public void actionPerformed(ActionEvent JavaDoc e) {
302             returnCode = OK_OPTION;
303             chosenPath = textField.getText();
304             dispose();
305         }
306     }
307
308     /**
309      * The action associated with the 'Cancel' button of the URI chooser
310      */

311     protected class CancelButtonAction extends AbstractAction JavaDoc {
312         public void actionPerformed(ActionEvent JavaDoc e) {
313             returnCode = CANCEL_OPTION;
314             dispose();
315             textField.setText(chosenPath);
316         }
317     }
318
319     /**
320      * The action associated with the 'Clear' button of the URI chooser
321      */

322     protected class ClearButtonAction extends AbstractAction JavaDoc {
323         public void actionPerformed(ActionEvent JavaDoc e) {
324             textField.setText("");
325         }
326     }
327
328     // ActionMap implementation
329

330     /**
331      * The map that contains the listeners
332      */

333     protected Map JavaDoc listeners = new HashMap JavaDoc(10);
334
335     /**
336      * Returns the action associated with the given string
337      * or null on error
338      * @param key the key mapped with the action to get
339      * @throws MissingListenerException if the action is not found
340      */

341     public Action JavaDoc getAction(String JavaDoc key) throws MissingListenerException {
342         return (Action JavaDoc)listeners.get(key);
343     }
344 }
345
Popular Tags