KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > wizard > unit > PersistenceUnitWizardDescriptor


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.unit;
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.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 /**
44  *
45  * @author Martin Adamek
46  */

47 public class PersistenceUnitWizardDescriptor implements WizardDescriptor.FinishablePanel, ChangeListener JavaDoc {
48     
49     private PersistenceUnitWizardPanelDS p;
50     private PersistenceUnitWizardPanelJdbc jdbcPanel;
51     private PersistenceUnitWizardPanel panel;
52     private List JavaDoc changeListeners = new ArrayList JavaDoc();
53     private WizardDescriptor wizardDescriptor;
54     private Project project;
55     private boolean isContainerManaged;
56     private static String JavaDoc 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 JavaDoc l) {
64         changeListeners.add(l);
65     }
66     
67     public java.awt.Component JavaDoc 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 JavaDoc() {
77                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
78                     if (evt.getPropertyName().equals(PersistenceUnitWizardPanel.IS_VALID)) {
79                         Object JavaDoc newvalue = evt.getNewValue();
80                         if ((newvalue != null) && (newvalue instanceof Boolean JavaDoc)) {
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")); //NOI18N
101
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")); //NOI18N
108
}
109             } catch (InvalidPersistenceXmlException ipx){
110                     wizardDescriptor.putProperty(ERROR_MSG_KEY,
111                             NbBundle.getMessage(PersistenceUnitWizardDescriptor.class,"ERR_InvalidPersistenceXml", ipx.getPath())); //NOI18N
112

113             }
114             return false;
115         }
116         wizardDescriptor.putProperty(ERROR_MSG_KEY, " "); //NOI18N
117
return true;
118     }
119     
120     
121     public void readSettings(Object JavaDoc settings) {
122         wizardDescriptor = (WizardDescriptor) settings;
123         project = Templates.getProject(wizardDescriptor);
124     }
125     
126     public void removeChangeListener(javax.swing.event.ChangeListener JavaDoc l) {
127         changeListeners.remove(l);
128     }
129     
130     public void storeSettings(Object JavaDoc settings) {
131     }
132     
133     public boolean isFinishPanel() {
134         return isValid();
135     }
136     
137     protected final void fireChangeEvent() {
138         Iterator JavaDoc it;
139         synchronized (changeListeners) {
140             it = new HashSet JavaDoc(changeListeners).iterator();
141         }
142         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
143         while (it.hasNext()) {
144             ((ChangeListener JavaDoc)it.next()).stateChanged(ev);
145         }
146     }
147     
148     public void stateChanged(ChangeEvent JavaDoc e) {
149         fireChangeEvent();
150     }
151     
152     String JavaDoc 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 JavaDoc 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 JavaDoc getNonDefaultProvider() {
181         return p == null ? null : p.getNonDefaultProvider();
182     }
183     
184     String JavaDoc getTableGeneration() {
185         return panel.getTableGeneration();
186     }
187     
188     Provider getSelectedProvider(){
189         return panel.getSelectedProvider();
190     }
191 }
192
Popular Tags