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: package org.netbeans.modules.collab.channel.filesharing.ui;
042:
043: import org.openide.loaders.DataObject;
044: import org.openide.nodes.*;
045: import org.openide.util.NbBundle;
046: import org.openide.util.actions.SystemAction;
047:
048: import java.util.ArrayList;
049: import java.util.List;
050:
051: import javax.swing.Action;
052:
053: import org.netbeans.modules.collab.channel.filesharing.FilesharingContext;
054: import org.netbeans.modules.collab.channel.filesharing.ui.actions.CreateNonProjectAction;
055: import org.netbeans.modules.collab.core.Debug;
056:
057: /**
058: *
059: * @author Todd Fast, todd.fast@sun.com
060: */
061: public class SharedNonProjectNode extends FilterNode {
062: ////////////////////////////////////////////////////////////////////////////
063: // Instance variables
064: ////////////////////////////////////////////////////////////////////////////
065: private String displayName;
066: private boolean isLocal = false;
067: private boolean isFolder = true;
068: private FilesharingContext context = null;
069:
070: /**
071: *
072: *
073: */
074: public SharedNonProjectNode(Node node, boolean isLocal,
075: FilesharingContext context) {
076: this (node.getName(), node, isLocal, context);
077: }
078:
079: /**
080: *
081: *
082: */
083: public SharedNonProjectNode(String displayName, Node node,
084: boolean isLocal, FilesharingContext context) {
085: super (node, new SharedNonProjectNodeChildren(node, isLocal,
086: context));
087: this .displayName = displayName;
088: this .isLocal = isLocal;
089: this .context = context;
090:
091: DataObject d = (DataObject) this .getCookie(DataObject.class);
092:
093: if ((d != null) && (d.getPrimaryFile() != null)
094: && d.getPrimaryFile().isData()) {
095: this .isFolder = false;
096: }
097: }
098:
099: /**
100: *
101: *
102: */
103: public SharedNonProjectNode(Node node, boolean isLocal,
104: FilesharingContext context, boolean canHaveChild) {
105: super (node);
106: this .isLocal = isLocal;
107: this .context = context;
108:
109: DataObject d = (DataObject) this .getCookie(DataObject.class);
110:
111: if ((d != null) && (d.getPrimaryFile() != null)
112: && d.getPrimaryFile().isData()) {
113: this .isFolder = false;
114: }
115: }
116:
117: /**
118: *
119: *
120: */
121: public String getDisplayName() {
122: return NbBundle.getMessage(SharedProjectNode.class,
123: "LBL_SharedFileNode_FileAnnotation",
124: (this .displayName != null) ? this .displayName : super
125: .getDisplayName());
126: }
127:
128: public Action[] getActions(boolean context) {
129: Debug.out.println("SNPN getActions: context");
130:
131: SystemAction[] actions = super .getActions();
132: Debug.out.println("SNPN getActions: " + actions);
133:
134: List newActions = new ArrayList();
135:
136: if (isLocal && isFolder) {
137: newActions.add(new CreateNonProjectAction(this .context,
138: this ));
139: }
140:
141: for (int i = 0; i < actions.length; i++) {
142: Action action = actions[i];
143:
144: if (action != null) {
145: String actionName = (String) action
146: .getValue(Action.NAME);
147: Debug.out.println("actionName: " + actionName);
148: Debug.out.println("isLocal: " + isLocal);
149:
150: DataObject d = (DataObject) this
151: .getCookie(DataObject.class);
152:
153: if (d != null) {
154: if ((d.getPrimaryFile() != null)
155: && d.getPrimaryFile().isData()) {
156: if (isLocal || strContains(actionName, "Edit")) { //NoI18n
157: newActions.add(actions[i]);
158: }
159: } else {
160: if (isLocal
161: && !strContains(actionName, "Rename")) { //NoI18n
162: newActions.add(actions[i]);
163: }
164: }
165: }
166: }
167: }
168:
169: return (SystemAction[]) newActions.toArray(new SystemAction[0]);
170: }
171:
172: public boolean canCopy() {
173: return true;
174: }
175:
176: public boolean canCut() {
177: return false;
178: }
179:
180: public boolean canDestroy() {
181: return isLocal;
182: }
183:
184: public boolean canRename() {
185: return isLocal && !isFolder;
186: }
187:
188: private static boolean strContains(String str, String pattern) {
189: return str.indexOf(pattern) != -1;
190: }
191:
192: /** Represents children of SharedNonProjectNode
193: *
194: */
195: private static class SharedNonProjectNodeChildren extends
196: FilterNode.Children {
197: private boolean isLocal;
198: private FilesharingContext context = null;
199:
200: /**
201: *
202: *
203: */
204: public SharedNonProjectNodeChildren(Node node, boolean isLocal,
205: FilesharingContext context) {
206: super (node);
207: this .isLocal = isLocal;
208: this .context = context;
209: }
210:
211: /**
212: *
213: *
214: */
215: public boolean add(Node[] nodes) {
216: Debug.out.println("SNPN add: " + nodes.length);
217:
218: for (int i = 0; i < nodes.length; i++) {
219: if (super .findChild(nodes[i].getName()) == null) {
220: super .add(createNodes(nodes[i]));
221: }
222: }
223:
224: return true;
225: }
226:
227: /**
228: *
229: *
230: */
231: protected Node[] createNodes(Node node) {
232: Debug.out.println("In SNPN createNodes: ");
233:
234: Node[] result = new Node[] {};
235:
236: try {
237: DataObject d = (DataObject) node
238: .getCookie(DataObject.class);
239:
240: if (d != null) {
241: if (d.getPrimaryFile().isData()) {
242: String fileName = d.getPrimaryFile()
243: .getNameExt();
244:
245: if (fileName.equals(".nbattrs")
246: || fileName.startsWith(".LCK")) {
247: return new Node[] {};
248: }
249:
250: result = new Node[] { new SharedNonProjectNode(
251: d.getNodeDelegate(), this .isLocal,
252: this .context, false) };
253: } else {
254: result = new Node[] { new SharedNonProjectNode(
255: d.getNodeDelegate(), this .isLocal,
256: this .context) };
257: }
258: }
259: } catch (Exception e) {
260: Debug.debugNotify(e);
261: }
262:
263: return result;
264: }
265: }
266: }
|