org.openide.filesystems.FileChangeListener Java Examples

The following examples show how to use org.openide.filesystems.FileChangeListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RootsListener.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void safeAddRecursiveListener(
        @NonNull final FileChangeListener listener,
        @NonNull final File path,
        @NullAllowed final ClassPath.Entry entry) {
    if (USE_RECURSIVE_LISTENERS) {
        final FileFilter filter = entry == null?
            null:
            (pathname) -> {
                try {
                    return entry.includes(BaseUtilities.toURI(pathname).toURL());
                } catch (MalformedURLException ex) {
                    Exceptions.printStackTrace(ex);
                    return true;
                }
            };
        performAsync(() -> {
                FileUtil.addRecursiveListener(
                    listener,
                    path,
                    filter,
                    () -> !listens);
                return null;
            });
    }
}
 
Example #2
Source File: LinuxNotifier235632Test.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Test of nextEvent method, of class LinuxNotifier.
 *
 * @throws java.lang.Exception
 */
public void testNextEvent() throws Exception {

    prepareFiles();

    final AtomicBoolean folder2refreshed = new AtomicBoolean(false);
    Logger log = Logger.getLogger(FolderObj.class.getName());

    Handler h = createHandler(folder2refreshed);
    log.addHandler(h);
    try {
        FileChangeListener l = new FileChangeAdapter();
        FileUtil.addFileChangeListener(l, folder1text1Txt);
        // This causes an IN_IGNORED native event.
        FileUtil.removeFileChangeListener(l, folder1text1Txt);
        // Native listeners may need some time.
        Thread.sleep(2000);
    } finally {
        log.removeHandler(h);
    }
    assertFalse("Folder folder2 should not be refreshed.",
            folder2refreshed.get());
}
 
Example #3
Source File: GraphTopComponent.java    From netbeans with Apache License 2.0 6 votes vote down vote up
ChangeSupport(
        @NonNull final FileObject file,
        @NonNull final Runnable resetAction) {
    Parameters.notNull("file", file);   //NOI18N
    Parameters.notNull("resetAction", resetAction); //NOI18N
    this.resetAction = resetAction;
    this.file = file;
    this.file.addFileChangeListener(WeakListeners.create(FileChangeListener.class, this, this.file));
    EditorCookie.Observable cookie = null;
    try {
        final DataObject dobj = DataObject.find(file);
        cookie = dobj.getLookup().lookup(EditorCookie.Observable.class);
    } catch (DataObjectNotFoundException e) {
        //pass
    }
    this.ec = cookie;
    if (this.ec != null) {
        this.ec.addPropertyChangeListener(WeakListeners.propertyChange(this, this.ec));
        assignDocListener(this.ec);
    }
}
 
Example #4
Source File: ProjectHelper.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static void addCfgFileChangeListener(Project prj, 
        FileChangeListener l){
    
    FileObject fo = getFOForBindingConfigFile(prj);
    FileChangeListener fcl = null;
    if (fo != null) {
        fcl = FileUtil.weakFileChangeListener(l, fo);
        fo.addFileChangeListener(fcl);
    } else {
        fo = getFOForNBProjectDir(prj);
        if (fo != null) {
            fcl = FileUtil.weakFileChangeListener(l, fo);                
            fo.addFileChangeListener(fcl);
        }
    }
}
 
Example #5
Source File: TemplatesPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean acceptTemplate (FileObject fo) {
    if (fo.isFolder() &&
        (TEMPLATES_FOLDER+"/Properties").equals(fo.getPath())) {
        
        return false;
    }
    boolean attachListener;
    synchronized (filesWeListenOn) {
        attachListener = filesWeListenOn.add(fo);
    }
    if (attachListener) {
        FileChangeListener fileChangeListener = FileUtil.weakFileChangeListener(this, fo);
        fo.addFileChangeListener(fileChangeListener);
    }
    if (isTemplate (fo) || fo.isFolder()) {
        Object o = fo.getAttribute ("simple"); // NOI18N
        return o == null || Boolean.TRUE.equals (o);
    } else {
        return false;
    }
}
 
Example #6
Source File: FolderObjTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testRefresh69744() throws Exception {
    File thisTest = new File(getWorkDir(),"thisTest");
    thisTest.createNewFile();
    FileObject testf = FileBasedFileSystem.getFileObject(thisTest);
    assertNotNull(testf);
    assertGC("",new WeakReference<FileObject>(testf.getParent()));
    modifyFileObject(testf, "abc");
    FileSystem fs = testf.getFileSystem();
    final List<FileEvent> l = new ArrayList<FileEvent>();
    FileChangeListener fcl = new FileChangeAdapter() {
        @Override
        public void fileChanged(FileEvent fe) {
            l.add(fe);
        }
    };
    Thread.sleep(1500);
    fs.addFileChangeListener(fcl);
    try {
        modifyFileObject(testf, "def");
        assertFalse(l.isEmpty());
    } finally {
        fs.removeFileChangeListener(fcl);
    }
}
 
Example #7
Source File: OSXNotifierTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Test
public void testNextEvent() throws Exception {
    final AtomicBoolean folder2refreshed = new AtomicBoolean(false);
    Logger log = Logger.getLogger(FolderObj.class.getName());

    Handler h = createHandler(folder2refreshed);
    log.addHandler(h);
    try {
        FileChangeListener l = new FileChangeAdapter();
        FileUtil.addFileChangeListener(l, folder1text1Txt);
        FileUtil.removeFileChangeListener(l, folder1text1Txt);
        Thread.sleep(2000);
    } finally {
        log.removeHandler(h);
    }
    assertFalse("Folder folder2 should not be refreshed.",
            folder2refreshed.get());
}
 
Example #8
Source File: MimeTypesTracker.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new tracker for tracking mime types under the <code>basePath</code>
 * folder.
 * 
 * @param settingsType The type of settings to track mime types for. If not
 *   <code>null</code> the tracker will only list mime types that declare
 *   settings of this type.
 * @param basePath The path on the system <code>FileSystem</code> where the
 *   mime types should be tracked.
 */
/* package */ MimeTypesTracker(SettingsType.Locator locator, String basePath) {
    this.locator = locator;
    this.basePath = basePath;
    this.basePathElements = basePath.split("/"); //NOI18N
    
    rebuild();
    
    // Start listening
    this.listener = new Listener();
    FileSystem sfs = null;
    try {
        sfs = FileUtil.getConfigRoot().getFileSystem();
    } catch (FileStateInvalidException ex) {
        Exceptions.printStackTrace(ex);
    }
    sfs.addFileChangeListener(WeakListeners.create(FileChangeListener.class,listener, sfs));
}
 
Example #9
Source File: ProfilesTracker.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new instance of ProfilesTracker.
 * 
 * @param type 
 * @param mimeTypes 
 * @param strict 
 */
/* package */ProfilesTracker(Locator locator, MimeTypesTracker mimeTypes) {
    this.locator = locator;
    this.mimeTypes = mimeTypes;

    rebuild();

    // Start listening
    this.listener = new Listener();
    FileSystem sfs = null;
    try {
        sfs = FileUtil.getConfigRoot().getFileSystem();
    } catch (FileStateInvalidException ex) {
        Exceptions.printStackTrace(ex);
    }
    this.systemFileSystem = sfs;
    this.systemFileSystem.addFileChangeListener(WeakListeners.create(FileChangeListener.class, listener, this.systemFileSystem));
    this.mimeTypes.addPropertyChangeListener(listener);
}
 
Example #10
Source File: WatcherDeadlockTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testDeadlockWhileRefesh() throws IOException {
    clearWorkDir();
    
    MockServices.setServices(Watcher.class, AnnotationProviderImpl.class);
    final File root = new File(getWorkDir(), "root");
    
    File f = new File(new File(new File(root, "x"), "y"), "z");
    f.mkdirs();
    final FileObject r = FileUtil.toFileObject(root);
    r.refresh(true);
    
    Set<FileObject> all = new HashSet<FileObject>();
    Enumeration<? extends FileObject> en = r.getChildren(true);
    while (en.hasMoreElements()) {
        FileObject fileObject = en.nextElement();
        all.add(fileObject);
    }
    assertEquals("Some files: " + all, 3, all.size());
    
    FileChangeListener l = new FileChangeAdapter();
    FileUtil.addRecursiveListener(l, root);
    
    FileChangeListener l2 = new FileChangeAdapter();
    FileUtil.addRecursiveListener(l2, root);
}
 
Example #11
Source File: ReadOnlyFilesHighlighting.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void checkFileStatus(FileObject fo) {
    boolean update = false;
    synchronized (this) {
        if (lastFile == null || lastFile.get() != fo) {
            lastFile = new WeakReference<FileObject>(fo);
            update = true;
        }
    }
    if (update) {
        fo.addFileChangeListener(WeakListeners.create(FileChangeListener.class, this, fo));
        boolean readOnly = !fo.canWrite(); // Access without monitor on this class and document's lock
        updateFileReadOnly(readOnly);
    }
}
 
Example #12
Source File: ProjectClassPathImplementation.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void updateListener(FileChangeListener listener, File oldDir, File newDir) {
    if (oldDir == null || !oldDir.equals(newDir)) {
        if (oldDir != null) {
            FileUtil.removeFileChangeListener(listener, oldDir);
        }
        if (newDir != null) {
            FileUtil.addFileChangeListener(listener, newDir);
        }
    }
}
 
Example #13
Source File: ErrorAnnotator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void ensureListensOnFS(FileObject f) {
    try {
        FileSystem fs = f.getFileSystem();
        if (!system2RecursiveListener.containsKey(fs)) {
            FileChangeListener l = new RootAddedDeletedListener();
            system2RecursiveListener.put(fs, l);
            fs.addFileChangeListener(l);
        }
    } catch (FileStateInvalidException ex) {
        LOG.log(Level.FINE, null, ex);
    }
}
 
Example #14
Source File: Util.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Removes given listener to all FileSystems
 * @param l listener to remove
 */    
public static void removeFileSystemsListener(FileChangeListener l) {
    FileSystem[] fileSystems = getFileSystems();
    for (int i=0; i<fileSystems.length; i++) {
        fileSystems[i].removeFileChangeListener(l);
    }
}
 
Example #15
Source File: ImportantFilesSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates new support for the given directory and file name(s).
 * @param directory directory
 * @param fileNames file name(s)
 * @return new support
 */
public static ImportantFilesSupport create(FileObject directory, String... fileNames) {
    Parameters.notNull("directory", directory); // NOI18N
    Parameters.notNull("fileNames", fileNames); // NOI18N
    ImportantFilesSupport support = new ImportantFilesSupport(directory, fileNames);
    directory.addFileChangeListener(WeakListeners.create(FileChangeListener.class, support.fileChangeListener, directory));
    return support;
}
 
Example #16
Source File: HistoryComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void unregisterFileListeners() {
    synchronized(listeners) {
        if(!listeners.isEmpty()) {
            Iterator<Entry<FileObject, FileChangeListener>> it = listeners.entrySet().iterator();
            while(it.hasNext()) {
                Entry<FileObject, FileChangeListener> e = it.next();
                e.getKey().removeFileChangeListener(e.getValue());
                it.remove();
            }
        }
    }
}
 
Example #17
Source File: EditorContextDispatcher.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static void addFileChangeListener(FileObject fo, FileChangeListener l) {
    //System.err.println("addFileChangeListener("+fo+") in AWT");
    //long start = System.nanoTime();
    doWork(new Work(AddRemove.ADD, fo, l));
    //long end = System.nanoTime();
    //System.err.println("addFileChangeListener("+fo+") in AWT done in "+((end-start)/1000000.0)+"ms.");
}
 
Example #18
Source File: AndroidResValuesProvider.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
private void init() {
    for (File srcDir : androidProject.getDefaultConfig().getSourceProvider().getResDirectories()) {
        if (!srcDir.exists() || srcDir.isFile()) {
            continue;
        }
        FileObject fo = FileUtil.toFileObject(srcDir).getFileObject("values");
        if (fo != null && fo.isValid() && fo.isFolder()) {
            parseFolder(fo);
            fo.addRecursiveListener(WeakListeners.create(FileChangeListener.class, this, fo));
        }
    }
}
 
Example #19
Source File: GrailsCommandSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void updateListener(FileChangeListener listener, File oldDir, File newDir) {
    if (oldDir == null || !oldDir.equals(newDir)) {
        if (oldDir != null) {
            FileUtil.removeFileChangeListener(listener, oldDir);
        }
        if (newDir != null) {
            FileUtil.addFileChangeListener(listener, newDir);
        }
    }
}
 
Example #20
Source File: MainClassUpdater.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void addFileChangeListener () {
    synchronized (MainClassUpdater.this) {
        if (current != null && listener != null) {
            current.removeFileChangeListener(listener);
            current = null;
            listener = null;
        }            
    }
    final String mainClassName = MainClassUpdater.this.eval.getProperty(mainClassPropName);
    if (mainClassName != null) {
        try {
            FileObject[] roots = sourcePath.getRoots();
            if (roots.length>0) {
                ClassPath bootCp = ClassPath.getClassPath(roots[0], ClassPath.BOOT);
                ClassPath compileCp = ClassPath.getClassPath(roots[0], ClassPath.COMPILE);
                final ClasspathInfo cpInfo = ClasspathInfo.create(bootCp, compileCp, sourcePath);
                JavaSource js = JavaSource.create(cpInfo);
                js.runWhenScanFinished(new Task<CompilationController>() {

                    public void run(CompilationController c) throws Exception {
                        TypeElement te = c.getElements().getTypeElement(mainClassName);
                         if (te != null) {
                            synchronized (MainClassUpdater.this) {
                                current = SourceUtils.getFile(te, cpInfo);
                                listener = WeakListeners.create(FileChangeListener.class, MainClassUpdater.this, current);
                                if (current != null && sourcePath.contains(current)) {
                                    current.addFileChangeListener(listener);
                                }
                            }
                        }                            
                    }

                }, true);
            }
        } catch (IOException ioe) {
        }
    }        
}
 
Example #21
Source File: ImportantFilesSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates new support for the given directory and file name(s).
 * @param directory directory
 * @param fileNames file name(s)
 * @return new support
 */
public static ImportantFilesSupport create(FileObject directory, String... fileNames) {
    Parameters.notNull("directory", directory); // NOI18N
    Parameters.notNull("fileNames", fileNames); // NOI18N
    ImportantFilesSupport support = new ImportantFilesSupport(directory, fileNames);
    directory.addFileChangeListener(WeakListeners.create(FileChangeListener.class, support.fileChangeListener, directory));
    return support;
}
 
Example #22
Source File: SingleDiffPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void setListeners () {
    FileObject baseParent = base.getParent();
    FileObject modifiedParent = modified.getParent();
    if (baseParent != null) {
        baseParent.addFileChangeListener(WeakListeners.create(FileChangeListener.class, baseFCL = new DiffFileChangeListener(), baseParent));
    }
    if (baseParent != modifiedParent && modifiedParent != null) {
        modifiedParent.addFileChangeListener(WeakListeners.create(FileChangeListener.class, modifiedFCL = new DiffFileChangeListener(), modifiedParent));
    }
}
 
Example #23
Source File: DefaultDataLoadersBridge.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * @param fo The file object to listen upon at DataObject level
 * @param fcl The FileChangeListener to be called when the DataObject has been invalidated
 * 
 * @throws org.openide.loaders.DataObjectNotFoundException
 */
public DataObjectListener(FileObject fo, FileChangeListener fcl) throws DataObjectNotFoundException {
    this.fobj = fo;
    this.flisten = fcl;
    this.dobj = DataObject.find(fo);
    wlistener = WeakListeners.propertyChange(this, dobj);
    this.dobj.addPropertyChangeListener(wlistener);
}
 
Example #24
Source File: Util.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Adds given listener to all FileSystems
 * @param l listener to add
 */    
public static void addFileSystemsListener(FileChangeListener l) {
    FileSystem[] fileSystems = getFileSystems();
    for (int i=0; i<fileSystems.length; i++) {
        fileSystems[i].addFileChangeListener(l);
    }
}
 
Example #25
Source File: EditorContextDispatcher.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static void removeFileChangeListener(FileObject fo, FileChangeListener l) {
    //System.err.println("removeFileChangeListener("+fo+") in AWT");
    //long start = System.nanoTime();
    doWork(new Work(AddRemove.REMOVE, fo, l));
    //long end = System.nanoTime();
    //System.err.println("removeFileChangeListener("+fo+") in AWT done in "+((end-start)/1000000.0)+"ms.");
}
 
Example #26
Source File: JspSyntaxSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void initFileObject() {
    DataObject dobj = NbEditorUtilities.getDataObject(getDocument());
        if(dobj != null)  {
            fobj = NbEditorUtilities.getDataObject(getDocument()).getPrimaryFile();
            fobj.addFileChangeListener(WeakListeners.create(FileChangeListener.class, this, fobj));
        }
}
 
Example #27
Source File: ApkDataObject.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
public ApkFilterNode(Node original) {
    super(original, org.openide.nodes.Children.LEAF, new ProxyLookup(original.getLookup(), Lookups.fixed(new SignInfoHolder())));
    holder = getLookup().lookup(SignInfoHolder.class);
    project = FileOwnerQuery.getOwner(pf);
    pf.addFileChangeListener(WeakListeners.create(FileChangeListener.class, this, pf));
    refreshIcons();
}
 
Example #28
Source File: JSLineBreakpoint.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public JSLineBreakpoint(EditorLineHandler line) {
    this.line = line;
    lineChangesWeak = WeakListeners.propertyChange(lineChangeslistener, line);
    line.addPropertyChangeListener(lineChangesWeak);
    FileObject fileObject = line.getFileObject();
    if( fileObject != null ){
        myWeakListener = WeakListeners.create( 
                FileChangeListener.class, myListener, fileObject);
        fileObject.addFileChangeListener( myWeakListener );
    }
}
 
Example #29
Source File: JSLineBreakpoint.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void setLineHandler(EditorLineHandler line) {
    dispose();
    EditorLineHandler oldLine = this.line;
    this.line = line;
    lineChangesWeak = WeakListeners.propertyChange(lineChangeslistener, line);
    line.addPropertyChangeListener(lineChangesWeak);
    FileObject fileObject = line.getFileObject();
    if (fileObject != null) {
        myWeakListener = WeakListeners.create(
                FileChangeListener.class, myListener, fileObject);
        fileObject.addFileChangeListener(myWeakListener);
    }
    firePropertyChange(PROP_FILE, oldLine.getFileObject(), line.getFileObject());
    firePropertyChange(PROP_LINE_NUMBER, oldLine.getLineNumber(), line.getLineNumber());
}
 
Example #30
Source File: RootsListener.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void safeAddFileChangeListener(
        @NonNull final FileChangeListener listener,
        @NonNull final File path) {
    performSave(() -> {
            FileUtil.addFileChangeListener(listener, path);
            return null;
        });
}