KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
59 import java.io.File JavaDoc;
60 import java.util.List JavaDoc;
61
62 import javax.swing.JFileChooser JavaDoc;
63 import javax.swing.JOptionPane JavaDoc;
64
65 import org.objectstyle.cayenne.gen.DefaultClassGenerator;
66 import org.objectstyle.cayenne.map.DataMap;
67 import org.objectstyle.cayenne.modeler.Application;
68 import org.objectstyle.cayenne.modeler.ProjectController;
69 import org.objectstyle.cayenne.modeler.dialog.pref.GeneralPreferences;
70 import org.objectstyle.cayenne.modeler.pref.DataMapDefaults;
71 import org.objectstyle.cayenne.modeler.pref.FSPath;
72 import org.objectstyle.cayenne.modeler.util.FileFilters;
73 import org.objectstyle.cayenne.pref.Domain;
74 import org.objectstyle.cayenne.pref.PreferenceDetail;
75 import org.objectstyle.cayenne.project.Project;
76 import org.objectstyle.cayenne.project.validator.Validator;
77 import org.scopemvc.controller.basic.BasicController;
78 import org.scopemvc.core.Control;
79 import org.scopemvc.core.ControlException;
80 import org.scopemvc.view.swing.STable;
81
82 /**
83  * @author Andrei Adamchik
84  */

85 public class ClassGeneratorController extends BasicController {
86
87     public static final String JavaDoc CANCEL_CONTROL = "cayenne.modeler.classgenerator.cancel.button";
88     public static final String JavaDoc GENERATE_CLASSES_CONTROL = "cayenne.modeler.classgenerator.generate.button";
89     public static final String JavaDoc SELECT_ALL_CONTROL = "cayenne.modeler.classgenerator.selectall.button";
90     public static final String JavaDoc CHOOSE_LOCATION_CONTROL = "cayenne.modeler.classgenerator.choose.button";
91     public static final String JavaDoc CHOOSE_TEMPLATE_CONTROL = "cayenne.modeler.classgenerator.choosetemplate.button";
92     public static final String JavaDoc CHOOSE_SUPERTEMPLATE_CONTROL = "cayenne.modeler.classgenerator.choosesupertemplate.button";
93
94     public ClassGeneratorController(ProjectController parent) {
95         setModel(prepareModel(parent));
96     }
97
98     protected Object JavaDoc prepareModel(ProjectController parent) {
99         Project project = parent.getProject();
100         DataMap map = parent.getCurrentDataMap();
101         DataMapDefaults preferences = parent.getDataMapPreferences();
102
103         // validate entities
104
Validator validator = project.getValidator();
105         validator.validate();
106
107         ClassGeneratorModel model = new ClassGeneratorModel(
108                 map,
109                 preferences,
110                 parent.getCurrentObjEntity(),
111                 validator.validationResults());
112
113         return model;
114     }
115
116     /**
117      * Creates and runs the class generation dialog.
118      */

119     public void startup() {
120         setView(new ClassGeneratorDialog());
121         super.startup();
122     }
123
124     protected void doHandleControl(Control control) throws ControlException {
125         if (control.matchesID(CANCEL_CONTROL)) {
126             shutdown();
127         }
128         else if (control.matchesID(GENERATE_CLASSES_CONTROL)) {
129             generateClasses();
130         }
131         else if (control.matchesID(CHOOSE_LOCATION_CONTROL)) {
132             chooseLocation();
133         }
134         else if (control.matchesID(CHOOSE_TEMPLATE_CONTROL)) {
135             chooseClassTemplate();
136         }
137         else if (control.matchesID(CHOOSE_SUPERTEMPLATE_CONTROL)) {
138             chooseSuperclassTemplate();
139         }
140         else if (control.matchesID(SELECT_ALL_CONTROL)) {
141             selectAllClasses();
142         }
143     }
144
145     protected void selectAllClasses() {
146         ClassGeneratorModel model = (ClassGeneratorModel) getModel();
147         if (model.selectAllEnabled()) {
148             // force model update
149
STable table = ((ClassGeneratorDialog) getView()).getTable();
150             table.refresh();
151         }
152     }
153
154     protected void generateClasses() {
155         ClassGeneratorModel model = (ClassGeneratorModel) getModel();
156         File JavaDoc outputDir = model.getOutputDirectory();
157
158         // no destination folder
159
if (outputDir == null) {
160             JOptionPane.showMessageDialog((Component JavaDoc) this.getView(),
161                     "Select directory for source files.");
162             return;
163         }
164
165         // no such folder
166
if (!outputDir.exists() && !outputDir.mkdirs()) {
167             JOptionPane.showMessageDialog((Component JavaDoc) this.getView(),
168                     "Can't create directory " + outputDir + ". Select a different one.");
169             return;
170         }
171
172         // not a directory
173
if (!outputDir.isDirectory()) {
174             JOptionPane.showMessageDialog((Component JavaDoc) this.getView(), outputDir
175                     + " is not a valid directory.");
176             return;
177         }
178
179         File JavaDoc classTemplate = null;
180         if (model.getCustomClassTemplate() != null) {
181             classTemplate = new File JavaDoc(model.getCustomClassTemplate());
182
183             if (!classTemplate.canRead()) {
184                 JOptionPane.showMessageDialog((Component JavaDoc) this.getView(), model
185                         .getCustomClassTemplate()
186                         + " is not a valid template file.");
187                 return;
188             }
189         }
190
191         File JavaDoc superClassTemplate = null;
192         if (model.getCustomSuperclassTemplate() != null) {
193             superClassTemplate = new File JavaDoc(model.getCustomSuperclassTemplate());
194
195             if (!superClassTemplate.canRead()) {
196                 JOptionPane.showMessageDialog((Component JavaDoc) this.getView(), model
197                         .getCustomClassTemplate()
198                         + " is not a valid template file.");
199                 return;
200             }
201         }
202
203         List JavaDoc selected = model.getSelectedEntities();
204         DefaultClassGenerator generator = new DefaultClassGenerator(model.getMap(), selected);
205
206         // configure encoding from preferences
207
Domain generatorPrefs = Application
208                 .getInstance()
209                 .getPreferenceDomain()
210                 .getSubdomain(DefaultClassGenerator.class);
211
212         PreferenceDetail detail = generatorPrefs
213                 .getDetail(GeneralPreferences.ENCODING_PREFERENCE, false);
214         if (detail != null) {
215             generator.setEncoding(detail
216                     .getProperty(GeneralPreferences.ENCODING_PREFERENCE));
217         }
218
219         generator.setDestDir(outputDir);
220         generator.setMakePairs(model.isPairs());
221         generator.setSuperPkg(model.getSuperClassPackage());
222         generator.setSuperTemplate(superClassTemplate);
223         generator.setTemplate(classTemplate);
224
225         try {
226             generator.execute();
227             JOptionPane.showMessageDialog((Component JavaDoc) this.getView(),
228                     "Class generation finished");
229             shutdown();
230         }
231         catch (Exception JavaDoc e) {
232             e.printStackTrace();
233             JOptionPane.showMessageDialog((Component JavaDoc) this.getView(),
234                     "Error generating classes - " + e.getMessage());
235         }
236     }
237
238     protected void chooseLocation() {
239         ClassGeneratorModel model = (ClassGeneratorModel) getModel();
240         File JavaDoc startDir = model.getOutputDirectory();
241
242         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
243         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
244         chooser.setDialogType(JFileChooser.OPEN_DIALOG);
245
246         // guess start directory
247
if (startDir != null) {
248             chooser.setCurrentDirectory(startDir);
249         }
250         else {
251             FSPath lastDir = Application
252                     .getInstance()
253                     .getFrameController()
254                     .getLastDirectory();
255             lastDir.updateChooser(chooser);
256         }
257
258         int result = chooser.showOpenDialog((Component JavaDoc) this.getView());
259         if (result == JFileChooser.APPROVE_OPTION) {
260             File JavaDoc selected = chooser.getSelectedFile();
261
262             // update model
263
model.setOutputDir(selected.getAbsolutePath());
264         }
265     }
266
267     protected void chooseSuperclassTemplate() {
268         ClassGeneratorModel model = (ClassGeneratorModel) getModel();
269         String JavaDoc template = chooseTemplate(model.getCustomSuperclassTemplate(),
270                 "Select Custom Superclass Template");
271
272         if (template != null) {
273             model.setCustomSuperclassTemplate(template);
274         }
275     }
276
277     protected void chooseClassTemplate() {
278         ClassGeneratorModel model = (ClassGeneratorModel) getModel();
279         String JavaDoc template = chooseTemplate(model.getCustomClassTemplate(),
280                 "Select Custom Class Template");
281
282         if (template != null) {
283             model.setCustomClassTemplate(template);
284         }
285     }
286
287     /**
288      * Picks and returns class generation velocity template.
289      */

290     private String JavaDoc chooseTemplate(String JavaDoc oldTemplate, String JavaDoc title) {
291
292         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
293         chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
294         chooser.setDialogType(JFileChooser.OPEN_DIALOG);
295         chooser.setAcceptAllFileFilterUsed(true);
296         chooser.addChoosableFileFilter(FileFilters.getVelotemplateFilter());
297
298         chooser.setDialogTitle(title);
299
300         File JavaDoc startDir = (oldTemplate != null)
301                 ? new File JavaDoc(oldTemplate).getParentFile()
302                 : null;
303         if (startDir != null) {
304             chooser.setCurrentDirectory(startDir);
305         }
306         else {
307             FSPath lastDir = Application
308                     .getInstance()
309                     .getFrameController()
310                     .getLastDirectory();
311             lastDir.updateChooser(chooser);
312         }
313
314         File JavaDoc selected = null;
315         int result = chooser.showOpenDialog((Component JavaDoc) this.getView());
316         if (result == JFileChooser.APPROVE_OPTION) {
317             selected = chooser.getSelectedFile();
318
319             FSPath lastDir = Application
320                     .getInstance()
321                     .getFrameController()
322                     .getLastDirectory();
323             lastDir.updateFromChooser(chooser);
324
325         }
326
327         return (selected != null) ? selected.getAbsolutePath() : null;
328     }
329 }
Popular Tags