001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.earproject.ui.actions;
043:
044: import java.util.LinkedList;
045: import java.util.List;
046: import java.util.Set;
047: import org.netbeans.api.project.Project;
048: import org.netbeans.api.project.ui.OpenProjects;
049: import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeApplicationProvider;
050: import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
051: import org.netbeans.spi.project.ui.LogicalViewProvider;
052: import org.netbeans.spi.project.SubprojectProvider;
053: import org.openide.nodes.AbstractNode;
054: import org.openide.nodes.Children;
055: import org.openide.nodes.FilterNode;
056: import org.openide.nodes.Node;
057: import org.openide.nodes.NodeAcceptor;
058: import org.openide.nodes.NodeOperation;
059: import org.openide.util.HelpCtx;
060: import org.openide.util.NbBundle;
061: import org.openide.util.UserCancelException;
062: import org.openide.util.actions.CookieAction;
063: import org.openide.util.lookup.Lookups;
064:
065: import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties;
066:
067: import org.netbeans.spi.project.support.ant.AntProjectHelper;
068: import org.netbeans.modules.j2ee.earproject.EarProject;
069: import org.netbeans.api.project.FileOwnerQuery;
070:
071: /**
072: * Action that allows selection and assembly of J2EE module projects.
073: * @author Chris Webster
074: * @author vince kraemer
075: */
076: public class AddModuleAction extends CookieAction {
077: private static final long serialVersionUID = 1L;
078:
079: private static final String FOLDER_ICON = "org/netbeans/modules/j2ee/earproject/ui/resources/folder.gif";
080:
081: private static final Class[] COOKIE_ARRAY = new Class[] { AntProjectHelper.class };
082:
083: public Class[] cookieClasses() {
084: return COOKIE_ARRAY;
085: }
086:
087: public int mode() {
088: return CookieAction.MODE_EXACTLY_ONE;
089: }
090:
091: public void performAction(Node[] activeNodes) {
092: try {
093: AntProjectHelper aph = activeNodes[0].getLookup().lookup(
094: AntProjectHelper.class);
095: Project[] moduleProjects = getSelectedProjects(aph);
096: // XXX Vince add code here to add to application.xml and
097: // build script
098: Project p = FileOwnerQuery.getOwner(aph
099: .getProjectDirectory());
100: EarProject ep = p.getLookup().lookup(EarProject.class);
101: EarProjectProperties.addJ2eeSubprojects(ep, moduleProjects);
102: } catch (UserCancelException uce) {
103: // this action has been cancelled
104: }
105: }
106:
107: public String getName() {
108: return NbBundle.getMessage(AddModuleAction.class,
109: "LBL_AddModuleAction");
110: }
111:
112: public HelpCtx getHelpCtx() {
113: return HelpCtx.DEFAULT_HELP;
114: }
115:
116: @Override
117: protected boolean asynchronous() {
118: return false;
119: }
120:
121: private Project[] getSelectedProjects(AntProjectHelper epp)
122: throws UserCancelException {
123: Project[] allProjects = OpenProjects.getDefault()
124: .getOpenProjects();
125: List<Node> moduleProjectNodes = new LinkedList<Node>();
126: for (int i = 0; i < allProjects.length; i++) {
127: if (allProjects[i].getLookup().lookup(
128: J2eeModuleProvider.class) != null
129: && allProjects[i].getLookup().lookup(
130: J2eeApplicationProvider.class) == null) {
131: LogicalViewProvider lvp = allProjects[i].getLookup()
132: .lookup(LogicalViewProvider.class);
133: Node mn = lvp.createLogicalView();
134: Node n = new FilterNode(mn,
135: new FilterNode.Children(mn), Lookups
136: .singleton(allProjects[i]));
137: moduleProjectNodes.add(n);
138: }
139: }
140: Children.Array children = new Children.Array();
141: children.add(moduleProjectNodes
142: .toArray(new Node[moduleProjectNodes.size()]));
143: final AbstractNode root = new AbstractNode(children);
144: String moduleSelector = NbBundle.getMessage(
145: AddModuleAction.class, "LBL_ModuleSelectorTitle");
146:
147: Project parent = FileOwnerQuery.getOwner(epp
148: .getProjectDirectory());
149: SubprojectProvider spp = parent.getLookup().lookup(
150: SubprojectProvider.class);
151: if (null != spp) {
152: final Set s = spp.getSubprojects();
153: NodeAcceptor na = new NodeAcceptor() {
154: public boolean acceptNodes(Node[] nodes) {
155: for (int i = 0; i < nodes.length; i++) {
156: if (nodes[i].getParentNode() != root) {
157: return false;
158: }
159: // do not put this test befor the root test...
160: Project p = nodes[i].getLookup().lookup(
161: Project.class);
162: if (null == p) {
163: return false;
164: }
165: if (s.contains(p)) {
166: return false;
167: }
168: }
169: return nodes.length > 0;
170: }
171: };
172: root.setDisplayName(NbBundle.getMessage(
173: AddModuleAction.class, "LBL_J2EEModules"));
174: root.setIconBaseWithExtension(FOLDER_ICON);
175: Node[] selected = NodeOperation.getDefault().select(
176: moduleSelector, root.getDisplayName(), root, na);
177: Project[] modules = new Project[selected.length];
178: for (int i = 0; i < modules.length; i++) {
179: modules[i] = selected[i].getLookup().lookup(
180: Project.class);
181: }
182: return modules;
183: } else {
184: return new Project[0];
185: }
186: }
187: }
|