KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > wizard > entity > EntityWizardDescriptor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.wizard.entity;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.swing.event.ChangeEvent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
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 JavaDoc {
41     
42     private EntityWizardPanel p;
43     private List JavaDoc changeListeners = new ArrayList JavaDoc();
44     private WizardDescriptor wizardDescriptor;
45     private Project project;
46     
47     public void addChangeListener(javax.swing.event.ChangeListener JavaDoc l) {
48         changeListeners.add(l);
49     }
50     
51     public java.awt.Component JavaDoc getComponent() {
52         if (p == null) {
53             p = new EntityWizardPanel(this);
54             p.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
55                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
56                     if (evt.getPropertyName().equals(EntityWizardPanel.IS_VALID)) {
57                         Object JavaDoc newvalue = evt.getNewValue();
58                         if ((newvalue != null) && (newvalue instanceof Boolean JavaDoc)) {
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         // XXX add the following checks
74
// p.getName = valid NmToken
75
// p.getName not already in module
76
if (wizardDescriptor == null) {
77             return true;
78         }
79         if (SourceLevelChecker.isSourceLevel14orLower(project)) {
80             wizardDescriptor.putProperty("WizardPanel_errorMessage",
81                     NbBundle.getMessage(EntityWizardDescriptor.class, "ERR_NeedProperSourceLevel")); // NOI18N
82
return false;
83         }
84         if (p.getPrimaryKeyClassName().trim().equals("")) { //NOI18N
85
wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(EntityWizardDescriptor.class,"ERR_PrimaryKeyNotEmpty")); //NOI18N
86
return false;
87         }
88         try{
89             if (!isPersistenceUnitDefined()) {
90                 wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(EntityWizardDescriptor.class, "ERR_NoPersistenceUnit"));
91                 return true; // just warning
92
}
93         } catch (InvalidPersistenceXmlException ipx){
94             wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(EntityWizardDescriptor.class, "ERR_InvalidPersistenceXml", ipx.getPath()));
95             return true; // just a warning
96
}
97         
98         wizardDescriptor.putProperty("WizardPanel_errorMessage", " "); //NOI18N
99
return true;
100     }
101     
102     public void readSettings(Object JavaDoc 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 JavaDoc l) {
125         changeListeners.remove(l);
126     }
127     
128     public void storeSettings(Object JavaDoc settings) {
129         
130     }
131     
132     public String JavaDoc 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 JavaDoc it;
145         synchronized (changeListeners) {
146             it = new HashSet JavaDoc(changeListeners).iterator();
147         }
148         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
149         while (it.hasNext()) {
150             ((ChangeListener JavaDoc)it.next()).stateChanged(ev);
151         }
152     }
153     
154     public void stateChanged(ChangeEvent JavaDoc e) {
155         fireChangeEvent();
156     }
157     
158 }
159
160
Popular Tags