KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > wizard > SchemaTransformWizard


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.abe.wizard;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.NoSuchElementException JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import org.netbeans.modules.xml.axi.AXIModel;
32 import org.netbeans.modules.xml.axi.AXIModelFactory;
33 import org.netbeans.modules.xml.axi.SchemaGenerator;
34 import org.netbeans.modules.xml.axi.SchemaGeneratorFactory;
35 import org.netbeans.modules.xml.schema.model.SchemaModel;
36 import org.openide.DialogDescriptor;
37 import org.openide.DialogDisplayer;
38 import org.openide.ErrorManager;
39 import org.openide.WizardDescriptor;
40 import org.openide.filesystems.FileObject;
41 import org.openide.util.NbBundle;
42 import org.openide.util.RequestProcessor;
43
44 /**
45  * An action on the SchemaDataObject node (SchemaNode)
46  * to "Transform" the schema (from one design pattern to another)
47  *
48  * @author Ayub Khan
49  */

50 public class SchemaTransformWizard implements WizardDescriptor.InstantiatingIterator {
51     private static final long serialVersionUID = 1L;
52     
53     public static final String JavaDoc SINGLE_GLOBAL_ELEMENT_KEY = "singleGlobalElementKey"; //NOI18N
54

55     public static final String JavaDoc TYPE_REUSE_KEY = "typeReuseKey"; //NOI18N
56

57     public static final String JavaDoc INFERED_DESIGN_PATTERN_KEY = "inferedDesignPatternKey"; //NOI18N
58

59     public static final String JavaDoc SELECTED_DESIGN_PATTERN_KEY = "selectedDesignPatternKey"; //NOI18N
60

61     public static final String JavaDoc SCHEMA_MODEL_KEY = "schemaModelKey"; //NOI18N
62

63     private int index;
64     
65     private WizardDescriptor.Panel[] panels;
66     
67     private WizardDescriptor wizard;
68     
69     private RequestProcessor.Task transformTask;
70     
71 // private ProgressHandle progressHandle;
72

73     private SchemaTransformProgressPanel progressPanel = new SchemaTransformProgressPanel();
74     
75     private SchemaModel sm;
76     
77     private String JavaDoc fileName;
78     
79     private boolean isCancelled;
80     
81     private boolean finishTransform;
82     
83     /** Creates a new instance of SchemaTransformWizard */
84     public SchemaTransformWizard(final SchemaModel sm) {
85         this.sm = sm;
86         FileObject fo = (FileObject) sm.getModelSource().getLookup().
87                 lookup(FileObject.class);
88         if(fo != null)
89             this.fileName = fo.getNameExt();
90     }
91     
92     /**
93      * Initialize panels representing individual wizard's steps and sets
94      * various properties for them influencing wizard appearance.
95      */

96     private WizardDescriptor.Panel[] getPanels() {
97         if (panels == null) {
98             SchemaGenerator.Pattern inferedPattern = inferSchemaDesignPattern();
99             panels = new WizardDescriptor.Panel[] {
100                 new SchemaTransformPatternSelection(inferedPattern)
101             };
102             String JavaDoc[] steps = new String JavaDoc[panels.length];
103             for (int i = 0; i < panels.length; i++) {
104                 Component JavaDoc c = panels[i].getComponent();
105                 // Default step name to component name of panel. Mainly useful
106
// for getting the name of the target chooser to appear in the
107
// list of steps.
108
steps[i] = c.getName();
109                 if (c instanceof JComponent JavaDoc) { // assume Swing components
110
JComponent JavaDoc jc = (JComponent JavaDoc) c;
111                     // Sets step number of a component
112
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i));//NOI18n
113
// Sets steps names for a panel
114
jc.putClientProperty("WizardPanel_contentData", steps);//NOI18n
115
// Turn on subtitle creation on each step
116
jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE);//NOI18n
117
// Show steps on the left side with the image on the background
118
jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE);//NOI18n
119
// Turn on numbering of all steps
120
jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE);//NOI18n
121
}
122             }
123         }
124         return panels;
125     }
126     
127     public SchemaGenerator.Pattern show() {
128         WizardDescriptor wizardDescriptor = new WizardDescriptor(getPanels());
129         initialize(wizardDescriptor);
130         
131         wizardDescriptor.setTitleFormat(new MessageFormat JavaDoc("{0}"));
132         wizardDescriptor.setTitle(
133                 NbBundle.getMessage(SchemaTransformWizard.class,"TITLE_SchemaTransform"));
134         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(wizardDescriptor);
135         dialog.getAccessibleContext().setAccessibleName(
136                 NbBundle.getMessage(SchemaTransformWizard.class,"TITLE_SchemaTransform"));
137         dialog.getAccessibleContext().setAccessibleDescription(
138                 NbBundle.getMessage(SchemaTransformWizard.class,"HINT_SchemaTransform"));
139         dialog.setVisible(true);
140         dialog.toFront();
141         
142         final SchemaGenerator.Pattern selectedPattern = (SchemaGenerator.Pattern)
143         wizard.getProperty(SchemaTransformWizard.SELECTED_DESIGN_PATTERN_KEY);
144         boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
145         this.isCancelled = cancelled;
146         if (!cancelled) {
147             transformTask = RequestProcessor.getDefault().create(new Runnable JavaDoc() {
148                 public void run() {
149                     try {
150                         SchemaGeneratorFactory.getDefault().transformSchema(
151                                 sm, selectedPattern);
152                     } catch(IOException JavaDoc iox) {
153                         ErrorManager.getDefault().notify(iox);
154                     } finally {
155                         finishTransform = true;
156                         finishProgress();
157                     }
158                 }
159             });
160             transformTask.schedule(50);
161             finishTransform = false;
162             startProgress(NbBundle.getMessage(SchemaTransformWizard.class,
163                     "MSG_SchemaTransform_ProgressMessage", fileName));
164         }
165         return selectedPattern;
166     }
167     
168     /**
169      * Starts associated progress if not yet started. Allows to share
170      * progress with execution preparation phase (cache ops).
171      *
172      * @param details progress detail messag eor null
173      */

174     private void startProgress(String JavaDoc details) {
175         DialogDescriptor d = null;
176         
177         //keep showing the dialog if user closes the dialog and transform is not finished
178
while(!finishTransform) {
179             // clear/hide dialog if any
180
progressPanel.hideDialog();
181             
182             d = progressPanel.createDialog(fileName);
183             progressPanel.showDialog(fileName);
184         }
185     }
186     
187     private void finishProgress() {
188         progressPanel.hideDialog();
189     }
190     
191     private SchemaGenerator.Pattern inferSchemaDesignPattern() {
192         AXIModel am = AXIModelFactory.getDefault().getModel(sm);
193         //inferedPattern = am.getSchemaDesignPattern();
194
SchemaGenerator.Pattern inferedPattern =
195                 SchemaGeneratorFactory.getDefault().inferDesignPattern(am);
196         if(inferedPattern == null)
197             inferedPattern = SchemaGenerator.DEFAULT_DESIGN_PATTERN;
198         return inferedPattern;
199     }
200     
201     private void selectInitialDesignPattern(WizardDescriptor wizard, SchemaGenerator.Pattern p) {
202         if(p == SchemaGenerator.Pattern.RUSSIAN_DOLL) {
203             wizard.putProperty(SchemaTransformWizard.SINGLE_GLOBAL_ELEMENT_KEY,
204                     Boolean.valueOf(true));
205             wizard.putProperty(SchemaTransformWizard.TYPE_REUSE_KEY,
206                     Boolean.valueOf(false));
207         } else if(p == SchemaGenerator.Pattern.VENITIAN_BLIND) {
208             wizard.putProperty(SchemaTransformWizard.SINGLE_GLOBAL_ELEMENT_KEY,
209                     Boolean.valueOf(true));
210             wizard.putProperty(SchemaTransformWizard.TYPE_REUSE_KEY,
211                     Boolean.valueOf(true));
212         } else if(p == SchemaGenerator.Pattern.SALAMI_SLICE) {
213             wizard.putProperty(SchemaTransformWizard.SINGLE_GLOBAL_ELEMENT_KEY,
214                     Boolean.valueOf(false));
215             wizard.putProperty(SchemaTransformWizard.TYPE_REUSE_KEY,
216                     Boolean.valueOf(false));
217         } else if(p == SchemaGenerator.Pattern.GARDEN_OF_EDEN) {
218             wizard.putProperty(SchemaTransformWizard.SINGLE_GLOBAL_ELEMENT_KEY,
219                     Boolean.valueOf(false));
220             wizard.putProperty(SchemaTransformWizard.TYPE_REUSE_KEY,
221                     Boolean.valueOf(true));
222         }
223     }
224     
225     public boolean isCancelled() {
226         return isCancelled;
227     }
228     
229     public Set JavaDoc instantiate() throws IOException JavaDoc {
230         return Collections.emptySet();
231     }
232     
233     public void initialize(WizardDescriptor wizard) {
234         SchemaGenerator.Pattern inferedPattern = inferSchemaDesignPattern();
235         
236         wizard.putProperty(SchemaTransformWizard.SCHEMA_MODEL_KEY, this.sm);
237         wizard.putProperty(SchemaTransformWizard.SELECTED_DESIGN_PATTERN_KEY,
238                 inferedPattern);
239         wizard.putProperty(SchemaTransformWizard.INFERED_DESIGN_PATTERN_KEY,
240                 inferedPattern);
241         selectInitialDesignPattern(wizard, inferedPattern);
242         this.wizard = wizard;
243     }
244     
245     public void uninitialize(WizardDescriptor wizard) {
246         panels = null;
247     }
248     
249     public WizardDescriptor.Panel current() {
250         return getPanels()[index];
251     }
252     
253     public String JavaDoc name() {
254         return index + 1 + ". from " + getPanels().length;
255     }
256     
257     public boolean hasNext() {
258         return index < getPanels().length - 1;
259     }
260     
261     public boolean hasPrevious() {
262         return index > 0;
263     }
264     
265     public void nextPanel() {
266         if (!hasNext()) {
267             throw new NoSuchElementException JavaDoc();
268         }
269         index++;
270     }
271     
272     public void previousPanel() {
273         if (!hasPrevious()) {
274             throw new NoSuchElementException JavaDoc();
275         }
276         index--;
277     }
278     
279     public void addChangeListener(ChangeListener JavaDoc l) {}
280     
281     public void removeChangeListener(ChangeListener JavaDoc l) {}
282 }
283
Popular Tags