1 19 20 package org.netbeans.modules.xml.schema.abe.wizard; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.io.IOException ; 25 import java.text.MessageFormat ; 26 import java.util.Collections ; 27 import java.util.NoSuchElementException ; 28 import java.util.Set ; 29 import javax.swing.JComponent ; 30 import javax.swing.event.ChangeListener ; 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 50 public class SchemaTransformWizard implements WizardDescriptor.InstantiatingIterator { 51 private static final long serialVersionUID = 1L; 52 53 public static final String SINGLE_GLOBAL_ELEMENT_KEY = "singleGlobalElementKey"; 55 public static final String TYPE_REUSE_KEY = "typeReuseKey"; 57 public static final String INFERED_DESIGN_PATTERN_KEY = "inferedDesignPatternKey"; 59 public static final String SELECTED_DESIGN_PATTERN_KEY = "selectedDesignPatternKey"; 61 public static final String SCHEMA_MODEL_KEY = "schemaModelKey"; 63 private int index; 64 65 private WizardDescriptor.Panel[] panels; 66 67 private WizardDescriptor wizard; 68 69 private RequestProcessor.Task transformTask; 70 71 73 private SchemaTransformProgressPanel progressPanel = new SchemaTransformProgressPanel(); 74 75 private SchemaModel sm; 76 77 private String fileName; 78 79 private boolean isCancelled; 80 81 private boolean finishTransform; 82 83 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 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 [] steps = new String [panels.length]; 103 for (int i = 0; i < panels.length; i++) { 104 Component c = panels[i].getComponent(); 105 steps[i] = c.getName(); 109 if (c instanceof JComponent ) { JComponent jc = (JComponent ) c; 111 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE); jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE); } 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 ("{0}")); 132 wizardDescriptor.setTitle( 133 NbBundle.getMessage(SchemaTransformWizard.class,"TITLE_SchemaTransform")); 134 Dialog 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 () { 148 public void run() { 149 try { 150 SchemaGeneratorFactory.getDefault().transformSchema( 151 sm, selectedPattern); 152 } catch(IOException 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 174 private void startProgress(String details) { 175 DialogDescriptor d = null; 176 177 while(!finishTransform) { 179 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 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 instantiate() throws IOException { 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 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 (); 268 } 269 index++; 270 } 271 272 public void previousPanel() { 273 if (!hasPrevious()) { 274 throw new NoSuchElementException (); 275 } 276 index--; 277 } 278 279 public void addChangeListener(ChangeListener l) {} 280 281 public void removeChangeListener(ChangeListener l) {} 282 } 283 | Popular Tags |