KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > 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.retouche.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.retouche.source.Source;
27 import org.netbeans.editor.Registry;
28 import org.netbeans.modules.retouche.source.usages.RepositoryUpdater;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.loaders.DataObject;
32 import org.openide.util.Exceptions;
33
34 /**
35  * This file is originally from Retouche, the Java Support
36  * infrastructure in NetBeans. I have modified the file as little
37  * as possible to make merging Retouche fixes back as simple as
38  * possible.
39  *
40  *
41  * @author Jan Lahoda
42  */

43 public class ActivatedDocumentListener implements ChangeListener JavaDoc {
44     
45     private static ActivatedDocumentListener INSTANCE;
46     
47     public static void register() {
48         INSTANCE = new ActivatedDocumentListener();
49     }
50     
51     private FileObject lastValidFile;
52     
53     /**
54      * Creates a new instance of ActivatedDocumentListener
55      */

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

116     public static boolean IGNORE_COMPILE_REQUESTS = false;
117
118 }
119
Popular Tags