1 19 20 package org.netbeans.modules.apisupport.project.ui.wizard.winsys; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.Set ; 28 import org.netbeans.api.project.Project; 29 import org.netbeans.modules.apisupport.project.CreatedModifiedFiles; 30 import org.netbeans.modules.apisupport.project.Util; 31 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider; 32 import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator; 33 import org.openide.ErrorManager; 34 import org.openide.WizardDescriptor; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileStateInvalidException; 37 import org.openide.filesystems.FileSystem; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.util.NbBundle; 40 41 46 final class NewTCIterator extends BasicWizardIterator { 47 48 private NewTCIterator.DataModel data; 49 50 private NewTCIterator() { }; 51 52 public static NewTCIterator createIterator() { 53 return new NewTCIterator(); 54 } 55 56 public Set instantiate() throws IOException { 57 CreatedModifiedFiles cmf = data.getCreatedModifiedFiles(); 58 cmf.run(); 59 return getCreatedFiles(cmf, data.getProject()); 60 } 61 62 protected BasicWizardIterator.Panel[] createPanels(WizardDescriptor wiz) { 63 data = new NewTCIterator.DataModel(wiz); 64 return new BasicWizardIterator.Panel[] { 65 new BasicSettingsPanel(wiz, data), 66 new NameAndLocationPanel(wiz, data) 67 }; 68 } 69 70 public void uninitialize(WizardDescriptor wiz) { 71 super.uninitialize(wiz); 72 data = null; 73 } 74 75 static final class DataModel extends BasicWizardIterator.BasicDataModel { 76 77 private String name; 78 private String icon; 79 private String mode; 80 private boolean opened = false; 81 82 private CreatedModifiedFiles files; 83 84 DataModel(WizardDescriptor wiz) { 85 super(wiz); 86 } 87 88 public CreatedModifiedFiles getCreatedModifiedFiles() { 89 return getFiles(); 90 } 91 92 public void setCreatedModifiedFiles(CreatedModifiedFiles files) { 93 this.setFiles(files); 94 } 95 96 public String getName() { 97 return name; 98 } 99 100 public void setName(String name) { 101 this.name = name; 102 } 103 104 public CreatedModifiedFiles getFiles() { 105 return files; 106 } 107 108 public void setFiles(CreatedModifiedFiles files) { 109 this.files = files; 110 } 111 112 public String getIcon() { 113 return icon; 114 } 115 116 public void setIcon(String icon) { 117 this.icon = icon; 118 } 119 120 public String getMode() { 121 return mode; 122 } 123 124 public void setMode(String mode) { 125 this.mode = mode; 126 } 127 128 public boolean isOpened() { 129 return opened; 130 } 131 132 public void setOpened(boolean opened) { 133 this.opened = opened; 134 } 135 136 } 137 138 public static void generateFileChanges(DataModel model) { 139 CreatedModifiedFiles fileChanges = new CreatedModifiedFiles(model.getProject()); 140 Project project = model.getProject(); 141 NbModuleProvider moduleInfo = model.getModuleInfo(); 142 final String name = model.getName(); 143 final String packageName = model.getPackageName(); 144 final String mode = model.getMode(); 145 146 HashMap replaceTokens = new HashMap (); 147 replaceTokens.put("@@TEMPLATENAME@@", name); replaceTokens.put("@@PACKAGENAME@@", packageName); replaceTokens.put("@@MODE@@", mode); replaceTokens.put("@@OPENED@@", model.isOpened() ? "true" : "false"); 152 String icon = model.getIcon(); 154 File fil = null; 155 if (icon != null) { 156 fil = new File (icon); 157 if (!fil.exists()) { 158 fil = null; 159 } 160 } 161 if (fil != null) { 162 FileObject fo = FileUtil.toFileObject(fil); 163 String relativeIconPath = null; 164 if (!FileUtil.isParentOf(Util.getResourceDirectory(project), fo)) { 165 String iconPath = getRelativePath(moduleInfo.getResourceDirectoryPath(false), packageName, 166 "", fo.getNameExt()); try { 168 fileChanges.add(fileChanges.createFile(iconPath, fo.getURL())); 169 relativeIconPath = packageName.replace('.', '/') + "/" + fo.getNameExt(); } catch (FileStateInvalidException exc) { 171 ErrorManager.getDefault().notify(exc); 172 } 173 } else { 174 relativeIconPath = FileUtil.getRelativePath(Util.getResourceDirectory(project), fo); 175 } 176 replaceTokens.put("@@ICONPATH@@", relativeIconPath); replaceTokens.put("@@COMMENTICON@@", ""); 179 } else { 180 replaceTokens.put("@@ICONPATH@@", "SET/PATH/TO/ICON/HERE"); replaceTokens.put("@@COMMENTICON@@", "//"); } 183 184 185 replaceTokens.put("@@MODULENAME@@", moduleInfo.getCodeNameBase()); replaceTokens.put("@@SPECVERSION@@", moduleInfo.getSpecVersion()); fileChanges.add(fileChanges.addModuleDependency("org.openide.windows")); fileChanges.add(fileChanges.addModuleDependency("org.openide.util")); fileChanges.add(fileChanges.addModuleDependency("org.openide.awt")); 193 final String tcName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName, 195 name, "TopComponent.java"); URL template = NewTCIterator.class.getResource("templateTopComponent.javx"); fileChanges.add(fileChanges.createFileWithSubstitutions(tcName, template, replaceTokens)); 199 final String tcFormName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName, 201 name, "TopComponent.form"); template = NewTCIterator.class.getResource("templateTopComponent.frmx"); fileChanges.add(fileChanges.createFileWithSubstitutions(tcFormName, template, replaceTokens)); 205 206 final String actionName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName, 207 name, "Action.java"); template = NewTCIterator.class.getResource("templateAction.javx"); fileChanges.add(fileChanges.createFileWithSubstitutions(actionName, template, replaceTokens)); 211 212 final String settingsName = name + "TopComponent.settings"; template = NewTCIterator.class.getResource("templateSettings.xml"); fileChanges.add(fileChanges.createLayerEntry("Windows2/Components/" + settingsName, template, replaceTokens, null, null)); 217 final String wstcrefName = name + "TopComponent.wstcref"; template = NewTCIterator.class.getResource("templateWstcref.xml"); fileChanges.add(fileChanges.createLayerEntry("Windows2/Modes/" + mode + "/" + wstcrefName, template, replaceTokens, null, null)); 222 223 fileChanges.add(fileChanges.layerModifications(new CreateActionEntryOperation(name + "Action", packageName), Collections.EMPTY_SET)); 225 String bundlePath = getRelativePath(moduleInfo.getResourceDirectoryPath(false), packageName, "", "Bundle.properties"); fileChanges.add(fileChanges.bundleKey(bundlePath, "CTL_" + name + "Action", NbBundle.getMessage(NewTCIterator.class, "LBL_TemplateActionName", name))); 229 fileChanges.add(fileChanges.bundleKey(bundlePath, "CTL_" + name + "TopComponent", NbBundle.getMessage(NewTCIterator.class, "LBL_TemplateTCName", name))); fileChanges.add(fileChanges.bundleKey(bundlePath, "HINT_" + name + "TopComponent", NbBundle.getMessage(NewTCIterator.class, "HINT_TemplateTCName", name))); 234 model.setCreatedModifiedFiles(fileChanges); 235 } 236 237 private static String getRelativePath(String rootpath, String fullyQualifiedPackageName, 238 String prefix, String postfix) { 239 StringBuffer sb = new StringBuffer (); 240 241 sb.append(rootpath).append('/').append(fullyQualifiedPackageName.replace('.','/')) 242 .append('/').append(prefix).append(postfix); 243 244 return sb.toString(); 245 } 246 247 static class CreateActionEntryOperation implements CreatedModifiedFiles.LayerOperation { 248 private String name; 249 private String packageName; 250 251 public CreateActionEntryOperation(String actionname, String packageName) { 252 this.packageName = packageName; 253 this.name = actionname; 254 } 255 256 public void run(FileSystem layer) throws IOException { 257 FileObject folder = layer.getRoot().getFileObject("Actions/Window"); if (folder == null) { 259 folder = FileUtil.createFolder(layer.getRoot(), "Actions/Window"); } 261 String instance = packageName.replace('.','-') + "-" + name; FileObject file = folder.createData(instance, "instance"); folder = layer.getRoot().getFileObject("Menu/Window"); if (folder == null) { 265 folder = FileUtil.createFolder(layer.getRoot(), "Menu/Window"); } 267 file = folder.createData(name, "shadow"); file.setAttribute("originalFile", "Actions/Window/" + instance + ".instance"); } 270 } 271 272 } 273 | Popular Tags |