1 19 20 package org.netbeans.modules.apisupport.project.ui.wizard.loader; 21 22 import java.io.CharConversionException ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Set ; 34 import java.util.StringTokenizer ; 35 import java.util.logging.Level ; 36 import java.util.logging.Logger ; 37 import java.util.regex.Pattern ; 38 import org.netbeans.modules.apisupport.project.CreatedModifiedFiles; 39 import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator; 40 import org.netbeans.modules.apisupport.project.universe.ModuleEntry; 41 import org.netbeans.modules.apisupport.project.universe.ModuleList; 42 import org.openide.WizardDescriptor; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileSystem; 45 import org.openide.filesystems.FileUtil; 46 import org.openide.loaders.DataFolder; 47 import org.openide.loaders.DataObject; 48 import org.openide.modules.SpecificationVersion; 49 import org.openide.util.NbBundle; 50 import org.openide.xml.XMLUtil; 51 52 57 final class NewLoaderIterator extends BasicWizardIterator { 58 59 private NewLoaderIterator.DataModel data; 60 61 private NewLoaderIterator() { }; 62 63 public static NewLoaderIterator createIterator() { 64 return new NewLoaderIterator(); 65 } 66 67 public Set instantiate() throws IOException { 68 CreatedModifiedFiles cmf = data.getCreatedModifiedFiles(); 69 cmf.run(); 70 return getCreatedFiles(cmf, data.getProject()); 71 } 72 73 protected BasicWizardIterator.Panel[] createPanels(WizardDescriptor wiz) { 74 data = new NewLoaderIterator.DataModel(wiz); 75 return new BasicWizardIterator.Panel[] { 76 new FileRecognitionPanel(wiz, data), 77 new NameAndLocationPanel(wiz, data) 78 }; 79 } 80 81 public void uninitialize(WizardDescriptor wiz) { 82 super.uninitialize(wiz); 83 data = null; 84 } 85 86 87 static final class DataModel extends BasicWizardIterator.BasicDataModel { 88 89 private String prefix; 90 private String iconPath; 91 private String mimeType; 92 private boolean extensionBased = true; 93 private String extension; 94 private String namespace; 95 96 private CreatedModifiedFiles files; 97 98 DataModel(WizardDescriptor wiz) { 99 super(wiz); 100 } 101 102 public CreatedModifiedFiles getCreatedModifiedFiles() { 103 return files; 104 } 105 106 public void setCreatedModifiedFiles(CreatedModifiedFiles files) { 107 this.files = files; 108 } 109 110 public String getPrefix() { 111 return prefix; 112 } 113 114 public void setPrefix(String prefix) { 115 this.prefix = prefix; 116 } 117 118 public String getIconPath() { 119 return iconPath; 120 } 121 122 public void setIconPath(String iconPath) { 123 this.iconPath = iconPath; 124 } 125 126 public String getMimeType() { 127 return mimeType; 128 } 129 130 public void setMimeType(String mimeType) { 131 this.mimeType = mimeType; 132 } 133 134 public boolean isExtensionBased() { 135 return extensionBased; 136 } 137 138 public void setExtensionBased(boolean extensionBased) { 139 this.extensionBased = extensionBased; 140 } 141 142 public String getExtension() { 143 return extension; 144 } 145 146 public void setExtension(String extension) { 147 this.extension = extension; 148 } 149 150 public String getNamespace() { 151 return namespace; 152 } 153 154 public void setNamespace(String namespace) { 155 this.namespace = namespace; 156 } 157 158 } 159 160 public static void generateFileChanges(DataModel model) { 161 CreatedModifiedFiles fileChanges = new CreatedModifiedFiles(model.getProject()); 162 163 String namePrefix = model.getPrefix(); 164 String packageName = model.getPackageName(); 165 final String mime = model.getMimeType(); 166 HashMap replaceTokens = new HashMap (); 167 replaceTokens.put("@@PREFIX@@", namePrefix); replaceTokens.put("@@PACKAGENAME@@", packageName); replaceTokens.put("@@MIMETYPE@@", mime); replaceTokens.put("@@EXTENSIONS@@", formatExtensions(model.isExtensionBased(), model.getExtension(), mime)); replaceTokens.put("@@NAMESPACES@@", formatNameSpace(model.isExtensionBased(), model.getNamespace(), mime)); 173 String origIconPath = model.getIconPath(); 175 if (origIconPath != null && new File (origIconPath).exists()) { 176 String relativeIconPath = model.addCreateIconOperation(fileChanges, origIconPath); 177 replaceTokens.put("@@IMAGESNIPPET@@", formatImageSnippet(relativeIconPath)); replaceTokens.put("@@ICONPATH@@", relativeIconPath); replaceTokens.put("@@COMMENTICON@@", ""); 181 } else { 182 replaceTokens.put("@@IMAGESNIPPET@@", formatImageSnippet(null)); replaceTokens.put("@@ICONPATH@@", "SET/PATH/TO/ICON/HERE"); replaceTokens.put("@@COMMENTICON@@", "//"); } 186 187 String loaderName = model.getDefaultPackagePath(namePrefix + "DataLoader.java", false); URL template = NewLoaderIterator.class.getResource("templateDataLoader.javx"); fileChanges.add(fileChanges.createFileWithSubstitutions(loaderName, template, replaceTokens)); 192 String loaderInfoName = model.getDefaultPackagePath(namePrefix + "DataLoaderBeanInfo.java", false); template = NewLoaderIterator.class.getResource("templateDataLoaderBeanInfo.javx"); fileChanges.add(fileChanges.createFileWithSubstitutions(loaderInfoName, template, replaceTokens)); 195 196 final boolean isEditable = Pattern.matches("(application/([a-zA-Z0-9_.-])*\\+xml|text/([a-zA-Z0-9_.+-])*)", mime); 199 if (isEditable) { 200 StringBuffer editorBuf = new StringBuffer (); 201 editorBuf.append(" CookieSet cookies = getCookieSet();\n"); editorBuf.append(" cookies.add((Node.Cookie) DataEditorSupport.create(this, getPrimaryEntry(), cookies));"); replaceTokens.put("@@EDITOR_SUPPORT_SNIPPET@@", editorBuf.toString()); replaceTokens.put("@@EDITOR_SUPPORT_IMPORT@@", "import org.openide.text.DataEditorSupport;"); } else { 206 replaceTokens.put("@@EDITOR_SUPPORT_SNIPPET@@", ""); replaceTokens.put("@@EDITOR_SUPPORT_IMPORT@@", ""); } 210 211 String doName = model.getDefaultPackagePath(namePrefix + "DataObject.java", false); template = null; 213 try { 214 SpecificationVersion current = model.getModuleInfo().getDependencyVersion("org.openide.loaders"); 215 SpecificationVersion desired = new SpecificationVersion("6.0"); if (current.compareTo(desired) >= 0) { 217 template = NewLoaderIterator.class.getResource("templateDataObjectWithLookup.javx"); } 219 } catch (IOException ex) { 220 Logger.getLogger(NewLoaderIterator.class.getName()).log(Level.INFO, null, ex); 221 } 222 if (template == null) { 223 template = NewLoaderIterator.class.getResource("templateDataObject.javx"); } 225 fileChanges.add(fileChanges.createFileWithSubstitutions(doName, template, replaceTokens)); 226 227 String nodeName = model.getDefaultPackagePath(namePrefix + "DataNode.java", false); template = NewLoaderIterator.class.getResource("templateDataNode.javx"); fileChanges.add(fileChanges.createFileWithSubstitutions(nodeName, template, replaceTokens)); 231 232 template = NewLoaderIterator.class.getResource("templateresolver.xml"); fileChanges.add(fileChanges.createLayerEntry("Services/MIMEResolver/" + namePrefix + "Resolver.xml", template, 236 replaceTokens, 237 NbBundle.getMessage(NewLoaderIterator.class, "LBL_LoaderName", namePrefix), null)); 239 240 fileChanges.add(fileChanges.addModuleDependency("org.openide.filesystems")); fileChanges.add(fileChanges.addModuleDependency("org.openide.loaders")); fileChanges.add(fileChanges.addModuleDependency("org.openide.nodes")); fileChanges.add(fileChanges.addModuleDependency("org.openide.util")); if (isEditable) { 246 fileChanges.add(fileChanges.addModuleDependency("org.openide.text")); } 248 if (isEditable) { 249 fileChanges.add(fileChanges.addModuleDependency("org.openide.windows")); } 251 252 String bundlePath = model.getDefaultPackagePath("Bundle.properties", true); fileChanges.add(fileChanges.bundleKey(bundlePath, "LBL_" + namePrefix + "_loader_name", NbBundle.getMessage(NewLoaderIterator.class, "LBL_LoaderName", namePrefix))); 257 boolean isXml = Pattern.matches("(application/([a-zA-Z0-9_.-])*\\+xml|text/([a-zA-Z0-9_.-])*\\+xml)", mime); 260 String installBefore = null; 261 if (isXml) { 262 installBefore = "org.openide.loaders.XMLDataObject, org.netbeans.modules.xml.core.XMLDataObject"; } 264 265 fileChanges.add(fileChanges.addLoaderSection(packageName.replace('.', '/') + "/" + namePrefix + "DataLoader", installBefore)); 267 fileChanges.add(fileChanges.addLookupRegistration("org.openide.loaders.DataLoader", packageName + '.' + namePrefix + "DataLoader", true)); 270 272 fileChanges.add(fileChanges.layerModifications(new CreatedModifiedFiles.LayerOperation() { 273 public void run(FileSystem layer) throws IOException { 274 List <String > actions = new ArrayList (); 275 if (isEditable) { 276 actions.add("org.openide.actions.OpenAction"); } 278 actions.addAll(Arrays.asList(new String [] { 279 "org.openide.actions.FileSystemAction", null, 281 "org.openide.actions.CutAction", "org.openide.actions.CopyAction", null, 284 "org.openide.actions.DeleteAction", "org.openide.actions.RenameAction", null, 287 "org.openide.actions.SaveAsTemplateAction", null, 289 "org.openide.actions.ToolsAction", "org.openide.actions.PropertiesAction", })); 292 FileObject folder = FileUtil.createFolder(layer.getRoot(), "Loaders/" + mime + "/Actions"); List <DataObject> kids = new ArrayList (); 294 Iterator it = actions.iterator(); 295 int i = 0; 296 while (it.hasNext()) { 297 String name = (String ) it.next(); 298 FileObject kid; 299 if (name != null) { 300 kid = folder.createData(name.replace('.', '-') + ".instance"); } else { 302 kid = folder.createData("sep-" + (++i) + ".instance"); kid.setAttribute("instanceClass", "javax.swing.JSeparator"); } 305 kids.add(DataObject.find(kid)); 306 } 307 DataFolder.findFolder(folder).setOrder((DataObject[]) kids.toArray(new DataObject[kids.size()])); 308 } 309 }, Collections.EMPTY_SET)); 310 311 String suffix = null; 313 if (model.isExtensionBased()) { 314 suffix = "Template." + getFirstExtension(model.getExtension()); template = NewLoaderIterator.class.getResource("templateNew1"); } else { 317 template = NewLoaderIterator.class.getResource("templateNew2"); suffix = "Template.xml"; try { 320 replaceTokens.put("@@NAMESPACE@@", XMLUtil.toElementContent(model.getNamespace())); } catch (CharConversionException ex) { 322 assert false: ex; 323 } 324 } 325 Map attrs = new HashMap (); 326 attrs.put("template", Boolean.TRUE); fileChanges.add(fileChanges.createLayerEntry("Templates/Other/" + namePrefix + suffix, template, 329 replaceTokens, 330 NbBundle.getMessage(NewLoaderIterator.class, "LBL_fileTemplateName", namePrefix), 331 attrs)); model.setCreatedModifiedFiles(fileChanges); 333 } 334 335 private static String formatExtensions(boolean isExtensionBased, String ext, String mime) { 336 if (!isExtensionBased) { 337 return ""; 338 } 339 StringBuffer buff = new StringBuffer (); 340 StringTokenizer tokens = new StringTokenizer (ext, " ,"); while (tokens.hasMoreTokens()) { 342 String element = tokens.nextToken().trim(); 343 if (element.startsWith(".")) { element = element.substring(1); 345 } 346 buff.append(" <ext name=\"").append(element).append("\"/>\n"); } 348 buff.append(" <resolver mime=\"").append(mime).append("\"/>"); return buff.toString(); 350 } 351 352 private static String getFirstExtension(String ext) { 353 StringTokenizer tokens = new StringTokenizer (ext," ,"); String element = "someextension"; if (tokens.hasMoreTokens()) { 356 element = tokens.nextToken().trim(); 357 if (element.startsWith(".")) { element = element.substring(1); 359 } 360 } 361 return element; 362 } 363 364 private static String formatNameSpace(boolean isExtensionBased, String namespace, String mime) { 365 if (isExtensionBased) { 366 return ""; 367 } 368 StringBuffer buff = new StringBuffer (); 369 buff.append(" <ext name=\"xml\"/>\n"); buff.append(" <resolver mime=\"").append(mime).append("\">\n"); buff.append(" <xml-rule>\n"); try { 373 buff.append(" <element ns=\"").append(XMLUtil.toElementContent(namespace)).append("\"/>\n"); } catch (CharConversionException ex) { 375 assert false : ex; 376 } 377 buff.append(" </xml-rule>\n"); buff.append(" </resolver>"); return buff.toString(); 380 } 381 382 private static String formatImageSnippet(String path) { 383 if (path == null) { 384 return "return super.getIcon(type); // TODO add a custom icon here\n"; } 387 StringBuffer buff = new StringBuffer (); 388 buff.append(" if (type == BeanInfo.ICON_COLOR_16x16 || type == BeanInfo.ICON_MONO_16x16) {\n"); buff.append(" return Utilities.loadImage(\""); buff.append(path).append("\");\n"); buff.append(" } else {\n"); buff.append(" return null;\n }\n"); return buff.toString(); 394 } 395 396 } 397 398 | Popular Tags |