PageRenderTime 26ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/javacard.project/src/org/netbeans/modules/javacard/project/JCProjectSourceNodeFactory.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 416 lines | 321 code | 48 blank | 47 comment | 48 complexity | 297fc3f722b3c930e20dd8bb698376e0 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-2009 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.javacard.project;
  45. import java.awt.Image;
  46. import java.io.IOException;
  47. import org.netbeans.api.java.project.JavaProjectConstants;
  48. import org.netbeans.api.project.Project;
  49. import org.netbeans.api.project.ProjectUtils;
  50. import org.netbeans.api.project.SourceGroup;
  51. import org.netbeans.api.project.Sources;
  52. import org.netbeans.spi.java.project.support.ui.PackageView;
  53. import org.netbeans.spi.project.ui.support.NodeFactory;
  54. import org.netbeans.spi.project.ui.support.NodeFactorySupport;
  55. import org.netbeans.spi.project.ui.support.NodeList;
  56. import org.openide.filesystems.FileObject;
  57. import org.openide.loaders.DataObject;
  58. import org.openide.loaders.DataObjectNotFoundException;
  59. import org.openide.nodes.AbstractNode;
  60. import org.openide.nodes.FilterNode;
  61. import org.openide.nodes.Node;
  62. import org.openide.util.ChangeSupport;
  63. import org.openide.util.Exceptions;
  64. import org.openide.util.NbBundle;
  65. import org.openide.util.lookup.Lookups;
  66. import javax.swing.event.ChangeEvent;
  67. import javax.swing.event.ChangeListener;
  68. import java.util.ArrayList;
  69. import java.util.Collections;
  70. import java.util.List;
  71. import javax.swing.Action;
  72. import javax.swing.JOptionPane;
  73. import javax.swing.SwingUtilities;
  74. import org.netbeans.modules.javacard.project.deps.ui.DependenciesNode;
  75. import org.netbeans.modules.javacard.spi.ProjectKind;
  76. import org.netbeans.spi.actions.Single;
  77. import org.netbeans.validation.api.Problems;
  78. import org.netbeans.validation.api.builtin.stringvalidation.StringValidators;
  79. import org.openide.DialogDisplayer;
  80. import org.openide.NotifyDescriptor;
  81. import org.openide.actions.NewTemplateAction;
  82. import org.openide.actions.PasteAction;
  83. import org.openide.cookies.EditCookie;
  84. import org.openide.cookies.OpenCookie;
  85. import org.openide.filesystems.FileUtil;
  86. import org.openide.loaders.DataFolder;
  87. import org.openide.util.ImageUtilities;
  88. import org.openide.util.actions.SystemAction;
  89. import org.openide.util.lookup.ProxyLookup;
  90. import org.openide.windows.WindowManager;
  91. public class JCProjectSourceNodeFactory implements NodeFactory {
  92. public NodeList<?> createNodes(Project p) {
  93. JCProject project =
  94. p.getLookup().lookup(JCProject.class);
  95. assert project != null;
  96. return new JCNodeList(project);
  97. }
  98. private static class ImportantFilesNode extends AbstractNode {
  99. public ImportantFilesNode(JCProject project) {
  100. super(NodeFactorySupport.createCompositeChildren(project,
  101. project.kind().importantFilesPath()),
  102. Lookups.singleton(project));
  103. setDisplayName(NbBundle.getMessage(JCProjectSourceNodeFactory.class,
  104. "LBL_IMPORTANT_FILES")); //NOI18N
  105. setIconBaseWithExtension(
  106. "org/netbeans/modules/javacard/resources/importantfiles.png"); //NOI18N
  107. }
  108. }
  109. static enum ProjectNodeKinds {
  110. IMPORTANT_FILES,
  111. SCRIPTS_OR_WEB_PAGES,
  112. LIBRARIES,
  113. }
  114. private static class JCNodeList implements NodeList<Object>, ChangeListener {
  115. private JCProject project;
  116. private final ChangeSupport changeSupport = new ChangeSupport(this);
  117. public JCNodeList(JCProject proj) {
  118. project = proj;
  119. }
  120. public List<Object> keys() {
  121. if (this.project.getProjectDirectory() == null || !this.project.getProjectDirectory().isValid()) {
  122. return Collections.<Object>emptyList();
  123. }
  124. Sources sources = getSources();
  125. SourceGroup[] groups = sources.getSourceGroups(
  126. JavaProjectConstants.SOURCES_TYPE_JAVA);
  127. List<Object> result = new ArrayList<Object>(groups.length);
  128. for (int i = 0; i < groups.length; i++) {
  129. result.add(new SourceGroupKey(groups[i]));
  130. }
  131. if (!project.kind().isLibrary()) {
  132. result.add(ProjectNodeKinds.SCRIPTS_OR_WEB_PAGES);
  133. }
  134. result.add(ProjectNodeKinds.IMPORTANT_FILES);
  135. result.add(ProjectNodeKinds.LIBRARIES);
  136. return result;
  137. }
  138. public void addChangeListener(ChangeListener l) {
  139. changeSupport.addChangeListener(l);
  140. }
  141. public void removeChangeListener(ChangeListener l) {
  142. changeSupport.removeChangeListener(l);
  143. }
  144. public Node node(Object key) {
  145. if (key instanceof SourceGroupKey) {
  146. SourceGroupKey sgKey = (SourceGroupKey) key;
  147. try {
  148. return new PackageViewFilterNode(sgKey.group, project);
  149. } catch (DataObjectNotFoundException ex) {
  150. Exceptions.printStackTrace(ex);
  151. }
  152. } else if (key instanceof ProjectNodeKinds) {
  153. switch ((ProjectNodeKinds) key) {
  154. case IMPORTANT_FILES :
  155. return new ImportantFilesNode(project);
  156. case LIBRARIES :
  157. return new DependenciesNode(project);
  158. case SCRIPTS_OR_WEB_PAGES :
  159. try {
  160. return new ScriptsNode(project.getProjectDirectory().getFileObject(
  161. project.kind().isApplet() ? "scripts" : "html"), project); //NOI18N
  162. } catch (DataObjectNotFoundException ex) {
  163. Exceptions.printStackTrace(ex);
  164. }
  165. break;
  166. default :
  167. throw new AssertionError(key + ""); //NOI18N
  168. }
  169. }
  170. throw new AssertionError(key.getClass());
  171. }
  172. public void addNotify() {
  173. getSources().addChangeListener(this);
  174. }
  175. public void removeNotify() {
  176. getSources().removeChangeListener(this);
  177. }
  178. public void stateChanged(ChangeEvent e) {
  179. SwingUtilities.invokeLater(new Runnable() {
  180. public void run() {
  181. changeSupport.fireChange();
  182. }
  183. });
  184. }
  185. private Sources getSources() {
  186. return ProjectUtils.getSources(project);
  187. }
  188. }
  189. private static class ScriptsNode extends FilterNode {
  190. ScriptsNode(FileObject fileObject, JCProject project) throws DataObjectNotFoundException {
  191. this(fileObject == null ? Node.EMPTY : DataObject.find(fileObject).getNodeDelegate(), project);
  192. }
  193. private ScriptsNode(Node n, JCProject project) {
  194. super(n, new FilterNode.Children(n), new ProxyLookup(n.getLookup(), Lookups.fixed(project)));
  195. disableDelegation(DELEGATE_GET_NAME);
  196. disableDelegation(DELEGATE_GET_SHORT_DESCRIPTION);
  197. disableDelegation(DELEGATE_GET_DISPLAY_NAME);
  198. disableDelegation(DELEGATE_GET_ACTIONS);
  199. disableDelegation(DELEGATE_SET_NAME);
  200. disableDelegation(DELEGATE_SET_SHORT_DESCRIPTION);
  201. disableDelegation(DELEGATE_SET_DISPLAY_NAME);
  202. setName (n.getName());
  203. String key = project.kind().isApplet() ? "SCRIPTS_NODE_NAME" : "WEB_PAGES_NODE_NAME"; //NOI18N
  204. setDisplayName(NbBundle.getMessage(ScriptsNode.class, key));
  205. }
  206. @Override
  207. public Action[] getActions(boolean context) {
  208. JCProject p = getLookup().lookup(JCProject.class);
  209. return new Action[]{new AddTemplateAction(p.kind() == ProjectKind.WEB),
  210. SystemAction.get(NewTemplateAction.class),
  211. SystemAction.get(PasteAction.class)};
  212. }
  213. @Override
  214. public String getHtmlDisplayName() {
  215. if (getLookup().lookup(DataObject.class) == null) {
  216. //dir missing
  217. return "<font color=\"nb.errorForeground\">" + getDisplayName();
  218. }
  219. return null;
  220. }
  221. @Override
  222. public Image getIcon(int type) {
  223. JCProject p = getLookup().lookup(JCProject.class);
  224. return ImageUtilities.loadImage(p.kind().isApplet() ? "org/netbeans/modules/javacard/resources/scripts.png" : //NOI18N
  225. "org/netbeans/modules/javacard/resources/webpages.png"); //NOI18N
  226. }
  227. @Override
  228. public Image getOpenedIcon(int type) {
  229. return getIcon(type);
  230. }
  231. }
  232. private static final class AddTemplateAction extends Single<DataFolder> {
  233. private final boolean html;
  234. AddTemplateAction(boolean html) {
  235. super(DataFolder.class);
  236. String key = html ? "ACTION_NEW_HTML" : "ACTION_NEW_SCRIPT"; //NOI18N
  237. putValue(NAME, NbBundle.getMessage(AddTemplateAction.class, key));
  238. this.html = html;
  239. }
  240. @Override
  241. protected void actionPerformed(DataFolder target) {
  242. DataObject tpl = getTemplate();
  243. assert tpl != null;
  244. NotifyDescriptor.InputLine line = new NotifyDescriptor.InputLine(
  245. NbBundle.getMessage(AddTemplateAction.class,
  246. "TTL_NEW_FILE_ACTION"), //NOI18N
  247. NbBundle.getMessage(AddTemplateAction.class,
  248. "NEW_FILE_ACTION", tpl.getName())); //NOI18N
  249. if (NotifyDescriptor.OK_OPTION.equals(DialogDisplayer.getDefault().notify(line))) {
  250. String filename = line.getInputText();
  251. Problems problems = new Problems();
  252. StringValidators.REQUIRE_VALID_FILENAME.validate(problems,
  253. NbBundle.getMessage(JCProjectSourceNodeFactory.class,
  254. "FILENAME"), filename); //NOI18N
  255. // TODO validator doesn't work, investigate
  256. if (filename.length() == 0) {
  257. NotifyDescriptor nd = new NotifyDescriptor.Message(
  258. NbBundle.getMessage(JCProjectSourceNodeFactory.class, "WARN_FILENAME_INCORRECT"), // NOI18N
  259. NotifyDescriptor.WARNING_MESSAGE);
  260. DialogDisplayer.getDefault().notify(nd);
  261. return;
  262. }
  263. if (problems.hasFatal()) {
  264. NotifyDescriptor nd = new NotifyDescriptor.Message(
  265. problems.getLeadProblem().getMessage(),
  266. NotifyDescriptor.WARNING_MESSAGE);
  267. DialogDisplayer.getDefault().notify(nd);
  268. return;
  269. }
  270. try {
  271. DataObject ob = tpl.createFromTemplate(target, filename);
  272. EditCookie ec = ob.getLookup().lookup(EditCookie.class);
  273. if (ec == null) {
  274. OpenCookie oc = ob.getLookup().lookup(OpenCookie.class);
  275. if (oc != null) {
  276. oc.open();
  277. }
  278. } else {
  279. ec.edit();
  280. }
  281. } catch (IOException ex) {
  282. JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
  283. ex.getLocalizedMessage());
  284. }
  285. }
  286. }
  287. private DataObject getTemplate() {
  288. String template = html ? "Templates/Other/html.html" : "Templates/Other/ApduTemplate.scr"; //NOI18N
  289. FileObject fo = FileUtil.getConfigFile(template);
  290. if (fo != null) {
  291. try {
  292. return DataObject.find(fo);
  293. } catch (DataObjectNotFoundException ex) {
  294. Exceptions.printStackTrace(ex);
  295. }
  296. }
  297. return null;
  298. }
  299. @Override
  300. protected boolean isEnabled(DataFolder target) {
  301. return getTemplate() != null && target.getPrimaryFile().canWrite();
  302. }
  303. }
  304. private static class SourceGroupKey {
  305. public final SourceGroup group;
  306. public final FileObject fileObject;
  307. SourceGroupKey(SourceGroup group) {
  308. this.group = group;
  309. this.fileObject = group.getRootFolder();
  310. }
  311. @Override
  312. public int hashCode() {
  313. int hash = 5;
  314. String disp = this.group.getDisplayName();
  315. hash = 79 * hash + (fileObject != null ? fileObject.hashCode() : 0);
  316. hash = 79 * hash + (disp != null ? disp.hashCode() : 0);
  317. return hash;
  318. }
  319. @Override
  320. public boolean equals(Object obj) {
  321. if (!(obj instanceof SourceGroupKey)) {
  322. return false;
  323. } else {
  324. SourceGroupKey otherKey = (SourceGroupKey) obj;
  325. if (fileObject != otherKey.fileObject &&
  326. (fileObject == null || !fileObject.equals(otherKey.fileObject))) {
  327. return false;
  328. }
  329. String thisDisplayName = this.group.getDisplayName();
  330. String otherDisplayName = otherKey.group.getDisplayName();
  331. boolean oneNull = thisDisplayName == null;
  332. boolean twoNull = otherDisplayName == null;
  333. if (oneNull != twoNull || thisDisplayName != null && !thisDisplayName.equals(otherDisplayName)) {
  334. return false;
  335. }
  336. return true;
  337. }
  338. }
  339. }
  340. /** Yet another cool filter node just to add properties action
  341. */
  342. private static class PackageViewFilterNode extends FilterNode {
  343. Action[] actions;
  344. public PackageViewFilterNode(SourceGroup sourceGroup, Project project) throws DataObjectNotFoundException {
  345. super(sourceGroup instanceof ScriptsSourceGroup ? DataObject.find(sourceGroup.getRootFolder()).getNodeDelegate() : PackageView.createPackageView(sourceGroup));
  346. if (sourceGroup instanceof ScriptsSourceGroup) {
  347. super.disableDelegation(DELEGATE_SET_DISPLAY_NAME |
  348. DELEGATE_GET_DISPLAY_NAME);
  349. setDisplayName(sourceGroup.getDisplayName());
  350. }
  351. }
  352. @Override
  353. public Action[] getActions(boolean context) {
  354. if (!context) {
  355. if (actions == null) {
  356. Action superActions[] = super.getActions(context);
  357. actions = new Action[superActions.length + 1];
  358. System.arraycopy(superActions, 0,
  359. actions, 0, superActions.length);
  360. actions[superActions.length] = null;
  361. }
  362. return actions;
  363. } else {
  364. return super.getActions(context);
  365. }
  366. }
  367. }
  368. }