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-2007 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.compapp.projects.jbi.ui.actions;
043:
044: import java.awt.event.ActionEvent;
045: import java.util.ArrayList;
046: import org.netbeans.api.project.Project;
047: import org.netbeans.modules.compapp.projects.jbi.CasaHelper;
048: import org.netbeans.modules.compapp.projects.jbi.JbiProject;
049: import org.netbeans.modules.compapp.projects.jbi.ProjectPropertyProvider;
050: import org.netbeans.modules.compapp.projects.jbi.ui.customizer.JbiProjectProperties;
051: import org.netbeans.modules.compapp.projects.jbi.ui.customizer.VisualClassPathItem;
052:
053: import org.openide.nodes.Node;
054:
055: import org.openide.util.HelpCtx;
056: import org.openide.util.NbBundle;
057:
058: import org.openide.windows.IOProvider;
059: import org.openide.windows.OutputWriter;
060:
061: import java.util.List;
062: import org.netbeans.modules.compapp.projects.jbi.JbiSubprojectProvider;
063: import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
064: import org.openide.DialogDisplayer;
065: import org.openide.NotifyDescriptor;
066: import org.openide.util.actions.SystemAction;
067:
068: /**
069: * DOCUMENT ME!
070: *
071: * @author
072: * @version
073: */
074: public class DeleteModuleAction extends SystemAction {
075: /**
076: * DOCUMENT ME!
077: *
078: * @param activatedNodes DOCUMENT ME!
079: *
080: * @return DOCUMENT ME!
081: */
082: protected boolean enable(Node[] activatedNodes) {
083: return true;
084: }
085:
086: /**
087: * DOCUMENT ME!
088: *
089: * @return DOCUMENT ME!
090: */
091: protected boolean asynchronous() {
092: return false;
093: }
094:
095: /**
096: * DOCUMENT ME!
097: *
098: * @param activatedNodes DOCUMENT ME!
099: */
100: public void performAction(Node[] activatedNodes) {
101: for (Node node : activatedNodes) {
102: String mName = node.getDisplayName();
103: //log("Delete Node: " + activatedNodes.length + ", " + mName);
104:
105: JbiProjectCookie jpc = (node.getParentNode()
106: .getCookie(JbiProjectCookie.class));
107:
108: if (jpc != null) {
109: JbiProject jbiProject = jpc.getProject();
110: CasaHelper.saveCasa(jbiProject);
111: boolean success = removeProject(jbiProject, mName);
112: if (!success) {
113: String msg = NbBundle.getMessage(
114: DeleteModuleAction.class,
115: "MSG_CantDeleteModule", mName); // NOI18N
116: NotifyDescriptor d = new NotifyDescriptor.Message(
117: msg, NotifyDescriptor.ERROR_MESSAGE);
118: DialogDisplayer.getDefault().notify(d);
119: }
120: }
121: }
122: }
123:
124: protected void deleteModuleProperties(Project jbiProject,
125: VisualClassPathItem vcpi, String artifactName) {
126: // No action, to be overridden by Java EE/module specific delete actions
127: }
128:
129: protected void updateModuleProperties(Project jbiProject,
130: JbiProjectProperties projProp,
131: List<VisualClassPathItem> subprojJars, String subProjName) {
132: // No action, to be overridden by Java EE/module specific delete actions
133: }
134:
135: /**
136: * Removes a JBI module from a JBI project.
137: *
138: * @param jbiProject a JBI project
139: * @param artifactName the artifact name of a JBI module
140: */
141: public boolean removeProject(final Project jbiProject,
142: String artifactName) {
143:
144: JbiProjectProperties projProperties = ((ProjectPropertyProvider) jbiProject)
145: .getProjectProperties();
146: @SuppressWarnings("unchecked")
147: List<VisualClassPathItem> oldCompProjList = (List) projProperties
148: .get(JbiProjectProperties.JBI_CONTENT_ADDITIONAL);
149: List<VisualClassPathItem> newCompProjList = new ArrayList<VisualClassPathItem>();
150: String subProjName = null;
151: int itemRemovedIndex = -1;
152:
153: Project subproject = null;
154:
155: for (int i = 0; i < oldCompProjList.size(); i++) {
156: VisualClassPathItem cp = oldCompProjList.get(i);
157:
158: if (artifactName.equalsIgnoreCase(cp.getShortName())) {
159: itemRemovedIndex = i;
160: deleteModuleProperties(jbiProject, cp, artifactName);
161: subProjName = cp.getProjectName();
162: subproject = cp.getAntArtifact().getProject();
163: } else {
164: newCompProjList.add(oldCompProjList.get(i));
165: }
166: }
167:
168: if (itemRemovedIndex != -1) {
169: // Need to keep target component list in sync.
170: @SuppressWarnings("unchecked")
171: List<String> targetComps = new ArrayList<String>(
172: (List) projProperties
173: .get(JbiProjectProperties.JBI_CONTENT_COMPONENT));
174: assert targetComps.size() == oldCompProjList.size() : "Properties jbi.content.additional and jbi.content.component are not in sync."; // NOI18N
175:
176: targetComps.remove(itemRemovedIndex);
177:
178: projProperties.put(
179: JbiProjectProperties.JBI_CONTENT_COMPONENT,
180: targetComps);
181:
182: projProperties.put(
183: JbiProjectProperties.JBI_CONTENT_ADDITIONAL,
184: newCompProjList);
185:
186: updateModuleProperties(jbiProject, projProperties,
187: newCompProjList, subProjName);
188: projProperties.store();
189:
190: jbiProject.getLookup().lookup(JbiSubprojectProvider.class)
191: .subprojectRemoved(subproject);
192:
193: return true;
194: } else {
195: return false;
196: }
197: }
198:
199: /**
200: * DOCUMENT ME!
201: *
202: * @return DOCUMENT ME!
203: */
204: public String getName() {
205: return NbBundle.getMessage(DeleteModuleAction.class,
206: "LBL_DeleteModuleAction_Name"); // NOI18N
207: }
208:
209: /**
210: * DOCUMENT ME!
211: *
212: * @return DOCUMENT ME!
213: */
214: public HelpCtx getHelpCtx() {
215: return HelpCtx.DEFAULT_HELP;
216:
217: // If you will provide context help then use:
218: // return new HelpCtx(AddModuleAction.class);
219: }
220:
221: private void log(String str) {
222: OutputWriter out = IOProvider.getDefault().getStdOut();
223: out.println(str);
224: out.flush();
225: }
226:
227: public void actionPerformed(ActionEvent ev) {
228: throw new UnsupportedOperationException("Not supported yet.");
229: }
230: }
|