Java Code Examples for org.openide.filesystems.FileUtil#removeFileChangeListener()

The following examples show how to use org.openide.filesystems.FileUtil#removeFileChangeListener() . 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: 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 2
Source File: WatchedFile.java    From netbeans with Apache License 2.0 6 votes vote down vote up
void clear(boolean newFile) {
    if (newFile) {
        synchronized (this) {
            if (file != null) {
                try {
                    FileUtil.removeFileChangeListener(fileListener, file);
                    LOGGER.log(Level.FINE, "Stopped listening to {0}", file);
                } catch (IllegalArgumentException ex) {
                    // not listeneing yet, ignore
                    LOGGER.log(Level.FINE, "Not listening yet to {0}", file);
                }
                LOGGER.log(Level.FINE, "Clearing cached file path {0}", file);
                file = null;
            }
        }
    }
    fireChange();
}
 
Example 3
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 4
Source File: BuildXml.java    From netbeans with Apache License 2.0 6 votes vote down vote up
void reset(boolean newFile) {
    if (newFile) {
        synchronized (this) {
            if (buildXml != null) {
                try {
                    FileUtil.removeFileChangeListener(buildXmlListener, buildXml);
                    LOGGER.log(Level.FINE, "Stopped listening to {0}", buildXml);
                } catch (IllegalArgumentException ex) {
                    // not listeneing yet, ignore
                    LOGGER.log(Level.FINE, "Not listening yet to {0}", buildXml);
                }
                buildXml = null;
            }
        }
    }
    // fire change
    changeSupport.fireChange();
}
 
Example 5
Source File: EjbJarProject.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void initialize() throws FileStateInvalidException {
    EjbJarProject.this.evaluator().addPropertyChangeListener(this);
    
    if (!isCopyOnSaveEnabled()) {
        return;
    }
    
    metaBase = getEjbModule().getMetaInf();
    metaBaseValue = evaluator().getProperty(EjbJarProjectProperties.META_INF);
    if (resources != null) {
        FileUtil.removeFileChangeListener(this, resources);
    }
    resources = getEjbModule().getResourceDirectory();
    buildClasses = evaluator().getProperty(ProjectProperties.BUILD_CLASSES_DIR);

    if (metaBase != null) {
        metaBase.getFileSystem().addFileChangeListener(this);
    }

    if (resources != null) {
        FileUtil.addFileChangeListener(this, resources);
    }

    LOGGER.log(Level.FINE, "Meta directory is {0}", metaBaseValue);
}
 
Example 6
Source File: WebProject.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void cleanup() throws FileStateInvalidException {
    if (docBase != null) {
        docBase.removeRecursiveListener(this);
    }
    if (webInf != null && !FileUtil.isParentOf(docBase, webInf)) {
        webInf.removeRecursiveListener(this);
    }
    if (resources != null) {
        FileUtil.removeFileChangeListener(this, resources);
        resources = null;
    }

    WebProject.this.evaluator().removePropertyChangeListener(this);

    webModule.getConfigSupport().removeDeployOnSaveListener(this);
}
 
Example 7
Source File: EjbJarProject.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void cleanup() throws FileStateInvalidException {
    if (metaBase != null) {
        metaBase.getFileSystem().removeFileChangeListener(this);
    }
    if (resources != null) {
        FileUtil.removeFileChangeListener(this, resources);
        resources = null;
    }

    EjbJarProject.this.evaluator().removePropertyChangeListener(this);
}
 
Example 8
Source File: ChangeLiveSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void destroy() {
    if (sourceChangeRoots.length == 0) {
        FileUtil.removeFileChangeListener(sourceChangeListener);
    } else {
        for (File root : sourceChangeRoots) {
            FileUtil.removeRecursiveListener(sourceChangeListener, root);
        }
    }
}
 
Example 9
Source File: J2SEActionProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void safeRemoveFileChangeListener(@NonNull final File f) {
    try {
        FileUtil.removeFileChangeListener(this, f);
    } catch (IllegalArgumentException e) {
        //not important
    }
}
 
Example 10
Source File: DomainXMLChangeListener.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Register this listener for Payara instance configuration file
 * <code>domain.xml</code> changes.
 * <p/>
 * @param instance Payara server instance.
 */
public static void unregisterListener(PayaraInstance instance) {
    String domainDirPath = instance.getDomainsFolder();
    String domainName = instance.getDomainName();
    String domainXMLName = org.netbeans.modules.payara.tooling.utils.ServerUtils
            .getDomainConfigFile(domainDirPath, domainName);
    File configPAth = FileUtil.normalizeFile(new File(domainXMLName));
    FileUtil.removeFileChangeListener(
            instance.getDomainXMLChangeListener(), configPAth);
}
 
Example 11
Source File: DomainXMLChangeListener.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Register this listener for GlassFish instance configuration file
 * <code>domain.xml</code> changes.
 * <p/>
 * @param instance GlassFish server instance.
 */
public static void unregisterListener(GlassfishInstance instance) {
    String domainDirPath = instance.getDomainsFolder();
    String domainName = instance.getDomainName();
    String domainXMLName = org.netbeans.modules.glassfish.tooling.utils.ServerUtils
            .getDomainConfigFile(domainDirPath, domainName);
    File configPAth = FileUtil.normalizeFile(new File(domainXMLName));
    FileUtil.removeFileChangeListener(
            instance.getDomainXMLChangeListener(), configPAth);
}
 
Example 12
Source File: LibrariesNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void removePropertyChangeListener(@NonNull final PropertyChangeListener listener) {
    Parameters.notNull("listener", listener);   //NOI18N
    if (!state.compareAndSet(1, 2)) {
        throw new IllegalStateException("Already in state: " + state.get());    //NOI18N
    }
    support.removePropertyChangeListener(listener);
    for (File f : listensOn) {
        FileUtil.removeFileChangeListener(this, f);
    }
}
 
Example 13
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 14
Source File: NbMavenProject.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void removeWatchedPath(URI uri) {
    //#110599
    boolean removeListener;
    File fil = Utilities.toFile(uri);
    synchronized (files) {
//#216001 addWatchedPath appeared to accumulate files forever on each project reload.
//that's because source roots get added repeatedly but never get removed and listening to changed happens elsewhere.
//the imagined fix for 216001 is to keep each item just once. That would break once multiple sources add a given file and one of them removes it
//as a hotfix this solution is ok, if we don't get some data updated, we should remove the watchedPath pattern altogether.
        removeListener = files.remove(fil);
    }
    if (removeListener) {
        FileUtil.removeFileChangeListener(listener, fil);
    }
}
 
Example 15
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 16
Source File: PersistenceScopesHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Call this method in order to change the persistence scope returned by the
 * <code>PersistenceScopes</code> instance returned by {@link #getPersistenceScopes}
 * or the corresponding persistence.xml file.
 *
 * @param  newPersistenceScope the new persistence scope; can be null, but in this case
 *         the <code>newPersistenceXml</code> parameter must be null too.
 * @param  newPersistenceXml the new persistence.xml file; can be null.
 *
 * @throws IllegalArgumentException if <code>newPersistenceScope</code> is null
 *         and <code>newPersistenceXml</code> is not.
 */
public void changePersistenceScope(PersistenceScope newPersistenceScope, File newPersistenceXml) {
    if (newPersistenceScope == null && newPersistenceXml != null) {
        throw new IllegalArgumentException("The persistenceScope parameter cannot be null when newPersistenceXml is non-null"); // NOI18N
    }

    boolean oldPersistenceExists, newPersistenceExists;
    PersistenceScope oldPersistenceScope;

    synchronized (this) {
        oldPersistenceExists = persistenceExists;
        oldPersistenceScope = persistenceScope;

        LOG.fine("changePersistenceScope: newPersistenceXml=" + newPersistenceXml); // NOI18N

        if (persistenceXml != null) {
            FileUtil.removeFileChangeListener(fileChangeListener, persistenceXml);
        }
        if (newPersistenceXml != null) {
            persistenceXml = newPersistenceXml;
            FileUtil.addFileChangeListener(fileChangeListener, persistenceXml);
        } else {
            persistenceXml = null;
        }

        persistenceScope = newPersistenceScope;
        persistenceXml = newPersistenceXml;

        change();

        newPersistenceExists = persistenceExists;

        LOG.fine("changePersistenceScope: oldPersistenceExists=" + oldPersistenceExists + ", newPersistenceExists=" + newPersistenceExists); // NOI18N
    }

    if (oldPersistenceExists != newPersistenceExists || (oldPersistenceScope != newPersistenceScope && newPersistenceExists)) {
        LOG.fine("changePersistenceScope: firing PROP_PERSISTENCE_SCOPES change"); // NOI18N
        propChangeSupport.firePropertyChange(PersistenceScopes.PROP_PERSISTENCE_SCOPES, null, null);
    }
}
 
Example 17
Source File: ModuleOraculum.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@CheckForNull
private String computeModuleIfAbsent(
        @NonNull FileObject root,
        @NonNull final Function<FileObject,String> provider) {
    final Pair<Pair<Reference<FileObject>,File>,String> entry = modNameCache.get();
    FileObject owner;
    String modName;
    if (entry == null ||
            (owner = entry.first().first().get()) == null ||
            !owner.equals(root)) {
        modName = provider.apply(root);
        final File modInfo = Optional.ofNullable(FileUtil.toFile(root))
                .map((rf) -> new File(rf, String.format("%s.%s", FileObjects.MODULE_INFO, FileObjects.JAVA)))   //NOI18N
                .orElse(null);
        final Pair<Pair<Reference<FileObject>,File>,String> newEntry = Pair.of(
                Pair.of(new WeakReference<>(root), modInfo),
                modName);
        if (modNameCache.compareAndSet(entry, newEntry)) {
            if (entry != null && entry.first().second() != null) {
                FileUtil.removeFileChangeListener(this, entry.first().second());
            }
            if (newEntry.first().second() != null) {
                FileUtil.addFileChangeListener(this, newEntry.first().second());
            }
        }
        LOG.log(Level.FINE, "modNameCache updated: {0}", modName);  //NOI18N
    } else {
        modName = entry.second();
    }
    return modName;
}
 
Example 18
Source File: ClassPath.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void safeRemoveFileChangeListener(@NonNull final File file) {
    try {
        FileUtil.removeFileChangeListener(this, file);
    } catch (IllegalArgumentException iae) {
        LOG.log(Level.FINE, iae.getMessage());
    }
}
 
Example 19
Source File: FreeformSources.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    FileUtil.removeFileChangeListener(this, file);
}
 
Example 20
Source File: DefaultSourceLevelQueryImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private boolean isModular(final FileObject javaFile) {
    //Module-info always modular
    if (MODULE_INFO.equals(javaFile.getName())) {
        return true;
    }
    //Try to find module-info
    final Pair<Reference<FileObject>,Reference<FileObject>> entry = rootCache.get();
    FileObject file, root;
    if (entry == null ||
            (file = entry.first().get()) == null || !file.equals(javaFile) ||
            (root = entry.second().get()) == null) {
        root = Optional.ofNullable(ClassPath.getClassPath(javaFile, ClassPath.SOURCE))
                .map((scp) -> scp.findOwnerRoot(javaFile))
                .orElseGet(() -> {
                    final String pkg = parsePackage(javaFile);
                    final String[] pkgElements = pkg.isEmpty() ?
                            new String[0] :
                            pkg.split("\\.");   //NOI18N
                    FileObject owner = javaFile.getParent();
                    for (int i = 0; owner != null && i < pkgElements.length; i++) {
                        owner = owner.getParent();
                    }
                    return owner;
                });
        rootCache.set(Pair.of(
                new WeakReference<>(javaFile),
                new WeakReference<>(root)));
        LOG.log(Level.FINE, "rootCache updated: {0}", root);  //NOI18N
    }
    if (root == null) {
        return false;
    }
    final Pair<Pair<Reference<FileObject>,File>,Boolean> modEntry = modCache.get();
    FileObject meKye;
    if (modEntry == null ||
            (meKye = modEntry.first().first().get()) == null || !meKye.equals(root)) {
        final FileObject modInfo = root.getFileObject(MODULE_INFO, JAVA_EXT);
        final boolean res =  modInfo != null && modInfo.isData();
        final Pair<Pair<Reference<FileObject>,File>,Boolean> newModEntry = Pair.of(
                Pair.of(new WeakReference<>(root), FileUtil.toFile(root)),
                res);
        if (modCache.compareAndSet(modEntry, newModEntry)) {
            if (modEntry != null && modEntry.first().second() != null) {
                FileUtil.removeFileChangeListener(this, modEntry.first().second());
            }
            if (newModEntry != null && newModEntry.first().second() != null) {
                FileUtil.addFileChangeListener(this, newModEntry.first().second());
            }
            LOG.log(Level.FINE, "modCache updated: {0}", res);  //NOI18N
        }
        return res;
    } else {
        return modEntry.second();
    }
}