1 19 20 package org.openide.loaders; 21 22 23 import java.awt.*; 24 import java.awt.event.ActionEvent ; 25 import java.beans.*; 26 import java.io.IOException ; 27 import java.net.*; 28 import java.text.MessageFormat ; 29 import java.util.*; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 import javax.swing.*; 33 import org.netbeans.api.progress.ProgressHandle; 34 import org.openide.*; 35 import org.openide.WizardDescriptor.Panel; 36 import org.openide.filesystems.*; 37 import org.openide.nodes.Node; 38 import org.openide.util.*; 39 40 44 public class TemplateWizard extends WizardDescriptor { 45 46 private static final String EA_DESCRIPTION = "templateWizardURL"; 48 private static final String EA_ITERATOR = "templateWizardIterator"; 52 @Deprecated 53 private static final String EA_DESC_RESOURCE = "templateWizardDescResource"; 55 56 private static final String CUSTOM_DESCRIPTION = "instantiatingWizardURL"; 58 private static final String CUSTOM_ITERATOR = "instantiatingIterator"; 60 62 private static final String PROP_CONTENT_SELECTED_INDEX = "WizardPanel_contentSelectedIndex"; 65 private static final String PROP_CONTENT_DATA = "WizardPanel_contentData"; 67 68 static java.awt.Dimension PREF_DIM = new java.awt.Dimension (560, 350); 69 70 71 private Panel<WizardDescriptor> templateChooser; 72 73 private Panel<WizardDescriptor> targetChooser; 74 75 private boolean showTargetChooser = true; 76 77 78 private Iterator targetIterator; 79 80 private TemplateWizardIteratorWrapper iterator; 81 82 83 private DataObject template; 84 85 86 private DataFolder templatesFolder; 87 88 89 private String targetName = null; 90 91 private DataFolder targetDataFolder; 92 93 private boolean titleFormatSet = false; 94 95 96 private PropertyChangeListener pcl; 97 98 99 private Component lastComp; 100 101 private Set<DataObject> newObjects = null; 102 103 private ProgressHandle progressHandle; 104 105 106 public TemplateWizard () { 107 this (new TemplateWizardIteratorWrapper.InstantiatingIterator (new TemplateWizardIterImpl ())); 108 } 109 110 112 private TemplateWizard (TemplateWizardIteratorWrapper it) { 113 super (it); 114 115 this.iterator = it; 116 117 iterator.initialize(this); 119 120 putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); putProperty("WizardPanel_contentNumbered", Boolean.TRUE); setTitle(NbBundle.getMessage(TemplateWizard.class,"CTL_TemplateTitle")); setTitleFormat(new MessageFormat ("{0}")); } 126 127 132 protected TemplateWizard (TemplateWizard.Iterator it) { 133 this(); 134 iterator.setIterator(it, false); 135 } 136 137 139 protected void initialize () { 140 if (iterator != null) { 141 iterator.initialize(this); 142 newObjects = null; 143 } 144 super.initialize (); 145 } 146 147 149 final void setTemplateImpl (DataObject obj, boolean notify) { 150 DataObject old = template; 151 if (template != obj) { 152 template = obj; 153 } 154 setTitle (getTitleFormat().format(new Object [] { obj.getNodeDelegate().getDisplayName() })); 155 if (old != template) { 156 Iterator it; 157 if ( 158 obj == null || 159 (it = getIterator (obj)) == null 160 ) { 161 it = defaultIterator (); 162 } 163 164 if (it instanceof InstantiatingIteratorBridge) { 166 WizardDescriptor.InstantiatingIterator newIt = ((InstantiatingIteratorBridge) it).getOriginalIterator (); 167 if (newIt instanceof WizardDescriptor.ProgressInstantiatingIterator) { 168 TemplateWizardIteratorWrapper newIterImplWrapper = new TemplateWizardIteratorWrapper.ProgressInstantiatingIterator (this.iterator.getOriginalIterImpl ()); 169 this.iterator = newIterImplWrapper; 170 this.setPanels (newIterImplWrapper); 171 } else if (newIt instanceof WizardDescriptor.AsynchronousInstantiatingIterator) { 172 TemplateWizardIteratorWrapper newIterImplWrapper = new TemplateWizardIteratorWrapper.AsynchronousInstantiatingIterator (this.iterator.getOriginalIterImpl ()); 173 this.iterator = newIterImplWrapper; 174 this.setPanels (newIterImplWrapper); 175 } 176 } 177 this.iterator.setIterator (it, notify); 178 } 179 } 180 181 184 public DataObject getTemplate () { 185 return template; 186 } 187 188 193 public void setTemplate (DataObject obj) { 194 if (obj != null) { 195 setTemplateImpl (obj, true); 196 } 197 } 198 199 205 public void setTemplatesFolder (DataFolder folder) { 206 templatesFolder = folder; 207 } 208 209 212 public DataFolder getTemplatesFolder () { 213 DataFolder df = templatesFolder; 214 if (df == null) { 215 FileObject fo = Repository.getDefault ().getDefaultFileSystem ().findResource ("/Templates"); if (fo != null && fo.isFolder ()) { 217 return DataFolder.findFolder (fo); 218 } 219 } 220 return df; 221 } 222 223 224 225 230 public DataFolder getTargetFolder () throws IOException { 231 if (targetDataFolder == null) { 232 throw new IOException (NbBundle.getMessage(TemplateWizard.class, "ERR_NoFilesystem")); 233 } 234 return targetDataFolder; 235 } 236 237 private void reload (DataObject obj) { 239 Iterator it; 240 if ( 241 obj == null || 242 (it = getIterator (obj)) == null 243 ) { 244 it = defaultIterator (); 245 } 246 this.iterator.setIterator (it, true); 247 } 248 249 253 public void setTargetFolder (DataFolder f) { 254 targetDataFolder = f; 255 } 256 257 260 public String getTargetName () { 261 return targetName; 262 } 263 264 267 public void setTargetName (String name) { 268 targetName = name; 269 } 270 271 274 public Panel<WizardDescriptor> templateChooser () { 275 synchronized (this) { 276 if (templateChooser == null) { 277 templateChooser = createTemplateChooser (); 278 } 279 } 280 return templateChooser; 281 } 282 283 287 public Panel<WizardDescriptor> targetChooser () { 288 synchronized (this) { 289 if (targetChooser == null) { 290 targetChooser = createTargetChooser (); 291 } 292 } 293 return targetChooser; 294 } 295 296 298 final synchronized Iterator defaultIterator () { 299 if (targetIterator == null) { 300 targetIterator = createDefaultIterator (); 301 } 302 return targetIterator; 303 } 304 305 310 protected Panel<WizardDescriptor> createTemplateChooser () { 311 return new TemplateWizardPanel1 (); 312 } 313 314 318 protected Panel<WizardDescriptor> createTargetChooser () { 319 if (showTargetChooser) { 320 return new TemplateWizardPanel2 (); 321 } else { 322 return new NewObjectWizardPanel (); 323 } 324 } 325 326 335 protected Iterator createDefaultIterator () { 336 return new DefaultIterator (); 337 } 338 339 344 public Set<DataObject> instantiate() throws IOException { 345 showTargetChooser = true; 346 return instantiateImpl (null, null); 347 } 348 349 356 public Set<DataObject> instantiate(DataObject template) throws IOException { 357 showTargetChooser = true; 358 return instantiateImpl (template, null); 359 } 360 361 370 public Set<DataObject> instantiate( 371 DataObject template, DataFolder targetFolder 372 ) throws IOException { 373 showTargetChooser = false; 374 return instantiateImpl (template, targetFolder); 375 } 376 377 private ProgressHandle getProgressHandle () { 378 return progressHandle; 379 } 380 381 Set<DataObject> instantiateNewObjects (ProgressHandle handle) throws IOException { 382 progressHandle = handle; 383 try { 384 Object option = getValue(); 387 if(option == FINISH_OPTION || option == YES_OPTION 388 || option == OK_OPTION) { 389 390 showWaitCursor (); 392 393 newObjects = handleInstantiate (); 394 if (lastComp != null) { 395 lastComp.removePropertyChangeListener(propL()); 396 lastComp = null; 397 } 398 } else { 399 if (lastComp != null) { 400 lastComp.removePropertyChangeListener(propL()); 401 lastComp = null; 402 } 403 newObjects = null; 404 } 405 406 } finally { 407 408 showNormalCursor (); 410 } 411 412 return newObjects; 413 } 414 415 421 private Set<DataObject> instantiateImpl( 422 DataObject template, DataFolder targetFolder 423 ) throws IOException { 424 425 showTargetChooser |= targetFolder == null; 426 targetChooser = null; 427 428 Throwable thrownMessage = null; 432 433 if (targetFolder != null) { 436 setTargetFolder (targetFolder); 437 } 438 439 if (template != null) { 440 this.template = null; 442 setTemplate (template); 443 444 if (iterator != null) { 446 iterator.initialize(this); 447 } 448 } else if (iterator != null) { 449 iterator.initialize(this); 450 iterator.first(); 451 } 452 453 try { 454 updateState(); 455 setValue (null); 457 458 final java.awt.Dialog d = DialogDisplayer.getDefault().createDialog(this); 459 if (thrownMessage != null) { 462 final Throwable t = thrownMessage; 463 thrownMessage = null; 464 d.addComponentListener(new java.awt.event.ComponentAdapter () { 465 466 public void componentShown(java.awt.event.ComponentEvent e) { 467 if (t.getMessage() != null) { 468 DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Exception(t)); 470 } else { 471 Exceptions.printStackTrace(t); 474 } 475 d.removeComponentListener(this); 476 } 477 }); 478 } 479 d.setVisible(true); 480 } catch (IllegalStateException ise) { 481 thrownMessage = ise; 482 } 483 484 return newObjects; 487 } 488 489 private void showWaitCursor () { 490 org.openide.util.Mutex.EVENT.writeAccess(new java.lang.Runnable () { 494 495 public void run() { 496 try { 497 java.awt.Frame f = org.openide.windows.WindowManager.getDefault().getMainWindow(); 498 499 if (f instanceof javax.swing.JFrame ) { 500 java.awt.Component c = ((javax.swing.JFrame ) f).getGlassPane(); 501 502 c.setVisible(true); 503 c.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR)); 504 } 505 } 506 catch (java.lang.NullPointerException npe) { 507 Logger.getLogger(TemplateWizard.class.getName()).log(Level.WARNING, 508 null, 509 npe); 510 } 511 } 512 }); 513 } 514 515 private void showNormalCursor () { 516 org.openide.util.Mutex.EVENT.writeAccess(new java.lang.Runnable () { 520 521 public void run() { 522 try { 523 java.awt.Frame f = org.openide.windows.WindowManager.getDefault().getMainWindow(); 524 525 if (f instanceof javax.swing.JFrame ) { 526 java.awt.Component c = ((javax.swing.JFrame ) f).getGlassPane(); 527 528 c.setCursor(null); 529 c.setVisible(false); 530 } 531 } 532 catch (java.lang.NullPointerException npe) { 533 Logger.getLogger(TemplateWizard.class.getName()).log(Level.WARNING, 534 null, 535 npe); 536 } 537 } 538 }); 539 } 540 541 542 545 @Override 546 public void setTitleFormat(MessageFormat format) { 547 titleFormatSet = true; super.setTitleFormat(format); 549 } 550 551 554 @Override 555 public MessageFormat getTitleFormat () { 556 if (!titleFormatSet) { 557 setTitleFormat (new MessageFormat (NbBundle.getMessage(TemplateWizard.class, "CTL_TemplateTitle"))); 560 } 561 return super.getTitleFormat(); 562 } 563 564 565 571 protected Set<DataObject> handleInstantiate() throws IOException { 572 return iterator.getIterator ().instantiate (this); 573 } 574 575 582 public static void setDescription (DataObject obj, URL url) throws IOException { 583 obj.getPrimaryFile().setAttribute(EA_DESCRIPTION, url); 584 obj.getPrimaryFile().setAttribute(CUSTOM_DESCRIPTION, url); 585 } 586 587 591 public static URL getDescription (DataObject obj) { 592 URL desc = (URL)obj.getPrimaryFile().getAttribute(CUSTOM_DESCRIPTION); 593 if (desc != null) return desc; 594 desc = (URL)obj.getPrimaryFile().getAttribute(EA_DESCRIPTION); 595 if (desc != null) return desc; 596 String rsrc = (String ) obj.getPrimaryFile ().getAttribute (EA_DESC_RESOURCE); 598 if (rsrc != null) { 599 try { 600 URL better = new URL ("nbresloc:/" + rsrc); try { 602 setDescription (obj, better); 603 } catch (IOException ioe) { 604 } 606 return better; 607 } catch (MalformedURLException mfue) { 608 Exceptions.printStackTrace(mfue); 609 } 610 } 611 return null; 612 } 613 614 620 @Deprecated 621 public static void setDescriptionAsResource (DataObject obj, String rsrc) throws IOException { 622 if (rsrc != null && rsrc.startsWith ("/")) { Logger.getLogger(TemplateWizard.class.getName()).warning("auto-stripping leading slash from resource path in TemplateWizard.setDescriptionAsResource: " + rsrc); 624 rsrc = rsrc.substring (1); 625 } 626 obj.getPrimaryFile ().setAttribute (EA_DESC_RESOURCE, rsrc); 627 } 628 629 634 @Deprecated 635 public static String getDescriptionAsResource (DataObject obj) { 636 return (String ) obj.getPrimaryFile ().getAttribute (EA_DESC_RESOURCE); 637 } 638 639 653 @Deprecated 654 public static void setIterator (DataObject obj, Iterator iter) 655 throws IOException { 656 obj.getPrimaryFile().setAttribute(CUSTOM_ITERATOR, iter); 657 obj.getPrimaryFile().setAttribute(EA_ITERATOR, iter); 658 } 659 660 668 @SuppressWarnings ("unchecked") 669 public static Iterator getIterator (DataObject obj) { 670 Object unknownIterator = obj.getPrimaryFile ().getAttribute(CUSTOM_ITERATOR); 671 if (unknownIterator == null) { 672 unknownIterator = obj.getPrimaryFile ().getAttribute(EA_ITERATOR); 673 } 674 Iterator it = null; 675 if (unknownIterator instanceof Iterator) { 676 it = (Iterator)unknownIterator; 678 } if (unknownIterator instanceof WizardDescriptor.InstantiatingIterator) { 680 it = new InstantiatingIteratorBridge((WizardDescriptor.InstantiatingIterator<WizardDescriptor>) unknownIterator); 681 } 682 if (it != null) { 683 return it; 684 } 685 686 return obj.getCookie (Iterator.class); 687 } 688 689 697 static boolean checkCaseInsensitiveName (FileObject folder, String name, String extension) { 698 Enumeration children = folder.getChildren (false); 700 FileObject fo; 701 while (children.hasMoreElements ()) { 702 fo = (FileObject) children.nextElement (); 703 if (extension.equalsIgnoreCase (fo.getExt ()) && name.equalsIgnoreCase (fo.getName ())) { 704 return true; 705 } 706 } 707 return false; 708 } 709 710 713 protected void updateState() { 714 super.updateState(); 715 716 if (lastComp != null) { 717 lastComp.removePropertyChangeListener(propL()); 718 } 719 720 lastComp = iterator.current().getComponent(); 722 lastComp.addPropertyChangeListener(propL()); 723 724 putProperty(PROP_CONTENT_SELECTED_INDEX, 726 new Integer (getContentSelectedIndex())); 727 if (getContentData() != null) { 728 putProperty(PROP_CONTENT_DATA, getContentData()); 729 } 730 } 731 732 736 private String [] getContentData() { 737 Component first = templateChooser().getComponent(); 738 if (iterator.current() == templateChooser()) { 739 return (String [])((JComponent)first).getClientProperty(PROP_CONTENT_DATA); 741 } 742 String [] cd = null; 743 Component c = iterator.current().getComponent(); 744 if (c instanceof JComponent) { 745 Object property = ((JComponent)c).getClientProperty(PROP_CONTENT_DATA); 747 if (property instanceof String []) { 748 String [] cont = (String [])property; 749 Object value = ((JComponent)first).getClientProperty(PROP_CONTENT_DATA); 750 if (value instanceof String []) { 751 cd = new String [cont.length + 1]; 752 cd[0] = ((String [])value)[0]; 753 System.arraycopy(cont, 0, cd, 1, cont.length); 754 } else { 755 cd = new String [cont.length]; 756 System.arraycopy(cont, 0, cd, 0, cont.length); 757 } 758 } 759 } 760 return cd; 761 } 762 763 766 private int getContentSelectedIndex() { 767 if (iterator.current() == templateChooser()) { 768 return 0; 769 } 770 Component c = iterator.current().getComponent(); 771 if (c instanceof JComponent) { 772 Object property = ((JComponent)c).getClientProperty(PROP_CONTENT_SELECTED_INDEX); 774 if ((property instanceof Integer )) { 775 return ((Integer )property).intValue() + 1; 776 } 777 } 778 return 1; 779 } 780 781 784 private PropertyChangeListener propL() { 785 if (pcl == null) { 786 pcl = new PropertyChangeListener() { 787 public void propertyChange(PropertyChangeEvent ev) { 788 if (PROP_CONTENT_SELECTED_INDEX.equals(ev.getPropertyName())) { 789 putProperty(PROP_CONTENT_SELECTED_INDEX, 790 new Integer (getContentSelectedIndex())); 791 } else { 792 if ((PROP_CONTENT_DATA.equals(ev.getPropertyName())) && (getContentData() != null)) { 793 putProperty(PROP_CONTENT_DATA, getContentData()); 794 } 795 } 796 } 797 }; 798 } 799 return pcl; 800 } 801 802 final TemplateWizardIterImpl getIterImpl () { 804 return iterator.getOriginalIterImpl (); 805 } 806 807 816 public interface Iterator extends WizardDescriptor.Iterator<WizardDescriptor>, 817 java.io.Serializable , org.openide.nodes.Node.Cookie { 818 826 public Set<DataObject> instantiate(TemplateWizard wiz) 827 throws IOException ; 828 829 834 public void initialize(TemplateWizard wiz); 835 836 841 public void uninitialize(TemplateWizard wiz); 842 } 844 846 private final class DefaultIterator implements Iterator { 847 DefaultIterator() {} 848 849 850 public String name () { 851 return ""; } 853 854 862 public Set<DataObject> instantiate(TemplateWizard wiz) throws IOException { 863 String n = wiz.getTargetName (); 864 DataFolder folder = wiz.getTargetFolder (); 865 DataObject template = wiz.getTemplate (); 866 DataObject obj = template.createFromTemplate (folder, n, wiz.getProperties()); 867 868 final Node node = obj.getNodeDelegate (); 870 final Action a = node.getPreferredAction(); 871 if (a != null) { 872 SwingUtilities.invokeLater(new Runnable () { 873 public void run() { 874 a.actionPerformed(new ActionEvent (node, ActionEvent.ACTION_PERFORMED, "")); } 876 }); 877 } 878 879 return Collections.singleton(obj); 880 } 881 882 884 public void initialize(TemplateWizard wiz) { 885 } 886 887 889 public void uninitialize(TemplateWizard wiz) { 890 } 891 892 895 public Panel<WizardDescriptor> current() { 896 return targetChooser (); 897 } 898 899 902 public boolean hasNext() { 903 return false; 904 } 905 906 909 public boolean hasPrevious() { 910 return false; 911 } 912 913 917 public void nextPanel() { 918 throw new java.util.NoSuchElementException (); 919 } 920 921 925 public void previousPanel() { 926 throw new java.util.NoSuchElementException (); 927 } 928 929 933 public void addChangeListener(javax.swing.event.ChangeListener l) { 934 } 935 936 939 public void removeChangeListener(javax.swing.event.ChangeListener l) { 940 } 941 } 942 943 private static class InstantiatingIteratorBridge implements TemplateWizard.Iterator { 944 private WizardDescriptor.InstantiatingIterator<WizardDescriptor> instantiatingIterator; 945 public InstantiatingIteratorBridge (WizardDescriptor.InstantiatingIterator<WizardDescriptor> it) { 946 instantiatingIterator = it; 947 } 948 949 private WizardDescriptor.InstantiatingIterator getOriginalIterator () { 950 return instantiatingIterator; 951 } 952 953 public void addChangeListener (javax.swing.event.ChangeListener l) { 954 instantiatingIterator.addChangeListener (l); 955 } 956 957 public org.openide.WizardDescriptor.Panel<WizardDescriptor> current () { 958 return instantiatingIterator.current (); 959 } 960 961 public boolean hasNext () { 962 return instantiatingIterator.hasNext (); 963 } 964 965 public boolean hasPrevious () { 966 return instantiatingIterator.hasPrevious (); 967 } 968 969 public String name () { 970 return instantiatingIterator.name (); 971 } 972 973 public void nextPanel () { 974 instantiatingIterator.nextPanel (); 975 } 976 977 public void previousPanel () { 978 instantiatingIterator.previousPanel (); 979 } 980 981 public void removeChangeListener (javax.swing.event.ChangeListener l) { 982 instantiatingIterator.removeChangeListener (l); 983 } 984 985 public void initialize (TemplateWizard wiz) { 986 instantiatingIterator.initialize (wiz); 987 } 988 989 public Set<DataObject> instantiate (TemplateWizard wiz) throws IOException { 990 Set workSet = null; 992 if (instantiatingIterator instanceof WizardDescriptor.ProgressInstantiatingIterator) { 993 assert wiz.getProgressHandle () != null : "ProgressHandle cannot be null."; 994 workSet = ((ProgressInstantiatingIterator)instantiatingIterator).instantiate (wiz.getProgressHandle ()); 995 } else { 996 workSet = instantiatingIterator.instantiate (); 997 } 998 java.util.Iterator it = workSet.iterator (); 999 Object obj; 1000 DataObject dobj; 1001 Set<DataObject> resultSet = new LinkedHashSet<DataObject>(workSet.size()); 1002 while (it.hasNext ()) { 1003 obj = it.next (); 1004 assert obj != null; 1005 if (obj instanceof DataObject) { 1006 continue; 1008 } 1009 if (obj instanceof FileObject) { 1010 try { 1011 dobj = DataObject.find ((FileObject)obj); 1012 resultSet.add (dobj); 1013 } catch (DataObjectNotFoundException ex) { 1014 assert false : obj; 1015 } 1016 } else if (obj instanceof Node) { 1017 dobj = ((Node) obj).getCookie(DataObject.class); 1018 assert dobj != null : obj; resultSet.add (dobj); 1020 } 1021 } 1022 return resultSet; 1023 } 1024 1025 public void uninitialize (TemplateWizard wiz) { 1026 instantiatingIterator.uninitialize (wiz); 1027 } 1028 1029 } 1030 1031} 1032 | Popular Tags |