1 19 package org.netbeans.modules.java.ui.wizard; 20 21 import java.io.IOException ; 22 import java.text.MessageFormat ; 23 import java.util.Collections ; 24 import java.util.NoSuchElementException ; 25 import javax.swing.event.ChangeListener ; 26 27 import org.openide.ErrorManager; 28 import org.openide.WizardDescriptor; 29 30 import org.openide.filesystems.FileUtil; 31 import org.openide.filesystems.FileObject; 32 import org.openide.loaders.DataObject; 33 import org.openide.loaders.DataFolder; 34 import org.openide.loaders.TemplateWizard; 35 import org.netbeans.api.java.classpath.ClassPath; 36 37 41 class JavaPackageIterator implements TemplateWizard.Iterator { 42 WizardDescriptor.Panel targetChooser; 43 TemplateWizard wizardInstance; 44 45 static TemplateWizard.Iterator create() { 46 return new JavaPackageIterator(); 47 } 48 49 public boolean hasNext() { 50 return false; 51 } 52 53 public void previousPanel() { 54 throw new NoSuchElementException (); 55 } 56 57 public void removeChangeListener(ChangeListener l) { 58 } 59 60 public void addChangeListener(ChangeListener l) { 61 } 62 63 public String name() { 64 return ""; } 66 67 public void nextPanel() { 68 throw new NoSuchElementException (); 69 } 70 71 public boolean hasPrevious() { 72 return false; 73 } 74 75 public WizardDescriptor.Panel current() { 76 return wizardInstance.targetChooser(); 77 } 78 79 public void initialize(TemplateWizard wiz) { 80 wizardInstance = wiz; 81 } 82 83 public void uninitialize(TemplateWizard wiz) { 84 } 85 86 private void throwIllegalName(String key, String offending) throws IllegalStateException { 87 String msg; 88 if (offending == null) { 89 msg = Util.getString(key); 90 } 91 else { 92 msg = MessageFormat.format(Util.getString(key), 93 new Object [] { 94 offending}); 95 } 96 97 IllegalStateException x = 98 (IllegalStateException )ErrorManager.getDefault().annotate( 99 new IllegalStateException (msg), 100 ErrorManager.USER, null, msg, 101 null, null 102 ); 103 throw x; 104 } 105 106 public java.util.Set instantiate(TemplateWizard wiz) throws IOException { 107 DataFolder fld = wiz.getTargetFolder(); 108 FileObject fldFO = fld.getPrimaryFile(); 109 ClassPath cp = ClassPath.getClassPath(fldFO,ClassPath.SOURCE); 110 if (cp == null) { 111 throwIllegalName("ERR_NotInSourcePath",null); } 113 String s = cp.getResourceName(fldFO,'.',false); 114 if (!Util.isValidPackageName(s)) { 115 throwIllegalName("FMTERR_InvalidPackage", s); } 117 String tn = wiz.getTargetName(); 118 if (tn == null) { 119 tn = Util.getString("TXT_DefaultPackageName"); } else if (!Util.isValidPackageName(tn)) { 122 throwIllegalName("FMTERR_InvalidPackage", tn); } 124 125 s = s + '.' + tn; 126 String pkgPath = s.replace('.', '/'); 127 128 FileObject target = cp.findResource(pkgPath); 131 if (target != null && target.isFolder()) { 132 throwIllegalName("FMTERR_pkg_already_exist", tn); } 134 135 DataObject d = DataObject.find( 136 FileUtil.createFolder(fldFO, tn.replace('.','/'))); 137 138 s = d.getName(); 140 fld = d.getFolder(); 141 142 d = d.createFromTemplate(fld, s); 144 145 return Collections.singleton(d); 146 } 147 } 148 | Popular Tags |