1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.entity; 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.project.Project; 31 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 32 import org.netbeans.modules.j2ee.persistence.provider.InvalidPersistenceXmlException; 33 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 34 import org.netbeans.modules.j2ee.persistence.util.SourceLevelChecker; 35 import org.netbeans.spi.project.ui.templates.support.Templates; 36 import org.openide.WizardDescriptor; 37 import org.openide.util.HelpCtx; 38 import org.openide.util.NbBundle; 39 40 public class EntityWizardDescriptor implements WizardDescriptor.FinishablePanel, ChangeListener { 41 42 private EntityWizardPanel p; 43 private List changeListeners = new ArrayList (); 44 private WizardDescriptor wizardDescriptor; 45 private Project project; 46 47 public void addChangeListener(javax.swing.event.ChangeListener l) { 48 changeListeners.add(l); 49 } 50 51 public java.awt.Component getComponent() { 52 if (p == null) { 53 p = new EntityWizardPanel(this); 54 p.addPropertyChangeListener(new PropertyChangeListener () { 55 public void propertyChange(PropertyChangeEvent evt) { 56 if (evt.getPropertyName().equals(EntityWizardPanel.IS_VALID)) { 57 Object newvalue = evt.getNewValue(); 58 if ((newvalue != null) && (newvalue instanceof Boolean )) { 59 stateChanged(null); 60 } 61 } 62 } 63 }); 64 } 65 return p; 66 } 67 68 public org.openide.util.HelpCtx getHelp() { 69 return new HelpCtx(EntityWizardDescriptor.class); 70 } 71 72 public boolean isValid() { 73 if (wizardDescriptor == null) { 77 return true; 78 } 79 if (SourceLevelChecker.isSourceLevel14orLower(project)) { 80 wizardDescriptor.putProperty("WizardPanel_errorMessage", 81 NbBundle.getMessage(EntityWizardDescriptor.class, "ERR_NeedProperSourceLevel")); return false; 83 } 84 if (p.getPrimaryKeyClassName().trim().equals("")) { wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(EntityWizardDescriptor.class,"ERR_PrimaryKeyNotEmpty")); return false; 87 } 88 try{ 89 if (!isPersistenceUnitDefined()) { 90 wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(EntityWizardDescriptor.class, "ERR_NoPersistenceUnit")); 91 return true; } 93 } catch (InvalidPersistenceXmlException ipx){ 94 wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(EntityWizardDescriptor.class, "ERR_InvalidPersistenceXml", ipx.getPath())); 95 return true; } 97 98 wizardDescriptor.putProperty("WizardPanel_errorMessage", " "); return true; 100 } 101 102 public void readSettings(Object settings) { 103 wizardDescriptor = (WizardDescriptor) settings; 104 if (project == null) { 105 project = Templates.getProject(wizardDescriptor); 106 p.setProject(project); 107 } 108 109 try{ 110 if (ProviderUtil.isValidServerInstanceOrNone(project) && !isPersistenceUnitDefined()) { 111 wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(EntityWizardDescriptor.class, "ERR_NoPersistenceUnit")); 112 p.setPersistenceUnitButtonVisibility(true); 113 } 114 } catch (InvalidPersistenceXmlException ipx){ 115 wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(EntityWizardDescriptor.class, "ERR_InvalidPersistenceXml", ipx.getPath())); 116 p.setPersistenceUnitButtonVisibility(false); 117 } 118 } 119 120 private boolean isPersistenceUnitDefined() throws InvalidPersistenceXmlException { 121 return ProviderUtil.persistenceExists(project) || getPersistenceUnit() != null; 122 } 123 124 public void removeChangeListener(javax.swing.event.ChangeListener l) { 125 changeListeners.remove(l); 126 } 127 128 public void storeSettings(Object settings) { 129 130 } 131 132 public String getPrimaryKeyClassName() { 133 return p.getPrimaryKeyClassName(); 134 } 135 136 public PersistenceUnit getPersistenceUnit(){ 137 return p.getPersistenceUnit(); 138 } 139 public boolean isFinishPanel() { 140 return isValid(); 141 } 142 143 protected final void fireChangeEvent() { 144 Iterator it; 145 synchronized (changeListeners) { 146 it = new HashSet (changeListeners).iterator(); 147 } 148 ChangeEvent ev = new ChangeEvent (this); 149 while (it.hasNext()) { 150 ((ChangeListener )it.next()).stateChanged(ev); 151 } 152 } 153 154 public void stateChanged(ChangeEvent e) { 155 fireChangeEvent(); 156 } 157 158 } 159 160 | Popular Tags |