KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > jdbcimpl > wizard > DBSchemaWizardIterator


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.dbschema.jdbcimpl.wizard;
21
22 import java.io.IOException JavaDoc;
23 import java.util.*;
24
25 import javax.swing.event.ChangeListener JavaDoc;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.spi.project.ui.templates.support.Templates;
28
29 import org.openide.loaders.TemplateWizard;
30 import org.openide.NotifyDescriptor;
31 import org.openide.util.NbBundle;
32 import org.openide.WizardDescriptor;
33 import org.openide.filesystems.FileObject;
34 import org.openide.loaders.DataFolder;
35
36 /** Iterator implementation which can iterate through two
37  * panels which forms dbschema template wizard
38  */

39 public class DBSchemaWizardIterator implements TemplateWizard.Iterator {
40     
41     static final long serialVersionUID = 9197272899287477324L;
42     
43     ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.dbschema.jdbcimpl.resources.Bundle"); //NOI18N
44

45     private WizardDescriptor.Panel panels[];
46     private static String JavaDoc panelNames[];
47     private static final int PANEL_COUNT = 3;
48     private int panelIndex;
49     private static DBSchemaWizardIterator instance;
50     private TemplateWizard wizardInstance;
51     private boolean guiInitialized;
52     private DBSchemaWizardData myData;
53     
54     public DBSchemaWizardIterator() {
55         super();
56         panelIndex = 0;
57     }
58     
59     public static synchronized DBSchemaWizardIterator singleton() {
60         if(instance == null)
61             instance = new DBSchemaWizardIterator();
62         
63         return instance;
64     }
65     
66     public Set instantiate(TemplateWizard wiz) throws IOException JavaDoc {
67 // System.out.println(wiz.getTargetFolder());
68
myData.setName(wiz.getTargetName());
69         myData.setDestinationPackage(wiz.getTargetFolder());
70         
71         CaptureSchema capture = new CaptureSchema(myData);
72         capture.start();
73         
74         return null;///Collections.singleton(null);
75
}
76     
77     public org.openide.WizardDescriptor.Panel current() {
78         return panels[panelIndex];
79     }
80     
81     public String JavaDoc name() {
82         return panelNames[panelIndex];
83     }
84     
85     public boolean hasNext() {
86         return panelIndex < PANEL_COUNT - 1;
87     }
88     
89     public boolean hasPrevious() {
90         return panelIndex > 0;
91     }
92     
93     public void nextPanel() {
94         if (panelIndex == 1) {//== connection panel
95
((DBSchemaConnectionPanel) panels[1].getComponent()).initData();
96             if (! (((DBSchemaTablesPanel) panels[2].getComponent()).init()))
97                 return;
98         }
99         
100         panelIndex++;
101     }
102     
103     public void previousPanel() {
104         panelIndex--;
105     }
106     
107     public void addChangeListener(ChangeListener JavaDoc l) {
108     }
109     
110     public void removeChangeListener(ChangeListener JavaDoc l) {
111     }
112     
113     public void initialize(TemplateWizard wizard) {
114         wizardInstance = wizard;
115         setDefaultTarget();
116         String JavaDoc[] prop = (String JavaDoc[]) wizard.getProperty("WizardPanel_contentData"); // NOI18N
117
String JavaDoc[] stepsNames;
118         if (wizard.targetChooser().getClass().toString().trim().equalsIgnoreCase("class org.openide.loaders.TemplateWizard2")) {
119             stepsNames = new String JavaDoc[] {
120                 bundle.getString("TargetLocation") ,
121                 bundle.getString("TargetLocation"),
122                 bundle.getString("ConnectionChooser"),
123                 bundle.getString("TablesChooser")
124             };
125         } else if (null != prop) {
126             stepsNames = new String JavaDoc[] {
127                 prop[0],
128                 bundle.getString("TargetLocation"),
129                 bundle.getString("ConnectionChooser"),
130                 bundle.getString("TablesChooser")
131             };
132         } else {
133             stepsNames = new String JavaDoc[] {
134                 bundle.getString("TargetLocation"),
135                 bundle.getString("ConnectionChooser"),
136                 bundle.getString("TablesChooser")
137             };
138         }
139         wizardInstance.putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); //NOI18N
140
wizardInstance.putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); //NOI18N
141
wizardInstance.putProperty("WizardPanel_contentNumbered", Boolean.TRUE); //NOI18N
142
wizardInstance.putProperty("WizardPanel_contentData", stepsNames); //NOI18N
143

144         if(!guiInitialized) {
145             initialize();
146             
147             myData = new DBSchemaWizardData();
148             panels = new WizardDescriptor.Panel[PANEL_COUNT];
149             
150             DBSchemaTargetPanel targetPanel = new DBSchemaTargetPanel();
151             targetPanel.setPanel(wizard.targetChooser());
152             
153             java.awt.Component JavaDoc panel = targetPanel.getComponent();
154             if (panel instanceof javax.swing.JComponent JavaDoc) {
155                 ((javax.swing.JComponent JavaDoc) panel).putClientProperty("WizardPanel_contentData", stepsNames); //NOI18N
156
((javax.swing.JComponent JavaDoc) panel).putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(0)); //NOI18N
157
}
158             
159             panels[0] = targetPanel.getPanel();
160             panels[1] = new DBSchemaConnectionWizardPanel(myData);
161             panels[2] = new DBSchemaTablesWizardPanel(myData);
162         }
163         
164         panelIndex = 0;
165     }
166     
167     public void uninitialize(TemplateWizard wiz) {
168         if (wiz.getValue() == NotifyDescriptor.CANCEL_OPTION)
169             ((DBSchemaTablesPanel) panels[2].getComponent()).uninit();
170         
171         panels = null;
172         myData = null;
173         guiInitialized = false;
174     }
175     
176     protected void initialize() {
177         if(panelNames == null) {
178             panelNames = new String JavaDoc[PANEL_COUNT];
179             panelNames[0] = ""; //NOI18N
180
panelNames[1] = ""; //NOI18N
181
panelNames[2] = ""; //NOI18N
182
}
183     }
184     
185     /**
186      * Hack which sets the default target to the src/conf or src directory,
187      * whichever exists.
188      */

189     private void setDefaultTarget() {
190         FileObject targetFO;
191         try {
192             DataFolder target = wizardInstance.getTargetFolder();
193             targetFO = target.getPrimaryFile();
194         } catch (IOException JavaDoc e) {
195             targetFO = null;
196         }
197         
198         Project targetProject = Templates.getProject(wizardInstance);
199         if (targetProject != null) {
200             FileObject projectDir = targetProject.getProjectDirectory();
201             if (targetFO == null || targetFO.equals(projectDir)) {
202                 FileObject newTargetFO = projectDir.getFileObject("src/conf"); // NOI18N
203
if (newTargetFO == null || !newTargetFO.isValid()) {
204                     newTargetFO = projectDir.getFileObject("src/META-INF"); // NOI18N
205
if (newTargetFO == null || !newTargetFO.isValid()) {
206                         newTargetFO = projectDir.getFileObject("src"); // NOI18N
207
if (newTargetFO == null || !newTargetFO.isValid()) {
208                             return;
209                         }
210                     }
211                 }
212
213                 DataFolder newTarget = DataFolder.findFolder(newTargetFO);
214                 wizardInstance.setTargetFolder(newTarget);
215             }
216         }
217     }
218 }
219
Popular Tags