KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > codegen > wizard > AppOptionPanel1


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

24 package org.enhydra.tool.codegen.wizard;
25
26 // ToolBox
27
import org.enhydra.tool.ToolBoxInfo;
28 import org.enhydra.tool.codegen.internal.AppOptionSet;
29 import org.enhydra.tool.codegen.GeneratorOption;
30 import org.enhydra.tool.codegen.GeneratorException;
31 import org.enhydra.tool.codegen.ValidationException;
32 import org.enhydra.tool.codegen.ValidationUtil;
33 import org.enhydra.tool.common.ToolException;
34 import org.enhydra.tool.common.PathHandle;
35 import org.enhydra.tool.common.SwingUtil;
36
37 // JDK
38
import javax.swing.*;
39 import javax.swing.border.*;
40 import java.awt.*;
41 import java.awt.event.ActionEvent JavaDoc;
42 import java.awt.event.ActionListener JavaDoc;
43 import java.beans.*;
44 import java.io.File JavaDoc;
45 import java.util.Properties JavaDoc;
46 import java.util.ResourceBundle JavaDoc;
47
48 /**
49  * Panel for entering the default options: project name, package
50  * and destination.
51  */

52 public class AppOptionPanel1 extends CodeGenPanel implements RootEditor {
53
54     // not to be resourced
55
private final String JavaDoc DEF_PACKAGE = "untitled"; // nores
56
private final String JavaDoc DEF_PROJECT = "Untitled1"; // nores
57

58     // All swing controls are public to allow for easy modification in IDE
59
// implementations.
60

61     /**
62      * Label for project name
63      */

64     transient public JLabel labelProjectName;
65
66     /**
67      * Text field for project name
68      */

69     transient public JTextField textProjectName;
70
71     /**
72      * Label for package
73      */

74     transient public JLabel labelPackage;
75
76     /**
77      * Text field for package
78      */

79     transient public JTextField textPackage;
80
81     /**
82      * Panel for destination
83      */

84     transient public JPanel panelDest;
85
86     /**
87      * Button for setting destination directory
88      */

89     transient public JButton buttonSet;
90
91     /**
92      * Text field for destination
93      */

94     transient public JTextField textDest;
95
96     //
97
transient private JPanel panelFiller;
98     transient private GridBagLayout layoutMain;
99     transient private GridBagLayout layoutDest;
100     transient private LocalButtonListener buttonListener;
101     transient private TitledBorder borderDest;
102     transient private JLabel labelClient;
103     transient private JComboBox comboClient;
104     transient private JPanel panelEnhydraRoot;
105     transient private JTextField textEnhydraRoot;
106     transient private GridBagLayout layoutEnhydraRoot;
107     transient private Border borderEnhydraRoot;
108     transient private JButton buttonEnhydraRoot;
109
110     /**
111      * Create default option panel 1 for entering the
112      * following default options: project name, package and
113      * destination.
114      */

115     public AppOptionPanel1() {
116         try {
117             jbInit();
118             pmInit();
119         } catch (Exception JavaDoc ex) {
120             ex.printStackTrace();
121         }
122     }
123
124     // implements RootEditor
125
public boolean isAllowRootEdit() {
126       return textProjectName.isEnabled();
127     }
128
129     // implements RootEditor
130
public void setAllowRootEdit(boolean allow) {
131       textProjectName.setEnabled(allow);
132       textDest.setEnabled(allow);
133     }
134
135     /**
136      * Read project name, package and destination values from the
137      * option set into the swing controls.
138      *
139      * @exception GeneratorException
140      * Thrown if unable to update Swing controls with option set values.
141      */

142     public void readOptionSet() throws GeneratorException {
143         GeneratorOption option = null;
144         PathHandle handle = null;
145
146         // client type
147
option = getOptionSet().lookup(AppOptionSet.CLIENT);
148         comboClient.setSelectedItem(option.getValue());
149
150         // project name
151
option = getOptionSet().lookup(AppOptionSet.PROJECT);
152         textProjectName.setText(option.getValue());
153
154         // package
155
option = getOptionSet().lookup(AppOptionSet.PACKAGE);
156         textPackage.setText(option.getValue());
157
158         // destination
159
option = getOptionSet().lookup(AppOptionSet.ROOT);
160         handle = PathHandle.createPathHandle(option.getValue());
161         textDest.setText(handle.getPath());
162     }
163
164     /**
165      * Write project name, package and destination values from the
166      * swing controls into the option set.
167      *
168      * @exception GeneratorException
169      * Thrown if unable to update option set from Swing control values.
170      */

171     public void writeOptionSet() throws GeneratorException {
172         String JavaDoc value = new String JavaDoc();
173
174         // client
175
value = comboClient.getSelectedItem().toString().trim();
176         getOptionSet().lookup(AppOptionSet.CLIENT).setValue(value);
177
178         // name
179
value = textProjectName.getText().trim();
180         getOptionSet().lookup(AppOptionSet.PROJECT).setValue(value);
181
182         // package
183
value = textPackage.getText().trim();
184         getOptionSet().lookup(AppOptionSet.PACKAGE).setValue(value);
185
186         // destination
187
value = textDest.getText().trim();
188         getOptionSet().lookup(AppOptionSet.ROOT).setValue(value);
189     }
190
191     /**
192      * Validate project name, package and destination values
193      * in the swing controls.
194      *
195      * @exception ValidationException
196      * Thrown if swing control values are not valid for the
197      * current option set.
198      */

199     public void validateOptionSet() throws ValidationException {
200         String JavaDoc value = new String JavaDoc();
201
202         // project name
203
value = textProjectName.getText().trim();
204         if (!ValidationUtil.isJavaIdentifier(value)) {
205             throw new ValidationException(res.getString("Project_directory"));
206         }
207
208         // package
209
value = textPackage.getText().trim();
210         if (!ValidationUtil.isJavaPackage(value)) {
211             throw new ValidationException(res.getString("Package_is_not_valid"));
212         }
213
214         // destination
215
value = textDest.getText().trim();
216         if (!ValidationUtil.isParentDirectory(value)) {
217             throw new ValidationException("Invalid Project Root");
218         }
219         if (ToolBoxInfo.isRootSettable()) {
220             if (!ToolBoxInfo.isEnhydraRoot(ToolBoxInfo.getEnhydraRoot())) {
221                 throw new ValidationException("Invalid Application Server Root");
222             }
223         }
224     }
225
226     /**
227      * Get the title to use on the current page.
228      *
229      * @return
230      * A string to place at the top of a CodeGen wizard panel.
231      */

232     public String JavaDoc getPageTitle() {
233         return res.getString("Client_type_and");
234     }
235
236     /**
237      * Get the instructions for entering option values for the
238      * current page.
239      *
240      * @return
241      * A string to place below the page title.
242      */

243     public String JavaDoc getInstructions() {
244         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
245
246         buf.append(res.getString("Instruct1"));
247         buf.append(res.getString("Instruct2"));
248         buf.append(res.getString("Instruct3"));
249         buf.append(res.getString("Instruct4"));
250         return buf.toString();
251     }
252
253     //
254
// PRIVATE
255
//
256
private void chooseEnhydraRoot() {
257         File JavaDoc choice = null;
258         PathHandle path = null;
259         Properties JavaDoc props = null;
260         String JavaDoc message = null;
261
262         choice = SwingUtil.getDirectoryChoice(this,
263                                               ToolBoxInfo.getEnhydraRoot(),
264                                               "Select Application Server Root");
265         path = PathHandle.createPathHandle(choice);
266         if (path.isDirectory() && ToolBoxInfo.isEnhydraRoot(path.getPath())) {
267             try {
268                 props = ToolBoxInfo.loadProperties();
269                 props.setProperty(ToolBoxInfo.ENHYDRA_ROOT, path.getPath());
270                 ToolBoxInfo.storeProperties(props);
271                 textEnhydraRoot.setText(ToolBoxInfo.getEnhydraRoot());
272                 textEnhydraRoot.setToolTipText(textEnhydraRoot.getText());
273             } catch (ToolException e) {
274                 message = e.getMessage();
275             }
276         } else {
277             message = "Invalid Application Server Root";
278         }
279         if (message != null) {
280           JOptionPane.showMessageDialog(this, message, "Kelp Error",
281                                         JOptionPane.ERROR_MESSAGE);
282         }
283     }
284
285     /**
286      * Method declaration
287      *
288      *
289      * @exception Exception
290      *
291      * @see
292      */

293     private void jbInit() throws Exception JavaDoc {
294         textProjectName =
295             (JTextField) Beans.instantiate(getClass().getClassLoader(),
296                                            JTextField.class.getName());
297         labelPackage = (JLabel) Beans.instantiate(getClass().getClassLoader(),
298                                                   JLabel.class.getName());
299         textPackage =
300             (JTextField) Beans.instantiate(getClass().getClassLoader(),
301                                            JTextField.class.getName());
302         panelDest = (JPanel) Beans.instantiate(getClass().getClassLoader(),
303                                                JPanel.class.getName());
304         layoutDest =
305             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
306                                               GridBagLayout.class.getName());
307         buttonSet = (JButton) Beans.instantiate(getClass().getClassLoader(),
308                                                 JButton.class.getName());
309         textDest = (JTextField) Beans.instantiate(getClass().getClassLoader(),
310                                                   JTextField.class.getName());
311         layoutMain =
312             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
313                                               GridBagLayout.class.getName());
314         labelProjectName =
315             (JLabel) Beans.instantiate(getClass().getClassLoader(),
316                                        JLabel.class.getName());
317         panelFiller = (JPanel) Beans.instantiate(getClass().getClassLoader(),
318                                                  JPanel.class.getName());
319         labelClient = (JLabel) Beans.instantiate(getClass().getClassLoader(),
320                                                  JLabel.class.getName());
321         comboClient =
322             (JComboBox) Beans.instantiate(getClass().getClassLoader(),
323                                           JComboBox.class.getName());
324         panelEnhydraRoot =
325             (JPanel) Beans.instantiate(getClass().getClassLoader(),
326                                        JPanel.class.getName());
327         textEnhydraRoot =
328             (JTextField) Beans.instantiate(getClass().getClassLoader(),
329                                            JTextField.class.getName());
330         buttonEnhydraRoot =
331             (JButton) Beans.instantiate(getClass().getClassLoader(),
332                                         JButton.class.getName());
333         layoutEnhydraRoot =
334             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
335                                               GridBagLayout.class.getName());
336         borderEnhydraRoot =
337             BorderFactory.createTitledBorder("Application Server Root");
338         labelProjectName.setMaximumSize(new Dimension(120, 17));
339         labelProjectName.setMinimumSize(new Dimension(120, 17));
340         labelProjectName.setPreferredSize(new Dimension(120, 17));
341         labelProjectName.setText(res.getString("Project_directory1"));
342         labelPackage.setText(res.getString("Package_"));
343         buttonSet.setText(res.getString("Set_"));
344         panelDest.setBorder(BorderFactory.createTitledBorder("Project Root"));
345         panelDest.setLayout(layoutDest);
346         labelClient.setText(res.getString("Client_type_"));
347         panelDest.add(textDest,
348                       new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0,
349                                              GridBagConstraints.WEST,
350                                              GridBagConstraints.HORIZONTAL,
351                                              new Insets(4, 5, 3, 5), 0, 0));
352         panelDest.add(buttonSet,
353                       new GridBagConstraints(1, 0, 1, 2, 0.0, 0.0,
354                                              GridBagConstraints.CENTER,
355                                              GridBagConstraints.NONE,
356                                              new Insets(4, 5, 3, 10), 0, 0));
357         textEnhydraRoot.setEnabled(false);
358         buttonEnhydraRoot.setText(res.getString("Set_"));
359         panelEnhydraRoot.setLayout(layoutEnhydraRoot);
360         panelEnhydraRoot.setBorder(borderEnhydraRoot);
361         panelEnhydraRoot.add(textEnhydraRoot,
362                              new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0,
363                                                     GridBagConstraints.WEST,
364                                                     GridBagConstraints.HORIZONTAL,
365                                                     new Insets(4, 5, 3, 5),
366                                                     0, 0));
367         panelEnhydraRoot.add(buttonEnhydraRoot,
368                              new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
369                                                     GridBagConstraints.CENTER,
370                                                     GridBagConstraints.NONE,
371                                                     new Insets(4, 5, 3, 10),
372                                                     0, 0));
373         this.setLayout(layoutMain);
374         this.add(labelProjectName, new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0
375             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 5, 3, 5), 50, 0));
376         this.add(labelPackage, new GridBagConstraints(0, 1, 1, 1, 0.1, 0.0
377             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 5, 3, 5), 50, 0));
378         this.add(textProjectName, new GridBagConstraints(1, 0, 1, 1, 0.1, 0.0
379             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 5, 3, 5), 150, 0));
380         this.add(panelDest,
381                  new GridBagConstraints(0, 3, 2, 1, 0.1, 0.0,
382                                         GridBagConstraints.CENTER,
383                                         GridBagConstraints.BOTH,
384                                         new Insets(3, 5, 3, 5), 0, 0));
385         this.add(textPackage, new GridBagConstraints(1, 1, 1, 1, 0.1, 0.0
386             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 5, 3, 5), 150, 0));
387         this.add(panelFiller,
388                  new GridBagConstraints(0, 5, 2, 1, 0.4, 0.4,
389                                         GridBagConstraints.CENTER,
390                                         GridBagConstraints.BOTH,
391                                         new Insets(0, 0, 0, 0), 0, 0));
392         this.add(labelClient, new GridBagConstraints(0, 2, 1, 1, 0.1, 0.0
393             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 3, 5), 50, 0));
394         this.add(comboClient, new GridBagConstraints(1, 2, 1, 1, 0.1, 0.0
395             ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 3, 5), 50, 0));
396         this.add(panelEnhydraRoot,
397                  new GridBagConstraints(0, 4, 4, 1, 0.1, 0.0,
398                                         GridBagConstraints.CENTER,
399                                         GridBagConstraints.BOTH,
400                                         new Insets(3, 5, 5, 5), 0, 0));
401     }
402
403     /**
404      * Method declaration
405      *
406      *
407      * @see
408      */

409     private void pmInit() {
410         String JavaDoc[] docTypes = new String JavaDoc[0];
411
412         buttonListener = new LocalButtonListener();
413         buttonSet.addActionListener(buttonListener);
414         textDest.setText(new String JavaDoc());
415         textPackage.setText(DEF_PACKAGE);
416         textProjectName.setText(DEF_PROJECT);
417         comboClient.removeAllItems();
418         docTypes = ToolBoxInfo.getSupportedDocTypes();
419         for (int i = 0; i < docTypes.length; i++) {
420             comboClient.addItem(docTypes[i].toUpperCase());
421         }
422         textEnhydraRoot.setText(ToolBoxInfo.getEnhydraRoot());
423         textEnhydraRoot.setToolTipText(textEnhydraRoot.getText());
424         textEnhydraRoot.setEnabled(false);
425         if (ToolBoxInfo.isRootSettable()) {
426             buttonListener = new LocalButtonListener();
427             buttonEnhydraRoot.addActionListener(buttonListener);
428             buttonEnhydraRoot.setEnabled(true);
429         } else {
430             buttonEnhydraRoot.setVisible(false);
431             panelEnhydraRoot.remove(buttonEnhydraRoot);
432         }
433     }
434
435     private void browseForFile() {
436         File JavaDoc destDir = null;
437
438         destDir =
439             SwingUtil.getDirectoryChoice(this, textDest.getText(),
440                                          res.getString("Choose_the_root_path"));
441         if (destDir != null) {
442             textDest.setText(PathHandle.createPathString(destDir));
443         }
444     }
445
446     private class LocalButtonListener implements ActionListener JavaDoc {
447         public void actionPerformed(ActionEvent JavaDoc event) {
448             Object JavaDoc source = event.getSource();
449
450             if (source == buttonSet) {
451                 browseForFile();
452             } else if (source == buttonEnhydraRoot) {
453                 chooseEnhydraRoot();
454             }
455         }
456
457     }
458 }
459
Popular Tags