KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > importer > PathPanel


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common.importer;
24
25 // ToolBox imports
26
import org.enhydra.tool.common.PathHandle;
27 import org.enhydra.tool.common.SwingUtil;
28
29 // Kelp imports
30
import org.enhydra.kelp.common.PathUtil;
31 import org.enhydra.kelp.common.ValidationException;
32 import org.enhydra.kelp.common.ValidationUtil;
33
34 // Standard imports
35
import javax.swing.*;
36 import javax.swing.border.*;
37 import java.awt.*;
38 import java.awt.event.ActionEvent JavaDoc;
39 import java.awt.event.ActionListener JavaDoc;
40 import java.beans.*;
41 import java.io.File JavaDoc;
42 import java.util.ResourceBundle JavaDoc;
43
44 /**
45  * Panel for entering the default options: project name, package
46  * and destination.
47  *
48  * @author Paul Mahar
49  */

50 public class PathPanel extends ImporterPanel {
51     static ResourceBundle JavaDoc res =
52         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
53
transient private JPanel panelSource;
54     transient private JTextField textSource;
55     transient private JButton buttonSource;
56     transient private JPanel panelResource;
57     transient private JTextField textResource;
58     transient private JButton buttonResource;
59
60     //
61
transient private JPanel panelFiller;
62     transient private GridBagLayout layoutMain;
63     transient private GridBagLayout layoutSource;
64     transient private GridBagLayout layoutResource;
65     transient private LocalButtonListener buttonListener;
66     transient private TitledBorder borderSource;
67     transient private TitledBorder borderResource;
68
69     /**
70      * Create default option panel 1 for entering the
71      * following default options: project name, package and
72      * destination.
73      */

74     public PathPanel() {
75         try {
76             jbInit();
77             pmInit();
78         } catch (Exception JavaDoc ex) {
79             ex.printStackTrace();
80         }
81     }
82
83     /**
84      * Read project name, package and destination values from the
85      * option set into the swing controls.
86      *
87      * @exception GeneratorException
88      * Thrown if unable to update Swing controls with option set values.
89      */

90     public void readOptions() {
91
92         // wait till activated to to any real reading
93
}
94
95     /**
96      * Write project name, package and destination values from the
97      * swing controls into the option set.
98      *
99      * @exception GeneratorException
100      * Thrown if unable to update option set from Swing control values.
101      */

102     public void writeOptions() throws ValidationException {
103         String JavaDoc value = new String JavaDoc();
104
105         if (getPaths() == null) {
106             throw new ValidationException(res.getString("Import_paths_not_set"));
107         }
108
109         // source
110
value = textSource.getText().trim();
111         getPaths().setSourcePath(value);
112
113         // resource
114
value = textResource.getText().trim();
115         getPaths().setResourcePath(value);
116     }
117
118     /**
119      * Validate project name, package and destination values
120      * in the swing controls.
121      *
122      * @exception ValidationException
123      * Thrown if swing control values are not valid for the
124      * current option set.
125      */

126     public void validateOptions() throws ValidationException {
127         String JavaDoc value = new String JavaDoc();
128
129         // package
130
value = textSource.getText().trim();
131         if (!ValidationUtil.isDirectory(value)) {
132             throw new ValidationException(res.getString("SourcePathInvalid")
133                                           + res.getString("DirectoryNotFound"));
134         }
135
136         // destination
137
value = textResource.getText().trim();
138         if (!ValidationUtil.isDirectory(value)) {
139             throw new ValidationException(res.getString("ResourcePathInvalid")
140                                           + res.getString("DirectoryNotFound"));
141         }
142     }
143
144     /**
145      * Get the title to use on the current page.
146      *
147      * @return
148      * A string to place at the top of a CodeGen wizard panel.
149      */

150     public String JavaDoc getPageTitle() {
151         return res.getString("Path_details");
152     }
153
154     /**
155      * Get the instructions for entering option values for the
156      * current page.
157      *
158      * @return
159      * A string to place below the page title.
160      */

161     public String JavaDoc getInstructions() {
162         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
163
164         buf.append(res.getString("PathInstruct1"));
165         buf.append(res.getString("PathInstruct2"));
166         buf.append(res.getString("PathInstruct3"));
167         buf.append(res.getString("PathInstruct4"));
168         buf.append(res.getString("PathInstruct5"));
169         buf.append(res.getString("PathInstruct6"));
170         buf.append(res.getString("PathInstruct7"));
171         buf.append(res.getString("PathInstruct8"));
172         buf.append(res.getString("PathInstruct9"));
173         return buf.toString();
174     }
175
176     public void activated() {
177         initPaths();
178     }
179
180     private void initPaths() {
181         String JavaDoc path = null;
182
183         // source path
184
path = getPaths().getSourcePath();
185         if (path == null) {
186             textSource.setText(getPaths().getDefaultSourcePath());
187         } else {
188             textSource.setText(path);
189         }
190
191         // resource path
192
path = getPaths().getResourcePath();
193         if (path == null) {
194             textResource.setText(getPaths().getDefaultResourcePath());
195         } else {
196             textResource.setText(path);
197         }
198     }
199
200     // /
201
// /
202

203     /**
204      * Method declaration
205      *
206      *
207      * @exception Exception
208      *
209      * @see
210      */

211     private void jbInit() throws Exception JavaDoc {
212         textSource =
213             (JTextField) Beans.instantiate(getClass().getClassLoader(),
214                                            JTextField.class.getName());
215         panelSource = (JPanel) Beans.instantiate(getClass().getClassLoader(),
216                                                  JPanel.class.getName());
217         panelResource =
218             (JPanel) Beans.instantiate(getClass().getClassLoader(),
219                                        JPanel.class.getName());
220         layoutSource =
221             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
222                                               GridBagLayout.class.getName());
223         layoutResource =
224             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
225                                               GridBagLayout.class.getName());
226         buttonSource =
227             (JButton) Beans.instantiate(getClass().getClassLoader(),
228                                         JButton.class.getName());
229         buttonResource =
230             (JButton) Beans.instantiate(getClass().getClassLoader(),
231                                         JButton.class.getName());
232         textResource =
233             (JTextField) Beans.instantiate(getClass().getClassLoader(),
234                                            JTextField.class.getName());
235         layoutMain =
236             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
237                                               GridBagLayout.class.getName());
238         panelFiller = (JPanel) Beans.instantiate(getClass().getClassLoader(),
239                                                  JPanel.class.getName());
240         buttonSource.setText("..."); // nores
241
buttonResource.setText("..."); // nores
242
panelSource.setLayout(layoutSource);
243         panelSource.add(textSource,
244                         new GridBagConstraints(0, 0, 1, 1, 0.1, 0.1,
245                                                GridBagConstraints.WEST,
246                                                GridBagConstraints.HORIZONTAL,
247                                                new Insets(5, 5, 5, 5), 0, 0));
248         panelSource.add(buttonSource,
249                         new GridBagConstraints(1, 0, 1, 2, 0.0, 0.0,
250                                                GridBagConstraints.CENTER,
251                                                GridBagConstraints.NONE,
252                                                new Insets(5, 5, 5, 20), 0,
253                                                0));
254         panelResource.setLayout(layoutResource);
255         panelResource.add(textResource,
256                           new GridBagConstraints(0, 0, 1, 1, 0.1, 0.1,
257                                                  GridBagConstraints.WEST,
258                                                  GridBagConstraints.HORIZONTAL,
259                                                  new Insets(5, 5, 5, 5), 0,
260                                                  0));
261         panelResource.add(buttonResource,
262                           new GridBagConstraints(1, 0, 1, 2, 0.0, 0.0,
263                                                  GridBagConstraints.CENTER,
264                                                  GridBagConstraints.NONE,
265                                                  new Insets(5, 5, 5, 20), 0,
266                                                  0));
267         this.setLayout(layoutMain);
268         this.add(panelSource,
269                  new GridBagConstraints(0, 0, 1, 1, 0.1, 0.1,
270                                         GridBagConstraints.CENTER,
271                                         GridBagConstraints.BOTH,
272                                         new Insets(5, 5, 5, 5), 0, 0));
273         this.add(panelResource,
274                  new GridBagConstraints(0, 1, 1, 1, 0.1, 0.1,
275                                         GridBagConstraints.CENTER,
276                                         GridBagConstraints.BOTH,
277                                         new Insets(5, 5, 5, 5), 0, 0));
278     }
279
280     /**
281      * Method declaration
282      *
283      *
284      * @see
285      */

286     private void pmInit() {
287         Border border = BorderFactory.createEtchedBorder();
288
289         borderSource = BorderFactory.createTitledBorder(border);
290         borderSource.setTitle(res.getString("borderSource_Title"));
291         borderResource = BorderFactory.createTitledBorder(border);
292         borderResource.setTitle(res.getString("borderResource_Title"));
293         panelSource.setBorder(borderSource);
294         panelResource.setBorder(borderResource);
295         buttonListener = new LocalButtonListener();
296         buttonResource.addActionListener(buttonListener);
297         buttonSource.addActionListener(buttonListener);
298         textResource.setText(new String JavaDoc());
299         textSource.setText(new String JavaDoc());
300     }
301
302     /**
303      * Method declaration
304      *
305      *
306      * @see
307      */

308     private void browseForFile(JTextField field, String JavaDoc title) {
309         File JavaDoc destDir = null;
310
311         destDir = SwingUtil.getDirectoryChoice(this, field.getText(), title);
312         if (destDir != null) {
313             field.setText(PathHandle.createPathString(destDir));
314         }
315     }
316
317     /**
318      * Class declaration
319      *
320      *
321      * @author Paul Mahar
322      */

323     private class LocalButtonListener implements ActionListener JavaDoc {
324
325         public void actionPerformed(ActionEvent JavaDoc event) {
326             Object JavaDoc source = event.getSource();
327
328             if (source == buttonSource) {
329                 browseForFile(textSource,
330                               res.getString("Select_source_path"));
331             } else {
332                 browseForFile(textResource,
333                               res.getString("Select_resource_path"));
334             }
335         }
336
337     }
338 }
339
Popular Tags