1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.unit; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.ArrayList ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import javax.swing.event.ChangeEvent ; 29 import javax.swing.event.ChangeListener ; 30 import org.netbeans.api.db.explorer.DatabaseConnection; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.api.project.libraries.Library; 33 import org.netbeans.modules.j2ee.persistence.provider.InvalidPersistenceXmlException; 34 import org.netbeans.modules.j2ee.persistence.provider.Provider; 35 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 36 import org.netbeans.modules.j2ee.persistence.wizard.Util; 37 import org.netbeans.modules.j2ee.persistence.wizard.entity.EntityWizardDescriptor; 38 import org.netbeans.spi.project.ui.templates.support.Templates; 39 import org.openide.WizardDescriptor; 40 import org.openide.util.HelpCtx; 41 import org.openide.util.NbBundle; 42 43 47 public class PersistenceUnitWizardDescriptor implements WizardDescriptor.FinishablePanel, ChangeListener { 48 49 private PersistenceUnitWizardPanelDS p; 50 private PersistenceUnitWizardPanelJdbc jdbcPanel; 51 private PersistenceUnitWizardPanel panel; 52 private List changeListeners = new ArrayList (); 53 private WizardDescriptor wizardDescriptor; 54 private Project project; 55 private boolean isContainerManaged; 56 private static String ERROR_MSG_KEY = "WizardPanel_errorMessage"; 57 58 public PersistenceUnitWizardDescriptor(Project project) { 59 this.project = project; 60 this.isContainerManaged = Util.isSupportedJavaEEVersion(project); 61 } 62 63 public void addChangeListener(javax.swing.event.ChangeListener l) { 64 changeListeners.add(l); 65 } 66 67 public java.awt.Component getComponent() { 68 if (panel == null) { 69 if (isContainerManaged) { 70 p = new PersistenceUnitWizardPanelDS(project, this, true); 71 panel = p; 72 } else { 73 jdbcPanel= new PersistenceUnitWizardPanelJdbc(project, this, true); 74 panel = jdbcPanel; 75 } 76 panel.addPropertyChangeListener(new PropertyChangeListener () { 77 public void propertyChange(PropertyChangeEvent evt) { 78 if (evt.getPropertyName().equals(PersistenceUnitWizardPanel.IS_VALID)) { 79 Object newvalue = evt.getNewValue(); 80 if ((newvalue != null) && (newvalue instanceof Boolean )) { 81 stateChanged(null); 82 } 83 } 84 } 85 }); 86 } 87 return panel; 88 } 89 90 public org.openide.util.HelpCtx getHelp() { 91 return new HelpCtx(EntityWizardDescriptor.class); 92 } 93 94 public boolean isValid() { 95 if (wizardDescriptor == null) { 96 return true; 97 } 98 if (!ProviderUtil.isValidServerInstanceOrNone(project)){ 99 wizardDescriptor.putProperty(ERROR_MSG_KEY, 100 NbBundle.getMessage(PersistenceUnitWizardDescriptor.class,"ERR_MissingServer")); return false; 102 } 103 if (panel != null && !panel.isValidPanel()) { 104 try { 105 if (!panel.isNameUnique()){ 106 wizardDescriptor.putProperty(ERROR_MSG_KEY, 107 NbBundle.getMessage(PersistenceUnitWizardDescriptor.class,"ERR_PersistenceUnitNameNotUnique")); } 109 } catch (InvalidPersistenceXmlException ipx){ 110 wizardDescriptor.putProperty(ERROR_MSG_KEY, 111 NbBundle.getMessage(PersistenceUnitWizardDescriptor.class,"ERR_InvalidPersistenceXml", ipx.getPath())); 113 } 114 return false; 115 } 116 wizardDescriptor.putProperty(ERROR_MSG_KEY, " "); return true; 118 } 119 120 121 public void readSettings(Object settings) { 122 wizardDescriptor = (WizardDescriptor) settings; 123 project = Templates.getProject(wizardDescriptor); 124 } 125 126 public void removeChangeListener(javax.swing.event.ChangeListener l) { 127 changeListeners.remove(l); 128 } 129 130 public void storeSettings(Object settings) { 131 } 132 133 public boolean isFinishPanel() { 134 return isValid(); 135 } 136 137 protected final void fireChangeEvent() { 138 Iterator it; 139 synchronized (changeListeners) { 140 it = new HashSet (changeListeners).iterator(); 141 } 142 ChangeEvent ev = new ChangeEvent (this); 143 while (it.hasNext()) { 144 ((ChangeListener )it.next()).stateChanged(ev); 145 } 146 } 147 148 public void stateChanged(ChangeEvent e) { 149 fireChangeEvent(); 150 } 151 152 String getPersistenceUnitName() { 153 return panel.getPersistenceUnitName(); 154 } 155 156 Library getPersistenceLibrary() { 157 return jdbcPanel == null ? null : jdbcPanel.getPersistenceLibrary(); 158 } 159 160 DatabaseConnection getPersistenceConnection() { 161 return jdbcPanel == null ? null : jdbcPanel.getPersistenceConnection(); 162 } 163 164 String getDatasource() { 165 return p == null ? null : p.getDatasource(); 166 } 167 168 boolean isContainerManaged() { 169 return isContainerManaged; 170 } 171 172 boolean isJTA() { 173 return p == null ? false : p.isJTA(); 174 } 175 176 boolean isNonDefaultProviderEnabled() { 177 return p == null ? false : p.isNonDefaultProviderEnabled(); 178 } 179 180 String getNonDefaultProvider() { 181 return p == null ? null : p.getNonDefaultProvider(); 182 } 183 184 String getTableGeneration() { 185 return panel.getTableGeneration(); 186 } 187 188 Provider getSelectedProvider(){ 189 return panel.getSelectedProvider(); 190 } 191 } 192 | Popular Tags |