KickJava   Java API By Example, From Geeks To Geeks.

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


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.CodeGen;
29 import org.enhydra.tool.codegen.GeneratorOption;
30 import org.enhydra.tool.codegen.GeneratorException;
31 import org.enhydra.tool.codegen.Generator;
32 import org.enhydra.tool.codegen.ValidationException;
33 import org.enhydra.tool.common.PathHandle;
34 import org.enhydra.tool.common.SwingUtil;
35
36 // Standard imports
37
import javax.swing.*;
38 import javax.swing.border.*;
39 import java.awt.event.ActionEvent JavaDoc;
40 import java.awt.event.ActionListener JavaDoc;
41 import java.awt.event.ItemEvent JavaDoc;
42 import java.awt.event.ItemListener JavaDoc;
43 import java.awt.*;
44 import java.beans.*;
45 import java.io.File JavaDoc;
46 import java.util.ResourceBundle JavaDoc;
47
48 /**
49  * This class defines the type of panel you can add to an
50  * OpenTool's BasicWizardPage or an CodeGenPage.
51  */

52 public class GenSelectionPanel extends CodeGenPanel {
53
54     //
55
transient private GridBagLayout layoutMain;
56     transient private BorderLayout layoutDescription;
57     transient private JComboBox comboGenerator;
58     transient private JLabel labelGenerator;
59     transient private JPanel panelDescription;
60     transient private JTextArea textDescription;
61     transient private Generator[] generators = new Generator[0];
62     transient private LocalItemListener itemListener = null;
63     transient private Generator selection = null;
64     transient private JPanel panelFiller;
65
66     /**
67      * Create an CodeGenPanel for use in an OpenTool
68      * BasicWizardPage or an CodeGenPage.
69      */

70     public GenSelectionPanel() {
71         try {
72             jbInit();
73             pmInit();
74         } catch (Exception JavaDoc ex) {
75             ex.printStackTrace();
76         }
77     }
78
79     /**
80      * Get the title to use on the current page.
81      *
82      * @return
83      * A string to place at the top of a CodeGen wizard panel.
84      */

85     public String JavaDoc getPageTitle() {
86         return "Select a component type";
87     }
88
89     /**
90      * Get the instructions for selecting a generator.
91      *
92      * @return
93      * A string to place below the page title.
94      */

95     public String JavaDoc getInstructions() {
96         return "Select the type of component you would like "
97                + "to generate. The description applies the the current "
98                + "selection.";
99     }
100
101     /**
102      * No-op method for selection page.
103      */

104     public void readOptionSet() {}
105
106     /**
107      * No-op method for selection page.
108      */

109     public void writeOptionSet() {}
110
111     /**
112      * Write values from the swing controls into the option array.
113      */

114     public void validateOptionSet() throws ValidationException {}
115
116     /**
117      * Get the list of available generators.
118      *
119      * @return
120      * An array of generators from which a user can make a selection.
121      */

122     public Generator[] getGenerators() {
123         return generators;
124     }
125
126     /**
127      * Set the list of available generators.
128      *
129      * @param generators
130      * An array of generators from which a user can make a selection.
131      */

132     public void setGenerators(Generator[] g) {
133         generators = g;
134         comboGenerator.removeAllItems();
135         for (int i = 0; i < generators.length; i++) {
136             comboGenerator.addItem(generators[i]);
137         }
138         comboGenerator.repaint();
139     }
140
141     /**
142      * Get the generator selected by the user.
143      *
144      * @return
145      * The generator to show options for and to use for generating a project.
146      */

147     public Generator getSelection() {
148         return selection;
149     }
150
151     protected void setSelection(Generator sel) {
152         ComboBoxModel model = comboGenerator.getModel();
153         int count = model.getSize();
154
155         for (int i = 0; i < count; i++) {
156             if (model.getElementAt(i) instanceof Generator) {
157                 Generator cursor = (Generator) model.getElementAt(i);
158
159                 if (cursor.getDescription().equalsIgnoreCase(sel.getDescription())) {
160                     comboGenerator.setSelectedIndex(i);
161                     break;
162                 }
163             }
164         }
165     }
166
167     // /
168
// / PRIVATE METHODS
169
// /
170
private void jbInit() throws Exception JavaDoc {
171         panelDescription =
172             (JPanel) Beans.instantiate(getClass().getClassLoader(),
173                                        JPanel.class.getName());
174         labelGenerator =
175             (JLabel) Beans.instantiate(getClass().getClassLoader(),
176                                        JLabel.class.getName());
177         textDescription =
178             (JTextArea) Beans.instantiate(getClass().getClassLoader(),
179                                           JTextArea.class.getName());
180         comboGenerator =
181             (JComboBox) Beans.instantiate(getClass().getClassLoader(),
182                                           JComboBox.class.getName());
183         layoutMain =
184             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
185                                               GridBagLayout.class.getName());
186         layoutDescription =
187             (BorderLayout) Beans.instantiate(getClass().getClassLoader(),
188                                              BorderLayout.class.getName());
189         panelFiller = (JPanel) Beans.instantiate(getClass().getClassLoader(),
190                                                  JPanel.class.getName());
191         labelGenerator.setMaximumSize(new Dimension(80, 20));
192         labelGenerator.setMinimumSize(new Dimension(80, 20));
193         labelGenerator.setPreferredSize(new Dimension(80, 20));
194         labelGenerator.setLabelFor(comboGenerator);
195         labelGenerator.setText("Component type:");
196         textDescription.setDisabledTextColor(SystemColor.controlText);
197         textDescription.setBackground(SystemColor.text);
198         textDescription.setBounds(2, 2, 2, 2);
199         textDescription.setMargin(new Insets(5, 5, 5, 5));
200         textDescription.setEnabled(false);
201         textDescription.setEditable(false);
202         textDescription.setWrapStyleWord(true);
203         textDescription.setFont(labelGenerator.getFont());
204         textDescription.setLineWrap(true);
205         panelDescription.setLayout(layoutDescription);
206         panelDescription.setBorder(BorderFactory.createEtchedBorder());
207         panelDescription.setMaximumSize(new Dimension(220, 70));
208         panelDescription.setMinimumSize(new Dimension(220, 70));
209         panelDescription.setPreferredSize(new Dimension(220, 70));
210         panelDescription.add(textDescription, BorderLayout.CENTER);
211         comboGenerator.setMaximumSize(new Dimension(100, 21));
212         comboGenerator.setMinimumSize(new Dimension(100, 21));
213         comboGenerator.setPreferredSize(new Dimension(100, 21));
214         this.setLayout(layoutMain);
215         this.add(panelFiller,
216                  new GridBagConstraints(3, 0, 1, 1, 0.1, 0.1,
217                                         GridBagConstraints.CENTER,
218                                         GridBagConstraints.BOTH,
219                                         new Insets(0, 0, 0, 0), 0, 0));
220         this.add(comboGenerator,
221                  new GridBagConstraints(1, 0, 2, 1, 0.5, 0.5,
222                                         GridBagConstraints.WEST,
223                                         GridBagConstraints.NONE,
224                                         new Insets(5, 5, 5, 5), 100, 0));
225         this.add(labelGenerator,
226                  new GridBagConstraints(0, 0, 1, 1, 0.5, 0.5,
227                                         GridBagConstraints.WEST,
228                                         GridBagConstraints.NONE,
229                                         new Insets(5, 10, 5, 0), 60, 15));
230         this.add(panelDescription,
231                  new GridBagConstraints(0, 1, 4, 1, 0.5, 0.5,
232                                         GridBagConstraints.NORTHWEST,
233                                         GridBagConstraints.VERTICAL,
234                                         new Insets(5, 5, 5, 5), 150, 80));
235     }
236
237     private void pmInit() {
238         itemListener = new LocalItemListener();
239         comboGenerator.addItemListener(itemListener);
240     }
241
242     private class LocalItemListener implements ItemListener JavaDoc {
243         public void itemStateChanged(ItemEvent JavaDoc event) {
244             Object JavaDoc item = comboGenerator.getSelectedItem();
245
246             if (item instanceof Generator) {
247                 selection = (Generator) item;
248                 textDescription.setText(selection.getDescription());
249             } else {
250                 selection = null;
251                 textDescription.setText(new String JavaDoc());
252             }
253         }
254
255     }
256 }
257
Popular Tags