KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > queries > AlwaysRelativeCollocationQuery


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
20 package org.netbeans.modules.queries;
21
22 import java.io.File JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26 import org.netbeans.spi.queries.CollocationQueryImplementation;
27
28 /**
29  * The CollocationQueryImplementation which makes all the paths in the single tree
30  * relative. On the UNIX all the files lies within a single tree, all the files
31  * are collocated. On the Windows (the VMS) all the files from single disk (volume) are collocated,
32  * files lying on the different disks (volumes) are not collocated.
33  * @author Tomas Zezula
34  */

35 public class AlwaysRelativeCollocationQuery implements CollocationQueryImplementation {
36
37     private File JavaDoc[] roots;
38
39     /** Creates a new instance of AlwaysRelativeCollocationQuery */
40     public AlwaysRelativeCollocationQuery() {
41     }
42
43     public File JavaDoc findRoot(File JavaDoc file) {
44         final File JavaDoc[] roots = getFileSystemRoots ();
45         if (roots.length == 0) {
46             assert false : "Cannot find filesystem roots";
47             return null;
48         }
49         else if (roots.length == 1) {
50             //On UNIX always relative
51
return roots[0];
52         }
53         else {
54             final Set JavaDoc<File JavaDoc> rootsSet = new HashSet JavaDoc<File JavaDoc>(Arrays.asList(this.roots != null ? this.roots : roots));
55             return getRoot (file, rootsSet);
56         }
57     }
58
59     public boolean areCollocated(File JavaDoc file1, File JavaDoc file2) {
60         File JavaDoc root1 = findRoot (file1);
61         File JavaDoc root2 = findRoot (file2);
62         return root1 != null && root1.equals(root2);
63     }
64
65     // ---------------- Unit test helper methods -----------------------
66

67     private File JavaDoc[] getFileSystemRoots () {
68         if (this.roots != null) {
69             return this.roots;
70         }
71         else {
72             return File.listRoots();
73         }
74     }
75
76     private File JavaDoc getRoot(File JavaDoc f, final Set JavaDoc<File JavaDoc> roots) {
77         //We have to compare the file to File.listRoots(),
78
//the test file.getParent() == null does not work on Windows
79
//when the file was selected from the JFileChooser and user browsed
80
//through the "This Computer" node
81
while (f != null && !roots.contains(f)) {
82             f = f.getParentFile();
83         }
84         return f;
85     }
86
87     final void setFileSystemRoots (File JavaDoc[] roots) {
88         this.roots = roots;
89     }
90
91 }
92
Popular Tags