1 19 package org.netbeans.modules.xml.core.wizard; 20 21 import java.awt.event.ActionEvent ; 22 import java.io.OutputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.Writer ; 25 import java.io.OutputStreamWriter ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 import java.util.HashMap ; 29 import java.beans.PropertyChangeListener ; 30 import java.beans.PropertyChangeEvent ; 31 import java.util.Set ; 32 import java.io.IOException ; 33 import java.net.URL ; 34 import java.text.DateFormat ; 35 import java.util.Date ; 36 37 import javax.swing.event.ChangeListener ; 38 import javax.swing.event.ChangeEvent ; 39 import javax.swing.JComponent ; 40 41 import org.openide.loaders.TemplateWizard; 42 import org.openide.WizardDescriptor; 43 import org.openide.loaders.DataFolder; 44 import org.openide.filesystems.FileSystem; 45 import org.openide.filesystems.FileObject; 46 import org.openide.loaders.DataObject; 47 import org.openide.filesystems.FileLock; 48 import org.openide.nodes.Node; 49 import org.openide.util.actions.CallableSystemAction; 50 import org.openide.util.actions.SystemAction; 51 52 58 public class XMLWizardIterator implements TemplateWizard.Iterator { 59 60 private static final long serialVersionUID = 5070430920636117204L; 61 62 63 private static final String XML_EXT = "xml"; 65 67 private transient TemplateWizard templateWizard; 68 69 71 private transient DocumentModel model; 72 73 75 private transient int current; 76 77 private static final int TARGET_PANEL = 0; 78 private transient WizardDescriptor.Panel targetPanel; 79 80 private static final int DOCUMENT_PANEL = 1; 81 private transient DocumentPanel documentPanel; 82 83 private static final int CONSTRAINT_PANEL = 2; 84 private transient SchemaPanel schemaPanel; 85 private transient DTDPanel dtdPanel; 86 87 89 private static XMLWizardIterator instance; 90 91 private transient Map listenersMap = new HashMap (2); 92 private transient String [] beforeSteps; 93 private transient Object targetSteps; 94 95 96 99 public static synchronized XMLWizardIterator singleton() { 100 if (instance == null) { 101 instance = new XMLWizardIterator(); 102 } 103 return instance; 104 } 105 106 public void initialize(TemplateWizard templateWizard) { 107 this.templateWizard = templateWizard; 108 current = TARGET_PANEL; 109 URL targetFolderURL = null; 110 try { 111 DataFolder folder = templateWizard.getTargetFolder(); 112 targetFolderURL = folder.getPrimaryFile().getURL(); 113 if (targetFolderURL.toExternalForm().endsWith("/") == false) { 115 targetFolderURL = new URL (targetFolderURL.toExternalForm() + "/"); 116 } 117 } catch (IOException ignore) { 118 } 119 model = new DocumentModel(targetFolderURL); 120 Object prop = templateWizard.getProperty ("WizardPanel_contentData"); if (prop != null && prop instanceof String []) { 122 beforeSteps = (String [])prop; 123 } 124 } 125 126 public void uninitialize(TemplateWizard templateWizard) { 127 if (targetPanel!=null) { 128 ((JComponent )targetPanel.getComponent()).putClientProperty("WizardPanel_contentData", targetSteps); 129 targetPanel = null; 130 } 131 current = -1; 132 model = null; 133 templateWizard = null; 134 schemaPanel = null; 135 dtdPanel = null; 136 documentPanel = null; 137 } 138 139 public Set instantiate(TemplateWizard templateWizard) throws IOException { 140 final DataFolder folder = templateWizard.getTargetFolder(); 141 142 final String extension = XML_EXT; 143 144 147 String targetName = templateWizard.getTargetName(); 148 if (targetName == null || "null".equals(targetName)) { targetName = "XMLDocument"; } 151 final FileObject targetFolder = folder.getPrimaryFile(); 152 String uniqueTargetName = targetName; 153 int i = 2; 154 155 while (targetFolder.getFileObject(uniqueTargetName, extension) != null) { 156 uniqueTargetName = targetName + i; 157 i++; 158 } 159 160 final String name = uniqueTargetName; 161 162 164 FileSystem filesystem = targetFolder.getFileSystem(); 165 166 final FileObject[] fileObject = new FileObject[1]; 167 FileSystem.AtomicAction fsAction = new FileSystem.AtomicAction() { 168 public void run() throws IOException { 169 FileObject fo = targetFolder.createData(name, extension); 170 FileLock lock = null; 171 try { 172 lock = fo.lock(); 173 OutputStream out = fo.getOutputStream(lock); 174 out = new BufferedOutputStream (out, 999); 175 Writer writer = new OutputStreamWriter (out, "UTF8"); 177 String root = model.getRoot(); 178 if (root == null) root = "root"; 179 180 writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); writer.write("\n"); String nameExt = name + "." + extension; Date now = new Date (); 187 String currentDate = DateFormat.getDateInstance (DateFormat.LONG).format (now); 188 String currentTime = DateFormat.getTimeInstance (DateFormat.SHORT).format (now); 189 String userName = System.getProperty ("user.name"); 190 writer.write ("<!--\n"); writer.write (" Document : " + nameExt + "\n"); writer.write (" Created on : " + currentDate + ", " + currentTime + "\n"); writer.write (" Author : " + userName + "\n"); writer.write (" Description:\n"); writer.write (" Purpose of the document follows.\n"); writer.write ("-->\n"); writer.write ("\n"); 199 if (model.getType() == model.DTD) { 200 if (model.getPublicID() == null) { 201 writer.write("<!DOCTYPE " + root + " SYSTEM '" + model.getSystemID() + "'>\n"); } else { 203 writer.write("<!DOCTYPE " + root + " PUBLIC '" + model.getPublicID() + "' '" + model.getSystemID() + "'>\n"); } 205 writer.write("<" + root + ">\n"); } else if (model.getType() == model.SCHEMA) { 207 String namespace = model.getNamespace(); 208 if (namespace == null || "".equals(namespace)) { 209 writer.write("<" + root + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'\n"); writer.write(" xsi:noNamespaceSchemaLocation='" + model.getSystemID() + "'>\n"); } else { 212 writer.write("<" + root + " xmlns='" + namespace + "'\n"); writer.write(" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'\n"); writer.write(" xsi:schemaLocation='" + namespace + " " + model.getSystemID() + "'>\n"); } 216 } else { 217 writer.write("<" + root + ">\n"); } 219 writer.write("\n"); writer.write("</" + root + ">\n"); 222 writer.flush(); 223 writer.close(); 224 225 lock.releaseLock(); 227 lock = null; 228 229 fileObject[0] = fo; 230 231 } finally { 232 if (lock != null) lock.releaseLock(); 233 } 234 } 235 }; 236 237 238 filesystem.runAtomicAction(fsAction); 239 240 242 Set set = new HashSet (1); 243 DataObject createdObject = DataObject.find(fileObject[0]); 244 Util.performDefaultAction(createdObject); 245 set.add(createdObject); 246 return set; 247 } 248 249 250 public WizardDescriptor.Panel current() { 251 WizardDescriptor.Panel panel = currentComponent(); 252 if (panel.getComponent() instanceof JComponent ) { 253 ((JComponent )panel.getComponent()).putClientProperty( 254 "WizardPanel_contentSelectedIndex", new Integer (current) 256 ); 257 } 258 return panel; 259 } 260 261 262 private WizardDescriptor.Panel currentComponent() { 263 switch (current) { 264 case TARGET_PANEL: 265 return getTargetPanel(); 266 case DOCUMENT_PANEL: 267 return getDocumentPanel(); 268 case CONSTRAINT_PANEL: 269 switch (model.getType()) { 270 case DocumentModel.DTD: 271 return getDTDPanel(); 272 case DocumentModel.SCHEMA: 273 return getSchemaPanel(); 274 default: 275 throw new IllegalStateException (); 276 } 277 default: 278 throw new IllegalStateException (); 279 } 280 } 281 282 public boolean hasNext() { 283 boolean none = model.getType() == model.NONE; 284 int length = none ? DOCUMENT_PANEL : CONSTRAINT_PANEL; 285 return current < length; 286 } 287 288 public boolean hasPrevious() { 289 return current > TARGET_PANEL; 290 } 291 292 public String name() { 293 return "//TODO"; 294 } 295 296 public void nextPanel() { 297 current++; 298 } 299 300 public void previousPanel() { 301 current--; 302 } 303 304 306 312 public void removeChangeListener(ChangeListener changeListener) { 313 if (changeListener == null) return; 314 synchronized (listenersMap) { 315 Object bridge = listenersMap.remove(changeListener); 316 if (bridge == null) return; 317 if (model == null) return; 318 model.removePropertyChangeListener((PropertyChangeListener ) bridge); 319 } 320 } 321 322 public void addChangeListener(final ChangeListener changeListener) { 323 if (changeListener == null) return; 324 synchronized (listenersMap) { 325 PropertyChangeListener listenerBridge = new PropertyChangeListener () { 326 final ChangeEvent EVENT = new ChangeEvent (XMLWizardIterator.this); 327 public void propertyChange(PropertyChangeEvent e) { 328 changeListener.stateChanged(EVENT); 329 } 330 }; 331 332 if (listenersMap.put(changeListener, listenerBridge) == null) { 333 model.addPropertyChangeListener(listenerBridge); 334 } 335 } 336 } 337 338 340 private WizardDescriptor.Panel getDocumentPanel() { 341 if (documentPanel == null) { 342 documentPanel = new DocumentPanel(); 343 documentPanel.setObject(model); 344 345 String [] steps = new String [3]; 346 steps[0] = getTargetPanelName(); 347 steps[1] = getDocumentPanelName(); 348 steps[2] = Util.THIS.getString("MSG_unknown"); 349 String [] newSteps = createSteps(beforeSteps,steps); 350 documentPanel.putClientProperty( 351 "WizardPanel_contentData", newSteps 353 ); 354 355 } 356 return new AbstractPanel.WizardStep(documentPanel); 357 } 358 359 private WizardDescriptor.Panel getDTDPanel() { 360 if (dtdPanel == null) { 361 dtdPanel = new DTDPanel(); 362 dtdPanel.setObject(model); 363 364 String [] steps = new String [3]; 365 steps[0] = getTargetPanelName(); 366 steps[1] = getDocumentPanelName(); 367 steps[2] = getDTDPanelName(); 368 String [] newSteps = createSteps(beforeSteps,steps); 369 dtdPanel.putClientProperty( 370 "WizardPanel_contentData", newSteps 372 ); 373 374 } 375 return new AbstractPanel.WizardStep(dtdPanel); 376 } 377 378 private WizardDescriptor.Panel getSchemaPanel() { 379 if (schemaPanel == null) { 380 schemaPanel = new SchemaPanel(); 381 schemaPanel.setObject(model); 382 383 String [] steps = new String [3]; 384 steps[0] = getTargetPanelName(); 385 steps[1] = getDocumentPanelName(); 386 steps[2] = getSchemaPanelName(); 387 String [] newSteps = createSteps(beforeSteps,steps); 388 schemaPanel.putClientProperty( 389 "WizardPanel_contentData", newSteps 391 ); 392 } 393 return new AbstractPanel.WizardStep(schemaPanel); 394 } 395 396 private WizardDescriptor.Panel getTargetPanel() { 397 if (targetPanel == null) { 398 targetPanel = templateWizard.targetChooser(); 399 401 if (targetPanel.getComponent() instanceof JComponent ) { 402 JComponent panel = (JComponent ) targetPanel.getComponent(); 403 targetSteps = panel.getClientProperty("WizardPanel_contentData"); 404 String [] steps = new String [3]; 405 steps[0] = getTargetPanelName(); 407 steps[1] = getDocumentPanelName(); 408 steps[2] = Util.THIS.getString("MSG_unknown"); 409 String [] newSteps = createSteps(beforeSteps,steps); 410 panel.putClientProperty( 411 "WizardPanel_contentData", newSteps 413 ); 414 } 415 416 } 417 return targetPanel; 418 } 419 420 private String getTargetPanelName() { 421 Object panel = getTargetPanel().getComponent(); 422 if (panel instanceof JComponent ) { 423 return ((JComponent )panel).getName(); 424 } else { 425 return ""; } 427 } 428 429 private String getDocumentPanelName() { 430 return Util.THIS.getString("PROP_doc_panel_name"); 431 } 432 433 private String getDTDPanelName() { 434 return Util.THIS.getString("PROP_dtd_panel_name"); 435 } 436 437 private String getSchemaPanelName() { 438 return Util.THIS.getString("PROP_schema_panel_name"); 439 } 440 441 private static String [] createSteps(String [] before, String [] panelNames) { 442 int diff = 0; 445 if (before == null) { 446 before = new String [0]; 447 } else if (before.length > 0) { 448 diff = ("...".equals (before[before.length - 1])) ? 1 : 0; } 450 String [] res = new String [ (before.length - diff) + panelNames.length]; 451 for (int i = 0; i < res.length; i++) { 452 if (i < (before.length - diff)) { 453 res[i] = before[i]; 454 } else { 455 res[i] = panelNames[i - before.length + diff]; 456 } 457 } 458 return res; 459 } 460 } 461 | Popular Tags |