KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > swing > gui > lib > FileChooserBox


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.explorer.swing.gui.lib;
28
29 import javax.swing.JFileChooser JavaDoc;
30 import javax.swing.JTextField JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.AbstractAction JavaDoc;
34 import javax.swing.Box JavaDoc;
35 import javax.swing.SwingConstants JavaDoc;
36
37 import org.objectweb.util.explorer.swing.gui.api.ValidateReport;
38
39 import java.awt.Component JavaDoc;
40 import java.awt.Dimension JavaDoc;
41 import java.io.File JavaDoc;
42 import java.net.MalformedURLException JavaDoc;
43 import java.net.URL JavaDoc;
44
45 /**
46  * This class represents the box which allows to choose a file.
47  *
48  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
49  * @version 0.1
50  */

51 public class FileChooserBox
52     extends AbstractElementBox {
53
54     //==================================================================
55
//
56
// Internal states.
57
//
58
//==================================================================
59

60     /** The file chooser that allow to find the file to load */
61     protected JFileChooser JavaDoc fileChooser_;
62
63     /** The chosen file */
64     protected File JavaDoc file_;
65
66     /** The value of the label */
67     protected String JavaDoc label_;
68
69     /** The textfield for specifying the URL to load */
70     protected JTextField JavaDoc urlField_;
71
72     //==================================================================
73
//
74
// Constructors.
75
//
76
//==================================================================
77

78     /**
79      * Default Constructor
80      * @param label The label of the box
81      * @param fileChooser The fileChooser to use
82      */

83     public FileChooserBox(String JavaDoc label, JFileChooser JavaDoc fileChooser){
84         this(label,fileChooser,true, true);
85     }
86
87     /**
88      * Constructor which specifies if the value is mandatory or not
89      * @param label The label of the box
90      * @param fileChooser The fileChooser to use
91      * @param isMandatory If the value is mandatory: TRUE, else: FALSE
92      */

93     public FileChooserBox(String JavaDoc label, JFileChooser JavaDoc fileChooser, boolean isMandatory){
94         this(label,fileChooser, isMandatory, true);
95     }
96
97     /**
98      * Constructor which specifies if the value is mandatory or not
99      * @param label The label of the box
100      * @param fileChooser The fileChooser to use
101      * @param isMandatory If the value is mandatory: TRUE, else: FALSE
102      * @param isEditable If the text field can be modify:TRUE else: FALSE
103      */

104     public FileChooserBox(String JavaDoc label, JFileChooser JavaDoc fileChooser, boolean isMandatory, boolean isEditable) {
105         super(isMandatory);
106         fileChooser_ = fileChooser;
107         label_ = label;
108         add(Box.createHorizontalGlue());
109         JLabel JavaDoc explorerLabel = new JLabel JavaDoc(label + ": ", SwingConstants.RIGHT);
110         explorerLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
111         explorerLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
112         add(explorerLabel);
113         add(Box.createHorizontalStrut(5));
114         urlField_ = new JTextField JavaDoc();
115         urlField_.setEditable(isEditable);
116         urlField_.setPreferredSize(new Dimension JavaDoc(200, 20));
117         urlField_.setMaximumSize(new Dimension JavaDoc(200, 20));
118         add(urlField_);
119         add(Box.createHorizontalStrut(5));
120         JButton JavaDoc browseButton = new JButton JavaDoc(new BrowseAction(this));
121         browseButton.setPreferredSize(new Dimension JavaDoc(20, 20));
122         browseButton.setMaximumSize(new Dimension JavaDoc(20, 20));
123         add(browseButton);
124     }
125
126     // ==================================================================
127
//
128
// Public methods for ElementBox.
129
//
130
// ==================================================================
131

132     /**
133      * Validates the content of the ElementBox. Indicates if the different value are acceptables or not.
134      * @return The corresponding ValidateReport.
135      */

136     public ValidateReport validateBox(){
137         if(isMandatory_){
138             if(urlField_.getText()==null || urlField_.getText().equals(""))
139                 return new DefaultValidateReport(false,"The \"" + label_ + "\" value is mandatory");
140         }
141         return new DefaultValidateReport();
142     }
143     
144     /**
145      * Returns the Box contains by the ElementBox.
146      * @return The Box contains by the ElementBox.
147      */

148     public Box JavaDoc getBox(){
149         return this;
150     }
151     
152     //==================================================================
153
//
154
// Public methods.
155
//
156
//==================================================================
157

158     /** Returns the chosen file (on the local file system) or null
159      * if the JChooserBox is not used.
160      */

161     public File JavaDoc getFile() {
162         return file_;
163     }
164
165     /**
166      * Returns the specified URL or null if the URL is malformed.
167      * @return The specified URL
168      */

169     public URL JavaDoc getURL() {
170         try {
171             return new URL JavaDoc(urlField_.getText());
172         } catch (MalformedURLException JavaDoc e) {
173             return null;
174         }
175     }
176
177     //==================================================================
178
//
179
// Internal classes.
180
//
181
//==================================================================
182

183     /**
184      * Action which
185      */

186     protected class BrowseAction extends AbstractAction JavaDoc {
187
188         /** The parent of the panel */
189         protected Component JavaDoc parent_;
190
191         /**
192          * Default constructor
193          */

194         protected BrowseAction(Component JavaDoc parent) {
195             super("...", null);
196             parent_ = parent;
197         }
198
199         public void actionPerformed(java.awt.event.ActionEvent JavaDoc ae) {
200             int returnVal = fileChooser_.showOpenDialog(parent_);
201             if (returnVal == JFileChooser.APPROVE_OPTION) {
202                 file_ = fileChooser_.getSelectedFile();
203                 try {
204                     urlField_.setText(fileChooser_.getSelectedFile().toURL().toString());
205                 } catch (MalformedURLException JavaDoc e) {
206                     System.out.println(getClass().getName() + " Malformed URL Exception !");
207                 }
208             }
209         }
210
211     }
212
213 }
214
Popular Tags