1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.unit; 21 22 import java.util.Collections ; 23 import java.util.NoSuchElementException ; 24 import java.util.Set ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 import javax.swing.event.ChangeListener ; 28 import org.netbeans.api.project.Project; 29 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 30 import org.netbeans.modules.j2ee.persistence.provider.InvalidPersistenceXmlException; 31 import org.netbeans.modules.j2ee.persistence.unit.PUDataObject; 32 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 33 import org.netbeans.modules.j2ee.persistence.wizard.Util; 34 import org.netbeans.spi.project.ui.templates.support.Templates; 35 import org.openide.WizardDescriptor; 36 import org.openide.util.NbBundle; 37 38 42 43 public class PersistenceUnitWizard implements WizardDescriptor.InstantiatingIterator { 44 45 private WizardDescriptor.Panel[] panels; 46 private int index = 0; 47 private Project project; 48 private PersistenceUnitWizardDescriptor descriptor; 49 50 public static PersistenceUnitWizard create() { 51 return new PersistenceUnitWizard(); 52 } 53 54 public String name() { 55 return NbBundle.getMessage(PersistenceUnitWizard.class, "LBL_WizardTitle"); 56 } 57 58 public boolean hasPrevious() { 59 return index > 0; 60 } 61 62 public boolean hasNext() { 63 return index < panels.length - 1; 64 } 65 66 public WizardDescriptor.Panel current() { 67 return panels[index]; 68 } 69 70 public void previousPanel() { 71 if (! hasPrevious()) { 72 throw new NoSuchElementException (); 73 } 74 index--; 75 } 76 77 public void nextPanel() { 78 if (! hasNext()) { 79 throw new NoSuchElementException (); 80 } 81 } 82 83 public void removeChangeListener(ChangeListener l) { 84 } 85 86 public void addChangeListener(ChangeListener l) { 87 } 88 89 public void uninitialize(WizardDescriptor wizard) { 90 } 91 92 public void initialize(WizardDescriptor wizard) { 93 project = Templates.getProject(wizard); 94 descriptor = new PersistenceUnitWizardDescriptor(project); 95 panels = new WizardDescriptor.Panel[] {descriptor}; 96 wizard.putProperty("NewFileWizard_Title", 97 NbBundle.getMessage(PersistenceUnitWizard.class, "Templates/Persistence/PersistenceUnit")); 98 Util.mergeSteps(wizard, panels, null); 99 } 100 101 public Set instantiate() throws java.io.IOException { 102 PersistenceUnit punit = null; 103 if (descriptor.isContainerManaged()) { 104 punit = new PersistenceUnit(); 105 if (descriptor.getDatasource() != null && !"".equals(descriptor.getDatasource())){ 106 if (descriptor.isJTA()) { 107 punit.setJtaDataSource(descriptor.getDatasource()); 108 } else { 109 punit.setNonJtaDataSource(descriptor.getDatasource()); 110 punit.setTransactionType("RESOURCE_LOCAL"); 111 } 112 } 113 if (descriptor.isNonDefaultProviderEnabled()) { 114 punit.setProvider(descriptor.getNonDefaultProvider()); 115 } 116 } else { 117 punit = ProviderUtil.buildPersistenceUnit(descriptor.getPersistenceUnitName(), 118 descriptor.getSelectedProvider(), descriptor.getPersistenceConnection()); 119 punit.setTransactionType("RESOURCE_LOCAL"); 120 if (descriptor.getPersistenceLibrary() != null){ 121 Util.addLibraryToProject(project, descriptor.getPersistenceLibrary()); 122 } 123 } 124 punit.setName(descriptor.getPersistenceUnitName()); 125 ProviderUtil.setTableGeneration(punit, descriptor.getTableGeneration(), project); 126 try{ 127 PUDataObject pud = ProviderUtil.getPUDataObject(project); 128 pud.addPersistenceUnit(punit); 129 pud.save(); 130 return Collections.singleton(pud.getPrimaryFile()); 131 } catch (InvalidPersistenceXmlException ipx){ 132 Logger.getLogger(PersistenceUnitWizard.class.getName()).log(Level.FINE, "Invalid persistence.xml: " + ipx.getPath(), ipx); return Collections.emptySet(); 136 } 137 } 138 139 } 140 | Popular Tags |