KickJava   Java API By Example, From Geeks To Geeks.

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


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 imports
27
import org.enhydra.tool.ToolBoxInfo;
28 import org.enhydra.tool.codegen.internal.ServiceOptionSet;
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 // Standard imports
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  * @author Paul Mahar
53  */

54 public class ServiceOptionPanel1 extends CodeGenPanel {
55
56     // not to be resourced
57
private final String JavaDoc DEF_PACKAGE = "untitled"; // nores
58
private final String JavaDoc DEF_PROJECT = "Untitled1"; // nores
59
private final String JavaDoc DEF_SERVICE = "Untitled1"; // nores
60

61     // Controls are public to easy modification in IDE Addin implementations.
62

63     /**
64      * Label for project name
65      */

66     transient public JLabel labelProjectName;
67
68     /**
69      * Text field for project name
70      */

71     transient public JTextField textProjectName;
72
73     /**
74      * Label for service name
75      */

76     transient public JLabel labelServiceName;
77
78     /**
79      * Text field for service name
80      */

81     transient public JTextField textServiceName;
82
83
84     /**
85      * Label for package
86      */

87     transient public JLabel labelPackage;
88
89     /**
90      * Text field for package
91      */

92     transient public JTextField textPackage;
93
94     /**
95      * Panel for destination
96      */

97     transient public JPanel panelDest;
98
99     /**
100      * Button for setting destination directory
101      */

102     transient public JButton buttonSet;
103
104     /**
105      * Text field for destination
106      */

107     transient public JTextField textDest;
108
109     //
110
transient private JPanel panelFiller;
111     transient private GridBagLayout layoutMain;
112     transient private GridBagLayout layoutDest;
113     transient private LocalButtonListener buttonListener;
114     transient private TitledBorder borderDest;
115     transient private JPanel panelEnhydraRoot;
116     transient private JTextField textEnhydraRoot;
117     transient private GridBagLayout layoutEnhydraRoot;
118     transient private Border borderEnhydraRoot;
119     transient private JButton buttonEnhydraRoot;
120
121     /**
122      * Create default option panel 1 for entering the
123      * following default options: project name, package and
124      * destination.
125      */

126     public ServiceOptionPanel1() {
127         try {
128             jbInit();
129             pmInit();
130         } catch (Exception JavaDoc ex) {
131             ex.printStackTrace();
132         }
133     }
134
135     // implements RootEditor
136
public boolean isAllowRootEdit() {
137       return textProjectName.isEnabled();
138     }
139
140     // implements RootEditor
141
public void setAllowRootEdit(boolean allow) {
142       textProjectName.setEnabled(allow);
143       textDest.setEnabled(allow);
144     }
145
146     /**
147      * Read project name, package and destination values from the
148      * option set into the swing controls.
149      *
150      * @exception GeneratorException
151      * Thrown if unable to update Swing controls with option set values.
152      */

153     public void readOptionSet() throws GeneratorException {
154         GeneratorOption option = null;
155         PathHandle handle = null;
156
157         // service name
158
option = getOptionSet().lookup(ServiceOptionSet.SERVICE);
159         textServiceName.setText(option.getValue());
160
161
162         // project name
163
option = getOptionSet().lookup(ServiceOptionSet.PROJECT);
164         textProjectName.setText(option.getValue());
165
166         // package
167
option = getOptionSet().lookup(ServiceOptionSet.PACKAGE);
168         textPackage.setText(option.getValue());
169
170         // destination
171
option = getOptionSet().lookup(ServiceOptionSet.ROOT);
172         handle = PathHandle.createPathHandle(option.getValue());
173         textDest.setText(handle.getPath());
174     }
175
176     /**
177      * Write project name, package and destination values from the
178      * swing controls into the option set.
179      *
180      * @exception GeneratorException
181      * Thrown if unable to update option set from Swing control values.
182      */

183     public void writeOptionSet() throws GeneratorException {
184         String JavaDoc value = new String JavaDoc();
185         boolean bool = false;
186
187         // name
188
value = textServiceName.getText().trim();
189         getOptionSet().lookup(ServiceOptionSet.SERVICE).setValue(value);
190
191         // project
192
value = textProjectName.getText().trim();
193         getOptionSet().lookup(ServiceOptionSet.PROJECT).setValue(value);
194
195         // package
196
value = textPackage.getText().trim();
197         getOptionSet().lookup(ServiceOptionSet.PACKAGE).setValue(value);
198
199         // destination
200
value = textDest.getText().trim();
201         getOptionSet().lookup(ServiceOptionSet.ROOT).setValue(value);
202     }
203
204     /**
205      * Validate project name, package and destination values
206      * in the swing controls.
207      *
208      * @exception ValidationException
209      * Thrown if swing control values are not valid for the
210      * current option set.
211      */

212     public void validateOptionSet() throws ValidationException {
213         String JavaDoc value = new String JavaDoc();
214
215
216         // service name
217
value = textServiceName.getText().trim();
218         if (!ValidationUtil.isJavaIdentifier(value)) {
219             throw new ValidationException("Invalid service name");
220         }
221
222         // project name
223
value = textProjectName.getText().trim();
224         if (!ValidationUtil.isJavaIdentifier(value)) {
225             throw new ValidationException(res.getString("Project_directory"));
226         }
227
228         // package
229
value = textPackage.getText().trim();
230         if (!ValidationUtil.isJavaPackage(value)) {
231             throw new ValidationException(res.getString("Package_is_not_valid"));
232         }
233
234         // destination
235
value = textDest.getText().trim();
236         if (!ValidationUtil.isParentDirectory(value)) {
237             throw new ValidationException("Invalid Project Root");
238         }
239         if (ToolBoxInfo.isRootSettable()) {
240             if (!ToolBoxInfo.isEnhydraRoot(ToolBoxInfo.getEnhydraRoot())) {
241                 throw new ValidationException("Invalid Application Server Root");
242             }
243         }
244     }
245
246     /**
247      * Get the title to use on the current page.
248      *
249      * @return
250      * A string to place at the top of a CodeGen wizard panel.
251      */

252     public String JavaDoc getPageTitle() {
253         return res.getString("Client_type_and");
254     }
255
256     /**
257      * Get the instructions for entering option values for the
258      * current page.
259      *
260      * @return
261      * A string to place below the page title.
262      */

263     public String JavaDoc getInstructions() {
264         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
265
266         buf.append(res.getString("Instruct1"));
267         buf.append(res.getString("Instruct2"));
268         buf.append(res.getString("Instruct3"));
269         buf.append(res.getString("Instruct4"));
270         return buf.toString();
271     }
272
273     //
274
// PRIVATE
275
//
276
private void chooseEnhydraRoot() {
277         File JavaDoc choice = null;
278         PathHandle path = null;
279         Properties JavaDoc props = null;
280         String JavaDoc message = null;
281
282         choice = SwingUtil.getDirectoryChoice(this,
283                                               ToolBoxInfo.getEnhydraRoot(),
284                                               "Select Application Server Root");
285         path = PathHandle.createPathHandle(choice);
286         if (path.isDirectory() && ToolBoxInfo.isEnhydraRoot(path.getPath())) {
287             try {
288                 props = ToolBoxInfo.loadProperties();
289                 props.setProperty(ToolBoxInfo.ENHYDRA_ROOT, path.getPath());
290                 ToolBoxInfo.storeProperties(props);
291                 textEnhydraRoot.setText(ToolBoxInfo.getEnhydraRoot());
292                 textEnhydraRoot.setToolTipText(textEnhydraRoot.getText());
293             } catch (ToolException e) {
294                 message = e.getMessage();
295             }
296         } else {
297             message = "Invalid Application Server Root";
298         }
299         if (message != null) {
300             JOptionPane.showMessageDialog(this, message, "Kelp Error",
301                                           JOptionPane.ERROR_MESSAGE);
302         }
303     }
304
305     private void jbInit() throws Exception JavaDoc {
306         labelServiceName =
307             (JLabel) Beans.instantiate(getClass().getClassLoader(),
308                                        JLabel.class.getName());
309
310         textServiceName =
311             (JTextField) Beans.instantiate(getClass().getClassLoader(),
312                                            JTextField.class.getName());
313
314         labelProjectName =
315             (JLabel) Beans.instantiate(getClass().getClassLoader(),
316                                        JLabel.class.getName());
317
318         textProjectName =
319             (JTextField) Beans.instantiate(getClass().getClassLoader(),
320                                            JTextField.class.getName());
321         labelPackage = (JLabel) Beans.instantiate(getClass().getClassLoader(),
322                                                   JLabel.class.getName());
323         textPackage =
324             (JTextField) Beans.instantiate(getClass().getClassLoader(),
325                                            JTextField.class.getName());
326         panelDest = (JPanel) Beans.instantiate(getClass().getClassLoader(),
327                                                JPanel.class.getName());
328         layoutDest =
329             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
330                                               GridBagLayout.class.getName());
331         buttonSet = (JButton) Beans.instantiate(getClass().getClassLoader(),
332                                                 JButton.class.getName());
333         textDest = (JTextField) Beans.instantiate(getClass().getClassLoader(),
334                                                   JTextField.class.getName());
335         layoutMain =
336             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
337                                               GridBagLayout.class.getName());
338         panelFiller = (JPanel) Beans.instantiate(getClass().getClassLoader(),
339                                                  JPanel.class.getName());
340
341         panelEnhydraRoot =
342             (JPanel) Beans.instantiate(getClass().getClassLoader(),
343                                        JPanel.class.getName());
344         textEnhydraRoot =
345             (JTextField) Beans.instantiate(getClass().getClassLoader(),
346                                            JTextField.class.getName());
347         buttonEnhydraRoot =
348             (JButton) Beans.instantiate(getClass().getClassLoader(),
349                                         JButton.class.getName());
350         layoutEnhydraRoot =
351             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
352                                               GridBagLayout.class.getName());
353         borderEnhydraRoot = BorderFactory.createTitledBorder("Application Server Root");
354         labelProjectName.setMaximumSize(new Dimension(120, 17));
355         labelProjectName.setMinimumSize(new Dimension(120, 17));
356         labelProjectName.setPreferredSize(new Dimension(120, 17));
357         labelProjectName.setText(res.getString("Project_directory1"));
358         labelPackage.setText(res.getString("Package_"));
359         buttonSet.setText(res.getString("Set_"));
360         panelDest.setBorder(BorderFactory.createTitledBorder("Project Root"));
361         panelDest.setLayout(layoutDest);
362         labelServiceName.setText("Service name:");
363     panelDest.add(textDest,
364                       new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0,
365                                              GridBagConstraints.WEST,
366                                              GridBagConstraints.HORIZONTAL,
367                                              new Insets(4, 5, 3, 5), 0, 0));
368         panelDest.add(buttonSet,
369                       new GridBagConstraints(1, 0, 1, 2, 0.0, 0.0,
370                                              GridBagConstraints.CENTER,
371                                              GridBagConstraints.NONE,
372                                              new Insets(4, 5, 3, 10), 0, 0));
373         textEnhydraRoot.setEnabled(false);
374         buttonEnhydraRoot.setText(res.getString("Set_"));
375         panelEnhydraRoot.setLayout(layoutEnhydraRoot);
376         panelEnhydraRoot.setBorder(borderEnhydraRoot);
377         panelEnhydraRoot.add(textEnhydraRoot,
378                              new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0,
379                                                     GridBagConstraints.WEST,
380                                                     GridBagConstraints.HORIZONTAL,
381                                                     new Insets(4, 5, 3, 5),
382                                                     0, 0));
383         panelEnhydraRoot.add(buttonEnhydraRoot,
384                              new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
385                                                     GridBagConstraints.CENTER,
386                                                     GridBagConstraints.NONE,
387                                                     new Insets(4, 5, 3, 10),
388                                                     0, 0));
389         this.setLayout(layoutMain);
390         this.add(labelProjectName, new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0
391             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 5, 3, 5), 50, 0));
392         this.add(labelPackage, new GridBagConstraints(0, 1, 1, 1, 0.1, 0.0
393             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 5, 3, 5), 50, 0));
394         this.add(textProjectName, new GridBagConstraints(1, 0, 1, 1, 0.1, 0.0
395             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 5, 3, 5), 150, 0));
396         this.add(panelDest,
397                  new GridBagConstraints(0, 3, 2, 1, 0.1, 0.0,
398                                         GridBagConstraints.CENTER,
399                                         GridBagConstraints.BOTH,
400                                         new Insets(3, 5, 3, 5), 0, 0));
401         this.add(textPackage, new GridBagConstraints(1, 1, 1, 1, 0.1, 0.0
402             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 5, 3, 5), 150, 0));
403         this.add(panelFiller,
404                  new GridBagConstraints(0, 5, 2, 1, 0.4, 0.4,
405                                         GridBagConstraints.CENTER,
406                                         GridBagConstraints.BOTH,
407                                         new Insets(0, 0, 0, 0), 0, 0));
408         this.add(labelServiceName, new GridBagConstraints(0, 2, 1, 1, 0.1, 0.0
409             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 3, 5), 50, 0));
410         this.add(textServiceName, new GridBagConstraints(1, 2, 1, 1, 0.1, 0.0
411             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 3, 5), 50, 0));
412
413         this.add(panelEnhydraRoot,
414                  new GridBagConstraints(0, 4, 4, 1, 0.1, 0.0,
415                                         GridBagConstraints.CENTER,
416                                         GridBagConstraints.BOTH,
417                                         new Insets(3, 5, 5, 5), 0, 0));
418     }
419
420     private void pmInit() {
421         String JavaDoc[] docTypes = new String JavaDoc[0];
422
423         buttonListener = new LocalButtonListener();
424         buttonSet.addActionListener(buttonListener);
425         textDest.setText(new String JavaDoc());
426         textPackage.setText(DEF_PACKAGE);
427         textProjectName.setText(DEF_PROJECT);
428         textServiceName.setText(DEF_SERVICE);
429         docTypes = ToolBoxInfo.getSupportedDocTypes();
430         textEnhydraRoot.setText(ToolBoxInfo.getEnhydraRoot());
431         textEnhydraRoot.setToolTipText(textEnhydraRoot.getText());
432         textEnhydraRoot.setEnabled(false);
433         if (ToolBoxInfo.isRootSettable()) {
434             buttonListener = new LocalButtonListener();
435             buttonEnhydraRoot.addActionListener(buttonListener);
436             buttonEnhydraRoot.setEnabled(true);
437         } else {
438             buttonEnhydraRoot.setVisible(false);
439             panelEnhydraRoot.remove(buttonEnhydraRoot);
440         }
441     }
442
443     private void browseForFile() {
444         File JavaDoc destDir = null;
445
446         destDir =
447             SwingUtil.getDirectoryChoice(this, textDest.getText(),
448                                          res.getString("Choose_the_root_path"));
449         if (destDir != null) {
450             textDest.setText(PathHandle.createPathString(destDir));
451         }
452     }
453
454     private class LocalButtonListener implements ActionListener JavaDoc {
455         public void actionPerformed(ActionEvent JavaDoc event) {
456             Object JavaDoc source = event.getSource();
457
458             if (source == buttonSet) {
459                 browseForFile();
460             } else if (source == buttonEnhydraRoot) {
461                 chooseEnhydraRoot();
462             }
463         }
464
465     }
466 }
467
Popular Tags