PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/winsys/NewTCIterator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 520 lines | 403 code | 63 blank | 54 comment | 52 complexity | 7731af58ab33c6e1bea91bbe18d673cd MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.apisupport.project.ui.wizard.winsys;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.io.OutputStream;
  48. import java.util.Collections;
  49. import java.util.HashMap;
  50. import java.util.HashSet;
  51. import java.util.Map;
  52. import java.util.Set;
  53. import java.util.TreeMap;
  54. import java.util.TreeSet;
  55. import java.util.logging.Level;
  56. import java.util.logging.Logger;
  57. import org.netbeans.api.project.Project;
  58. import org.netbeans.api.templates.TemplateRegistration;
  59. import org.netbeans.modules.apisupport.project.api.ManifestManager;
  60. import org.netbeans.modules.apisupport.project.api.UIUtil;
  61. import org.netbeans.modules.apisupport.project.api.Util;
  62. import org.netbeans.modules.apisupport.project.spi.LayerUtil;
  63. import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
  64. import org.netbeans.modules.apisupport.project.ui.wizard.common.BasicWizardIterator;
  65. import org.netbeans.modules.apisupport.project.ui.wizard.common.CreatedModifiedFiles;
  66. import org.openide.WizardDescriptor;
  67. import org.openide.filesystems.FileObject;
  68. import org.openide.filesystems.FileSystem;
  69. import org.openide.filesystems.FileUtil;
  70. import org.openide.modules.SpecificationVersion;
  71. import org.openide.util.NbBundle.Messages;
  72. import org.openide.windows.WindowManager;
  73. /**
  74. * Wizard for creating new TopComponent.
  75. *
  76. * @author Milos Kleint
  77. */
  78. @TemplateRegistration(
  79. folder=UIUtil.TEMPLATE_FOLDER,
  80. id=UIUtil.TEMPLATE_WINDOW_ID,
  81. position=200,
  82. displayName="#template_winsys",
  83. iconBase="org/netbeans/modules/apisupport/project/ui/wizard/winsys/newTC.png",
  84. description="newTC.html",
  85. category=UIUtil.TEMPLATE_CATEGORY
  86. )
  87. @Messages("template_winsys=Window")
  88. public final class NewTCIterator extends BasicWizardIterator {
  89. private NewTCIterator.DataModel data;
  90. @Override
  91. public Set instantiate() throws IOException {
  92. CreatedModifiedFiles cmf = data.getCreatedModifiedFiles();
  93. cmf.run();
  94. return getCreatedFiles(cmf, data.getProject());
  95. }
  96. @Override
  97. protected BasicWizardIterator.Panel[] createPanels(WizardDescriptor wiz) {
  98. data = new NewTCIterator.DataModel(wiz);
  99. return new BasicWizardIterator.Panel[] {
  100. new BasicSettingsPanel(wiz, data),
  101. new NameAndLocationPanel(wiz, data)
  102. };
  103. }
  104. public @Override void uninitialize(WizardDescriptor wiz) {
  105. super.uninitialize(wiz);
  106. data = null;
  107. }
  108. static final class DataModel extends BasicWizardIterator.BasicDataModel {
  109. private String name;
  110. private String icon;
  111. private String mode;
  112. private boolean opened = false;
  113. private boolean keepPrefSize = false;
  114. private boolean slidingNotAllowed = false;
  115. private boolean closingNotAllowed = false;
  116. private boolean draggingNotAllowed = false;
  117. private boolean undockingNotAllowed = false;
  118. private boolean maximizationNotAllowed = false;
  119. private Map<String,String> newModes;
  120. private Set<String> existingModes;
  121. private boolean ignorePreviousRun = true;
  122. private CreatedModifiedFiles files;
  123. FileSystem sfs;
  124. DataModel(WizardDescriptor wiz) {
  125. super(wiz);
  126. }
  127. public CreatedModifiedFiles getCreatedModifiedFiles() {
  128. return getFiles();
  129. }
  130. public void setCreatedModifiedFiles(CreatedModifiedFiles files) {
  131. this.setFiles(files);
  132. }
  133. public String getName() {
  134. return name;
  135. }
  136. public void setName(String name) {
  137. this.name = name;
  138. }
  139. public CreatedModifiedFiles getFiles() {
  140. return files;
  141. }
  142. public void setFiles(CreatedModifiedFiles files) {
  143. this.files = files;
  144. }
  145. public String getIcon() {
  146. return icon;
  147. }
  148. public void setIcon(String icon) {
  149. this.icon = icon;
  150. }
  151. public String getMode() {
  152. return mode;
  153. }
  154. public void defineMode(String name, String definition) {
  155. if (newModes == null) {
  156. newModes = new TreeMap<String, String>();
  157. }
  158. newModes.put(name, definition);
  159. }
  160. void existingMode(String name) {
  161. if (existingModes == null) {
  162. existingModes = new TreeSet<String>();
  163. }
  164. existingModes.add(name);
  165. }
  166. boolean isExistingMode(String name) {
  167. return existingModes != null && existingModes.contains(name);
  168. }
  169. Map<String,String> getNewModes() {
  170. if (newModes == null) {
  171. return null;
  172. }
  173. TreeMap<String,String> copy = new TreeMap<String,String>(newModes);
  174. if (existingModes != null) {
  175. copy.keySet().removeAll(existingModes);
  176. }
  177. return copy.isEmpty() ? null : copy;
  178. }
  179. public boolean isIgnorePreviousRun() {
  180. return ignorePreviousRun;
  181. }
  182. public void setIgnorePreviousRun(boolean ignorePreviousRun) {
  183. this.ignorePreviousRun = ignorePreviousRun;
  184. }
  185. public void setMode(String mode) {
  186. this.mode = mode;
  187. }
  188. public boolean isOpened() {
  189. return opened;
  190. }
  191. public void setOpened(boolean opened) {
  192. this.opened = opened;
  193. }
  194. public boolean isKeepPrefSize() {
  195. return keepPrefSize;
  196. }
  197. public void setKeepPrefSize(boolean keepPrefSize) {
  198. this.keepPrefSize = keepPrefSize;
  199. }
  200. public boolean isClosingNotAllowed() {
  201. return closingNotAllowed;
  202. }
  203. public void setClosingNotAllowed(boolean closingNotAllowed) {
  204. this.closingNotAllowed = closingNotAllowed;
  205. }
  206. public boolean isDraggingNotAllowed() {
  207. return draggingNotAllowed;
  208. }
  209. public void setDraggingNotAllowed(boolean draggingNotAllowed) {
  210. this.draggingNotAllowed = draggingNotAllowed;
  211. }
  212. public boolean isMaximizationNotAllowed() {
  213. return maximizationNotAllowed;
  214. }
  215. public void setMaximizationNotAllowed(boolean maximizationNotAllowed) {
  216. this.maximizationNotAllowed = maximizationNotAllowed;
  217. }
  218. public boolean isSlidingNotAllowed() {
  219. return slidingNotAllowed;
  220. }
  221. public void setSlidingNotAllowed(boolean slidingNotAllowed) {
  222. this.slidingNotAllowed = slidingNotAllowed;
  223. }
  224. public boolean isUndockingNotAllowed() {
  225. return undockingNotAllowed;
  226. }
  227. public void setUndockingNotAllowed(boolean undockingNotAllowed) {
  228. this.undockingNotAllowed = undockingNotAllowed;
  229. }
  230. void setSFS(FileSystem sfs) {
  231. this.sfs = sfs;
  232. }
  233. }
  234. static void generateFileChanges(final DataModel model) {
  235. CreatedModifiedFiles fileChanges = new CreatedModifiedFiles(model.getProject());
  236. Project project = model.getProject();
  237. NbModuleProvider moduleInfo = model.getModuleInfo();
  238. final String name = model.getName();
  239. final String packageName = model.getPackageName();
  240. final String mode = model.getMode();
  241. boolean actionLessTC;
  242. boolean xmlLessTC;
  243. try {
  244. SpecificationVersion current = model.getModuleInfo().getDependencyVersion("org.openide.windows");
  245. actionLessTC = current == null || current.compareTo(new SpecificationVersion("6.24")) >= 0; // NOI18N
  246. xmlLessTC = current == null || current.compareTo(new SpecificationVersion("6.37")) >= 0; // NOI18N
  247. } catch (IOException ex) {
  248. Logger.getLogger(NewTCIterator.class.getName()).log(Level.INFO, null, ex);
  249. actionLessTC = false;
  250. xmlLessTC = false;
  251. }
  252. boolean propertiesPersistence;
  253. try {
  254. SpecificationVersion current = model.getModuleInfo().getDependencyVersion("org.netbeans.modules.settings");
  255. propertiesPersistence = current == null || current.compareTo(new SpecificationVersion("1.18")) >= 0; // NOI18N
  256. } catch (IOException ex) {
  257. Logger.getLogger(NewTCIterator.class.getName()).log(Level.INFO, null, ex);
  258. propertiesPersistence = false;
  259. }
  260. if (name != null) {
  261. Map<String,String> replaceTokens = new HashMap<String,String>();
  262. replaceTokens.put("TEMPLATENAME", name);//NOI18N
  263. replaceTokens.put("PACKAGENAME", packageName);//NOI18N
  264. replaceTokens.put("MODE", mode); //NOI18N
  265. replaceTokens.put("OPENED", model.isOpened() ? "true" : "false"); //NOI18N
  266. replaceTokens.put("WINSYSBEHAVIOR", defineWinSysBehavior( model ) ); //NOI18N
  267. // 0. move icon file if necessary
  268. String icon = model.getIcon();
  269. File fil = null;
  270. if (icon != null) {
  271. fil = new File(icon);
  272. if (!fil.exists()) {
  273. fil = null;
  274. }
  275. }
  276. // XXX this should be using addCreateIconOperation
  277. String relativeIconPath = null;
  278. if (fil != null) {
  279. FileObject fo = FileUtil.toFileObject(fil);
  280. if (!FileUtil.isParentOf(Util.getResourceDirectory(project), fo)) {
  281. String iconPath = getRelativePath(moduleInfo.getResourceDirectoryPath(false), packageName,
  282. "", fo.getNameExt()); //NOI18N
  283. fileChanges.add(fileChanges.createFile(iconPath, fo));
  284. relativeIconPath = packageName.replace('.', '/') + "/" + fo.getNameExt(); // NOI18N
  285. } else {
  286. relativeIconPath = FileUtil.getRelativePath(Util.getResourceDirectory(project), fo);
  287. }
  288. replaceTokens.put("ICONPATH", relativeIconPath);//NOI18N
  289. replaceTokens.put("COMMENTICON", "");//NOI18N
  290. } else {
  291. replaceTokens.put("ICONPATH", "SET/PATH/TO/ICON/HERE"); //NOI18N
  292. replaceTokens.put("COMMENTICON", "//");//NOI18N
  293. }
  294. // 2. update project dependencies
  295. replaceTokens.put("MODULENAME", moduleInfo.getCodeNameBase()); // NOI18N
  296. String specVersion = moduleInfo.getSpecVersion();
  297. replaceTokens.put("SPECVERSION", specVersion != null ? specVersion : "0"); // NOI18N
  298. fileChanges.add(fileChanges.addModuleDependency("org.openide.windows")); //NOI18N
  299. fileChanges.add(fileChanges.addManifestToken(ManifestManager.OPENIDE_MODULE_REQUIRES, WindowManager.class.getName()));
  300. fileChanges.add(fileChanges.addModuleDependency("org.openide.util")); //NOI18N
  301. fileChanges.add(fileChanges.addModuleDependency("org.openide.util.lookup")); //NOI18N
  302. fileChanges.add(fileChanges.addModuleDependency("org.openide.awt")); //NOI18N
  303. if (propertiesPersistence) {
  304. fileChanges.add(fileChanges.addModuleDependency("org.netbeans.modules.settings")); //NOI18N
  305. }
  306. // x. generate java classes
  307. final String tcName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName,
  308. name, "TopComponent.java"); //NOI18N
  309. FileObject template = CreatedModifiedFiles.getTemplate(
  310. xmlLessTC ? "templateTopComponent637.java" :
  311. (propertiesPersistence ? "templateTopComponentAnno.java" : "templateTopComponent.java")
  312. );
  313. fileChanges.add(fileChanges.createFileWithSubstitutions(tcName, template, replaceTokens));
  314. // x. generate java classes
  315. final String tcFormName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName,
  316. name, "TopComponent.form"); //NOI18N
  317. template = CreatedModifiedFiles.getTemplate("templateTopComponent.form");//NOI18N
  318. fileChanges.add(fileChanges.createFileWithSubstitutions(tcFormName, template, replaceTokens));
  319. if (!actionLessTC) {
  320. final String actionName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName,
  321. name, "Action.java"); //NOI18N
  322. template = CreatedModifiedFiles.getTemplate("templateAction.java");//NOI18N
  323. fileChanges.add(fileChanges.createFileWithSubstitutions(actionName, template, replaceTokens));
  324. }
  325. if (!xmlLessTC) {
  326. final String settingsName = name + "TopComponent.settings"; //NOI18N
  327. template = CreatedModifiedFiles.getTemplate("templateSettings.xml");//NOI18N
  328. fileChanges.add(fileChanges.createLayerEntry("Windows2/Components/" + settingsName, template, replaceTokens, null, null)); // NOI18N
  329. }
  330. if (!xmlLessTC) {
  331. final String wstcrefName = name + "TopComponent.wstcref"; //NOI18N
  332. template = CreatedModifiedFiles.getTemplate("templateWstcref.xml");//NOI18N
  333. fileChanges.add(fileChanges.createLayerEntry("Windows2/Modes/" + mode + "/" + wstcrefName, // NOI18N
  334. template, replaceTokens, null, null));
  335. }
  336. String bundlePath = getRelativePath(moduleInfo.getResourceDirectoryPath(false), packageName, "", "Bundle.properties"); //NOI18N
  337. if (xmlLessTC) {
  338. // nothing in layer
  339. } else if (actionLessTC) {
  340. String path = "Actions/Window/" + packageName.replace('.','-') + "-" + name + "Action.instance"; // NOI18N
  341. {
  342. Map<String,Object> attrs = new HashMap<String,Object>();
  343. attrs.put("instanceCreate", "methodvalue:org.openide.windows.TopComponent.openAction"); // NOI18N
  344. attrs.put("component", "methodvalue:" + packageName + '.' + name + "TopComponent.findInstance"); // NOI18N
  345. if (relativeIconPath != null) {
  346. attrs.put("iconBase", relativeIconPath); // NOI18N
  347. }
  348. attrs.put("displayName", "bundlevalue:" + packageName + ".Bundle#CTL_" + name + "Action"); // NOI18N
  349. fileChanges.add(
  350. fileChanges.createLayerEntry(
  351. path,
  352. null,
  353. null,
  354. null,
  355. attrs
  356. )
  357. );
  358. }
  359. {
  360. fileChanges.add(
  361. fileChanges.createLayerEntry(
  362. "Menu/Window/" + name + "Action.shadow", // NOI18N
  363. null,
  364. null,
  365. null,
  366. Collections.singletonMap("originalFile", path)
  367. )
  368. );
  369. }
  370. } else {
  371. fileChanges.add(fileChanges.layerModifications(new CreateActionEntryOperation(name + "Action", packageName), // NOI18N
  372. Collections.<String>emptySet()));
  373. }
  374. if (!xmlLessTC) {
  375. fileChanges.add(fileChanges.bundleKey(bundlePath, "CTL_" + name + "Action", name)); //NOI18N
  376. fileChanges.add(fileChanges.bundleKey(bundlePath, "CTL_" + name + "TopComponent", name + " Window")); //NOI18N
  377. fileChanges.add(fileChanges.bundleKey(bundlePath, "HINT_" + name + "TopComponent", "This is a " + name + " window")); //NOI18N
  378. }
  379. }
  380. final Map<String, String> newModes = model.getNewModes();
  381. if (newModes != null) {
  382. Set<String> files = new HashSet<String>();
  383. FileObject parent = model.sfs.findResource("Windows2/Modes");
  384. for (String wsmode : newModes.keySet()) {
  385. files.add(LayerUtil.findGeneratedName(parent, wsmode + ".wsmode"));
  386. }
  387. fileChanges.add(fileChanges.layerModifications(new CreatedModifiedFiles.LayerOperation() {
  388. @Override
  389. public void run(FileSystem layer) throws IOException {
  390. FileObject fo = FileUtil.createFolder(layer.getRoot(), "Windows2/Modes");
  391. for (Map.Entry<String, String> entry : newModes.entrySet()) {
  392. FileObject wsmode = FileUtil.createData(fo, entry.getKey() + ".wsmode");
  393. OutputStream os = wsmode.getOutputStream();
  394. try {
  395. os.write(entry.getValue().getBytes("UTF-8"));
  396. } finally {
  397. os.close();
  398. }
  399. }
  400. }
  401. }, files));
  402. }
  403. model.setCreatedModifiedFiles(fileChanges);
  404. }
  405. private static String defineWinSysBehavior( DataModel model ) {
  406. StringBuilder res = new StringBuilder();
  407. if( model.isClosingNotAllowed() ) {
  408. res.append("\tputClientProperty(TopComponent.PROP_CLOSING_DISABLED, Boolean.TRUE);\n");
  409. }
  410. if( model.isDraggingNotAllowed() ) {
  411. res.append("\tputClientProperty(TopComponent.PROP_DRAGGING_DISABLED, Boolean.TRUE);\n");
  412. }
  413. if( model.isMaximizationNotAllowed() ) {
  414. res.append("\tputClientProperty(TopComponent.PROP_MAXIMIZATION_DISABLED, Boolean.TRUE);\n");
  415. }
  416. if( model.isSlidingNotAllowed() ) {
  417. res.append("\tputClientProperty(TopComponent.PROP_SLIDING_DISABLED, Boolean.TRUE);\n");
  418. }
  419. if( model.isUndockingNotAllowed() ) {
  420. res.append("\tputClientProperty(TopComponent.PROP_UNDOCKING_DISABLED, Boolean.TRUE);\n");
  421. }
  422. if( model.isKeepPrefSize() ) {
  423. res.append("\tputClientProperty(TopComponent.PROP_KEEP_PREFERRED_SIZE_WHEN_SLIDED_IN, Boolean.TRUE);\n");
  424. }
  425. return res.toString();
  426. }
  427. private static String getRelativePath(String rootpath, String fullyQualifiedPackageName,
  428. String prefix, String postfix) {
  429. StringBuilder sb = new StringBuilder();
  430. sb.append(rootpath).append('/').append(fullyQualifiedPackageName.replace('.','/'))
  431. .append('/').append(prefix).append(postfix);
  432. return sb.toString();
  433. }
  434. static class CreateActionEntryOperation implements CreatedModifiedFiles.LayerOperation {
  435. private String name;
  436. private String packageName;
  437. public CreateActionEntryOperation(String actionname, String packageName) {
  438. this.packageName = packageName;
  439. this.name = actionname;
  440. }
  441. @Override
  442. public void run(FileSystem layer) throws IOException {
  443. FileObject folder = layer.getRoot().getFileObject("Actions/Window");// NOI18N
  444. if (folder == null) {
  445. folder = FileUtil.createFolder(layer.getRoot(), "Actions/Window"); // NOI18N
  446. }
  447. String instance = packageName.replace('.','-') + "-" + name; // NOI18N
  448. folder.createData(instance, "instance"); // NOI18N
  449. folder = layer.getRoot().getFileObject("Menu/Window");// NOI18N
  450. if (folder == null) {
  451. folder = FileUtil.createFolder(layer.getRoot(), "Menu/Window"); // NOI18N
  452. }
  453. FileObject file = folder.createData(name, "shadow"); // NOI18N
  454. file.setAttribute("originalFile", "Actions/Window/" + instance + ".instance"); // NOI18N
  455. }
  456. }
  457. }