KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > dialog > classgen > ClassGeneratorDialog


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.modeler.dialog.classgen;
57
58 import java.awt.BorderLayout JavaDoc;
59 import java.awt.Component JavaDoc;
60 import java.awt.Dimension JavaDoc;
61 import java.awt.FlowLayout JavaDoc;
62 import java.awt.event.ItemEvent JavaDoc;
63
64 import javax.swing.Box JavaDoc;
65 import javax.swing.JPanel JavaDoc;
66 import javax.swing.JScrollPane JavaDoc;
67 import javax.swing.JTable JavaDoc;
68 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
69 import javax.swing.table.TableCellRenderer JavaDoc;
70
71 import org.objectstyle.cayenne.modeler.dialog.validator.ValidatorDialog;
72 import org.scopemvc.core.PropertyManager;
73 import org.scopemvc.core.Selector;
74 import org.scopemvc.view.swing.SAction;
75 import org.scopemvc.view.swing.SButton;
76 import org.scopemvc.view.swing.SCheckBox;
77 import org.scopemvc.view.swing.SPanel;
78 import org.scopemvc.view.swing.STable;
79 import org.scopemvc.view.swing.STableModel;
80 import org.scopemvc.view.swing.STextField;
81 import org.scopemvc.view.swing.SwingView;
82
83 import com.jgoodies.forms.builder.PanelBuilder;
84 import com.jgoodies.forms.layout.CellConstraints;
85 import com.jgoodies.forms.layout.FormLayout;
86
87 /**
88  * Dialog for generating Java classes from the DataMap.
89  *
90  * @author Michael Misha Shengaout
91  * @author Andrei Adamchik
92  */

93 public class ClassGeneratorDialog extends SPanel {
94
95     protected STable table;
96
97     public ClassGeneratorDialog() {
98
99         // create widgets
100
final STextField superClassPackage = new STextField(30);
101         superClassPackage.setSelector("superClassPackage");
102
103         final STextField superClassTemplate = new STextField(30);
104         superClassTemplate.setSelector("customSuperclassTemplate");
105
106         STextField classTemplate = new STextField(30);
107         classTemplate.setSelector("customClassTemplate");
108
109         STextField folder = new STextField(30);
110         folder.setSelector("outputDir");
111
112         SButton chooseButton = new SButton(new SAction(
113                 ClassGeneratorController.CHOOSE_LOCATION_CONTROL));
114         chooseButton.setEnabled(true);
115
116         SButton chooseTemplateButton = new SButton(new SAction(
117                 ClassGeneratorController.CHOOSE_TEMPLATE_CONTROL));
118         chooseTemplateButton.setEnabled(true);
119
120         final SButton chooseSuperTemplateButton = new SButton(new SAction(
121                 ClassGeneratorController.CHOOSE_SUPERTEMPLATE_CONTROL));
122         chooseSuperTemplateButton.setEnabled(true);
123
124         SButton selectAllButton = new SButton(new SAction(
125                 ClassGeneratorController.SELECT_ALL_CONTROL));
126         selectAllButton.setEnabled(true);
127
128         SButton generateButton = new SButton(new SAction(
129                 ClassGeneratorController.GENERATE_CLASSES_CONTROL));
130         generateButton.setEnabled(true);
131
132         SButton cancelButton = new SButton(new SAction(
133                 ClassGeneratorController.CANCEL_CONTROL));
134         cancelButton.setEnabled(true);
135
136         SCheckBox generateSuperclass = new SCheckBox() {
137
138             // (de)activate the text fields
139
public void itemStateChanged(ItemEvent JavaDoc inEvent) {
140                 boolean enabled = inEvent.getStateChange() == ItemEvent.SELECTED;
141                 superClassPackage.setEnabled(enabled);
142                 superClassTemplate.setEnabled(enabled);
143                 chooseSuperTemplateButton.setEnabled(enabled);
144
145                 super.itemStateChanged(inEvent);
146             }
147         };
148         generateSuperclass.setSelector("pairs");
149
150         // **** build entity table
151
table = new ClassGeneratorTable();
152         table.setRowHeight(25);
153         table.setRowMargin(3);
154         ClassGeneratorModel model = new ClassGeneratorModel(table);
155         model.setSelector("entities");
156         model.setColumnNames(new String JavaDoc[] {
157                 "Entity", "Class", "Generate", "Problems"
158         });
159         model.setColumnSelectors(new String JavaDoc[] {
160                 "entity.name", "entity.className", "selected", "validationMessage"
161         });
162
163         table.setModel(model);
164         table.getColumnModel().getColumn(2).setPreferredWidth(30);
165
166         // assemble..
167

168         CellConstraints cc = new CellConstraints();
169         PanelBuilder builder = new PanelBuilder(
170                 new FormLayout(
171                         "right:max(50dlu;pref), 3dlu, fill:pref:grow, 3dlu, left:70",
172                         "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, fill:100dlu:grow"));
173         builder.setDefaultDialogBorder();
174
175         builder.addSeparator("Generated Class Settings", cc.xywh(1, 1, 5, 1));
176         builder.addLabel("Output Directory:", cc.xy(1, 3));
177         builder.add(folder, cc.xy(3, 3));
178         builder.add(chooseButton, cc.xy(5, 3));
179         builder.addLabel("Custom Template:", cc.xy(1, 5));
180         builder.add(classTemplate, cc.xy(3, 5));
181         builder.add(chooseTemplateButton, cc.xy(5, 5));
182         builder.addSeparator("Generated Superclass Settings", cc.xywh(1, 7, 5, 1));
183         builder.addLabel("Generate Superclass:", cc.xy(1, 9));
184         builder.add(generateSuperclass, cc.xywh(3, 9, 3, 1));
185         builder.addLabel("Superclass Package:", cc.xy(1, 11));
186         builder.add(superClassPackage, cc.xy(3, 11));
187         builder.addLabel("Custom Template:", cc.xy(1, 13));
188         builder.add(superClassTemplate, cc.xy(3, 13));
189         builder.add(chooseSuperTemplateButton, cc.xy(5, 13));
190
191         builder.add(new JScrollPane JavaDoc(
192                 table,
193                 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
194                 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), cc.xywh(1, 15, 5, 1));
195
196         JPanel JavaDoc buttons = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.RIGHT));
197         buttons.add(selectAllButton);
198         buttons.add(Box.createHorizontalStrut(20));
199         buttons.add(cancelButton);
200         buttons.add(generateButton);
201
202         setDisplayMode(SwingView.MODAL_DIALOG);
203         setTitle("Generate Java Classes");
204         setLayout(new BorderLayout JavaDoc());
205
206         add(builder.getPanel(), BorderLayout.CENTER);
207         add(buttons, BorderLayout.SOUTH);
208     }
209
210     public STable getTable() {
211         return table;
212     }
213
214     class ClassGeneratorModel extends STableModel {
215
216         Selector enabledSelector = Selector.fromString("enabled");
217
218         /**
219          * Constructor for TableModel.
220          *
221          * @param table
222          */

223         public ClassGeneratorModel(JTable JavaDoc table) {
224             super(table);
225         }
226
227         public boolean isEnabledRow(int rowIndex) {
228             // check if this is a failed row
229
Object JavaDoc row = getElementAt(rowIndex);
230             PropertyManager manager = getItemsManager();
231             if (manager == null || row == null) {
232                 return false;
233             }
234
235             try {
236                 Boolean JavaDoc enabled = (Boolean JavaDoc) manager.get(row, enabledSelector);
237                 return enabled != null && enabled.booleanValue();
238             }
239             catch (Exception JavaDoc e) {
240                 return false;
241             }
242         }
243
244         /**
245          * @see javax.swing.table.TableModel#isCellEditable(int, int)
246          */

247         public boolean isCellEditable(int rowIndex, int columnIndex) {
248             // only checkbox is editable
249
if (columnIndex != 2) {
250                 return false;
251             }
252
253             return isEnabledRow(rowIndex);
254         }
255     }
256
257     class ClassGeneratorTable extends STable {
258
259         final Dimension JavaDoc preferredSize = new Dimension JavaDoc(500, 300);
260
261         DefaultTableCellRenderer JavaDoc problemRenderer;
262
263         ClassGeneratorTable() {
264             problemRenderer = new ClassGeneratorProblemRenderer();
265             problemRenderer.setBackground(ValidatorDialog.WARNING_COLOR);
266         }
267
268         public TableCellRenderer JavaDoc getCellRenderer(int row, int column) {
269             ClassGeneratorModel model = (ClassGeneratorModel) getModel();
270
271             return (model.isEnabledRow(row))
272                     ? super.getCellRenderer(row, column)
273                     : problemRenderer;
274         }
275
276         public Dimension JavaDoc getPreferredScrollableViewportSize() {
277             return preferredSize;
278         }
279     }
280
281     class ClassGeneratorProblemRenderer extends DefaultTableCellRenderer JavaDoc {
282
283         public Component JavaDoc getTableCellRendererComponent(
284                 JTable JavaDoc table,
285                 Object JavaDoc value,
286                 boolean isSelected,
287                 boolean hasFocus,
288                 int row,
289                 int column) {
290
291             if (value instanceof Boolean JavaDoc) {
292                 value = "";
293             }
294
295             return super.getTableCellRendererComponent(
296                     table,
297                     value,
298                     isSelected,
299                     hasFocus,
300                     row,
301                     column);
302         }
303
304     }
305
306 }
Popular Tags