KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > gui > lib > FileChooserBox


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2003 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.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

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

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

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

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

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

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

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

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

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

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

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

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

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

185     /**
186      * Action which
187      */

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

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