KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
59 import java.util.ArrayList JavaDoc;
60 import java.util.HashMap JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Map JavaDoc;
64
65 import org.objectstyle.cayenne.map.DataMap;
66 import org.objectstyle.cayenne.map.ObjEntity;
67 import org.objectstyle.cayenne.modeler.pref.DataMapDefaults;
68 import org.objectstyle.cayenne.project.validator.ValidationInfo;
69 import org.objectstyle.cayenne.util.Util;
70 import org.scopemvc.core.Selector;
71 import org.scopemvc.model.basic.BasicModel;
72
73 /**
74  * @author Andrei Adamchik
75  */

76 public class ClassGeneratorModel extends BasicModel {
77
78     protected DataMap map;
79     protected DataMapDefaults defaults;
80
81     protected String JavaDoc outputDir;
82     protected boolean pairs;
83     protected List JavaDoc entities;
84     protected String JavaDoc superClassPackage;
85     protected String JavaDoc customSuperclassTemplate;
86     protected String JavaDoc customClassTemplate;
87
88     public ClassGeneratorModel(DataMap map, DataMapDefaults defaults,
89             ObjEntity selectedEntity, List JavaDoc validationInfo) {
90         this.map = map;
91         this.defaults = defaults;
92
93         prepareEntities(selectedEntity, validationInfo);
94         initFromDefaults();
95     }
96
97     protected void initFromDefaults() {
98         this.customClassTemplate = defaults.getSubclassTemplate();
99         this.customSuperclassTemplate = defaults.getSuperclassTemplate();
100         this.pairs = (defaults.getGeneratePairs() != null) ? defaults
101                 .getGeneratePairs()
102                 .booleanValue() : true;
103         this.outputDir = defaults.getOutputPath();
104         this.superClassPackage = defaults.getSuperclassPackage();
105     }
106
107     protected void prepareEntities(ObjEntity selectedEntity, List JavaDoc validationInfo) {
108         Map JavaDoc failedEntities = new HashMap JavaDoc();
109
110         if (validationInfo != null) {
111             Iterator JavaDoc vit = validationInfo.iterator();
112             while (vit.hasNext()) {
113                 ValidationInfo info = (ValidationInfo) vit.next();
114                 ObjEntity ent = (ObjEntity) info.getPath().firstInstanceOf(
115                         ObjEntity.class);
116                 if (ent != null) {
117                     failedEntities.put(ent.getName(), info.getMessage());
118                 }
119             }
120         }
121
122         List JavaDoc tmp = new ArrayList JavaDoc();
123         Iterator JavaDoc it = map.getObjEntities().iterator();
124         while (it.hasNext()) {
125             ObjEntity entity = (ObjEntity) it.next();
126
127             // check if entity didn't pass the validation
128
ClassGeneratorEntityWrapper wrapper = null;
129             String JavaDoc errorMessage = (String JavaDoc) failedEntities.get(entity.getName());
130             if (errorMessage != null) {
131                 wrapper = new ClassGeneratorEntityWrapper(entity, false, errorMessage);
132             }
133             else {
134                 boolean enabled = (selectedEntity != null)
135                         ? selectedEntity == entity
136                         : true;
137                 wrapper = new ClassGeneratorEntityWrapper(entity, enabled);
138             }
139
140             tmp.add(wrapper);
141         }
142
143         this.entities = tmp;
144     }
145
146     public List JavaDoc getSelectedEntities() {
147         Iterator JavaDoc it = entities.iterator();
148         List JavaDoc selected = new ArrayList JavaDoc();
149         while (it.hasNext()) {
150             ClassGeneratorEntityWrapper wrapper = (ClassGeneratorEntityWrapper) it.next();
151             if (wrapper.isSelected()) {
152                 selected.add(wrapper.getEntity());
153             }
154         }
155
156         return selected;
157     }
158
159     public boolean selectAllEnabled() {
160         boolean changed = false;
161
162         Iterator JavaDoc it = entities.iterator();
163         while (it.hasNext()) {
164             ClassGeneratorEntityWrapper wrapper = (ClassGeneratorEntityWrapper) it.next();
165             if (wrapper.isEnabled() && !wrapper.isSelected()) {
166                 wrapper.setSelected(true);
167                 changed = true;
168             }
169         }
170
171         return changed;
172     }
173
174     /**
175      * Returns the map.
176      *
177      * @return DataMap
178      */

179     public DataMap getMap() {
180         return map;
181     }
182
183     /**
184      * Returns the outputDir.
185      *
186      * @return File
187      */

188     public File JavaDoc getOutputDirectory() {
189         return (outputDir != null) ? new File JavaDoc(outputDir) : null;
190     }
191
192     /**
193      * Returns the pairs.
194      *
195      * @return boolean
196      */

197     public boolean isPairs() {
198         return pairs;
199     }
200
201     /**
202      * Sets the pairs.
203      *
204      * @param pairs The pairs to set
205      */

206     public void setPairs(boolean pairs) {
207         if (this.pairs != pairs) {
208             this.pairs = pairs;
209             this.defaults.setGeneratePairs(Boolean.valueOf(pairs));
210             fireModelChange(VALUE_CHANGED, Selector.fromString("pairs"));
211         }
212     }
213
214     public String JavaDoc getCustomClassTemplate() {
215         return customClassTemplate;
216     }
217
218     public void setCustomClassTemplate(String JavaDoc customClassTemplate) {
219         if (!Util.nullSafeEquals(this.customClassTemplate, customClassTemplate)) {
220             this.customClassTemplate = customClassTemplate;
221             this.defaults.setSubclassTemplate(customClassTemplate);
222             fireModelChange(VALUE_CHANGED, Selector.fromString("customClassTemplate"));
223         }
224     }
225
226     public String JavaDoc getCustomSuperclassTemplate() {
227         return customSuperclassTemplate;
228     }
229
230     public void setCustomSuperclassTemplate(String JavaDoc customSuperclassTemplate) {
231         if (!Util.nullSafeEquals(this.customSuperclassTemplate, customSuperclassTemplate)) {
232             this.customSuperclassTemplate = customSuperclassTemplate;
233             this.defaults.setSuperclassTemplate(customSuperclassTemplate);
234             fireModelChange(VALUE_CHANGED, Selector
235                     .fromString("customSuperclassTemplate"));
236         }
237     }
238
239     /**
240      * Returns the entities.
241      *
242      * @return List
243      */

244     public List JavaDoc getEntities() {
245         return entities;
246     }
247
248     /**
249      * Returns the outputDir.
250      *
251      * @return String
252      */

253     public String JavaDoc getOutputDir() {
254         return outputDir;
255     }
256
257     /**
258      * Sets the outputDir.
259      *
260      * @param outputDir The outputDir to set
261      */

262     public void setOutputDir(String JavaDoc outputDir) {
263         if (!Util.nullSafeEquals(this.outputDir, outputDir)) {
264             this.outputDir = outputDir;
265             this.defaults.setOutputPath(outputDir);
266             fireModelChange(VALUE_CHANGED, Selector.fromString("outputDir"));
267         }
268     }
269
270     public String JavaDoc getSuperClassPackage() {
271         return superClassPackage;
272     }
273
274     public void setSuperClassPackage(String JavaDoc superClassPackage) {
275         if (!Util.nullSafeEquals(this.superClassPackage, superClassPackage)) {
276             this.superClassPackage = superClassPackage;
277
278             defaults.setSuperclassPackage(superClassPackage);
279             fireModelChange(VALUE_CHANGED, Selector.fromString("superClassPackage"));
280         }
281     }
282 }
Popular Tags