KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collections JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
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 /**
39  *
40  * @author Martin Adamek
41  */

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 JavaDoc 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 JavaDoc();
73         }
74         index--;
75     }
76     
77     public void nextPanel() {
78         if (! hasNext()) {
79             throw new NoSuchElementException JavaDoc();
80         }
81     }
82     
83     public void removeChangeListener(ChangeListener JavaDoc l) {
84     }
85     
86     public void addChangeListener(ChangeListener JavaDoc 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 JavaDoc instantiate() throws java.io.IOException JavaDoc {
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             // just log for debugging purposes, at this point the user has
133
// already been warned about an invalid persistence.xml
134
Logger.getLogger(PersistenceUnitWizard.class.getName()).log(Level.FINE, "Invalid persistence.xml: " + ipx.getPath(), ipx); //NO18N
135
return Collections.emptySet();
136         }
137     }
138     
139 }
140
Popular Tags