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-2007 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.xml.schema.abe.wizard;
043:
044: import java.awt.Component;
045: import java.awt.Dialog;
046: import java.io.IOException;
047: import java.text.MessageFormat;
048: import java.util.Collections;
049: import java.util.NoSuchElementException;
050: import java.util.Set;
051: import javax.swing.JComponent;
052: import javax.swing.event.ChangeListener;
053: import org.netbeans.modules.xml.axi.AXIModel;
054: import org.netbeans.modules.xml.axi.AXIModelFactory;
055: import org.netbeans.modules.xml.axi.SchemaGenerator;
056: import org.netbeans.modules.xml.axi.SchemaGeneratorFactory;
057: import org.netbeans.modules.xml.schema.model.SchemaModel;
058: import org.openide.DialogDescriptor;
059: import org.openide.DialogDisplayer;
060: import org.openide.ErrorManager;
061: import org.openide.WizardDescriptor;
062: import org.openide.filesystems.FileObject;
063: import org.openide.util.NbBundle;
064: import org.openide.util.RequestProcessor;
065:
066: /**
067: * An action on the SchemaDataObject node (SchemaNode)
068: * to "Transform" the schema (from one design pattern to another)
069: *
070: * @author Ayub Khan
071: */
072: public class SchemaTransformWizard implements
073: WizardDescriptor.InstantiatingIterator {
074: private static final long serialVersionUID = 1L;
075:
076: public static final String SINGLE_GLOBAL_ELEMENT_KEY = "singleGlobalElementKey"; //NOI18N
077:
078: public static final String TYPE_REUSE_KEY = "typeReuseKey"; //NOI18N
079:
080: public static final String INFERED_DESIGN_PATTERN_KEY = "inferedDesignPatternKey"; //NOI18N
081:
082: public static final String SELECTED_DESIGN_PATTERN_KEY = "selectedDesignPatternKey"; //NOI18N
083:
084: public static final String SCHEMA_MODEL_KEY = "schemaModelKey"; //NOI18N
085:
086: private int index;
087:
088: private WizardDescriptor.Panel[] panels;
089:
090: private WizardDescriptor wizard;
091:
092: private RequestProcessor.Task transformTask;
093:
094: // private ProgressHandle progressHandle;
095:
096: private SchemaTransformProgressPanel progressPanel = new SchemaTransformProgressPanel();
097:
098: private SchemaModel sm;
099:
100: private String fileName;
101:
102: private boolean isCancelled;
103:
104: private boolean finishTransform;
105:
106: /** Creates a new instance of SchemaTransformWizard */
107: public SchemaTransformWizard(final SchemaModel sm) {
108: this .sm = sm;
109: FileObject fo = (FileObject) sm.getModelSource().getLookup()
110: .lookup(FileObject.class);
111: if (fo != null)
112: this .fileName = fo.getNameExt();
113: }
114:
115: /**
116: * Initialize panels representing individual wizard's steps and sets
117: * various properties for them influencing wizard appearance.
118: */
119: private WizardDescriptor.Panel[] getPanels() {
120: if (panels == null) {
121: SchemaGenerator.Pattern inferedPattern = inferSchemaDesignPattern();
122: panels = new WizardDescriptor.Panel[] { new SchemaTransformPatternSelection(
123: inferedPattern) };
124: String[] steps = new String[panels.length];
125: for (int i = 0; i < panels.length; i++) {
126: Component c = panels[i].getComponent();
127: // Default step name to component name of panel. Mainly useful
128: // for getting the name of the target chooser to appear in the
129: // list of steps.
130: steps[i] = c.getName();
131: if (c instanceof JComponent) { // assume Swing components
132: JComponent jc = (JComponent) c;
133: // Sets step number of a component
134: jc.putClientProperty(
135: "WizardPanel_contentSelectedIndex",
136: new Integer(i));//NOI18n
137: // Sets steps names for a panel
138: jc.putClientProperty("WizardPanel_contentData",
139: steps);//NOI18n
140: // Turn on subtitle creation on each step
141: jc.putClientProperty("WizardPanel_autoWizardStyle",
142: Boolean.TRUE);//NOI18n
143: // Show steps on the left side with the image on the background
144: jc.putClientProperty(
145: "WizardPanel_contentDisplayed",
146: Boolean.TRUE);//NOI18n
147: // Turn on numbering of all steps
148: jc.putClientProperty("WizardPanel_contentNumbered",
149: Boolean.TRUE);//NOI18n
150: }
151: }
152: }
153: return panels;
154: }
155:
156: public SchemaGenerator.Pattern show() {
157: WizardDescriptor wizardDescriptor = new WizardDescriptor(
158: getPanels());
159: initialize(wizardDescriptor);
160:
161: wizardDescriptor.setTitleFormat(new MessageFormat("{0}"));
162: wizardDescriptor.setTitle(NbBundle.getMessage(
163: SchemaTransformWizard.class, "TITLE_SchemaTransform"));
164: Dialog dialog = DialogDisplayer.getDefault().createDialog(
165: wizardDescriptor);
166: dialog.getAccessibleContext().setAccessibleName(
167: NbBundle.getMessage(SchemaTransformWizard.class,
168: "TITLE_SchemaTransform"));
169: dialog.getAccessibleContext().setAccessibleDescription(
170: NbBundle.getMessage(SchemaTransformWizard.class,
171: "HINT_SchemaTransform"));
172: dialog.setVisible(true);
173: dialog.toFront();
174:
175: final SchemaGenerator.Pattern selectedPattern = (SchemaGenerator.Pattern) wizard
176: .getProperty(SchemaTransformWizard.SELECTED_DESIGN_PATTERN_KEY);
177: boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
178: this .isCancelled = cancelled;
179: if (!cancelled) {
180: transformTask = RequestProcessor.getDefault().create(
181: new Runnable() {
182: public void run() {
183: try {
184: SchemaGeneratorFactory.getDefault()
185: .transformSchema(sm,
186: selectedPattern);
187: } catch (IOException iox) {
188: ErrorManager.getDefault().notify(iox);
189: } finally {
190: finishTransform = true;
191: finishProgress();
192: }
193: }
194: });
195: transformTask.schedule(50);
196: finishTransform = false;
197: startProgress(NbBundle.getMessage(
198: SchemaTransformWizard.class,
199: "MSG_SchemaTransform_ProgressMessage", fileName));
200: }
201: return selectedPattern;
202: }
203:
204: /**
205: * Starts associated progress if not yet started. Allows to share
206: * progress with execution preparation phase (cache ops).
207: *
208: * @param details progress detail messag eor null
209: */
210: private void startProgress(String details) {
211: DialogDescriptor d = null;
212:
213: //keep showing the dialog if user closes the dialog and transform is not finished
214: while (!finishTransform) {
215: // clear/hide dialog if any
216: progressPanel.hideDialog();
217:
218: d = progressPanel.createDialog(fileName);
219: progressPanel.showDialog(fileName);
220: }
221: }
222:
223: private void finishProgress() {
224: progressPanel.hideDialog();
225: }
226:
227: private SchemaGenerator.Pattern inferSchemaDesignPattern() {
228: AXIModel am = AXIModelFactory.getDefault().getModel(sm);
229: //inferedPattern = am.getSchemaDesignPattern();
230: SchemaGenerator.Pattern inferedPattern = SchemaGeneratorFactory
231: .getDefault().inferDesignPattern(am);
232: if (inferedPattern == null)
233: inferedPattern = SchemaGenerator.DEFAULT_DESIGN_PATTERN;
234: return inferedPattern;
235: }
236:
237: private void selectInitialDesignPattern(WizardDescriptor wizard,
238: SchemaGenerator.Pattern p) {
239: if (p == SchemaGenerator.Pattern.RUSSIAN_DOLL) {
240: wizard.putProperty(
241: SchemaTransformWizard.SINGLE_GLOBAL_ELEMENT_KEY,
242: Boolean.valueOf(true));
243: wizard.putProperty(SchemaTransformWizard.TYPE_REUSE_KEY,
244: Boolean.valueOf(false));
245: } else if (p == SchemaGenerator.Pattern.VENITIAN_BLIND) {
246: wizard.putProperty(
247: SchemaTransformWizard.SINGLE_GLOBAL_ELEMENT_KEY,
248: Boolean.valueOf(true));
249: wizard.putProperty(SchemaTransformWizard.TYPE_REUSE_KEY,
250: Boolean.valueOf(true));
251: } else if (p == SchemaGenerator.Pattern.SALAMI_SLICE) {
252: wizard.putProperty(
253: SchemaTransformWizard.SINGLE_GLOBAL_ELEMENT_KEY,
254: Boolean.valueOf(false));
255: wizard.putProperty(SchemaTransformWizard.TYPE_REUSE_KEY,
256: Boolean.valueOf(false));
257: } else if (p == SchemaGenerator.Pattern.GARDEN_OF_EDEN) {
258: wizard.putProperty(
259: SchemaTransformWizard.SINGLE_GLOBAL_ELEMENT_KEY,
260: Boolean.valueOf(false));
261: wizard.putProperty(SchemaTransformWizard.TYPE_REUSE_KEY,
262: Boolean.valueOf(true));
263: }
264: }
265:
266: public boolean isCancelled() {
267: return isCancelled;
268: }
269:
270: public Set instantiate() throws IOException {
271: return Collections.emptySet();
272: }
273:
274: public void initialize(WizardDescriptor wizard) {
275: SchemaGenerator.Pattern inferedPattern = inferSchemaDesignPattern();
276:
277: wizard.putProperty(SchemaTransformWizard.SCHEMA_MODEL_KEY,
278: this .sm);
279: wizard.putProperty(
280: SchemaTransformWizard.SELECTED_DESIGN_PATTERN_KEY,
281: inferedPattern);
282: wizard.putProperty(
283: SchemaTransformWizard.INFERED_DESIGN_PATTERN_KEY,
284: inferedPattern);
285: selectInitialDesignPattern(wizard, inferedPattern);
286: this .wizard = wizard;
287: }
288:
289: public void uninitialize(WizardDescriptor wizard) {
290: panels = null;
291: }
292:
293: public WizardDescriptor.Panel current() {
294: return getPanels()[index];
295: }
296:
297: public String name() {
298: return index + 1 + ". from " + getPanels().length;
299: }
300:
301: public boolean hasNext() {
302: return index < getPanels().length - 1;
303: }
304:
305: public boolean hasPrevious() {
306: return index > 0;
307: }
308:
309: public void nextPanel() {
310: if (!hasNext()) {
311: throw new NoSuchElementException();
312: }
313: index++;
314: }
315:
316: public void previousPanel() {
317: if (!hasPrevious()) {
318: throw new NoSuchElementException();
319: }
320: index--;
321: }
322:
323: public void addChangeListener(ChangeListener l) {
324: }
325:
326: public void removeChangeListener(ChangeListener l) {
327: }
328: }
|