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;
043:
044: import java.io.IOException;
045: import java.io.File;
046:
047: import org.netbeans.modules.compapp.projects.jbi.ui.actions.DeleteModuleAction;
048: import org.netbeans.modules.compapp.projects.jbi.ui.customizer.VisualClassPathItem;
049: import org.netbeans.modules.compapp.projects.jbi.JbiProject;
050: import org.netbeans.modules.compapp.projects.jbi.api.JbiInstalledProjectPluginInfo;
051: import org.netbeans.modules.compapp.projects.jbi.api.InternalProjectTypePlugin;
052: import org.netbeans.modules.compapp.projects.jbi.api.JbiProjectActionPerformer;
053: import org.netbeans.api.project.Project;
054: import org.openide.actions.DeleteAction;
055: import org.openide.actions.CustomizeAction;
056:
057: import org.openide.nodes.*;
058:
059: import org.openide.util.HelpCtx;
060: import org.openide.util.Utilities;
061: import org.openide.util.RequestProcessor;
062: import org.openide.util.actions.SystemAction;
063: import org.openide.filesystems.FileUtil;
064:
065: import java.awt.*;
066: import java.util.List;
067: import java.util.ArrayList;
068:
069: import javax.swing.*;
070:
071: /**
072: * A simple node with no children. Often used in conjunction with some kind of underlying data
073: * model, where each node represents an element in that model. In this case, you should see the
074: * Container Node template which will permit you to create a whole tree of such nodes with the
075: * proper behavior.
076: *
077: * @author Tientien Li
078: */
079: public class JbiModuleNode extends AbstractNode implements Node.Cookie {
080: // static private Action[] actions = null;
081: List<Action> actions = null;
082:
083: private VisualClassPathItem model;
084: private InternalProjectTypePlugin plugin;
085: private Project suProj;
086:
087: // will frequently accept an element from some data model in the constructor:
088: public JbiModuleNode(VisualClassPathItem key) {
089: super (Children.LEAF);
090: model = key;
091: setName("preferablyUniqueNameForThisNodeAmongSiblings"); // NOI18N or, super.setName if needed
092: setDisplayName(key.getShortName());
093: suProj = model.getAntArtifact().getProject();
094: if (suProj != null) {
095: plugin = JbiInstalledProjectPluginInfo.getPlugin(suProj);
096: }
097: //setShortDescription(NbBundle.getMessage(JbiModuleNode.class, "HINT_ModuleNode"));
098: }
099:
100: // Create the popup menu:
101: @Override
102: public Action[] getActions(boolean context) {
103: if (null == actions) {
104: actions = new ArrayList<Action>();
105: actions.add(SystemAction.get(DeleteAction.class));
106: if ((plugin != null) && (plugin.hasCustomizer())) {
107: actions.add(SystemAction.get(CustomizeAction.class));
108: }
109: getCookieSet().add(this );
110: }
111:
112: return actions.toArray(new Action[actions.size()]);
113: }
114:
115: @Override
116: public boolean canDestroy() {
117: return true;
118: }
119:
120: @Override
121: public void destroy() throws IOException {
122: super .destroy();
123:
124: // remove internal su project files...
125: if (plugin != null) {
126: List<JbiProjectActionPerformer> acts = plugin
127: .getProjectActions();
128: for (JbiProjectActionPerformer act : acts) {
129: if (act.getActionType().equalsIgnoreCase(
130: JbiProjectActionPerformer.ACT_DELETE_PROJECT)) {
131: // perform plug-in specific delete action...
132: act.perform(suProj);
133: return;
134: }
135: }
136: }
137:
138: DeleteModuleAction deleteModuleAction = SystemAction
139: .get(DeleteModuleAction.class);
140: deleteModuleAction.performAction(new Node[] { this });
141:
142: // clean up the internal plug-in project generated files..
143: if (this .getParentNode() != null) { // TMP fix for NPE
144: JbiProject jbiProj = ((JbiModuleViewNode) this
145: .getParentNode()).getProject();
146: if (JbiInstalledProjectPluginInfo.isInternalSubproject(
147: jbiProj, suProj)) {
148: RequestProcessor.getDefault().post(new Runnable() {
149: public void run() {
150: boolean ok = deleteDir(FileUtil.toFile(suProj
151: .getProjectDirectory()));
152: }
153: });
154: }
155: }
156: }
157:
158: private boolean deleteDir(File dir) {
159: if (dir.isDirectory()) {
160: String[] children = dir.list();
161: for (int i = 0; i < children.length; i++) {
162: boolean success = deleteDir(new File(dir, children[i]));
163: if (!success) {
164: return false;
165: }
166: }
167: }
168:
169: return dir.delete();
170: }
171:
172: private Image getProjIcon() {
173: Icon ic = null;
174: Image ret = null;
175: if (this .model != null) {
176: ic = this .model.getProjectIcon();
177: if (ic instanceof ImageIcon) {
178: ret = ((ImageIcon) ic).getImage();
179: }
180: }
181:
182: return ret;
183: }
184:
185: @Override
186: public boolean hasCustomizer() {
187: if ((plugin != null) && (plugin.hasCustomizer())) {
188: return true;
189: }
190: return false;
191: }
192:
193: @Override
194: public Component getCustomizer() {
195: if ((plugin != null) && (plugin.hasCustomizer())) {
196: return plugin.getCustomizer(suProj);
197: }
198: return null;
199: }
200:
201: /**
202: * DOCUMENT ME!
203: *
204: * @param type DOCUMENT ME!
205: *
206: * @return DOCUMENT ME!
207: */
208: @Override
209: public Image getIcon(int type) {
210: Image ret = getProjIcon();
211: if (ret == null) {
212: ret = Utilities
213: .loadImage("org/netbeans/modules/compapp/projects/jbi/ui/resources/jar.gif"); // NOI18N
214: }
215:
216: return ret;
217: }
218:
219: /**
220: * DOCUMENT ME!
221: *
222: * @param type DOCUMENT ME!
223: *
224: * @return DOCUMENT ME!
225: */
226: @Override
227: public Image getOpenedIcon(int type) {
228: return getIcon(type);
229: }
230:
231: /**
232: * DOCUMENT ME!
233: *
234: * @return DOCUMENT ME!
235: */
236: @Override
237: public HelpCtx getHelpCtx() {
238: return HelpCtx.DEFAULT_HELP;
239: }
240: }
|