PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/netbeans-7.3/php.project/src/org/netbeans/modules/php/project/ui/wizards/NewFileWizardIterator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 318 lines | 216 code | 30 blank | 72 comment | 36 complexity | 9db452c92642d204f46232c8e9d23399 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 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. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2008 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.php.project.ui.wizards;
  43. import java.awt.Component;
  44. import java.io.IOException;
  45. import java.util.Arrays;
  46. import java.util.Collections;
  47. import java.util.NoSuchElementException;
  48. import java.util.Set;
  49. import java.util.logging.Level;
  50. import java.util.logging.Logger;
  51. import javax.swing.JComponent;
  52. import javax.swing.event.ChangeListener;
  53. import org.netbeans.api.project.Project;
  54. import org.netbeans.api.project.SourceGroup;
  55. import org.netbeans.modules.php.project.PhpProject;
  56. import org.netbeans.modules.php.project.PhpProjectValidator;
  57. import org.netbeans.modules.php.project.ProjectPropertiesSupport;
  58. import org.netbeans.modules.php.project.SourceRoots;
  59. import org.netbeans.modules.php.project.ui.Utils;
  60. import org.netbeans.modules.php.project.util.PhpProjectUtils;
  61. import org.netbeans.spi.project.ui.templates.support.Templates;
  62. import org.openide.WizardDescriptor;
  63. import org.openide.WizardDescriptor.Panel;
  64. import org.openide.filesystems.FileObject;
  65. import org.openide.loaders.CreateFromTemplateHandler;
  66. import org.openide.loaders.DataFolder;
  67. import org.openide.loaders.DataObject;
  68. /**
  69. * Just as simple wrapper for the standard new file iterator as possible.
  70. * @author Tomas Mysik
  71. */
  72. public final class NewFileWizardIterator implements WizardDescriptor.InstantiatingIterator<WizardDescriptor> {
  73. private static final Logger LOGGER = Logger.getLogger(NewFileWizardIterator.class.getName());
  74. private static final long serialVersionUID = 2262026971167469147L;
  75. private WizardDescriptor wizard;
  76. private WizardDescriptor.Panel<WizardDescriptor>[] wizardPanels;
  77. private int index;
  78. @Override
  79. public Set<FileObject> instantiate() throws IOException {
  80. FileObject dir = Templates.getTargetFolder(wizard);
  81. FileObject template = Templates.getTemplate(wizard);
  82. DataFolder dataFolder = DataFolder.findFolder(dir);
  83. DataObject dataTemplate = DataObject.find(template);
  84. DataObject createdFile = dataTemplate.createFromTemplate(dataFolder, Templates.getTargetName(wizard),
  85. Collections.singletonMap(CreateFromTemplateHandler.FREE_FILE_EXTENSION, true));
  86. // #187374
  87. try {
  88. PhpProjectUtils.reformatFile(createdFile);
  89. } catch (IOException exc) {
  90. LOGGER.log(Level.WARNING, exc.getMessage(), exc);
  91. }
  92. return Collections.singleton(createdFile.getPrimaryFile());
  93. }
  94. @Override
  95. public void initialize(WizardDescriptor wizard) {
  96. this.wizard = wizard;
  97. checkPhpProject();
  98. setTargetFolder();
  99. wizardPanels = getPanels();
  100. // Make sure list of steps is accurate.
  101. String[] beforeSteps = (String[]) wizard.getProperty(WizardDescriptor.PROP_CONTENT_DATA);
  102. int beforeStepLength = beforeSteps.length - 1;
  103. String[] steps = createSteps(beforeSteps);
  104. for (int i = 0; i < wizardPanels.length; i++) {
  105. Component c = wizardPanels[i].getComponent();
  106. if (c instanceof JComponent) { // assume Swing components
  107. JComponent jc = (JComponent) c;
  108. jc.putClientProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, Integer.valueOf(i + beforeStepLength - 1)); // NOI18N
  109. jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DATA, steps); // NOI18N
  110. }
  111. }
  112. }
  113. private void checkPhpProject() {
  114. PhpProject phpProject = getPhpProject();
  115. if (phpProject == null) {
  116. // not php project
  117. return;
  118. }
  119. if (PhpProjectValidator.isFatallyBroken(phpProject)) {
  120. Utils.warnInvalidSourcesDirectory(phpProject);
  121. }
  122. }
  123. private void setTargetFolder() {
  124. if (Templates.getTargetFolder(wizard) != null) {
  125. // already set
  126. return;
  127. }
  128. PhpProject phpProject = getPhpProject();
  129. if (phpProject == null) {
  130. // not php project
  131. return;
  132. }
  133. FileObject srcDir = ProjectPropertiesSupport.getSourcesDirectory(phpProject);
  134. if (srcDir != null && srcDir.isValid()) {
  135. Templates.setTargetFolder(wizard, srcDir);
  136. }
  137. }
  138. private PhpProject getPhpProject() {
  139. Project project = Templates.getProject(wizard);
  140. if (project == null) {
  141. // no project => ignore
  142. return null;
  143. }
  144. if (!(project instanceof PhpProject)) {
  145. LOGGER.log(Level.WARNING, "PHP project expected but found {0}", project.getClass().getName());
  146. return null;
  147. }
  148. return (PhpProject) project;
  149. }
  150. private String[] createSteps(String[] beforeSteps) {
  151. int beforeStepLength = beforeSteps.length - 1;
  152. String[] res = new String[beforeStepLength + wizardPanels.length];
  153. for (int i = 0; i < res.length; i++) {
  154. if (i < (beforeStepLength)) {
  155. res[i] = beforeSteps[i];
  156. } else {
  157. res[i] = wizardPanels[i - beforeStepLength].getComponent().getName();
  158. }
  159. }
  160. return res;
  161. }
  162. @Override
  163. public void uninitialize(WizardDescriptor wizard) {
  164. wizardPanels = null;
  165. }
  166. @Override
  167. public String name() {
  168. return ""; // NOI18N
  169. }
  170. /** Get the current panel.
  171. * @return the panel
  172. */
  173. @Override
  174. public Panel<WizardDescriptor> current() {
  175. return wizardPanels[index];
  176. }
  177. /** Test whether there is a next panel.
  178. * @return <code>true</code> if so
  179. */
  180. @Override
  181. public boolean hasNext() {
  182. return index < wizardPanels.length - 1;
  183. }
  184. /** Test whether there is a previous panel.
  185. * @return <code>true</code> if so
  186. */
  187. @Override
  188. public boolean hasPrevious() {
  189. return index > 0;
  190. }
  191. /** Move to the next panel.
  192. * I.e. increment its index, need not actually change any GUI itself.
  193. * @exception NoSuchElementException if the panel does not exist
  194. */
  195. @Override
  196. public void nextPanel() {
  197. if (!hasNext()) {
  198. throw new NoSuchElementException();
  199. }
  200. index++;
  201. }
  202. /** Move to the previous panel.
  203. * I.e. decrement its index, need not actually change any GUI itself.
  204. * @exception NoSuchElementException if the panel does not exist
  205. */
  206. @Override
  207. public void previousPanel() {
  208. if (!hasPrevious()) {
  209. throw new NoSuchElementException();
  210. }
  211. index--;
  212. }
  213. @Override
  214. public void addChangeListener(ChangeListener l) {
  215. }
  216. @Override
  217. public void removeChangeListener(ChangeListener l) {
  218. }
  219. private WizardDescriptor.Panel<WizardDescriptor>[] getPanels() {
  220. Project project = Templates.getProject(wizard);
  221. SourceGroup[] groups = PhpProjectUtils.getSourceGroups(project);
  222. // #218437
  223. PhpProject phpProject = getPhpProject();
  224. if (phpProject != null) {
  225. // php project found
  226. if (groups != null && groups.length == 0 && !PhpProjectValidator.isFatallyBroken(phpProject)) {
  227. // sources found but no source roots?!
  228. FileObject sources = ProjectPropertiesSupport.getSourcesDirectory(phpProject);
  229. FileObject tests = ProjectPropertiesSupport.getTestDirectory(phpProject, false);
  230. FileObject selenium = ProjectPropertiesSupport.getSeleniumDirectory(phpProject, false);
  231. SourceRoots sourceRoots = phpProject.getSourceRoots();
  232. SourceRoots testRoots = phpProject.getTestRoots();
  233. SourceRoots seleniumRoots = phpProject.getSeleniumRoots();
  234. StringBuilder sb = new StringBuilder(200);
  235. addDiagnosticForDirs(sb, phpProject, sources, tests, selenium);
  236. addDiagnosticForRoots(sb, sourceRoots, testRoots, seleniumRoots);
  237. LOGGER.log(Level.WARNING, sb.toString(),
  238. new IllegalStateException("No source roots found (attach your IDE log to https://netbeans.org/bugzilla/show_bug.cgi?id=218437)"));
  239. // try to recover...
  240. sourceRoots.fireChange();
  241. testRoots.fireChange();
  242. seleniumRoots.fireChange();
  243. sb = new StringBuilder(200);
  244. addDiagnosticForRoots(sb, sourceRoots, testRoots, seleniumRoots);
  245. LOGGER.log(Level.WARNING, sb.toString(),
  246. new IllegalStateException("Trying to fire changes for all source roots"));
  247. groups = PhpProjectUtils.getSourceGroups(project);
  248. }
  249. }
  250. WizardDescriptor.Panel<WizardDescriptor> simpleTargetChooserPanel = Templates.buildSimpleTargetChooser(project, groups).freeFileExtension().create();
  251. @SuppressWarnings("unchecked") // Generic Array Creation
  252. WizardDescriptor.Panel<WizardDescriptor>[] panels = new WizardDescriptor.Panel[] {
  253. simpleTargetChooserPanel
  254. };
  255. return panels;
  256. }
  257. private void addDiagnosticForDirs(StringBuilder sb, PhpProject project, FileObject sources, FileObject tests, FileObject selenium) {
  258. sb.append("project directory equals sources: "); // NOI18N
  259. sb.append(project.getProjectDirectory().equals(sources));
  260. sb.append(";\n sources (not null, valid): "); // NOI18N
  261. sb.append(sources != null);
  262. sb.append(", "); // NOI18N
  263. sb.append(sources != null && sources.isValid());
  264. sb.append(";\n tests (not null, valid): "); // NOI18N
  265. sb.append(tests != null);
  266. sb.append(", "); // NOI18N
  267. sb.append(tests != null && tests.isValid());
  268. sb.append(";\n selenium (not null, valid): "); // NOI18N
  269. sb.append(selenium != null);
  270. sb.append(", "); // NOI18N
  271. sb.append(selenium != null && selenium.isValid());
  272. }
  273. private void addDiagnosticForRoots(StringBuilder sb, SourceRoots sourceRoots, SourceRoots testRoots, SourceRoots seleniumRoots) {
  274. sb.append(";\n sourceRoots (fired changes): "); // NOI18N
  275. sb.append(Arrays.asList(sourceRoots.getRoots()));
  276. sb.append(" ("); // NOI18N
  277. sb.append(sourceRoots.getFiredChanges());
  278. sb.append(");\n testRoots (fired changes): "); // NOI18N
  279. sb.append(Arrays.asList(testRoots.getRoots()));
  280. sb.append(" ("); // NOI18N
  281. sb.append(testRoots.getFiredChanges());
  282. sb.append(");\n seleniumRoots (fired changes): "); // NOI18N
  283. sb.append(Arrays.asList(seleniumRoots.getRoots()));
  284. sb.append(" ("); // NOI18N
  285. sb.append(seleniumRoots.getFiredChanges());
  286. sb.append(")"); // NOI18N
  287. }
  288. }