KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > queries > CollocationQuery


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.api.queries;
21
22 import java.io.File JavaDoc;
23 import org.netbeans.spi.queries.CollocationQueryImplementation;
24 import org.openide.filesystems.FileUtil;
25 import org.openide.util.Lookup;
26
27 /**
28  * Find out whether some files logically belong in one directory tree,
29  * for example as part of a VCS checkout.
30  * @see CollocationQueryImplementation
31  * @author Jesse Glick
32  */

33 public final class CollocationQuery {
34
35     private static final Lookup.Result<CollocationQueryImplementation> implementations =
36         Lookup.getDefault().lookupResult(CollocationQueryImplementation.class);
37     
38     private CollocationQuery() {}
39     
40     /**
41      * Check whether two files are logically part of one directory tree.
42      * For example, if both files are stored in CVS, with the same server
43      * (<code>CVSROOT</code>) they might be considered collocated.
44      * If nothing is known about them, return false.
45      * @param file1 one file
46      * @param file2 another file
47      * @return true if they are probably part of one logical tree
48      */

49     public static boolean areCollocated(File JavaDoc file1, File JavaDoc file2) {
50         if (!file1.equals(FileUtil.normalizeFile(file1))) {
51             throw new IllegalArgumentException JavaDoc("Parameter file1 was not "+ // NOI18N
52
"normalized. Was "+file1+" instead of "+FileUtil.normalizeFile(file1)); // NOI18N
53
}
54         if (!file2.equals(FileUtil.normalizeFile(file2))) {
55             throw new IllegalArgumentException JavaDoc("Parameter file2 was not "+ // NOI18N
56
"normalized. Was "+file2+" instead of "+FileUtil.normalizeFile(file2)); // NOI18N
57
}
58         for (CollocationQueryImplementation cqi : implementations.allInstances()) {
59             if (cqi.areCollocated(file1, file2)) {
60                 return true;
61             }
62         }
63         return false;
64     }
65     
66     /**
67      * Find a root of a logical tree containing this file, if any.
68      * @param file a file on disk
69      * @return an ancestor directory which is the root of a logical tree,
70      * if any (else null)
71      */

72     public static File JavaDoc findRoot(File JavaDoc file) {
73         if (!file.equals(FileUtil.normalizeFile(file))) {
74             throw new IllegalArgumentException JavaDoc("Parameter file was not "+ // NOI18N
75
"normalized. Was "+file+" instead of "+FileUtil.normalizeFile(file)); // NOI18N
76
}
77         for (CollocationQueryImplementation cqi : implementations.allInstances()) {
78             File JavaDoc root = cqi.findRoot(file);
79             if (root != null) {
80                 return root;
81             }
82         }
83         return null;
84     }
85     
86 }
87
Popular Tags