KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > ActivatedDocumentListener


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.java.source;
20
21 import java.io.IOException JavaDoc;
22 import javax.swing.event.ChangeEvent JavaDoc;
23 import javax.swing.event.ChangeListener JavaDoc;
24 import javax.swing.text.Document JavaDoc;
25 import org.netbeans.api.java.classpath.ClassPath;
26 import org.netbeans.api.java.source.JavaSource;
27 import org.netbeans.api.timers.TimesCollector;
28 import org.netbeans.editor.Registry;
29 import org.netbeans.modules.java.source.usages.RepositoryUpdater;
30 import org.openide.filesystems.FileObject;
31 import org.openide.loaders.DataObject;
32 import org.openide.util.Exceptions;
33
34 /**
35  *
36  * @author Jan Lahoda
37  */

38 public class ActivatedDocumentListener implements ChangeListener JavaDoc {
39     
40     private static ActivatedDocumentListener INSTANCE;
41     
42     public static void register() {
43         INSTANCE = new ActivatedDocumentListener();
44     }
45     
46     private FileObject lastValidFile;
47     
48     /**
49      * Creates a new instance of ActivatedDocumentListener
50      */

51     private ActivatedDocumentListener() {
52         Registry.addChangeListener(this);
53     }
54
55     public synchronized void stateChanged(ChangeEvent JavaDoc e) {
56         Document JavaDoc active = Registry.getMostActiveDocument();
57         
58         if (active == null)
59             return ;
60         
61         Object JavaDoc sourceProperty = active.getProperty(Document.StreamDescriptionProperty);
62         
63         if (!(sourceProperty instanceof DataObject))
64             return ;
65         
66         DataObject source = (DataObject) sourceProperty;
67         
68         if (source == null)
69             return ;
70         
71         FileObject activeFile = source.getPrimaryFile();
72         
73         if (lastValidFile == activeFile)
74             return;
75         
76         if (lastValidFile != null) {
77             if (!IGNORE_COMPILE_REQUESTS) {
78                 ClassPath cp = ClassPath.getClassPath(lastValidFile, ClassPath.SOURCE);
79                 if (cp != null) {
80                     FileObject owner = cp.findOwnerRoot(lastValidFile);
81                     assert owner != null;
82                     try {
83                         if ("file".equals(lastValidFile.getURL().getProtocol())) { //NOI18N
84
RepositoryUpdater.getDefault().scheduleCompilation(lastValidFile, owner);
85                         }
86                     } catch (IOException JavaDoc ioe) {
87                         Exceptions.printStackTrace(ioe);
88                     }
89                 }
90             }
91             
92             lastValidFile = null;
93         }
94         
95         JavaSource activeJS = JavaSource.forFileObject(activeFile);
96         
97         if (activeJS == null) {
98             //not a Java document:
99
return ;
100         }
101         
102         lastValidFile = activeFile;
103         
104         TimesCollector.getDefault().select(activeFile);
105         JavaSourceAccessor.INSTANCE.revalidate(activeJS);
106     }
107
108     /**Set to switch off compilation, usefull in tests.
109      * Use SourceUtilsTestUtil.ignoreCompileRequests to set it:
110      */

111     public static boolean IGNORE_COMPILE_REQUESTS = false;
112
113 }
114
Popular Tags