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.projecthandler;
042:
043: import com.sun.collablet.CollabException;
044:
045: import org.openide.filesystems.FileChangeAdapter;
046: import org.openide.filesystems.FileEvent;
047: import org.openide.filesystems.FileObject;
048: import org.openide.filesystems.FileRenameEvent;
049: import org.openide.filesystems.FileSystem;
050:
051: import java.io.File;
052: import java.io.IOException;
053:
054: import javax.swing.Action;
055:
056: import org.netbeans.api.project.Project;
057:
058: import org.netbeans.modules.collab.channel.filesharing.FilesharingConstants;
059: import org.netbeans.modules.collab.channel.filesharing.FilesharingContext;
060: import org.netbeans.modules.collab.channel.filesharing.FilesharingEventNotifierFactory;
061: import org.netbeans.modules.collab.channel.filesharing.FilesharingEventProcessorFactory;
062: import org.netbeans.modules.collab.channel.filesharing.event.DeleteFileEvent;
063: import org.netbeans.modules.collab.channel.filesharing.event.RenameFileEvent;
064: import org.netbeans.modules.collab.channel.filesharing.filehandler.CollabFileHandler;
065: import org.netbeans.modules.collab.channel.filesharing.mdc.CollabEvent;
066: import org.netbeans.modules.collab.channel.filesharing.mdc.EventContext;
067: import org.netbeans.modules.collab.channel.filesharing.mdc.EventNotifier;
068: import org.netbeans.modules.collab.channel.filesharing.mdc.EventProcessor;
069: import org.netbeans.modules.collab.channel.filesharing.util.FileshareUtil;
070: import org.netbeans.modules.collab.core.Debug;
071:
072: /**
073: * SharedProject
074: *
075: * @author ayub.khan@sun.com
076: * @version 1.0
077: */
078: public class SharedProject extends Object implements
079: FilesharingConstants {
080: ////////////////////////////////////////////////////////////////////////////
081: // Constants
082: ////////////////////////////////////////////////////////////////////////////
083: public final static int UNKNOWN_TYPE = -1;
084: public final static int ANT_TYPE = 1;
085:
086: ////////////////////////////////////////////////////////////////////////////
087: // Instance variables
088: ////////////////////////////////////////////////////////////////////////////
089: private String projectID;
090: private String projectName;
091: private int projectType = UNKNOWN_TYPE;
092: private String projectOwner;
093: private SharedProjectManager manager = null;
094: private Project originalProject;
095: private Action[] pNodeactions;
096: private boolean isValid = true;
097: private EventNotifier fileChangeNotifier;
098: private FileSystem fs;
099: private FileChangeAdapter fcl;
100:
101: /**
102: *
103: * @param projectName
104: * @param user
105: * @param manager
106: */
107: public SharedProject(String projectName, String projectOwner,
108: Project originalProject, SharedProjectManager manager) {
109: super ();
110: this .projectID = projectOwner + "_" + projectName;
111: this .projectName = projectName;
112: this .projectOwner = projectOwner;
113: this .originalProject = originalProject;
114: this .manager = manager;
115: addFilesystemListener();
116: }
117:
118: ////////////////////////////////////////////////////////////////////////////
119: // methods
120: ////////////////////////////////////////////////////////////////////////////
121:
122: /**
123: *
124: * @return
125: */
126: public String getID() {
127: return projectID;
128: }
129:
130: /**
131: *
132: * @return
133: */
134: public String getName() {
135: return projectName;
136: }
137:
138: /**
139: *
140: * @return
141: */
142: public int getType() {
143: return this .projectType;
144: }
145:
146: /**
147: *
148: * @return
149: */
150: public String getProjectOwner() {
151: return this .projectOwner;
152: }
153:
154: /**
155: *
156: * @param user
157: */
158: public void setProjectOwner(String newUser) {
159: this .projectOwner = newUser;
160: }
161:
162: /**
163: *
164: * @return
165: */
166: public Project getOriginalProject() {
167: return originalProject;
168: }
169:
170: /**
171: *
172: * @return
173: */
174: public void setOriginalProject(Project originalProject) {
175: this .originalProject = originalProject;
176: }
177:
178: /**
179: *
180: * @return
181: */
182: public Action[] getProjectActions() {
183: return this .pNodeactions;
184: }
185:
186: /**
187: *
188: * @return
189: */
190: public void setProjectActions(Action[] pNodeactions) {
191: this .pNodeactions = pNodeactions;
192: }
193:
194: private FilesharingContext getContext() {
195: return (manager != null) ? manager.getContext() : null;
196: }
197:
198: private void addFilesystemListener() {
199: if (getOriginalProject() == null) {
200: return;
201: }
202:
203: try {
204: //create a notifer first
205: EventProcessor ep = FilesharingEventProcessorFactory
206: .getDefault().createEventProcessor(
207: getContext().getProcessorConfig(),
208: getContext());
209: fileChangeNotifier = FilesharingEventNotifierFactory
210: .getDefault().createEventNotifier(
211: getContext().getNotifierConfig(), ep);
212:
213: try {
214: fs = getOriginalProject().getProjectDirectory()
215: .getFileSystem();
216: } catch (Exception e) {
217: Debug.out
218: .println("SharedProject getFileSystem() failed"
219: + e);
220: e.printStackTrace(Debug.out);
221: }
222:
223: if (fs == null) {
224: return;
225: }
226:
227: fcl = new SharedProjectFileListener();
228: fs.addFileChangeListener(fcl);
229: } catch (CollabException e) {
230: e.printStackTrace(Debug.out);
231: }
232: }
233:
234: protected void finalize() throws Throwable {
235: super .finalize();
236:
237: if ((fs != null) && (fcl != null)) {
238: fs.removeFileChangeListener(fcl);
239: }
240: }
241:
242: /**
243: * setValid
244: *
245: * @param status
246: */
247: public void setValid(boolean valid) {
248: this .isValid = valid;
249: }
250:
251: /**
252: * getValid
253: *
254: * @return status if false handler is invalid
255: */
256: public boolean isValid() {
257: return this .isValid;
258: }
259:
260: public class SharedProjectFileListener extends FileChangeAdapter {
261: public void fileDeleted(FileEvent fe) {
262: FileObject fObj = fe.getFile();
263: Debug.out.println("File Delete: " + fObj.getNameExt());
264:
265: try {
266: if (!getContext().getSharedFileGroupManager().isShared(
267: fObj)) {
268: return;
269: }
270:
271: CollabFileHandler fh = getContext()
272: .getSharedFileGroupManager().getFileHandler(
273: fObj);
274: FileshareUtil
275: .deleteFileLink(fh.getName(), getContext());
276:
277: EventContext evContext = new EventContext(
278: DeleteFileEvent.getEventID(), fh);
279: CollabEvent ce = new DeleteFileEvent(evContext);
280: fileChangeNotifier.notify(ce);
281: } catch (Exception e) {
282: Debug.out.println("Exception sendDelete: " + e);
283: e.printStackTrace(Debug.out);
284: }
285: }
286:
287: public void fileRenamed(FileRenameEvent fre) {
288: FileObject fObj = fre.getFile();
289: String origName = fre.getName();
290: String origExt = fre.getExt();
291: Debug.out.println("File Rename from: " + origName + "."
292: + origExt);
293: Debug.out.println("File Rename to: " + fObj.getNameExt());
294:
295: try {
296: Debug.out.println("check isShared: "
297: + fObj.getNameExt());
298:
299: if (!getContext().getSharedFileGroupManager().isShared(
300: fObj)) {
301: return;
302: }
303:
304: CollabFileHandler fh = getContext()
305: .getSharedFileGroupManager().getFileHandler(
306: fObj);
307: String fileName = fh.getName();
308: Debug.out.println("shared: " + fileName);
309:
310: String folder = fileName.substring(0, fileName
311: .lastIndexOf(FILE_SEPERATOR));
312: File oldflnk = null;
313: File newflnk = null;
314: int lastIndex = fileName.lastIndexOf('.');
315:
316: if (lastIndex != -1) {
317: oldflnk = manager.getCollabFilesystem()
318: .getAbsoluteFile(
319: fileName.substring(0, lastIndex)
320: + ".shadow"); //NoI18n
321: newflnk = manager.getCollabFilesystem()
322: .getAbsoluteFile(
323: folder + FILE_SEPERATOR
324: + fObj.getName()
325: + ".shadow"); //NoI18n
326: } else {
327: oldflnk = manager.getCollabFilesystem()
328: .getAbsoluteFile(fileName + ".shadow"); //NoI18n
329: newflnk = manager.getCollabFilesystem()
330: .getAbsoluteFile(
331: folder + FILE_SEPERATOR
332: + fObj.getName()
333: + ".shadow"); //NoI18n
334: }
335:
336: final File oldfile = oldflnk;
337: final File newfile = newflnk;
338:
339: try {
340: manager.getCollabFilesystem().runAtomicAction(
341: new FileSystem.AtomicAction() {
342: public void run() throws IOException {
343: Debug.out
344: .println("File Link Rename: "
345: + oldfile.getPath());
346: oldfile.renameTo(newfile);
347: manager.getCollabFilesystem()
348: .refresh(false);
349: }
350: });
351: } catch (IOException iox) {
352: Debug.out.println("File Link Rename::ex: " + iox);
353: iox.printStackTrace(Debug.out);
354: } catch (Throwable th) {
355: Debug.out.println("File Link Rename::ex: " + th);
356: th.printStackTrace(Debug.out);
357: }
358:
359: //fh.setFileName(folder+FILE_SEPERATOR+fObj.getNameExt());
360: EventContext evContext = new EventContext(
361: RenameFileEvent.getEventID(), fh);
362: CollabEvent ce = new RenameFileEvent(evContext);
363: fileChangeNotifier.notify(ce);
364: } catch (Exception e) {
365: Debug.out.println("Exception sendRename: " + e);
366: e.printStackTrace(Debug.out);
367: }
368: }
369: }
370: }
|