KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
22  */

23 package org.enhydra.tool.codegen.wizard;
24
25 // ToolBox imports
26

27 import org.enhydra.tool.codegen.ProjectOptionSet;
28 import org.enhydra.tool.codegen.GeneratorOption;
29 import org.enhydra.tool.codegen.GeneratorException;
30 import org.enhydra.tool.codegen.ValidationException;
31 import org.enhydra.tool.common.ExtensionFilter;
32 import org.enhydra.tool.common.FileUtil;
33 import org.enhydra.tool.common.SwingUtil;
34
35 // Standard imports
36

37 import javax.swing.*;
38 import javax.swing.border.*;
39 import javax.swing.event.*;
40 import java.awt.*;
41 import java.awt.event.ActionEvent JavaDoc;
42 import java.awt.event.ActionListener JavaDoc;
43 import java.awt.event.ItemEvent JavaDoc;
44 import java.awt.event.ItemListener JavaDoc;
45 import java.beans.*;
46 import java.io.File JavaDoc;
47 import java.util.ResourceBundle JavaDoc;
48
49 /**
50  * Panel for entering the default options: copyright and
51  * copyrightfile.
52  *
53  * @author Paul Mahar
54  */

55 public class ProjectOptionPanel2 extends CodeGenPanel {
56
57     // not to be resourced
58

59     private final String JavaDoc TXT_TYPE = "txt"; // nores
60

61     // All swing controls are public to allow for easy modification in IDE
62
// implementations.
63

64     /**
65      * Panel for copyright
66      */

67     transient public JPanel panelCopyright;
68
69     /**
70      * Button for setting copyright file
71      */

72     transient public JButton buttonSet;
73
74     /**
75      * Text field for entering copyright file
76      */

77     transient public JTextField textCopyrightFile;
78
79     /**
80      * Text area for entering copyright
81      */

82     transient public JTextArea textCopyright;
83
84     /**
85      * Radio button for selecting a copyright file
86      */

87     transient public JRadioButton radioFile;
88
89     /**
90      * Radio button for entering copyright text
91      */

92     transient public JRadioButton radioText;
93
94     /**
95      * Radio button for selecting a no copyright
96      */

97     transient public JRadioButton radioNone;
98
99     /**
100      * Button group for grouping radio buttons
101      */

102     transient public ButtonGroup radioGroup;
103
104     //
105

106     transient private JScrollPane scroll;
107     transient private JPanel panelFiller;
108     transient private GridBagLayout layoutMain;
109     transient private GridBagLayout layoutDest;
110     transient private LocalRadioListener radioListener;
111     transient private LocalButtonListener buttonListener;
112
113     /**
114      * Create default option panel 2 for entering the
115      * following default options: copyright and copyright file.
116      */

117     public ProjectOptionPanel2 () {
118         try {
119             jbInit ();
120             pmInit ();
121         } catch (Exception JavaDoc ex) {
122             ex.printStackTrace ();
123         }
124     }
125
126     /**
127      * Read copyright and copyrightfile values from the
128      * option set into the swing controls.
129      *
130      * @exception GeneratorException
131      * Thrown if unable to update Swing controls with option set values.
132      */

133     public void readOptionSet () throws GeneratorException {
134         GeneratorOption option = null;
135
136         // copyright
137

138         option = getOptionSet ().lookup (ProjectOptionSet.COPYRIGHT);
139
140         textCopyright.setText (option.getValue ());
141
142         // copyrightfile
143

144         option = getOptionSet ().lookup (ProjectOptionSet.COPYRIGHTFILE);
145
146         if (FileUtil.isFile (option.getValue ())) {
147             textCopyrightFile.setText (option.getValue ());
148         } else {
149             textCopyrightFile.setText (new String JavaDoc ());
150         }
151
152         if (textCopyright.getText ().trim ().length () > 0) {
153             radioText.setSelected (true);
154         } else if (textCopyrightFile.getText ().trim ().length () > 0) {
155             radioFile.setSelected (true);
156         } else {
157             radioNone.setSelected (true);
158         }
159     }
160
161     /**
162      * Write copyright and copyrightfile values from the
163      * swing controls into the option set.
164      *
165      * @exception GeneratorException
166      * Thrown if unable to update option set from Swing control values.
167      */

168     public void writeOptionSet () throws GeneratorException {
169         String JavaDoc value = new String JavaDoc ();
170
171         // copyright
172

173         value = textCopyright.getText ().trim ();
174
175         getOptionSet ().lookup (ProjectOptionSet.COPYRIGHT).setValue (value);
176
177         // copyright file
178

179         value = textCopyrightFile.getText ();
180
181         getOptionSet ().lookup (ProjectOptionSet.COPYRIGHTFILE).setValue (value);
182     }
183
184     /**
185      * Validate the copyrightfile and copyright values
186      * in the swing controls.
187      *
188      * @exception ValidationException
189      * Thrown if swing control values are not valid for the
190      * current option set.
191      */

192     public void validateOptionSet () throws ValidationException {
193         String JavaDoc value = new String JavaDoc ();
194
195         if (textCopyrightFile.getText ().trim ().length () > 0) {
196             File JavaDoc file = new File JavaDoc (textCopyrightFile.getText ());
197
198             if ((!file.exists ()) || (!file.isFile ())) {
199                 throw new ValidationException (res.getString ("Copyright_file_is_not"));
200             }
201         }
202     }
203
204     /**
205      * Get the title to use on the current page.
206      *
207      * @return
208      * A string to place at the top of a CodeGen wizard panel.
209      */

210     public String JavaDoc getPageTitle () {
211         return res.getString ("Copyright");
212     }
213
214     /**
215      * Get the instructions for entering option values for the
216      * current page.
217      *
218      * @return
219      * A string to place below the page title.
220      */

221     public String JavaDoc getInstructions () {
222         StringBuffer JavaDoc buf = new StringBuffer JavaDoc ();
223
224         buf.append (res.getString ("InstructCopy1"));
225         buf.append (res.getString ("InstructCopy2"));
226         buf.append (res.getString ("InstructCopy3"));
227         buf.append (res.getString ("InstructCopy4"));
228
229         return buf.toString ();
230     }
231
232     // /
233
// / PRIVATE METHODS
234
// /
235

236     /**
237      * Method declaration
238      *
239      *
240      * @exception Exception
241      */

242     private void jbInit () throws Exception JavaDoc {
243         panelCopyright =
244             (JPanel) Beans.instantiate (getClass ().getClassLoader (),
245                                         JPanel.class.getName ());
246         layoutDest =
247             (GridBagLayout) Beans.instantiate (getClass ().getClassLoader (),
248                                                GridBagLayout.class.getName ());
249         buttonSet =
250             (JButton) Beans.instantiate (getClass ().getClassLoader (),
251                                          JButton.class.getName ());
252         textCopyrightFile =
253             (JTextField) Beans.instantiate (getClass ().getClassLoader (),
254                                             JTextField.class.getName ());
255         layoutMain =
256             (GridBagLayout) Beans.instantiate (getClass ().getClassLoader (),
257                                                GridBagLayout.class.getName ());
258         panelFiller =
259             (JPanel) Beans.instantiate (getClass ().getClassLoader (),
260                                         JPanel.class.getName ());
261         textCopyright =
262             (JTextArea) Beans.instantiate (getClass ().getClassLoader (),
263                                            JTextArea.class.getName ());
264         radioFile =
265             (JRadioButton) Beans.instantiate (getClass ().getClassLoader (),
266                                               JRadioButton.class.getName ());
267         radioText =
268             (JRadioButton) Beans.instantiate (getClass ().getClassLoader (),
269                                               JRadioButton.class.getName ());
270         radioNone =
271             (JRadioButton) Beans.instantiate (getClass ().getClassLoader (),
272                                               JRadioButton.class.getName ());
273         scroll = new JScrollPane (textCopyright);
274
275         buttonSet.setText (res.getString ("Set_"));
276         panelCopyright.setLayout (layoutDest);
277         radioFile.setText (res.getString ("Choose_file"));
278         radioText.setText (res.getString ("Enter_copyright_text"));
279         radioNone.setText (res.getString ("No_copyright"));
280         panelCopyright.add (textCopyrightFile,
281                             new GridBagConstraints (0, 2, 2, 1, 0.1, 0.1,
282                                                     GridBagConstraints.WEST,
283                                                     GridBagConstraints.HORIZONTAL,
284                                                     new Insets (0, 5, 5, 5),
285                                                     0, 0));
286         panelCopyright.add (scroll,
287                             new GridBagConstraints (0, 3, 4, 1, 0.1, 0.1,
288                                                     GridBagConstraints.CENTER,
289                                                     GridBagConstraints.BOTH,
290                                                     new Insets (5, 5, 5, 5),
291                                                     0, 50));
292         panelCopyright.add (radioFile,
293                             new GridBagConstraints (0, 0, 1, 1, 0.0, 0.0,
294                                                     GridBagConstraints.CENTER,
295                                                     GridBagConstraints.HORIZONTAL,
296                                                     new Insets (5, 5, 5, 5),
297                                                     0, 0));
298         panelCopyright.add (radioText,
299                             new GridBagConstraints (1, 0, 1, 1, 0.0, 0.0,
300                                                     GridBagConstraints.CENTER,
301                                                     GridBagConstraints.NONE,
302                                                     new Insets (5, 5, 5, 5),
303                                                     0, 0));
304         panelCopyright.add (buttonSet,
305                             new GridBagConstraints (2, 1, 2, 2, 0.0, 0.0,
306                                                     GridBagConstraints.EAST,
307                                                     GridBagConstraints.NONE,
308                                                     new Insets (5, 5, 5, 5),
309                                                     0, 0));
310         panelCopyright.add (radioNone,
311                             new GridBagConstraints (2, 0, 1, 1, 0.0, 0.0,
312                                                     GridBagConstraints.EAST,
313                                                     GridBagConstraints.NONE,
314                                                     new Insets (5, 5, 5, 5),
315                                                     0, 0));
316         this.setLayout (layoutMain);
317         this.add (panelCopyright,
318                   new GridBagConstraints (0, 1, 2, 1, 0.1, 0.1,
319                                           GridBagConstraints.CENTER,
320                                           GridBagConstraints.BOTH,
321                                           new Insets (5, 5, 5, 5), 0, 0));
322         this.add (panelFiller,
323                   new GridBagConstraints (0, 2, 2, 1, 0.4, 0.4,
324                                           GridBagConstraints.CENTER,
325                                           GridBagConstraints.BOTH,
326                                           new Insets (0, 0, 0, 0), 0, 0));
327     }
328
329     /**
330      * Method declaration
331      *
332      */

333     private void pmInit () {
334         radioGroup = new ButtonGroup ();
335
336         radioGroup.add (radioFile);
337         radioGroup.add (radioText);
338         radioGroup.add (radioNone);
339         radioNone.setSelected (true);
340
341         radioListener = new LocalRadioListener ();
342
343         radioText.addItemListener (radioListener);
344         radioFile.addItemListener (radioListener);
345         radioNone.addItemListener (radioListener);
346
347         buttonListener = new LocalButtonListener ();
348
349         buttonSet.addActionListener (buttonListener);
350         refreshControls ();
351     }
352
353     /**
354      * Method declaration
355      *
356      */

357     private void refreshControls () {
358         if (radioNone.isSelected ()) {
359             textCopyright.setText (new String JavaDoc ());
360             textCopyright.setEnabled (false);
361             textCopyrightFile.setText (new String JavaDoc ());
362             textCopyrightFile.setEnabled (false);
363             buttonSet.setEnabled (false);
364         } else if (radioText.isSelected ()) {
365             textCopyright.setEnabled (true);
366             textCopyrightFile.setText (new String JavaDoc ());
367             textCopyrightFile.setEnabled (false);
368             buttonSet.setEnabled (false);
369         } else {
370             textCopyright.setText (new String JavaDoc ());
371             textCopyright.setEnabled (false);
372             textCopyrightFile.setEnabled (true);
373             buttonSet.setEnabled (true);
374         }
375
376         if (textCopyright.isEnabled ()) {
377             textCopyright.setForeground (SystemColor.textText);
378             textCopyright.setBackground (SystemColor.textHighlightText);
379         } else {
380             textCopyright.setForeground (SystemColor.inactiveCaptionText);
381             textCopyright.setBackground (SystemColor.controlLtHighlight);
382         }
383
384         if (textCopyrightFile.isEnabled ()) {
385             textCopyrightFile.setForeground (SystemColor.textText);
386             textCopyrightFile.setBackground (SystemColor.textHighlightText);
387         } else {
388             textCopyrightFile.setForeground (SystemColor.inactiveCaptionText);
389             textCopyrightFile.setBackground (SystemColor.controlLtHighlight);
390         }
391     }
392
393     /**
394      * Method declaration
395      *
396      */

397     private void browseForFile () {
398         File JavaDoc file = null;
399         ExtensionFilter filter = null;
400
401         filter = new ExtensionFilter ();
402
403         filter.addExtension (TXT_TYPE);
404         filter.setDescriptionTitle (res.getString ("Copyright_text"));
405
406         file =
407             SwingUtil.getFileChoice (this, textCopyrightFile.getText (),
408                                      filter,
409                                      res.getString ("Choose_copyright_file"));
410
411         if (file != null) {
412             textCopyrightFile.setText (file.getAbsolutePath ());
413         }
414     }
415
416     //
417
//
418

419     private class LocalRadioListener implements ItemListener JavaDoc {
420         public void itemStateChanged (ItemEvent JavaDoc e) {
421             if (e.getStateChange () == ItemEvent.SELECTED) {
422                 refreshControls ();
423             }
424         }
425
426     }
427
428     //
429
//
430

431     private class LocalButtonListener implements ActionListener JavaDoc {
432         public void actionPerformed (ActionEvent JavaDoc event) {
433             browseForFile ();
434         }
435
436     }
437
438 }
439
440
Popular Tags