001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.dbschema.jdbcimpl.wizard;
043:
044: import java.io.IOException;
045: import java.util.*;
046:
047: import javax.swing.event.ChangeListener;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.spi.project.ui.templates.support.Templates;
050:
051: import org.openide.loaders.TemplateWizard;
052: import org.openide.NotifyDescriptor;
053: import org.openide.util.NbBundle;
054: import org.openide.WizardDescriptor;
055: import org.openide.filesystems.FileObject;
056: import org.openide.loaders.DataFolder;
057:
058: /** Iterator implementation which can iterate through two
059: * panels which forms dbschema template wizard
060: */
061: public class DBSchemaWizardIterator implements TemplateWizard.Iterator {
062:
063: static final long serialVersionUID = 9197272899287477324L;
064:
065: ResourceBundle bundle = NbBundle
066: .getBundle("org.netbeans.modules.dbschema.jdbcimpl.resources.Bundle"); //NOI18N
067:
068: private WizardDescriptor.Panel panels[];
069: private static String panelNames[];
070: private static final int PANEL_COUNT = 3;
071: private int panelIndex;
072: private static DBSchemaWizardIterator instance;
073: private TemplateWizard wizardInstance;
074: private boolean guiInitialized;
075: private DBSchemaWizardData myData;
076:
077: public DBSchemaWizardIterator() {
078: super ();
079: panelIndex = 0;
080: }
081:
082: public static synchronized DBSchemaWizardIterator singleton() {
083: if (instance == null)
084: instance = new DBSchemaWizardIterator();
085:
086: return instance;
087: }
088:
089: public Set instantiate(TemplateWizard wiz) throws IOException {
090: // System.out.println(wiz.getTargetFolder());
091: myData.setName(wiz.getTargetName());
092: myData.setDestinationPackage(wiz.getTargetFolder());
093:
094: CaptureSchema capture = new CaptureSchema(myData);
095: capture.start();
096:
097: return null;///Collections.singleton(null);
098: }
099:
100: public org.openide.WizardDescriptor.Panel current() {
101: return panels[panelIndex];
102: }
103:
104: public String name() {
105: return panelNames[panelIndex];
106: }
107:
108: public boolean hasNext() {
109: return panelIndex < PANEL_COUNT - 1;
110: }
111:
112: public boolean hasPrevious() {
113: return panelIndex > 0;
114: }
115:
116: public void nextPanel() {
117: if (panelIndex == 1) {//== connection panel
118: ((DBSchemaConnectionPanel) panels[1].getComponent())
119: .initData();
120: if (!(((DBSchemaTablesPanel) panels[2].getComponent())
121: .init()))
122: return;
123: }
124:
125: panelIndex++;
126: }
127:
128: public void previousPanel() {
129: panelIndex--;
130: }
131:
132: public void addChangeListener(ChangeListener l) {
133: }
134:
135: public void removeChangeListener(ChangeListener l) {
136: }
137:
138: public void initialize(TemplateWizard wizard) {
139: wizardInstance = wizard;
140: setDefaultTarget();
141: String[] prop = (String[]) wizard
142: .getProperty("WizardPanel_contentData"); // NOI18N
143: String[] stepsNames;
144: if (wizard.targetChooser().getClass().toString().trim()
145: .equalsIgnoreCase(
146: "class org.openide.loaders.TemplateWizard2")) {
147: stepsNames = new String[] {
148: bundle.getString("TargetLocation"),
149: bundle.getString("TargetLocation"),
150: bundle.getString("ConnectionChooser"),
151: bundle.getString("TablesChooser") };
152: } else if (null != prop) {
153: stepsNames = new String[] { prop[0],
154: bundle.getString("TargetLocation"),
155: bundle.getString("ConnectionChooser"),
156: bundle.getString("TablesChooser") };
157: } else {
158: stepsNames = new String[] {
159: bundle.getString("TargetLocation"),
160: bundle.getString("ConnectionChooser"),
161: bundle.getString("TablesChooser") };
162: }
163: wizardInstance.putProperty("WizardPanel_autoWizardStyle",
164: Boolean.TRUE); //NOI18N
165: wizardInstance.putProperty("WizardPanel_contentDisplayed",
166: Boolean.TRUE); //NOI18N
167: wizardInstance.putProperty("WizardPanel_contentNumbered",
168: Boolean.TRUE); //NOI18N
169: wizardInstance.putProperty("WizardPanel_contentData",
170: stepsNames); //NOI18N
171:
172: if (!guiInitialized) {
173: initialize();
174:
175: myData = new DBSchemaWizardData();
176: panels = new WizardDescriptor.Panel[PANEL_COUNT];
177:
178: DBSchemaTargetPanel targetPanel = new DBSchemaTargetPanel();
179: targetPanel.setPanel(wizard.targetChooser());
180:
181: java.awt.Component panel = targetPanel.getComponent();
182: if (panel instanceof javax.swing.JComponent) {
183: ((javax.swing.JComponent) panel).putClientProperty(
184: "WizardPanel_contentData", stepsNames); //NOI18N
185: ((javax.swing.JComponent) panel).putClientProperty(
186: "WizardPanel_contentSelectedIndex",
187: new Integer(0)); //NOI18N
188: }
189:
190: panels[0] = targetPanel.getPanel();
191: panels[1] = new DBSchemaConnectionWizardPanel(myData);
192: panels[2] = new DBSchemaTablesWizardPanel(myData);
193: }
194:
195: panelIndex = 0;
196: }
197:
198: public void uninitialize(TemplateWizard wiz) {
199: if (wiz.getValue() == NotifyDescriptor.CANCEL_OPTION)
200: ((DBSchemaTablesPanel) panels[2].getComponent()).uninit();
201:
202: panels = null;
203: myData = null;
204: guiInitialized = false;
205: }
206:
207: protected void initialize() {
208: if (panelNames == null) {
209: panelNames = new String[PANEL_COUNT];
210: panelNames[0] = ""; //NOI18N
211: panelNames[1] = ""; //NOI18N
212: panelNames[2] = ""; //NOI18N
213: }
214: }
215:
216: /**
217: * Hack which sets the default target to the src/conf or src directory,
218: * whichever exists.
219: */
220: private void setDefaultTarget() {
221: FileObject targetFO;
222: try {
223: DataFolder target = wizardInstance.getTargetFolder();
224: targetFO = target.getPrimaryFile();
225: } catch (IOException e) {
226: targetFO = null;
227: }
228:
229: Project targetProject = Templates.getProject(wizardInstance);
230: if (targetProject != null) {
231: FileObject projectDir = targetProject.getProjectDirectory();
232: if (targetFO == null || targetFO.equals(projectDir)) {
233: FileObject newTargetFO = projectDir
234: .getFileObject("src/conf"); // NOI18N
235: if (newTargetFO == null || !newTargetFO.isValid()) {
236: newTargetFO = projectDir
237: .getFileObject("src/META-INF"); // NOI18N
238: if (newTargetFO == null || !newTargetFO.isValid()) {
239: newTargetFO = projectDir.getFileObject("src"); // NOI18N
240: if (newTargetFO == null
241: || !newTargetFO.isValid()) {
242: return;
243: }
244: }
245: }
246:
247: DataFolder newTarget = DataFolder
248: .findFolder(newTargetFO);
249: wizardInstance.setTargetFolder(newTarget);
250: }
251: }
252: }
253: }
|