1 19 20 package org.netbeans.modules.java.ui.wizard; 21 22 import java.io.IOException ; 23 import java.text.MessageFormat ; 24 import java.util.NoSuchElementException ; 25 26 import java.awt.Component ; 27 import java.awt.event.ActionEvent ; 28 import javax.swing.SwingUtilities ; 29 import javax.swing.event.ChangeListener ; 30 31 import org.openide.ErrorManager; 32 import org.openide.WizardDescriptor; 33 import org.openide.filesystems.FileObject; 34 import org.openide.loaders.DataObject; 35 import org.openide.loaders.DataFolder; 36 import org.openide.loaders.TemplateWizard; 37 import org.openide.util.Utilities; 38 import org.openide.util.NbBundle; 39 40 import org.openide.src.Type; 41 42 import org.netbeans.modules.java.JavaDataLoader; 43 import org.netbeans.api.java.classpath.ClassPath; 44 45 public class JavaWizardIterator implements TemplateWizard.Iterator { 46 private static final long serialVersionUID = -1987345873459L; 47 48 50 private transient WizardDescriptor.Panel[] panels; 51 52 55 private static String [] panelNames; 56 57 59 private static JavaWizardIterator instance; 60 61 63 private transient int panelIndex = 0; 64 65 68 private transient TemplateWizard wizardInstance; 69 70 public JavaWizardIterator() { 71 } 72 73 76 public static synchronized JavaWizardIterator singleton() { 77 if (instance == null) { 78 instance = new JavaWizardIterator(); 79 } 80 return instance; 81 } 82 84 92 public java.util.Set instantiate(TemplateWizard wiz) throws IOException , IllegalArgumentException { 93 DataObject obj = instantiateTemplate(wiz.getTemplate(), wiz.getTargetFolder(), wiz.getTargetName()); 94 95 final org.openide.nodes.Node node = obj.getNodeDelegate (); 97 final org.openide.util.actions.SystemAction sa = node.getDefaultAction (); 98 if (sa != null) { 99 SwingUtilities.invokeLater(new Runnable () { 100 public void run () { 101 sa.actionPerformed (new ActionEvent (node, ActionEvent.ACTION_PERFORMED, "")); } 103 }); 104 } 105 return java.util.Collections.singleton(obj); 106 } 107 108 public WizardDescriptor.Panel current() { 109 return panels[panelIndex]; 110 } 111 112 public String name() { 113 return ""; } 115 116 public boolean hasNext() { 117 return false; 118 } 119 120 public boolean hasPrevious() { 121 return false; 122 } 123 124 public void nextPanel() { 125 throw new NoSuchElementException (); 126 } 127 128 public void previousPanel() { 129 throw new NoSuchElementException (); 130 } 131 132 136 public void addChangeListener(ChangeListener l) { 137 } 138 139 142 public void removeChangeListener(ChangeListener l) { 143 } 144 145 public void initialize(TemplateWizard wizard) { 146 this.wizardInstance = wizard; 147 148 if (panels == null) { 149 150 Component panel = wizard.targetChooser().getComponent(); 151 panelNames = new String []{panel.getName()}; 152 if (panel instanceof javax.swing.JComponent ) { 153 ((javax.swing.JComponent )panel).putClientProperty( 154 "WizardPanel_contentData", panelNames); } 156 panels = new WizardDescriptor.Panel[] { 157 wizardInstance.targetChooser() 158 }; 159 } 160 } 161 162 public void uninitialize(TemplateWizard wiz) { 163 panels = null; 164 wizardInstance = null; 165 } 166 167 169 private Object readResolve() { 170 return singleton(); 171 } 172 173 176 private DataObject instantiateTemplate(DataObject tpl, DataFolder target, String name) throws IOException { 177 if (name == null) { 178 name = getDefaultName(tpl, target); 179 } 180 181 checkValidPackageName(target); 182 checkTargetName(target, name); 183 return tpl.createFromTemplate(target, name); 184 } 185 186 private boolean isValidPackageName(String s) { 187 if ("".equals(s)) return true; 190 try { 191 Type t = Type.parse(s); 192 return true; 193 } catch (IllegalArgumentException ex) { 194 return false; 195 } 196 } 197 198 private void checkValidPackageName(DataFolder targetFolder) 199 throws IllegalStateException { 200 FileObject folder = targetFolder.getPrimaryFile(); 201 ClassPath cp = ClassPath.getClassPath(folder,ClassPath.SOURCE); 202 String msg = null; 203 if (cp != null) { 204 String fullTarget = cp.getResourceName(folder, '.',false); 205 if (isValidPackageName (fullTarget)) { 206 return; 207 } 208 else { 209 msg = MessageFormat.format(getString("FMTERR_IllegalFolderName"), new Object [] { 211 folder.getPath(), 212 fullTarget}); 213 } 214 } 215 else { 216 msg = getString ("ERR_NotInSourcePath"); 217 } 218 throw (IllegalStateException )ErrorManager.getDefault().annotate( 221 new IllegalStateException (msg), 222 ErrorManager.USER, null, msg, 223 null, null); 224 } 225 226 231 private boolean checkTargetName(DataFolder folder, String desiredName) { 232 if (!Utilities.isJavaIdentifier(desiredName)) { 233 String msg = MessageFormat.format(getString("FMTERR_IllegalTargetName"), new Object [] { 235 desiredName 236 }); 237 notifyError(msg); 238 return false; 239 } 240 241 FileObject f = folder.getPrimaryFile(); 242 if (f.getFileObject(desiredName, JavaDataLoader.JAVA_EXTENSION) != null) { 244 String msg = MessageFormat.format(getString("FMTERR_TargetExists"), new Object [] { 246 desiredName 247 }); 248 notifyError(msg); 249 return false; 250 } 251 return true; 252 } 253 254 private void notifyError(String msg) { 255 this.wizardInstance.putProperty("WizardPanel_errorMessage", msg); IllegalStateException ex = new IllegalStateException (msg); 257 ErrorManager.getDefault().annotate(ex, ErrorManager.USER, null, msg, null, null); 258 throw ex; 259 } 260 261 private String getDefaultName(DataObject template, DataFolder targetFolder) { 262 String desiredName = org.openide.filesystems.FileUtil.findFreeFileName(targetFolder.getPrimaryFile(), 263 template.getName(), JavaDataLoader.JAVA_EXTENSION); 264 return desiredName; 265 } 266 267 static String getString(String key) { 268 return NbBundle.getMessage(JavaWizardIterator.class, key); 269 } 270 271 static char getMnemonic(String key) { 272 return getString(key).charAt(0); 273 } 274 } 275 | Popular Tags |