KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.event.ChangeListener JavaDoc;
23 import org.netbeans.spi.queries.FileBuiltQueryImplementation;
24 import org.openide.filesystems.FileObject;
25 import org.openide.util.Lookup;
26
27 // XXX is the Status interface scalable enough for efficient use even from e.g. a Look?
28
// May need to be revisited, perhaps with an optional more efficient implementation...
29

30 /**
31  * Test whether a file can be considered to be built (up to date).
32  * @see FileBuiltQueryImplementation
33  * @author Jesse Glick
34  */

35 public final class FileBuiltQuery {
36     
37     private static final Lookup.Result<FileBuiltQueryImplementation> implementations =
38         Lookup.getDefault().lookupResult(FileBuiltQueryImplementation.class);
39     
40     private FileBuiltQuery() {}
41     
42     /**
43      * Check whether a (source) file has been <em>somehow</em> built
44      * or processed.
45      * <div class="nonnormative">
46      * <p>
47      * This would typically mean that at least its syntax has been
48      * validated by a build system, some conventional output file exists
49      * and is at least as new as the source file, etc.
50      * For example, for a <samp>Foo.java</samp> source file, this could
51      * check whether <samp>Foo.class</samp> exists (in the appropriate
52      * build directory) with at least as new a timestamp.
53      * </p>
54      * <p>
55      * <strong>Implementation note:</strong> the current implementation of this
56      * method does not react to changes in lookup results for
57      * {@link FileBuiltQueryImplementation}. For example, if there is initially
58      * no provider for a given file, the return value may be null, and a client
59      * will not be see the change if a provider is later installed dynamically.
60      * Similarly, removal of a provider will not automatically invalidate an
61      * existing {@link Status} object; and a change in the provider responsible
62      * for a given file will not produce updates in an existing {@link Status}.
63      * A future implementation may however be enhanced to return proxy statuses
64      * which react to changes in the provider responsible for the file and always
65      * delegate to the current provider, if there is one.
66      * </p>
67      * </div>
68      * @param file a source file which can be built to a direct product
69      * @return a status object that can be listened to, or null for no answer
70      */

71     public static Status getStatus(FileObject file) {
72         if (!file.isValid()) {
73             // Probably a race condition of some kind, abort gracefully.
74
return null;
75         }
76         for (FileBuiltQueryImplementation fbqi : implementations.allInstances()) {
77             Status s = fbqi.getStatus(file);
78             if (s != null) {
79                 return s;
80             }
81         }
82         return null;
83     }
84     
85     /**
86      * Result of getting built status for a file.
87      * Besides encoding the actual result, it permits listening to changes.
88      * @see #getStatus
89      */

90     public interface Status {
91         
92         /**
93          * Check whether the file is currently built.
94          * @return true if it is up-to-date, false if it may still need to be built
95          */

96         boolean isBuilt();
97         
98         /**
99          * Add a listener to changes.
100          * @param l a listener to add
101          */

102         void addChangeListener(ChangeListener JavaDoc l);
103         
104         /**
105          * Stop listening to changes.
106          * @param l a listener to remove
107          */

108         void removeChangeListener(ChangeListener JavaDoc l);
109         
110     }
111     
112 }
113
Popular Tags