1 19 package org.netbeans.modules.java.source; 20 21 import java.io.IOException ; 22 import javax.swing.event.ChangeEvent ; 23 import javax.swing.event.ChangeListener ; 24 import javax.swing.text.Document ; 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 38 public class ActivatedDocumentListener implements ChangeListener { 39 40 private static ActivatedDocumentListener INSTANCE; 41 42 public static void register() { 43 INSTANCE = new ActivatedDocumentListener(); 44 } 45 46 private FileObject lastValidFile; 47 48 51 private ActivatedDocumentListener() { 52 Registry.addChangeListener(this); 53 } 54 55 public synchronized void stateChanged(ChangeEvent e) { 56 Document active = Registry.getMostActiveDocument(); 57 58 if (active == null) 59 return ; 60 61 Object 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())) { RepositoryUpdater.getDefault().scheduleCompilation(lastValidFile, owner); 85 } 86 } catch (IOException 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 return ; 100 } 101 102 lastValidFile = activeFile; 103 104 TimesCollector.getDefault().select(activeFile); 105 JavaSourceAccessor.INSTANCE.revalidate(activeJS); 106 } 107 108 111 public static boolean IGNORE_COMPILE_REQUESTS = false; 112 113 } 114 | Popular Tags |