KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > CvsVisibilityQuery


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.versioning.system.cvss;
20
21 import org.netbeans.spi.queries.VisibilityQueryImplementation;
22 import org.netbeans.modules.versioning.util.VersioningListener;
23 import org.netbeans.modules.versioning.util.VersioningEvent;
24 import org.openide.filesystems.FileObject;
25 import org.openide.filesystems.FileUtil;
26
27 import javax.swing.event.ChangeListener JavaDoc;
28 import javax.swing.event.ChangeEvent JavaDoc;
29 import java.util.*;
30 import java.io.File JavaDoc;
31
32 /**
33  * Hides folders that have 'Localy removed' status.
34  *
35  * @author Maros Sandor
36  */

37 public class CvsVisibilityQuery implements VisibilityQueryImplementation, VersioningListener {
38
39     private List<ChangeListener JavaDoc> listeners = new ArrayList<ChangeListener JavaDoc>();
40
41     public CvsVisibilityQuery() {
42         CvsVersioningSystem.getInstance().getStatusCache().addVersioningListener(this);
43     }
44
45     public boolean isVisible(FileObject fileObject) {
46         if (fileObject.isData()) return true;
47         File JavaDoc file = FileUtil.toFile(fileObject);
48         return file == null || !isHiddenFolder(file);
49     }
50
51     public synchronized void addChangeListener(ChangeListener JavaDoc l) {
52         ArrayList<ChangeListener JavaDoc> newList = new ArrayList<ChangeListener JavaDoc>(listeners);
53         newList.add(l);
54         listeners = newList;
55     }
56
57     public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
58         ArrayList<ChangeListener JavaDoc> newList = new ArrayList<ChangeListener JavaDoc>(listeners);
59         newList.remove(l);
60         listeners = newList;
61     }
62
63     public void versioningEvent(VersioningEvent event) {
64         if (event.getId() == FileStatusCache.EVENT_FILE_STATUS_CHANGED) {
65             File JavaDoc file = (File JavaDoc) event.getParams()[0];
66             if (file == null) return;
67             File JavaDoc parent = file.getParentFile();
68             if (parent == null) return;
69             File JavaDoc marker = new File JavaDoc(parent, "CVS/.nb-removed");
70             if (marker.exists()) {
71                 fireVisibilityChanged();
72             }
73         }
74     }
75
76     static boolean isHiddenFolder(File JavaDoc file) {
77         File JavaDoc marker = new File JavaDoc(file, "CVS/.nb-removed");
78         if (marker.exists()) {
79             File JavaDoc [] files = file.listFiles();
80             for (File JavaDoc child : files) {
81                 if (child.lastModified() > marker.lastModified() && !CvsVersioningSystem.FILENAME_CVS.equals(child.getName())) {
82                     marker.delete();
83                     return false;
84                 }
85             }
86             return true;
87         } else {
88             return false;
89         }
90     }
91     
92     private void fireVisibilityChanged() {
93         ChangeEvent JavaDoc event = new ChangeEvent JavaDoc(this);
94         for (ChangeListener JavaDoc listener : listeners) {
95             listener.stateChanged(event);
96         }
97     }
98 }
99
Popular Tags