1 19 20 package org.netbeans.modules.apisupport.project.ui.wizard; 21 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.Collection ; 26 import java.util.HashSet ; 27 import java.util.NoSuchElementException ; 28 import java.util.Set ; 29 import javax.swing.event.ChangeListener ; 30 import javax.swing.text.Position ; 31 import org.netbeans.api.java.project.JavaProjectConstants; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.api.project.ProjectUtils; 34 import org.netbeans.api.project.SourceGroup; 35 import org.netbeans.api.project.Sources; 36 import org.netbeans.editor.BaseDocument; 37 import org.netbeans.editor.GuardedDocument; 38 import org.netbeans.modules.apisupport.project.CreatedModifiedFiles; 39 import org.netbeans.modules.apisupport.project.NbModuleProject; 40 import org.netbeans.modules.apisupport.project.Util; 41 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider; 42 import org.netbeans.spi.project.ui.templates.support.Templates; 43 import org.openide.ErrorManager; 44 import org.openide.WizardDescriptor; 45 import org.openide.cookies.EditorCookie; 46 import org.openide.filesystems.FileObject; 47 import org.openide.filesystems.FileStateInvalidException; 48 import org.openide.filesystems.FileUtil; 49 import org.openide.loaders.DataObject; 50 import org.openide.loaders.DataObjectNotFoundException; 51 import org.openide.util.HelpCtx; 52 import org.openide.util.NbBundle; 53 54 59 public abstract class BasicWizardIterator implements WizardDescriptor.AsynchronousInstantiatingIterator { 60 61 private int position = 0; 62 private BasicWizardIterator.PrivateWizardPanel[] wizardPanels; 63 64 65 protected BasicWizardIterator() {} 66 67 68 protected abstract BasicWizardIterator.Panel[] createPanels(WizardDescriptor wiz); 69 70 71 public abstract static class Panel extends BasicVisualPanel { 72 73 protected Panel(WizardDescriptor wiz) { 74 super(wiz); 75 } 76 77 82 protected abstract String getPanelName(); 83 84 90 protected abstract void storeToDataModel(); 91 92 98 protected abstract void readFromDataModel(); 99 100 protected abstract HelpCtx getHelp(); 101 102 } 103 104 105 public static class BasicDataModel { 106 107 private Project project; 108 private NbModuleProvider module; 109 private SourceGroup sourceRootGroup; 110 private String packageName; 111 112 113 public BasicDataModel(WizardDescriptor wiz) { 114 Project tmpProject = Templates.getProject(wiz); 115 116 if (tmpProject == null) { 117 throw new IllegalArgumentException (); 118 } 119 module = tmpProject.getLookup().lookup(NbModuleProvider.class); 120 if (module == null) { 121 throw new IllegalArgumentException (tmpProject.getClass().toString()); 122 } 123 124 project = tmpProject; 125 FileObject fo = Templates.getTargetFolder(wiz); 127 if (fo != null) { 128 Sources srcs = ProjectUtils.getSources(project); SourceGroup[] grps = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 130 for (int i = 0; i < grps.length; i++) { 131 if (FileUtil.isParentOf(grps[i].getRootFolder(), fo)) { 132 String relPath = FileUtil.getRelativePath(grps[i].getRootFolder(), fo); 133 relPath = relPath.replace('/', '.'); 134 setPackageName(relPath); 135 break; 136 } 137 } 138 } 139 } 140 141 public Project getProject() { 142 return project; 143 } 144 145 public NbModuleProvider getModuleInfo() { 146 return module; 147 } 148 149 public String getPackageName() { 150 return packageName; 151 } 152 153 public void setPackageName(String packageName) { 154 this.packageName = packageName; 155 } 156 157 public SourceGroup getSourceRootGroup() { 158 if (sourceRootGroup == null) { 159 FileObject tempSrcRoot = getModuleInfo().getSourceDirectory(); 160 assert tempSrcRoot != null; 161 162 Sources sources = ProjectUtils.getSources(project); 163 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 164 for (int i = 0; sourceRootGroup == null && i < groups.length; i++) { 165 if (groups[i].getRootFolder().equals(tempSrcRoot)) { 166 sourceRootGroup = groups[i]; 167 } 168 } 169 } 170 return sourceRootGroup; 171 } 172 173 public String getDefaultPackagePath(String fileName, boolean resource) { 174 return (resource ? getModuleInfo().getResourceDirectoryPath(false) : getModuleInfo().getSourceDirectoryPath()) + '/' + 175 getPackageName().replace('.','/') + '/' + fileName; 176 } 177 178 187 public String addCreateIconOperation(CreatedModifiedFiles cmf, String origIconPath) { 188 FileObject origIconFO = FileUtil.toFileObject(new File (origIconPath)); 189 String relativeIconPath = null; 190 if (!FileUtil.isParentOf(Util.getResourceDirectory(getProject()), origIconFO)) { 191 String iconPath = getDefaultPackagePath(origIconFO.getNameExt(), true); 192 try { 193 cmf.add(cmf.createFile(iconPath, origIconFO.getURL())); 194 relativeIconPath = getPackageName().replace('.', '/') + '/' + origIconFO.getNameExt(); 195 } catch (FileStateInvalidException exc) { 196 Util.err.notify(exc); 197 } 198 } else { 199 relativeIconPath = FileUtil.getRelativePath(Util.getResourceDirectory(getProject()), origIconFO); 200 } 201 return relativeIconPath; 202 } 203 204 } 205 206 public void initialize(WizardDescriptor wiz) { 207 String [] beforeSteps = null; 209 Object prop = wiz.getProperty("WizardPanel_contentData"); if (prop != null && prop instanceof String []) { 211 beforeSteps = (String [])prop; 212 } 213 position = 0; 214 BasicWizardIterator.Panel[] panels = createPanels(wiz); 215 String [] steps = BasicWizardIterator.createSteps(beforeSteps, panels); 216 wizardPanels = new BasicWizardIterator.PrivateWizardPanel[panels.length]; 217 218 for (int i = 0; i < panels.length; i++) { 219 wizardPanels[i] = new BasicWizardIterator.PrivateWizardPanel(panels[i], steps, i); 220 } 221 } 222 223 private static String [] createSteps(String [] before, BasicWizardIterator.Panel[] panels) { 225 assert panels != null; 226 int diff = 0; 228 if (before == null) { 229 before = new String [0]; 230 } else if (before.length > 0) { 231 diff = ("...".equals(before[before.length - 1])) ? 1 : 0; } 233 String [] res = new String [ (before.length - diff) + panels.length]; 234 for (int i = 0; i < res.length; i++) { 235 if (i < (before.length - diff)) { 236 res[i] = before[i]; 237 } else { 238 res[i] = panels[i - before.length + diff].getPanelName(); 239 } 240 } 241 return res; 242 } 243 244 public void uninitialize(WizardDescriptor wiz) { 245 wizardPanels = null; 246 } 247 248 public String name() { 249 return ((BasicWizardIterator.PrivateWizardPanel) 250 current()).getPanel().getPanelName(); 251 } 252 253 public boolean hasNext() { 254 return position < (wizardPanels.length - 1); 255 } 256 257 public boolean hasPrevious() { 258 return position > 0; 259 } 260 261 public void nextPanel() { 262 if (!hasNext()) { 263 throw new NoSuchElementException (); 264 } 265 position++; 266 } 267 268 public void previousPanel() { 269 if (!hasPrevious()) { 270 throw new NoSuchElementException (); 271 } 272 position--; 273 } 274 275 public WizardDescriptor.Panel current() { 276 return wizardPanels[position]; 277 } 278 279 282 protected final String getMessage(String key) { 283 return NbBundle.getMessage(getClass(), key); 284 } 285 286 public final void addChangeListener(ChangeListener l) {} 287 public final void removeChangeListener(ChangeListener l) {} 288 289 protected Set getCreatedFiles(final CreatedModifiedFiles cmf, final Project project) throws IOException { 290 Collection toBeShown = new HashSet (); 291 String [] paths = cmf.getCreatedPaths(); 292 Set set = new HashSet (); 293 for (int i = 0; i < paths.length; i++) { 294 FileObject fo = project.getProjectDirectory().getFileObject(paths[i]); 295 formatFile(fo); 296 DataObject dObj = DataObject.find(fo); 297 if (dObj != null && toBeShown.size() < 10 && toBeShown.add(dObj)) { 298 set.add(fo); 299 } 300 } 301 return set; 302 } 303 304 private static BaseDocument getDocument(final FileObject fo) throws DataObjectNotFoundException, IOException { 305 BaseDocument doc = null; 306 DataObject dObj = DataObject.find(fo); 307 if (dObj != null) { 308 EditorCookie editor = (EditorCookie) dObj.getCookie(EditorCookie.class); 309 if (editor != null) { 310 doc = (BaseDocument) editor.openDocument(); 311 } 312 } 313 return doc; 314 } 315 316 private static void formatFile(final FileObject fo) { 318 assert fo != null; 319 BaseDocument doc = null; 320 boolean saved = false; 321 try { 322 doc = BasicWizardIterator.getDocument(fo); 323 if (doc == null) { 324 return; 325 } 326 GuardedDocument gdoc = (doc instanceof GuardedDocument) ? (GuardedDocument) doc : null; 327 doc.atomicLock(); 328 int startPos = 0; 329 Position endPosition = doc.createPosition(doc.getLength()); 330 int pos = startPos; 331 if (gdoc != null) { 332 pos = gdoc.getGuardedBlockChain().adjustToBlockEnd(pos); 333 } 334 335 while (pos < endPosition.getOffset()) { 336 int stopPos = endPosition.getOffset(); 337 if (gdoc != null) { stopPos = gdoc.getGuardedBlockChain().adjustToNextBlockStart(pos); 339 if (stopPos == -1) { 340 stopPos = endPosition.getOffset(); 341 } 342 } 343 int reformattedLen = doc.getFormatter().reformat(doc, pos, stopPos); 344 pos = pos + reformattedLen; 345 if (gdoc != null) { pos = gdoc.getGuardedBlockChain().adjustToBlockEnd(pos); 347 } 348 } 349 saved = true; 350 } catch (Exception ex) { 351 ErrorManager.getDefault().log(ErrorManager.WARNING, "Cannot reformat the file: " + fo.getPath()); } finally { 354 if (doc != null) { 355 doc.atomicUnlock(); 356 if (saved) { 357 try { 358 ((EditorCookie) DataObject.find(fo).getCookie(EditorCookie.class)).saveDocument(); 359 } catch (IOException e) { 360 Util.err.notify(ErrorManager.INFORMATIONAL, e); 361 } 362 } 363 } 364 } 365 } 366 367 private static final class PrivateWizardPanel extends BasicWizardPanel { 368 369 private BasicWizardIterator.Panel panel; 370 371 PrivateWizardPanel(BasicWizardIterator.Panel panel, String [] allSteps, int stepIndex) { 372 super(panel.getSettings()); 373 panel.addPropertyChangeListener(this); 374 panel.setName(panel.getPanelName()); this.panel = panel; 376 panel.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (stepIndex)); panel.putClientProperty("WizardPanel_contentData", allSteps); } 380 381 private BasicWizardIterator.Panel getPanel() { 382 return panel; 383 } 384 385 public Component getComponent() { 386 return getPanel(); 387 } 388 389 public void storeSettings(Object settings) { 390 WizardDescriptor wiz = (WizardDescriptor) settings; 391 if (WizardDescriptor.NEXT_OPTION.equals(wiz.getValue()) || 392 WizardDescriptor.FINISH_OPTION.equals(wiz.getValue())) { 393 panel.storeToDataModel(); 394 } 395 ((WizardDescriptor) settings).putProperty("NewFileWizard_Title", null); } 398 399 public void readSettings(Object settings) { 400 WizardDescriptor wiz = (WizardDescriptor) settings; 401 Object substitute = getPanel().getClientProperty("NewFileWizard_Title"); if (substitute != null) { 406 wiz.putProperty("NewFileWizard_Title", substitute); } 408 409 if (WizardDescriptor.NEXT_OPTION.equals(wiz.getValue()) || wiz.getValue() == null) { 410 panel.readFromDataModel(); 411 } 412 } 413 414 public HelpCtx getHelp() { 415 return getPanel().getHelp(); 416 } 417 418 } 419 420 } 421 422 | Popular Tags |