1 56 package org.objectstyle.cayenne.modeler.dialog.classgen; 57 58 import java.awt.Component ; 59 import java.io.File ; 60 import java.util.List ; 61 62 import javax.swing.JFileChooser ; 63 import javax.swing.JOptionPane ; 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 85 public class ClassGeneratorController extends BasicController { 86 87 public static final String CANCEL_CONTROL = "cayenne.modeler.classgenerator.cancel.button"; 88 public static final String GENERATE_CLASSES_CONTROL = "cayenne.modeler.classgenerator.generate.button"; 89 public static final String SELECT_ALL_CONTROL = "cayenne.modeler.classgenerator.selectall.button"; 90 public static final String CHOOSE_LOCATION_CONTROL = "cayenne.modeler.classgenerator.choose.button"; 91 public static final String CHOOSE_TEMPLATE_CONTROL = "cayenne.modeler.classgenerator.choosetemplate.button"; 92 public static final String CHOOSE_SUPERTEMPLATE_CONTROL = "cayenne.modeler.classgenerator.choosesupertemplate.button"; 93 94 public ClassGeneratorController(ProjectController parent) { 95 setModel(prepareModel(parent)); 96 } 97 98 protected Object prepareModel(ProjectController parent) { 99 Project project = parent.getProject(); 100 DataMap map = parent.getCurrentDataMap(); 101 DataMapDefaults preferences = parent.getDataMapPreferences(); 102 103 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 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 STable table = ((ClassGeneratorDialog) getView()).getTable(); 150 table.refresh(); 151 } 152 } 153 154 protected void generateClasses() { 155 ClassGeneratorModel model = (ClassGeneratorModel) getModel(); 156 File outputDir = model.getOutputDirectory(); 157 158 if (outputDir == null) { 160 JOptionPane.showMessageDialog((Component ) this.getView(), 161 "Select directory for source files."); 162 return; 163 } 164 165 if (!outputDir.exists() && !outputDir.mkdirs()) { 167 JOptionPane.showMessageDialog((Component ) this.getView(), 168 "Can't create directory " + outputDir + ". Select a different one."); 169 return; 170 } 171 172 if (!outputDir.isDirectory()) { 174 JOptionPane.showMessageDialog((Component ) this.getView(), outputDir 175 + " is not a valid directory."); 176 return; 177 } 178 179 File classTemplate = null; 180 if (model.getCustomClassTemplate() != null) { 181 classTemplate = new File (model.getCustomClassTemplate()); 182 183 if (!classTemplate.canRead()) { 184 JOptionPane.showMessageDialog((Component ) this.getView(), model 185 .getCustomClassTemplate() 186 + " is not a valid template file."); 187 return; 188 } 189 } 190 191 File superClassTemplate = null; 192 if (model.getCustomSuperclassTemplate() != null) { 193 superClassTemplate = new File (model.getCustomSuperclassTemplate()); 194 195 if (!superClassTemplate.canRead()) { 196 JOptionPane.showMessageDialog((Component ) this.getView(), model 197 .getCustomClassTemplate() 198 + " is not a valid template file."); 199 return; 200 } 201 } 202 203 List selected = model.getSelectedEntities(); 204 DefaultClassGenerator generator = new DefaultClassGenerator(model.getMap(), selected); 205 206 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 ) this.getView(), 228 "Class generation finished"); 229 shutdown(); 230 } 231 catch (Exception e) { 232 e.printStackTrace(); 233 JOptionPane.showMessageDialog((Component ) this.getView(), 234 "Error generating classes - " + e.getMessage()); 235 } 236 } 237 238 protected void chooseLocation() { 239 ClassGeneratorModel model = (ClassGeneratorModel) getModel(); 240 File startDir = model.getOutputDirectory(); 241 242 JFileChooser chooser = new JFileChooser (); 243 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 244 chooser.setDialogType(JFileChooser.OPEN_DIALOG); 245 246 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 ) this.getView()); 259 if (result == JFileChooser.APPROVE_OPTION) { 260 File selected = chooser.getSelectedFile(); 261 262 model.setOutputDir(selected.getAbsolutePath()); 264 } 265 } 266 267 protected void chooseSuperclassTemplate() { 268 ClassGeneratorModel model = (ClassGeneratorModel) getModel(); 269 String 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 template = chooseTemplate(model.getCustomClassTemplate(), 280 "Select Custom Class Template"); 281 282 if (template != null) { 283 model.setCustomClassTemplate(template); 284 } 285 } 286 287 290 private String chooseTemplate(String oldTemplate, String title) { 291 292 JFileChooser chooser = new JFileChooser (); 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 startDir = (oldTemplate != null) 301 ? new File (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 selected = null; 315 int result = chooser.showOpenDialog((Component ) 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 |