Java Code Examples for org.openide.filesystems.FileSystem#addFileChangeListener()

The following examples show how to use org.openide.filesystems.FileSystem#addFileChangeListener() . 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: 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 2
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 3
Source File: DataLoaderGetActionsCompatibilityTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the testing environment by creating testing folders
 * on the system file system.
 */
@Override
protected void setUp() throws Exception {
    clearWorkDir();
    MockLookup.setInstances(
            new Repository(TestUtilHid.createLocalFileSystem(getWorkDir(), new String[0])),
            new Pool());
    
    MyDL loader = MyDL.getLoader(MyDL.class);

    FileSystem dfs = FileUtil.getConfigRoot().getFileSystem();
    dfs.refresh (true);        
    
    FileObject fo = FileUtil.createData (dfs.getRoot (), "a.txt");
    obj = DataObject.find (fo);
    
    assertEquals ("The correct loader", loader, obj.getLoader ());
    
    node = obj.getNodeDelegate ();    
    
    sfs = new PCL ();
    dfs.addFileChangeListener (sfs);
}
 
Example 4
Source File: DataObjectSaveAsTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the testing environment by creating testing folders
 * on the system file system.
 */
@Override
protected void setUp() throws Exception {
    clearWorkDir();
    MockLookup.setInstances(new Repository(TestUtilHid.createLocalFileSystem(getWorkDir(), new String[0])),
            new Pool());
    
    MyDL1 loader1 = MyDL1.getLoader(MyDL1.class);
    MyDL2 loader2 = MyDL2.getLoader(MyDL2.class);

    FileSystem dfs = FileUtil.getConfigRoot().getFileSystem();
    dfs.refresh (true);        
    
    FileObject fo = FileUtil.createData (dfs.getRoot (), "a.ext1");
    obj1 = DataObject.find (fo);
    
    assertEquals ("The correct loader", loader1, obj1.getLoader ());
    
    fo = FileUtil.createData (dfs.getRoot (), "b.ext2");
    obj2 = DataObject.find (fo);
    
    assertEquals ("The correct loader", loader2, obj2.getLoader ());
    
    sfs = new PCL ();
    dfs.addFileChangeListener (sfs);
}
 
Example 5
Source File: WebAppParseSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static WebAppParseProxy create(JspParserImpl jspParser, WebModule wm) {
    WebAppParseSupport instance = new WebAppParseSupport(jspParser, wm);

    // register file listener (listen to changes of tld files, web.xml)
    try {
        FileSystem fs = wm.getDocumentBase().getFileSystem();
        fs.addFileChangeListener(FileUtil.weakFileChangeListener(instance.fileSystemListener, fs));
    } catch (FileStateInvalidException ex) {
        LOG.log(Level.INFO, null, ex);
    }

    // register weak class path listeners
    if (instance.wmRoot != null) { // in fact should not happen, see #154892 for more information
        ClassPath compileCP = ClassPath.getClassPath(instance.wmRoot, ClassPath.COMPILE);
        if (compileCP != null) {
            compileCP.addPropertyChangeListener(WeakListeners.propertyChange(instance, compileCP));
        }
        ClassPath executeCP = ClassPath.getClassPath(instance.wmRoot, ClassPath.EXECUTE);
        if (executeCP != null) {
            executeCP.addPropertyChangeListener(WeakListeners.propertyChange(instance, executeCP));
        }
    }

    return instance;
}
 
Example 6
Source File: WritableXMLFileSystemTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@RandomlyFails // NB-Core-Build #4187
public void testExternalFileChangesRefired() throws Exception {
    Layer l = new Layer("");
    FileSystem fs = l.read();
    FileObject foo = fs.getRoot().createData("foo");
    TestUtil.dump(foo, "foo text");
    long start = foo.lastModified().getTime();
    assertEquals(start, l.externalLastModified("foo"));
    Thread.sleep(1000L); // make sure the timestamp actually changed
    Listener fcl = new Listener();
    fs.addFileChangeListener(fcl);
    l.editExternal("foo", "new text");
    long end = foo.lastModified().getTime();
    assertEquals(end, l.externalLastModified("foo"));
    assertTrue("foo was touched", end > start);
    assertEquals("expected things fired",
            Collections.singleton("foo"),
                    fcl.changes());
}
 
Example 7
Source File: ActionProviderSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
void start() {
    try {
        final FileSystem fs = project.getProjectDirectory().getFileSystem();
        // XXX would be more efficient to only listen while TRACK_FILE_CHANGES is set,
        // but it needs adding and removing of listeners depending on PropertyEvaluator events,
        // the file event handling is cheap when TRACK_FILE_CHANGES is disabled.
        fs.addFileChangeListener(FileUtil.weakFileChangeListener(modificationListener, fs));
    } catch (FileStateInvalidException x) {
        Exceptions.printStackTrace(x);
    }
}
 
Example 8
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 9
Source File: GrailsSources.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void startFSListener () {
    try {
        FileSystem fs = projectDir.getFileSystem();
        fs.addFileChangeListener(FileUtil.weakFileChangeListener(this, fs));
    } catch (FileStateInvalidException x) {
        Exceptions.printStackTrace(x);
    }
}
 
Example 10
Source File: EarDataObject.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void refreshSourceFolders () {
    ArrayList srcRootList = new ArrayList ();
    
    Project project = FileOwnerQuery.getOwner (getPrimaryFile ());
    if (project != null) {
        Sources sources = ProjectUtils.getSources(project);
        sources.removeChangeListener (this);
        sources.addChangeListener (this);
        SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
        for (int i = 0; i < groups.length; i++) {
            org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(groups[i].getRootFolder());

            if (ejbModule != null && ejbModule.getDeploymentDescriptor() != null) {
                try {
                    FileObject fo = groups[i].getRootFolder();

                    srcRootList.add(groups[i].getRootFolder());
                    FileSystem fs = fo.getFileSystem();

                    fs.removeFileChangeListener(this);
                    fs.addFileChangeListener(this);
                } catch (FileStateInvalidException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
    }
    srcRoots = (FileObject []) srcRootList.toArray (new FileObject [srcRootList.size ()]);
}
 
Example 11
Source File: EjbJarMultiViewDataObject.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void refreshSourceFolders() {
    SourceGroup[] groups;
    Project project = getProject();
    if (project != null) {
        Sources sources = ProjectUtils.getSources(project);
        groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
    } else {
        groups = null;
    }
    if (groups != null) {
        synchronized(srcRoots) {
            srcRoots.clear();
            for (int i = 0; i < groups.length; i++) {
                org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(groups[i].getRootFolder());
                if ((ejbModule != null) && (ejbModule.getDeploymentDescriptor() != null)) {
                    try {
                        FileObject fo = groups[i].getRootFolder();
                        srcRoots.add(fo);
                        FileSystem fs = fo.getFileSystem();
                        fs.removeFileChangeListener(this); //avoid being added multiple times
                        fs.addFileChangeListener(this);
                    } catch (FileStateInvalidException ex) {
                        Exceptions.printStackTrace(ex);
                    }
                }
            }
            initialized = true;
        }
    }
}
 
Example 12
Source File: TestAnnotationProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
void init() {
    Set filesystems = getRootFilesystems();
    for (Iterator i = filesystems.iterator(); i.hasNext();) {
        FileSystem fileSystem = (FileSystem) i.next();
        fileSystem.addFileChangeListener(interceptor);
    }
    events.clear();
}
 
Example 13
Source File: DeleteCreateTestAnnotationProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
void init() {
    Set filesystems = getRootFilesystems();
    for (Iterator i = filesystems.iterator(); i.hasNext();) {
        FileSystem fileSystem = (FileSystem) i.next();
        fileSystem.addFileChangeListener(interceptor);
    }
    events.clear();
}
 
Example 14
Source File: RecognizeInstanceFiles.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private OverFiles(String path, List<FOItem> items, AbstractLookup lkp, AbstractLookup.Content cnt) {
    super(computeDelegates(path, items, lkp));
    this.path = path;
    this.lkp = lkp;
    this.content = cnt;
    this.content.setPairs(order(items));
    FileSystem fs = null;
    try {
        fs = FileUtil.getSystemConfigRoot().getFileSystem();
    } catch (FileStateInvalidException ex) {
        Exceptions.printStackTrace(ex);
    }
    this.weakL = FileUtil.weakFileChangeListener(this, fs);
    fs.addFileChangeListener(weakL);
}
 
Example 15
Source File: BaseFileObjectTestHid.java    From netbeans with 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 16
Source File: DeleteCreateTestAnnotationProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
void init() {
    Set filesystems = getRootFilesystems();
    for (Iterator i = filesystems.iterator(); i.hasNext();) {
        FileSystem fileSystem = (FileSystem) i.next();
        fileSystem.addFileChangeListener(interceptor);
    }
    events.clear();
}
 
Example 17
Source File: FolderObjTest.java    From netbeans with 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 18
Source File: PackageViewChildren.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void run() {
    computeKeys();
    refreshKeys();
    try { 
        FileSystem fs = root.getFileSystem();
        wfcl = FileUtil.weakFileChangeListener(this, fs);
        fs.addFileChangeListener( wfcl );
    }
    catch ( FileStateInvalidException e ) {
        Exceptions.printStackTrace(e);
    }
    wvqcl = WeakListeners.change( this, VisibilityQuery.getDefault() );
    VisibilityQuery.getDefault().addChangeListener( wvqcl );
}
 
Example 19
Source File: BadgingSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public BadgingSupport(FileSystem fs) {
    this.fs = fs;
    fileChangeListener = FileUtil.weakFileChangeListener(this, null);
    fs.addFileChangeListener(fileChangeListener);
}
 
Example 20
Source File: FolderObjTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
EventsEvaluator(FileSystem fs) throws FileStateInvalidException {
    this.fs = fs;
    fs.refresh(true);
    fs.addFileChangeListener(this);
}