Java Code Examples for org.openide.filesystems.FileChangeListener

The following examples show how to use org.openide.filesystems.FileChangeListener. These examples are extracted from open source projects. 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 Project: netbeans   Source File: ProjectHelper.java    License: 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 2
Source Project: netbeans   Source File: GraphTopComponent.java    License: 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 3
Source Project: netbeans   Source File: OSXNotifierTest.java    License: 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 4
Source Project: netbeans   Source File: WatcherDeadlockTest.java    License: 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 5
Source Project: netbeans   Source File: FolderObjTest.java    License: 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 6
Source Project: netbeans   Source File: TemplatesPanel.java    License: 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 7
Source Project: netbeans   Source File: LinuxNotifier235632Test.java    License: 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 8
Source Project: netbeans   Source File: RootsListener.java    License: 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 9
Source Project: netbeans   Source File: MimeTypesTracker.java    License: 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 10
Source Project: netbeans   Source File: ProfilesTracker.java    License: 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 11
Source Project: netbeans   Source File: ProjectHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static void removeModelListner(Project prj, FileChangeListener l){
    // WeakChangeListener will return true for its proxy.
    FileObject fo = getFOForBindingConfigFile(prj);
    if (fo != null) {
        fo.removeFileChangeListener(l);
    }

    fo = getFOForNBProjectDir(prj);
    if (fo != null) {
        fo.removeFileChangeListener(l);
    }
}
 
Example 12
private static void safeAddFileListener(
        @NonNull final File file,
        @NonNull final FileChangeListener listener) {
    try {
        FileUtil.addFileChangeListener(listener, file);
    } catch (IllegalArgumentException e) {
        LOG.log(
                Level.WARNING,
                "Cannot add listener to: {0}",      //NOI18N
                file);
    }
}
 
Example 13
private static void safeRemoveFileListener(
        @NonNull final File file,
        @NonNull final FileChangeListener listener) {
    try {
        FileUtil.removeFileChangeListener(listener, file);
    } catch (IllegalArgumentException e) {
        LOG.log(
                Level.WARNING,
                "Cannot remove listener from: {0}",     //NOI18N
                file);
    }
}
 
Example 14
Source Project: netbeans   Source File: RunScriptAction.java    License: Apache License 2.0 5 votes vote down vote up
void dispose() {
    if (scriptFile != null) {
        
    }
    if (storage != null && scriptFile == null) {
        // unregister the root
        storage.removeChangeListener((ChangeListener)weakListen);
    } else if (scriptFile != null && scriptFile.isFolder()) {
        scriptFile.removeFileChangeListener((FileChangeListener)weakListen);
    }
}
 
Example 15
Source Project: netbeans   Source File: FileObjectKeeper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void fileDataCreated(FileEvent fe) {
    Collection<FileChangeListener> arr = listeners;
    if (arr == null) {
        return;
    }
    for (FileChangeListener l : arr) {
        l.fileDataCreated(fe);
    }
}
 
Example 16
Source Project: netbeans   Source File: FileObjectKeeper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void fileChanged(FileEvent fe) {
    Collection<FileChangeListener> arr = listeners;
    if (arr == null) {
        return;
    }
    for (FileChangeListener l : arr) {
        l.fileChanged(fe);
    }
}
 
Example 17
Source Project: netbeans   Source File: FileObjectKeeper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void fileRenamed(FileRenameEvent fe) {
    Collection<FileChangeListener> arr = listeners;
    if (arr == null) {
        return;
    }
    final FileObject f = fe.getFile();
    if (f.isFolder() && fe.getSource() == f && f != root) {
        // there will be another event for parent folder
        return;
    }
    for (FileChangeListener l : arr) {
        l.fileRenamed(fe);
    }
}
 
Example 18
Source Project: netbeans   Source File: FileObjectKeeper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void fileAttributeChanged(FileAttributeEvent fe) {
    Collection<FileChangeListener> arr = listeners;
    if (arr == null) {
        return;
    }
    for (FileChangeListener l : arr) {
        l.fileAttributeChanged(fe);
    }
}
 
Example 19
Source Project: netbeans   Source File: FolderObjTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testRefresh109490() throws Exception {
    final File wDir = getWorkDir();
    final FileObject wDirFo = FileBasedFileSystem.getFileObject(wDir);
    final List<FileEvent> fileEvents = new ArrayList<FileEvent>();
    FileSystem fs = wDirFo.getFileSystem();
    FileChangeListener fListener = new FileChangeAdapter() {

        @Override
        public void fileDataCreated(FileEvent fe) {
            super.fileDataCreated(fe);
            fileEvents.add(fe);
        }
    };
    try {
        fs.addFileChangeListener(fListener);

        File file = new File(wDir, "testao.f");
        File file2 = new File(wDir, "testc1.f");
        assertEquals(file.hashCode(), file2.hashCode());
        wDirFo.getChildren();
        assertTrue(file.createNewFile());
        assertTrue(file2.createNewFile());
        assertEquals(0, fileEvents.size());
        fs.refresh(true);
        assertEquals(2, fileEvents.size());
        assertEquals(Arrays.asList(wDirFo.getChildren()).toString(), 2,wDirFo.getChildren().length);
        assertTrue(Arrays.asList(wDirFo.getChildren()).toString().indexOf(file.getName()) != -1);            
        assertTrue(Arrays.asList(wDirFo.getChildren()).toString().indexOf(file2.getName()) != -1);                        
    } finally {
        fs.removeFileChangeListener(fListener);
    }
}
 
Example 20
Source Project: netbeans   Source File: FolderObjTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Test of delete method, of class org.netbeans.modules.masterfs.filebasedfs.fileobjects.FolderObj.
 */
public void testDelete() throws IOException {
    File f = testFile;
    
    FileObject testFo = FileBasedFileSystem.getFileObject(testFile);
    assertNotNull(testFo);
    
    final List<FileEvent> l = new ArrayList<FileEvent>();
    FileChangeListener fcl = new FileChangeAdapter () {
        @Override
        public void fileDeleted(FileEvent fe) {
            l.add(fe);
        }            
    };
    FileObject fo = FileUtil.createData(testFo, "delete/the/whole/structure/in/depth/todelete.txt");
    fo.addFileChangeListener(fcl);
    
    FileObject toDelete = testFo.getFileObject("delete");        
    assertNotNull(toDelete);
    toDelete.addFileChangeListener(fcl);

    FileObject toGC = testFo.getFileObject("delete/the/whole/structure");
    assertNotNull(toGC);
    Reference<FileObject> toGCRef = new WeakReference<FileObject>(toGC);
    toGC.addFileChangeListener(fcl);
    toGC = null;
    
    assertGC("", toGCRef);
    toDelete.delete();
    assertEquals(0,testFo.getChildren().length);
    toDelete = testFo.getFileObject("delete");        
    assertNull(toDelete);        
    assertEquals(2, l.size());
}
 
Example 21
Source Project: netbeans   Source File: BaseFileObjectTestHid.java    License: Apache License 2.0 5 votes vote down vote up
public void testRefresh109490() throws Exception {
    final File wDir = new File(getWorkDir(), getName());
    wDir.mkdir();
    final FileObject wDirFo = FileUtil.toFileObject(wDir);
    final List<FileEvent> fileEvents = new ArrayList<FileEvent>();
    FileSystem fs = wDirFo.getFileSystem();
    FileChangeListener fListener = new FileChangeAdapter(){
        @Override
            public void fileDataCreated(FileEvent fe) {
                super.fileDataCreated(fe);
                fileEvents.add(fe);
            }            
        };
    try {
        fs.refresh(true); // catch and skip changes made in VCS metadata, they are not part of this test
        fs.addFileChangeListener(fListener);

        File file = new File(wDir, "testao.f");
        File file2 = new File(wDir, "testc1.f");
        assertEquals(file.hashCode(), file2.hashCode());
        wDirFo.getChildren();
        assertTrue(file.createNewFile());
        assertTrue(file2.createNewFile());
        assertEquals(0, fileEvents.size());
        fs.refresh(true);
        assertEquals(2, fileEvents.size());
        assertEquals(Arrays.asList(wDirFo.getChildren()).toString(), 2,wDirFo.getChildren().length);
        assertTrue(Arrays.asList(wDirFo.getChildren()).toString().indexOf(file.getName()) != -1);            
        assertTrue(Arrays.asList(wDirFo.getChildren()).toString().indexOf(file2.getName()) != -1);                        
        
    } finally {
        fs.removeFileChangeListener(fListener);
    }
}
 
Example 22
Source Project: NBANDROID-V2   Source File: ApkDataObject.java    License: 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 23
Source Project: netbeans   Source File: JspSyntaxSupport.java    License: 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 24
Source Project: NBANDROID-V2   Source File: AndroidResValuesProvider.java    License: 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 25
Source Project: netbeans   Source File: MainClassUpdater.java    License: 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 26
Source Project: netbeans   Source File: ImportantFilesSupport.java    License: 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 27
Source Project: netbeans   Source File: GrailsCommandSupport.java    License: 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 28
Source Project: netbeans   Source File: ProjectClassPathImplementation.java    License: 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 29
Source Project: netbeans   Source File: Util.java    License: 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 30
Source Project: netbeans   Source File: Util.java    License: 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);
    }
}