1 56 package org.objectstyle.cayenne.modeler.dialog.classgen; 57 58 import java.io.File ; 59 import java.util.ArrayList ; 60 import java.util.HashMap ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 import java.util.Map ; 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 76 public class ClassGeneratorModel extends BasicModel { 77 78 protected DataMap map; 79 protected DataMapDefaults defaults; 80 81 protected String outputDir; 82 protected boolean pairs; 83 protected List entities; 84 protected String superClassPackage; 85 protected String customSuperclassTemplate; 86 protected String customClassTemplate; 87 88 public ClassGeneratorModel(DataMap map, DataMapDefaults defaults, 89 ObjEntity selectedEntity, List 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 validationInfo) { 108 Map failedEntities = new HashMap (); 109 110 if (validationInfo != null) { 111 Iterator 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 tmp = new ArrayList (); 123 Iterator it = map.getObjEntities().iterator(); 124 while (it.hasNext()) { 125 ObjEntity entity = (ObjEntity) it.next(); 126 127 ClassGeneratorEntityWrapper wrapper = null; 129 String errorMessage = (String ) 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 getSelectedEntities() { 147 Iterator it = entities.iterator(); 148 List selected = new ArrayList (); 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 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 179 public DataMap getMap() { 180 return map; 181 } 182 183 188 public File getOutputDirectory() { 189 return (outputDir != null) ? new File (outputDir) : null; 190 } 191 192 197 public boolean isPairs() { 198 return pairs; 199 } 200 201 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 getCustomClassTemplate() { 215 return customClassTemplate; 216 } 217 218 public void setCustomClassTemplate(String 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 getCustomSuperclassTemplate() { 227 return customSuperclassTemplate; 228 } 229 230 public void setCustomSuperclassTemplate(String 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 244 public List getEntities() { 245 return entities; 246 } 247 248 253 public String getOutputDir() { 254 return outputDir; 255 } 256 257 262 public void setOutputDir(String 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 getSuperClassPackage() { 271 return superClassPackage; 272 } 273 274 public void setSuperClassPackage(String 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 |