1 19 package org.netbeans.modules.retouche.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.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 43 public class ActivatedDocumentListener implements ChangeListener { 44 45 private static ActivatedDocumentListener INSTANCE; 46 47 public static void register() { 48 INSTANCE = new ActivatedDocumentListener(); 49 } 50 51 private FileObject lastValidFile; 52 53 56 private ActivatedDocumentListener() { 57 Registry.addChangeListener(this); 58 } 59 60 public synchronized void stateChanged(ChangeEvent e) { 61 Document active = Registry.getMostActiveDocument(); 62 63 if (active == null) 64 return ; 65 66 Object 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())) { RepositoryUpdater.getDefault().scheduleCompilation(lastValidFile, owner); 90 } 91 } catch (IOException 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 return ; 105 } 106 107 lastValidFile = activeFile; 108 109 SourceAccessor.INSTANCE.revalidate(activeJS); 111 } 112 113 116 public static boolean IGNORE_COMPILE_REQUESTS = false; 117 118 } 119 | Popular Tags |