KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > ui > actions > AddModuleAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.earproject.ui.actions;
21
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25 import org.netbeans.api.project.Project;
26 import org.netbeans.api.project.ui.OpenProjects;
27 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
28 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModuleContainer;
29 import org.netbeans.spi.project.ui.LogicalViewProvider;
30 import org.netbeans.spi.project.SubprojectProvider;
31 import org.openide.nodes.AbstractNode;
32 import org.openide.nodes.Children;
33 import org.openide.nodes.FilterNode;
34 import org.openide.nodes.Node;
35 import org.openide.nodes.NodeAcceptor;
36 import org.openide.nodes.NodeOperation;
37 import org.openide.util.HelpCtx;
38 import org.openide.util.NbBundle;
39 import org.openide.util.UserCancelException;
40 import org.openide.util.actions.CookieAction;
41 import org.openide.util.lookup.Lookups;
42
43 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties;
44
45 import org.netbeans.spi.project.support.ant.AntProjectHelper;
46 import org.netbeans.modules.j2ee.earproject.EarProject;
47 import org.netbeans.api.project.FileOwnerQuery;
48
49 /**
50  * Action that allows selection and assembly of J2EE module projects.
51  * @author Chris Webster
52  * @author vince kraemer
53  */

54 public class AddModuleAction extends CookieAction {
55     
56     private static final String JavaDoc FOLDER_ICON = "org/netbeans/modules/j2ee/earproject/ui/resources/folder.gif";
57     
58     private static final Class JavaDoc[] COOKIE_ARRAY =
59         new Class JavaDoc[] { AntProjectHelper.class };
60     
61     public Class JavaDoc[] cookieClasses() {
62         return COOKIE_ARRAY;
63     }
64     
65     public int mode() {
66         return CookieAction.MODE_EXACTLY_ONE;
67     }
68     
69     public void performAction(Node[] activeNodes) {
70         try {
71             AntProjectHelper aph =
72                 (AntProjectHelper) activeNodes[0].getLookup().lookup(AntProjectHelper.class);
73             Project[] moduleProjects = getSelectedProjects(aph);
74             // XXX Vince add code here to add to application.xml and
75
// build script
76
Project p = FileOwnerQuery.getOwner(aph.getProjectDirectory());
77             EarProject ep = (EarProject) p.getLookup().lookup(EarProject.class);
78             EarProjectProperties epp = (EarProjectProperties) ep.getProjectProperties();
79             epp.addJ2eeSubprojects(moduleProjects);
80         } catch (UserCancelException uce) {
81             // this action has been cancelled
82
}
83     }
84     
85     public String JavaDoc getName() {
86         return NbBundle.getMessage(AddModuleAction.class, "LBL_AddModuleAction");
87     }
88     
89     public HelpCtx getHelpCtx() {
90         return HelpCtx.DEFAULT_HELP;
91     }
92     
93     protected boolean asynchronous() {
94         return false;
95     }
96     
97     private Project[] getSelectedProjects(AntProjectHelper epp) throws UserCancelException {
98         Project[] allProjects = OpenProjects.getDefault().getOpenProjects();
99         List JavaDoc<Node> moduleProjectNodes = new LinkedList JavaDoc<Node>();
100         for (int i = 0; i < allProjects.length; i++) {
101             if (allProjects[i].getLookup().lookup(J2eeModule.class) != null &&
102                 allProjects[i].getLookup().lookup(J2eeModuleContainer.class) == null) {
103                 LogicalViewProvider lvp =
104                     (LogicalViewProvider) allProjects[i].getLookup().lookup(LogicalViewProvider.class);
105                 Node mn = lvp.createLogicalView();
106                 Node n = new FilterNode(mn, new FilterNode.Children(mn), Lookups.singleton(allProjects[i]));
107                 moduleProjectNodes.add(n);
108             }
109         }
110         Children.Array children = new Children.Array();
111         children.add(moduleProjectNodes.toArray(new Node[moduleProjectNodes.size()]));
112         final AbstractNode root = new AbstractNode(children);
113         String JavaDoc moduleSelector = NbBundle.getMessage(AddModuleAction.class, "LBL_ModuleSelectorTitle");
114         
115         Project parent = FileOwnerQuery.getOwner(epp.getProjectDirectory());
116         SubprojectProvider spp = (SubprojectProvider) parent.getLookup().lookup(SubprojectProvider.class);
117         if (null != spp) {
118             final Set JavaDoc s = spp.getSubprojects();
119             NodeAcceptor na = new NodeAcceptor() {
120                 public boolean acceptNodes(Node[] nodes) {
121                     for (int i = 0; i < nodes.length; i++) {
122                         if (nodes[i].getParentNode() != root) {
123                             return false;
124                         }
125                         // do not put this test befor the root test...
126
Project p = (Project) nodes[i].getLookup().lookup(Project.class);
127                         if (null == p)
128                             return false;
129                         if (s.contains(p)) return false;
130                     }
131                     return nodes.length > 0;
132                 }
133             };
134             root.setDisplayName(NbBundle.getMessage(AddModuleAction.class, "LBL_J2EEModules"));
135             root.setIconBaseWithExtension(FOLDER_ICON);
136             Node[] selected = NodeOperation.getDefault().select(moduleSelector, root.getDisplayName(), root, na);
137             Project[] modules = new Project[selected.length];
138             for (int i = 0; i < modules.length; i++) {
139                 modules[i] = (Project) selected[i].getLookup().lookup(Project.class);
140             }
141             return modules;
142       }
143         else {
144             return new Project[0];
145         }
146     }
147 }
148
Popular Tags