1 19 20 package org.netbeans.modules.apisupport.project.ui.wizard.options; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.util.Arrays ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 import java.util.Locale ; 29 import java.util.Map ; 30 import java.util.Set ; 31 import org.netbeans.modules.apisupport.project.CreatedModifiedFiles; 32 import org.netbeans.modules.apisupport.project.ManifestManager; 33 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider; 34 import org.netbeans.modules.apisupport.project.ui.UIUtil; 35 import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator; 36 import org.openide.util.Utilities; 37 import org.openide.WizardDescriptor; 38 import org.openide.util.NbBundle; 39 40 45 final class NewOptionsIterator extends BasicWizardIterator { 46 47 private NewOptionsIterator.DataModel data; 48 49 private NewOptionsIterator() { } 50 51 public static NewOptionsIterator createIterator() { 52 return new NewOptionsIterator(); 53 } 54 55 public Set instantiate() throws IOException { 56 CreatedModifiedFiles cmf = data.getCreatedModifiedFiles(); 57 cmf.run(); 58 return getCreatedFiles(cmf, data.getProject()); 59 } 60 61 protected BasicWizardIterator.Panel[] createPanels(WizardDescriptor wiz) { 62 data = new NewOptionsIterator.DataModel(wiz); 63 return new BasicWizardIterator.Panel[] { 64 new OptionsPanel0(wiz, data), 65 new OptionsPanel(wiz, data) 66 }; 67 } 68 69 public void uninitialize(WizardDescriptor wiz) { 70 super.uninitialize(wiz); 71 data = null; 72 } 73 74 static final class DataModel extends BasicWizardIterator.BasicDataModel { 75 76 private static final int ERR_BLANK_DISPLAYNAME = 1; 77 private static final int ERR_BLANK_TOOLTIP = 2; 78 private static final int ERR_BLANK_TITLE = 4; 79 private static final int ERR_BLANK_CATEGORY_NAME = 5; 80 private static final int ERR_BLANK_ICONPATH = 6; 81 private static final int ERR_BLANK_PACKAGE_NAME = 7; 82 private static final int ERR_BLANK_CLASSNAME_PREFIX = 8; 83 private static final int ERR_INVALID_CLASSNAME_PREFIX = 9; 84 85 86 private static final int WARNING_INCORRECT_ICON_SIZE = -1; 87 88 private static final String [] CATEGORY_BUNDLE_KEYS = new String [] { 89 "@@OptionsCategory_Title@@", "@@OptionsCategory_Name@@", }; 92 93 private static final String [] ADVANCED_BUNDLE_KEYS = new String [] { 94 "@@AdvancedOption_DisplayName@@", "@@AdvancedOption_Tooltip@@" }; 97 98 private static final String [] TOKENS = new String [] { 99 "@@PACKAGE_NAME@@", "@@AdvancedOption_CLASS_NAME@@", "@@OptionsCategory_CLASS_NAME@@", "@@Panel_CLASS_NAME@@", "@@OptionsPanelController_CLASS_NAME@@", "@@ICON_PATH@@", ADVANCED_BUNDLE_KEYS[0], 106 ADVANCED_BUNDLE_KEYS[1], 107 CATEGORY_BUNDLE_KEYS[0], 108 CATEGORY_BUNDLE_KEYS[1] 109 }; 110 111 private static final String FORM_TEMPLATE_SUFFIXES[] = new String []{"Panel"}; private static final String [] JAVA_TEMPLATE_SUFFIXES = new String [] { 113 "AdvancedOption", "OptionsCategory", "Panel", "OptionsPanelController" }; 118 private static final String JAVA_TEMPLATE_PREFIX = "template_myplugin"; private static final String FORM_TEMPLATE_PREFIX = "template_myplugin_form"; 121 private CreatedModifiedFiles files; 122 private String codeNameBase; 123 private boolean advanced; 124 125 private String displayName; 127 private String tooltip; 128 129 private String title; 131 private String categoryName; 132 private String iconPath; 133 134 private String classNamePrefix; 135 136 DataModel(WizardDescriptor wiz) { 137 super(wiz); 138 } 139 140 int setDataForAdvanced(final String displayName, final String tooltip) { 141 this.advanced = true; 142 this.displayName = displayName; 143 this.tooltip = tooltip; 144 return checkFirstPanel(); 145 } 146 147 int setDataForOptionCategory(final String title, 148 final String categoryName, final String iconPath) { 149 this.advanced = false; 150 this.title = title; 151 this.categoryName = categoryName; 152 this.iconPath = iconPath; 153 return checkFirstPanel(); 154 } 155 156 public String getPackageName() { 157 String retValue; 158 retValue = super.getPackageName(); 159 if (retValue == null) { 160 retValue = getCodeNameBase(); 161 super.setPackageName(retValue); 162 } 163 return retValue; 164 } 165 166 public int setPackageAndPrefix(String packageName, String classNamePrefix) { 167 setPackageName(packageName); 168 this.classNamePrefix = classNamePrefix; 169 int errCode = checkFinalPanel(); 170 if (isSuccessCode(errCode)) { 171 generateCreatedModifiedFiles(); 172 } 173 return errCode; 174 } 175 176 private Map getTokenMap() { 177 Map retval = new HashMap (); 178 for (int i = 0; i < TOKENS.length; i++) { 179 if (isAdvanced() && "@@ICON_PATH@@".equals(TOKENS[i])) { continue; 181 } 182 retval.put(TOKENS[i], getReplacement(TOKENS[i])); 183 } 184 return retval; 185 } 186 187 private String getReplacement(String key) { 188 if ("@@PACKAGE_NAME@@".equals(key)) { return getPackageName(); 190 } else if ("@@AdvancedOption_CLASS_NAME@@".equals(key)) { return getAdvancedOptionClassName(); 192 } else if ("@@OptionsCategory_CLASS_NAME@@".equals(key)) { return getOptionsCategoryClassName(); 194 } else if ("@@Panel_CLASS_NAME@@".equals(key)) { return getPanelClassName(); 196 } else if ("@@OptionsPanelController_CLASS_NAME@@".equals(key)) { return getOptionsPanelControllerClassName(); 198 } else if ("@@ICON_PATH@@".equals(key)) { return addCreateIconOperation(new CreatedModifiedFiles(getProject()), getIconPath()); 200 } else if (key.startsWith("@@") && key.endsWith("@@")) { return key.substring(2, key.length()-2)+"_"+getClassNamePrefix(); 202 }else { 203 throw new AssertionError (key); 204 } 205 206 } 207 208 209 private String getBundleValue(String key) { 210 if (key.startsWith("OptionsCategory_Title")) { return getTitle(); 212 } else if (key.startsWith("OptionsCategory_Name")) { return getCategoryName(); 214 } else if (key.startsWith("AdvancedOption_DisplayName")) { return getDisplayName(); 216 } else if (key.startsWith("AdvancedOption_Tooltip")) { return getTooltip(); 218 } else { 219 throw new AssertionError (key); 220 } 221 } 222 223 227 String getErrorMessage(int errCode) { 228 assert errCode > 0; 229 String field = null; 230 switch(errCode) { 231 case ERR_BLANK_DISPLAYNAME: 232 field = "FIELD_DisplayName"; break; 234 case ERR_BLANK_TOOLTIP: 235 field = "FIELD_Tooltip"; break; 237 case ERR_BLANK_TITLE: 238 field = "FIELD_Title"; break; 240 case ERR_BLANK_CATEGORY_NAME: 241 field = "FIELD_CategoryName"; break; 243 case ERR_BLANK_ICONPATH: 244 field = "FIELD_IconPath"; break; 246 case ERR_BLANK_PACKAGE_NAME: 247 field = "FIELD_PackageName"; break; 249 case ERR_BLANK_CLASSNAME_PREFIX: 250 field = "FIELD_ClassNamePrefix"; break; 252 case ERR_INVALID_CLASSNAME_PREFIX: 253 return NbBundle.getMessage(NewOptionsIterator.class, "ERR_Name_Prefix_Invalid"); default: 255 assert false : "Unknown errCode: " + errCode; 256 } 257 field = NbBundle.getMessage(NewOptionsIterator.class, field); 258 return (errCode > 0) ? 259 NbBundle.getMessage(NewOptionsIterator.class, "ERR_FieldInvalid",field) : ""; } 261 262 266 String getWarningMessage(int warningCode) { 267 assert warningCode < 0; 268 String result; 269 switch(warningCode) { 270 case WARNING_INCORRECT_ICON_SIZE: 271 File icon = new File (getIconPath()); 272 assert icon.exists(); 273 result = UIUtil.getIconDimensionWarning(icon, 32, 32); 274 break; 275 default: 276 assert false : "Unknown warningCode: " + warningCode; 277 result = ""; 278 } 279 return result; 280 } 281 282 static boolean isSuccessCode(int code) { 283 return code == 0; 284 } 285 286 static boolean isErrorCode(int code) { 287 return code > 0; 288 } 289 290 static boolean isWarningCode(int code) { 291 return code < 0; 292 } 293 294 private int checkFirstPanel() { 295 if (advanced) { 296 if (getDisplayName().length() == 0) { 297 return ERR_BLANK_DISPLAYNAME; 298 } else if (getTooltip().length() == 0) { 299 return ERR_BLANK_TOOLTIP; 300 } 301 } else { 302 if (getTitle().length() == 0) { 303 return ERR_BLANK_TITLE; 304 } else if (getCategoryName().length() == 0) { 305 return ERR_BLANK_CATEGORY_NAME; 306 } else if (getIconPath().length() == 0) { 307 return ERR_BLANK_ICONPATH; 308 } else if (getTitle().length() == 0) { 309 return ERR_BLANK_TITLE; 310 } else { 311 File icon = new File (getIconPath()); 312 if (!icon.exists()) { 313 return ERR_BLANK_ICONPATH; 314 } 315 } 316 File icon = new File (getIconPath()); 318 assert icon.exists(); 319 if (!UIUtil.isValidIcon(icon, 32, 32)) { 320 return WARNING_INCORRECT_ICON_SIZE; 321 } 322 } 323 return 0; 324 } 325 326 private int checkFinalPanel() { 327 if (getPackageName().length() == 0) { 328 return ERR_BLANK_PACKAGE_NAME; 329 } else if (getClassNamePrefix().length() == 0) { 330 return ERR_BLANK_CLASSNAME_PREFIX; 331 } else if (!Utilities.isJavaIdentifier(getClassNamePrefix())) { 332 return ERR_INVALID_CLASSNAME_PREFIX; 333 } 334 335 return 0; 336 } 337 338 public CreatedModifiedFiles getCreatedModifiedFiles() { 339 if (files == null) { 340 files = generateCreatedModifiedFiles(); 341 } 342 return files; 343 } 344 345 private CreatedModifiedFiles generateCreatedModifiedFiles() { 346 assert isSuccessCode(checkFirstPanel()) || isWarningCode(checkFirstPanel()); 347 assert isSuccessCode(checkFinalPanel()); 348 files = new CreatedModifiedFiles(getProject()); 349 generateFiles(); 350 generateBundleKeys(); 351 generateDependencies(); 352 generateLayerEntry(); 353 if (!isAdvanced()) { 354 addCreateIconOperation(files, getIconPath()); 355 } 356 return files; 357 } 358 359 private void generateFiles() { 360 List allForms = Arrays.asList(FORM_TEMPLATE_SUFFIXES); 361 for (int i = 0; i < JAVA_TEMPLATE_SUFFIXES.length; i++) { 362 boolean ommit = (isAdvanced()) ? "OptionsCategory".equals(JAVA_TEMPLATE_SUFFIXES[i]) : "AdvancedOption".equals(JAVA_TEMPLATE_SUFFIXES[i]); if (ommit) { 365 continue; 366 } 367 files.add(createJavaFileCopyOperation(JAVA_TEMPLATE_SUFFIXES[i])); 368 if (allForms.contains(JAVA_TEMPLATE_SUFFIXES[i])) { 369 files.add(createFormFileCopyOperation(JAVA_TEMPLATE_SUFFIXES[i])); 370 } 371 } 372 } 373 374 private void generateBundleKeys() { 375 String [] bundleKeys = (isAdvanced()) ? ADVANCED_BUNDLE_KEYS : CATEGORY_BUNDLE_KEYS; 376 for (int i = 0; i < bundleKeys.length; i++) { 377 String key = bundleKeys[i]; 378 if (key.startsWith("@@") && key.endsWith("@@")) { key = getReplacement(key); 380 } 381 String value = getBundleValue(key); 382 files.add(files.bundleKey(getDefaultPackagePath("Bundle.properties", true),key,value)); } 384 } 385 386 private void generateDependencies() { 387 files.add(files.addModuleDependency("org.openide.util")); files.add(files.addModuleDependency("org.netbeans.modules.options.api","0-1",null,true)); files.add(files.addModuleDependency("org.openide.awt")); } 391 392 private void generateLayerEntry() { 393 String resourcePathPrefix = (isAdvanced()) ? "OptionsDialog/Advanced/" : "OptionsDialog/"; String instanceName = isAdvanced() ? getAdvancedOptionClassName() : getOptionsCategoryClassName(); 395 String instanceFullPath = resourcePathPrefix + getPackageName().replace('.','-') + "-" + instanceName + ".instance"; files.add(files.createLayerEntry(instanceFullPath, null, null, null, null)); 397 } 398 399 private CreatedModifiedFiles.Operation createJavaFileCopyOperation(final String templateSuffix) { 400 URL template = NewOptionsIterator.class.getResource(JAVA_TEMPLATE_PREFIX+templateSuffix); 401 assert template != null : JAVA_TEMPLATE_PREFIX+templateSuffix; 402 return files.createFileWithSubstitutions(getFilePath(templateSuffix), template,getTokenMap()); 403 } 404 405 private String getFilePath(final String templateSuffix) { 406 String fileName = getClassNamePrefix()+templateSuffix+ ".java"; return getDefaultPackagePath(fileName, false); } 409 410 private CreatedModifiedFiles.Operation createFormFileCopyOperation(final String templateSuffix) { 411 URL template = NewOptionsIterator.class.getResource(FORM_TEMPLATE_PREFIX+templateSuffix); 412 assert template != null : JAVA_TEMPLATE_PREFIX+templateSuffix; 413 String fileName = getClassNamePrefix()+templateSuffix+ ".form"; String filePath = getDefaultPackagePath(fileName, false); 415 return files.createFile(filePath, template); 416 } 417 418 private String getCodeNameBase() { 419 if (codeNameBase == null) { 420 NbModuleProvider mod = getProject().getLookup().lookup(NbModuleProvider.class); 421 codeNameBase = mod.getCodeNameBase(); 422 } 423 return codeNameBase; 424 } 425 426 private String getDisplayName() { 427 assert !isAdvanced() || displayName != null; 428 return displayName; 429 } 430 431 private String getTooltip() { 432 assert !isAdvanced() || tooltip != null; 433 return tooltip; 434 } 435 436 private String getTitle() { 437 assert isAdvanced() || title != null; 438 return title; 439 } 440 441 442 private String getCategoryName() { 443 assert isAdvanced() || categoryName != null; 444 return categoryName; 445 } 446 447 private String getIconPath() { 448 assert isAdvanced() || iconPath != null; 449 return iconPath; 450 } 451 452 String getClassNamePrefix() { 453 if (classNamePrefix == null) { 454 classNamePrefix = getCodeNameBase(); 455 classNamePrefix = classNamePrefix.substring(classNamePrefix.lastIndexOf(".")+1); classNamePrefix = classNamePrefix.substring(0,1).toUpperCase(Locale.ENGLISH) + classNamePrefix.substring(1); } 458 return classNamePrefix; 459 } 460 461 private boolean isAdvanced() { 462 return advanced; 463 } 464 465 private String getAdvancedOptionClassName() { 466 return getClassName(JAVA_TEMPLATE_SUFFIXES[0]); 467 } 468 469 private String getOptionsCategoryClassName() { 470 return getClassName(JAVA_TEMPLATE_SUFFIXES[1]); 471 } 472 473 private String getPanelClassName() { 474 return getClassName(JAVA_TEMPLATE_SUFFIXES[2]); 475 } 476 477 private String getOptionsPanelControllerClassName() { 478 return getClassName(JAVA_TEMPLATE_SUFFIXES[3]); 479 } 480 481 private String getClassName(String suffix) { 482 return getClassNamePrefix() + suffix; 483 } 484 485 } 486 } 487 | Popular Tags |