1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.awt.Component ; 23 import java.io.IOException ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.Set ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import javax.swing.JComponent ; 30 import javax.swing.SwingUtilities ; 31 import org.netbeans.api.progress.ProgressHandle; 32 import org.netbeans.api.progress.ProgressHandleFactory; 33 import org.netbeans.api.project.Project; 34 import org.netbeans.api.project.ProjectUtils; 35 import org.netbeans.modules.j2ee.persistence.api.PersistenceLocation; 36 import org.netbeans.modules.j2ee.persistence.provider.InvalidPersistenceXmlException; 37 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 38 import org.netbeans.spi.project.ui.templates.support.Templates; 39 import org.openide.DialogDisplayer; 40 import org.openide.ErrorManager; 41 import org.openide.NotifyDescriptor; 42 import org.openide.WizardDescriptor; 43 import org.openide.filesystems.FileObject; 44 import org.openide.loaders.DataFolder; 45 import org.openide.loaders.DataObject; 46 import org.openide.loaders.TemplateWizard; 47 import org.openide.util.Lookup; 48 import org.openide.util.NbBundle; 49 import org.openide.util.RequestProcessor; 50 import org.openide.windows.WindowManager; 51 52 56 public class RelatedCMPWizard extends WizardDescriptor.ArrayIterator<WizardDescriptor> implements TemplateWizard.Iterator { 57 58 private static final String WIZARD_PANEL_CONTENT_DATA = "WizardPanel_contentData"; private static final String WIZARD_PANEL_CONTENT_SELECTED_INDEX = "WizardPanel_contentSelectedIndex"; 61 private static final String PROP_HELPER = "wizard-helper"; private static final String PROP_CMP = "wizard-is-cmp"; 64 private static final String TYPE_CMP = "cmp"; private static final String TYPE_JPA = "jpa"; 67 private static final Lookup.Result<PersistenceGeneratorProvider> PERSISTENCE_PROVIDERS = 68 Lookup.getDefault().lookupResult(PersistenceGeneratorProvider.class); 69 70 private final String type; 71 72 private WizardDescriptor.Panel<WizardDescriptor>[] panels; 73 private String [] steps; 74 private int stepsStartPos; 75 76 private WizardDescriptor wizardDescriptor; 77 78 private PersistenceGenerator generator; 79 private RelatedCMPHelper helper; 80 private ProgressPanel progressPanel; 81 82 public static RelatedCMPWizard createForJPA() { 83 return new RelatedCMPWizard(TYPE_JPA); 84 } 85 86 public static RelatedCMPWizard createForCMP() { 87 return new RelatedCMPWizard(TYPE_CMP); 88 } 89 90 public RelatedCMPWizard(String type) { 91 this.type = type; 92 } 93 94 private static PersistenceGenerator createPersistenceGenerator(String type) { 95 assert type != null; 96 97 Collection <? extends PersistenceGeneratorProvider> providers = PERSISTENCE_PROVIDERS.allInstances(); 98 for (PersistenceGeneratorProvider provider : providers) { 99 if (type.equals(provider.getGeneratorType())) { 100 return provider.createGenerator(); 101 } 102 } 103 throw new AssertionError ("Could not find a persistence generator of type " + type); } 105 106 private boolean isCMP() { 107 return TYPE_CMP.equals(type); 108 } 109 110 @SuppressWarnings ("unchecked") 111 protected WizardDescriptor.Panel<WizardDescriptor>[] initializePanels() { 112 panels = (WizardDescriptor.Panel<WizardDescriptor>[])new WizardDescriptor.Panel<?>[] { 113 new DatabaseTablesPanel.WizardPanel(), 114 new EntityClassesPanel.WizardPanel() 115 }; 116 return panels; 117 } 118 119 124 public WizardDescriptor.Panel<WizardDescriptor> current() { 125 WizardDescriptor.Panel<WizardDescriptor> panel = super.current(); 126 127 if (steps == null) { 128 mergeSteps(new String [] { 129 NbBundle.getMessage(RelatedCMPWizard.class, "LBL_DatabaseTables"), 130 NbBundle.getMessage(RelatedCMPWizard.class, isCMP() ? "LBL_EntityBeansLocation" : "LBL_EntityClasses"), 131 }); 132 } 133 134 JComponent component = (JComponent )panel.getComponent(); 135 if (component.getClientProperty(WIZARD_PANEL_CONTENT_DATA) == null) { 136 component.putClientProperty(WIZARD_PANEL_CONTENT_DATA, steps); 137 } 138 if (component.getClientProperty(WIZARD_PANEL_CONTENT_SELECTED_INDEX) == null) { 139 if (panel == panels[0]) { 140 component.putClientProperty(WIZARD_PANEL_CONTENT_SELECTED_INDEX, new Integer (0)); 141 component.setName(steps[steps.length - 2]); 144 } else { 145 component.putClientProperty(WIZARD_PANEL_CONTENT_SELECTED_INDEX, new Integer (1)); 146 component.setName(steps[steps.length - 1]); 149 } 150 } 151 152 return panel; 153 } 154 155 static RelatedCMPHelper getHelper(WizardDescriptor wizardDescriptor) { 156 return (RelatedCMPHelper)wizardDescriptor.getProperty(PROP_HELPER); 157 } 158 159 static boolean isCMP(WizardDescriptor wizardDescriptor) { 160 return ((Boolean )wizardDescriptor.getProperty(PROP_CMP)).booleanValue(); 161 } 162 163 public final void initialize(TemplateWizard wiz) { 164 wizardDescriptor = wiz; 165 166 Project project = Templates.getProject(wiz); 167 generator = createPersistenceGenerator(type); 168 169 FileObject configFilesFolder = PersistenceLocation.getLocation(project); 170 171 helper = new RelatedCMPHelper(project, configFilesFolder, generator); 172 173 wiz.putProperty(PROP_HELPER, helper); 174 wiz.putProperty(PROP_CMP, new Boolean (isCMP())); 175 176 String wizardBundleKey = isCMP() ? "Templates/J2EE/RelatedCMP" : "Templates/Persistence/RelatedCMP"; wiz.putProperty("NewFileWizard_Title", NbBundle.getMessage(RelatedCMPWizard.class, wizardBundleKey)); 179 generator.init(wiz); 180 } 181 182 public void mergeSteps(String [] thisSteps) { 183 Object prop = wizardDescriptor.getProperty(WIZARD_PANEL_CONTENT_DATA); 184 String [] beforeSteps; 185 186 if (prop instanceof String []) { 187 beforeSteps = (String []) prop; 188 stepsStartPos = beforeSteps.length; 189 if (stepsStartPos > 0 && ("...".equals(beforeSteps[stepsStartPos - 1]))) { stepsStartPos--; 191 } 192 } else { 193 beforeSteps = null; 194 stepsStartPos = 0; 195 } 196 197 steps = new String [stepsStartPos + thisSteps.length]; 198 System.arraycopy(beforeSteps, 0, steps, 0, stepsStartPos); 199 System.arraycopy(thisSteps, 0, steps, stepsStartPos, thisSteps.length); 200 } 201 202 public final void uninitialize(TemplateWizard wiz) { 203 generator.uninit(); 204 } 205 206 public Set <DataObject> instantiate(final TemplateWizard wiz) throws IOException { 207 Component c = WindowManager.getDefault().getMainWindow(); 208 209 if (helper.getPersistenceUnit() != null){ 211 try { 212 ProviderUtil.addPersistenceUnit(helper.getPersistenceUnit(), Templates.getProject(wiz)); 213 } catch (InvalidPersistenceXmlException ipx){ 214 Logger.getLogger(RelatedCMPWizard.class.getName()).log(Level.FINE, "Invalid persistence.xml: " + ipx.getPath(), ipx); } 218 } 219 220 final String title = NbBundle.getMessage(RelatedCMPWizard.class, isCMP() ? "TXT_EjbGeneration" : "TXT_EntityClassesGeneration"); 221 final ProgressHandle handle = ProgressHandleFactory.createHandle(title); 222 223 progressPanel = new ProgressPanel(); 224 final JComponent progressComponent = ProgressHandleFactory.createProgressComponent(handle); 225 226 final Runnable r = new Runnable () { 227 public void run() { 228 try { 229 createBeans(wiz, handle); 230 } catch (IOException ioe) { 231 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 232 NotifyDescriptor nd = 233 new NotifyDescriptor.Message(ioe.getLocalizedMessage(), 234 NotifyDescriptor.ERROR_MESSAGE); 235 DialogDisplayer.getDefault().notify(nd); 236 } finally { 237 generator.uninit(); 238 } 239 } 240 }; 241 242 255 SwingUtilities.invokeLater(new Runnable () { 256 private boolean first = true; 257 public void run() { 258 if (!first) { 259 RequestProcessor.getDefault().post(r); 260 progressPanel.open(progressComponent, title); 261 } else { 262 first = false; 263 SwingUtilities.invokeLater(this); 264 } 265 } 266 }); 267 268 274 280 return Collections.<DataObject>singleton(DataFolder.findFolder( 281 SourceGroupSupport.getFolderForPackage(helper.getLocation(), helper.getPackageName()) 282 )); 283 } 284 285 private void createBeans(TemplateWizard wiz, ProgressHandle handle) throws IOException { 286 try { 287 handle.start(); 288 Project project = Templates.getProject(wiz); 289 handle.progress(NbBundle.getMessage(RelatedCMPWizard.class, "TXT_SavingSchema")); 290 progressPanel.setText(NbBundle.getMessage(RelatedCMPWizard.class, "TXT_SavingSchema")); 291 292 FileObject dbschemaFile = helper.getDBSchemaFile(); 293 if (dbschemaFile == null) { 294 FileObject configFilesFolder = getHelper(wiz).getConfigFilesFolder(); 295 if (configFilesFolder == null && !isCMP()) { 296 configFilesFolder = PersistenceLocation.createLocation(project); 299 } 300 assert configFilesFolder != null : "Should have set configFilesFolder, e.g. by retrieving it from a PersistenceLocationProvider or EjbJar or by asking the user"; 302 String projectName = ProjectUtils.getInformation(project).getDisplayName(); 303 dbschemaFile = DBSchemaManager.updateDBSchemas(helper.getSchemaElement(), helper.getDBSchemaFileList(), configFilesFolder, projectName); 304 } 305 306 String extracting = NbBundle.getMessage(RelatedCMPWizard.class, isCMP() ? 307 "TXT_ExtractingBeansAndRelationships" : "TXT_ExtractingEntityClassesAndRelationships"); 308 309 handle.progress(extracting); 310 progressPanel.setText(extracting); 311 312 helper.buildBeans(); 313 314 FileObject pkg = SourceGroupSupport.getFolderForPackage(helper.getLocation(), helper.getPackageName()); 315 generator.generateBeans(progressPanel, helper, dbschemaFile, handle, false); 316 317 } finally { 325 handle.finish(); 326 SwingUtilities.invokeLater(new Runnable () { 327 public void run() { 328 progressPanel.close(); 329 } 330 }); 331 } 332 } 333 334 } 335 | Popular Tags |