Java Code Examples for org.openide.util.Lookup.Result#addLookupListener()

The following examples show how to use org.openide.util.Lookup.Result#addLookupListener() . 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: ComponentBreakpointActionProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public ComponentBreakpointActionProvider() {
    final Result<Node> nodeLookupResult = Utilities.actionsGlobalContext().lookupResult(Node.class);
    LookupListener ll = new LookupListener() {
        @Override
        public void resultChanged(LookupEvent ev) {
            Collection<? extends Node> nodeInstances = nodeLookupResult.allInstances();
            for (Node n : nodeInstances) {
                JavaComponentInfo ci = n.getLookup().lookup(JavaComponentInfo.class);
                if (ci != null) {
                    setEnabled(ActionsManager.ACTION_TOGGLE_BREAKPOINT, true);
                    return ;
                }
            }
            setEnabled(ActionsManager.ACTION_TOGGLE_BREAKPOINT, false);
        }
    };
    nodeLookupResult.addLookupListener(ll);
    ll.resultChanged(null); // To initialize
}
 
Example 2
Source File: MetaInfServicesLookupTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testDontCallMeUnderLock() throws Exception {
    final Lookup l = getTestedLookup(c2);
    ProxyLookup pl = new ProxyLookup(l) {
        @Override
        void beforeLookup(boolean call, Template<?> template) {
            super.beforeLookup(call, template);
            assertFalse("Don't hold MetaInfServicesLookup lock", Thread.holdsLock(l));
        }
    };
    Class<?> xface = c1.loadClass("org.foo.Interface");
    Result<?> res = pl.lookupResult(Object.class);
    res.addLookupListener(new LookupListener() {
        @Override
        public void resultChanged(LookupEvent ev) {
        }
    });
    assertTrue("Empty now", res.allItems().isEmpty());
    
    Object first = l.lookup(xface);
    assertEquals(first, l.lookupAll(xface).iterator().next());
    Object second = pl.lookup(xface);
    assertEquals(first, second);
}
 
Example 3
Source File: IssuesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** 
 * Issues of changes in layer
 */
public void testForChangeInLayer() throws IOException{
    
    // issue #63338
    // http://www.netbeans.org/issues/show_bug.cgi?id=63338
    // Subj: deadlock during showing annotations
    // fix: deadlock occured in after inproper firing of lookup changed event.
    //      event was fired even in cases, the lookup listener should be quiet.
    MimeLookup lookup = MimeLookup.getMimeLookup("text/jsp"); //NOI18N
    Result result = lookup.lookup(new Template(TestLookupObject.class));
    result.allInstances().size(); // remove this line if issue #60010 is fixed
    LookupListener listener = new LookupListener(){
        public void resultChanged(LookupEvent ev){
            resultChangedCount[0]++;
        }
    };
    result.addLookupListener(listener);
    
    //simulate module installation, new file will be added
    createFile("Editors/text/jsp/testLookup/org-openide-actions-PasteAction.instance"); //NOI18N        

    checkResultChange(0);
    
    TestUtilities.deleteFile(getWorkDir(),
            "Editors/text/jsp/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance");

    checkResultChange(1);
    
    result.removeLookupListener(listener);
    resultChangedCount[0] = 0;
    // end of issue #63338 ------------------------------------------------
    
    
    
}
 
Example 4
Source File: InstanceDataObjectRefreshesOnFileChangeTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testSwitchRefreshesIDO() throws Exception {
    FileObject fo = FileUtil.getConfigFile("Folder/MyInstance.instance");
    assertNotNull("File visible in SFS", fo);
    DataObject ido = DataObject.find(fo);
    Lookup lkp = ido.getLookup();
    Result<InstanceCookie> res = lkp.lookupResult(InstanceCookie.class);
    assertEquals("One cookie", 1, res.allItems().size());
    res.addLookupListener(this);
    ido.addPropertyChangeListener(this);

    assertInstance(lkp, "OldOne");

    assertEquals("no lookup change yet", 0, cnt);
    assertEquals("no pcl change yet", 0, pcl);

    DynamicFS dfs = Lookup.getDefault().lookup(DynamicFS.class);
    dfs.setDelegate(mem2);

    assertEquals("one pcl change now", 1, pcl);
    if (cnt == 0) {
        fail("At least one change in lookup shall be notified");
    }

    FileObject fo2 = FileUtil.getConfigFile("Folder/MyInstance.instance");
    assertNotNull("File is still visible in SFS", fo);
    DataObject ido2 = DataObject.find(fo2);

    assertSame("Data object remains", ido, ido2);

    assertInstance(lkp, "NewOne");
}
 
Example 5
Source File: ProxyLookupAvoidBeforeTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testDontCallBeforeLookup() throws Exception {
    InstanceContent ic = new InstanceContent();
    ic.add(1);
    ic.add(2L);
    
    ABefore one = new ABefore(ic);
    
    ProxyLookup lkp = new ProxyLookup(one);
    
    Result<Long> longResult = lkp.lookupResult(Long.class);
    longResult.addLookupListener(this);
    Result<Integer> intResult = lkp.lookupResult(Integer.class);
    intResult.addLookupListener(this);
    Result<Number> numResult = lkp.lookupResult(Number.class);
    numResult.addLookupListener(this);

    one.queryAllowed = true;
    assertEquals("Two", Long.valueOf(2L), longResult.allInstances().iterator().next());
    assertEquals("One", Integer.valueOf(1), intResult.allInstances().iterator().next());
    assertEquals("Two numbers", 2, numResult.allInstances().size());
    assertEquals("Two number items", 2, numResult.allItems().size());
    one.queryAllowed = false;
    
    NoBefore nob = new NoBefore();
    lkp.setLookups(one, nob);
    
    nob.queryAllowed = true;
    one.queryAllowed = true;
    assertEquals("Again Two", Long.valueOf(2L), lkp.lookup(Long.class));
    assertEquals("Again One", Integer.valueOf(1), lkp.lookup(Integer.class));
    assertEquals("Again Two numbers", 2, lkp.lookupAll(Number.class).size());
}
 
Example 6
Source File: SavableTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testEventDeliveredAsynchronously() throws Exception {
    class L implements LookupListener {
        int change;
        Object id = new Object();
        
        @Override
        public synchronized void resultChanged(LookupEvent ev) {
            change++;
            notifyAll();
        }
        
        public synchronized void createSavable() {
            assertEquals("No changes yet", 0, change);
            Savable s = new DoSave(id, null, null);
            assertEquals("The first", s, Savable.REGISTRY.lookup(Savable.class));
            assertEquals("Still no changes", 0, change);
        }
        
        public synchronized void waitForChange() throws InterruptedException {
            while (change == 0) {
                wait();
            }
            assertEquals("One change delivered", 1, change);
        }
    }
    L listener = new L();
    Result<Savable> res = Savable.REGISTRY.lookupResult(Savable.class);
    
    try {
        res.addLookupListener(listener);
        listener.createSavable();
        listener.waitForChange();
    } finally {
        res.removeLookupListener(listener);
    }
}
 
Example 7
Source File: AdditionalAntBasedTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testIfAntBasedProjectInstalled() throws Exception {
    FileObject fo = FileUtil.getConfigFile("Menu/Edit");
    assertNull("Default layer is on and Edit is hidden", fo);

    Result<AntBasedType> res = Lookup.getDefault().lookupResult(AntBasedType.class);
    assertEquals("no ant project types: " + res.allInstances(), 0, res.allInstances().size());
    res.addLookupListener(this);

    LOG.info("creating AntBasedType registration on disk");
    FileUtil.createData(FileUtil.getConfigRoot(), 
        "Services/" + AntBasedType.class.getName().replace('.', '-') + ".instance"
    );
    LOG.info("registration created");
    AntBasedType f = Lookup.getDefault().lookup(AntBasedType.class);
    LOG.info("looking up the result " + f);
    synchronized (this) {
        while (!delivered) {
            wait();
        }
    }

    assertNotNull("Ant found", f);
    LOG.info("waiting for FoDFileSystem to be updated");
    FoDLayersProvider.getInstance().waitFinished();
    LOG.info("waiting for FoDFileSystem to be waitFinished is over");

    for (int cnt = 0; cnt < 5; cnt++) {
        fo = FileUtil.getConfigFile("Menu/Edit");
        if (fo != null) {
            break;
        }
        Thread.sleep(500);
    }
    LOG.info("Edit found: " + fo);
    LOG.info("Menu items: " + Arrays.toString(FileUtil.getConfigFile("Menu").getChildren()));
    assertNotNull("Default layer is off and Edit is visible", fo);
}
 
Example 8
Source File: ValidatorSchemaFactoryRegistry.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void initialize() {
    schemaFactories = new Hashtable<String, ValidatorSchemaFactory>();
    Result<ValidatorSchemaFactory> lookupResult = Lookup.getDefault().lookupResult(ValidatorSchemaFactory.class);
    lookupResult.addLookupListener(new LookupListener() {
        public void resultChanged(LookupEvent ev) {
              refreshServices();
        }
    });
    refreshServices();
}
 
Example 9
Source File: LocalHistory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void addLookupListener(TopComponent tc) {
    Result<DataObject> r = tc.getLookup().lookupResult(DataObject.class);
    L l = new L(new WeakReference<TopComponent>(tc), r);
    synchronized(lookupListeners) {
        lookupListeners.add(l);
    }
    r.addLookupListener(l);
}
 
Example 10
Source File: FoldRegistry.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public R(FoldRegistry dom, MimePath mime, Result result, Result result2) {
    this.dom = dom;
    this.mime = mime;
    this.result = result;
    this.result2 = result2;
    result.addLookupListener(WeakListeners.create(LookupListener.class, 
            this, result));
    if (result2 != null) {
        result2.addLookupListener(WeakListeners.create(LookupListener.class, 
                this, result2));
    }
}
 
Example 11
Source File: NetigsoServicesTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testOSGiServicesVisibleInLookup() throws Exception {
    mgr.mutexPrivileged().enterWriteAccess();
    FileObject fo;
    try {
        String mfBar = "Bundle-SymbolicName: org.bar\n" +
            "Bundle-Version: 1.1.0\n" +
            "Bundle-ManifestVersion: 2\n" +
            "Import-Package: org.foo\n" +
            "\n\n";

        File j2 = changeManifest(new File(jars, "depends-on-simple-module.jar"), mfBar);
        Module m2 = mgr.create(j2, null, false, false, false);
        mgr.enable(m2);
    } finally {
        mgr.mutexPrivileged().exitWriteAccess();
    }

    Bundle b = findBundle("org.bar");
    assertNotNull("Bundle really found", b);
    IOException s = new IOException();
    Hashtable dict = new Hashtable();
    dict.put(Constants.SERVICE_DESCRIPTION, "tristatricettri");
    ServiceRegistration sr = b.getBundleContext().registerService(IOException.class.getName(), s, dict);
    assertBundles("Nobody is using the service yet", 0, sr.getReference().getUsingBundles());
    IOException found = Lookup.getDefault().lookup(IOException.class);
    assertNotNull("Result really found", found);
    assertBundles("Someone is using the service now", 1, sr.getReference().getUsingBundles());
    Result<IOException> res = Lookup.getDefault().lookupResult(IOException.class);
    res.addLookupListener(this);
    assertEquals("One instance found", 1, res.allInstances().size());
    
    Collection<? extends Item<IOException>> items = res.allItems();
    assertEquals("One item found: " + items, 1, items.size());
    Item<IOException> first = items.iterator().next();
    String expectedServiceID = "OSGiService[" + sr.getReference().getProperty(Constants.SERVICE_ID) + "]";
    assertEquals("Proper ID", expectedServiceID, first.getId());
    assertEquals("Right display name", "tristatricettri", first.getDisplayName());
    
    sr.unregister();
    IOException notFound = Lookup.getDefault().lookup(IOException.class);
    assertNull("Result not found", notFound);
    assertEquals("No instance found", 0, res.allInstances().size());
    assertEquals("One change", 1, cnt);
}
 
Example 12
Source File: FileObjectTestHid.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void  testMoveAndChanges() throws Exception {
    checkSetUp();
    FileObject fold = getTestFolder1(root);
    FileObject fo1 = getTestFile1(fold);
    FileObject sub;
    try {
        sub = fold.createFolder("sub");
    } catch (IOException ex) {
        fsAssert("OK, if the system is read-only",
            fs.isReadOnly() || root.isReadOnly());
        return;
    }
    
    class L implements LookupListener {
        int cnt;

        @Override
        public void resultChanged(LookupEvent ev) {
            cnt++;
        }
        
        public void assertChange(String msg) {
            assertTrue(msg, cnt > 0);
            cnt = 0;
        }
    }
    L listener = new L();

    Lookup first = fo1.getLookup();
    Result<FileObject> result = first.lookupResult(FileObject.class);
    result.addLookupListener(listener);
    
    Collection<? extends FileObject> all = result.allInstances();
    assertTrue("Contains itself before move: " + all, all.contains(fo1));
    FileLock lock = null;
    FileObject ret;
    try {
        lock = fo1.lock();
        ret = fo1.move(lock, sub, fo1.getName(), fo1.getExt());
    } finally {
        if (lock != null) {
            lock.releaseLock();
        }            
    }
    
    listener.assertChange("File object has changed");
    all = result.allInstances();
    assertTrue("Contains itself after move: " + all, all.contains(ret));
    
    FileObject ret2 = FileUtil.moveFile(ret, fold, "strange.name");

    listener.assertChange("Another change in the lookup");
    all = result.allInstances();
    assertTrue("Contains itself after move: " + all, all.contains(ret2));
}
 
Example 13
Source File: ListenersIgnoredTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testLookupBugTest() {
        class C0 {
        }
        class C1 {
        }
        InstanceContent content = new InstanceContent();
        AbstractLookup lookup = new AbstractLookup(content);
//force lookup to use InheritanceTree as Storage
        for (int i = 0; i < 12; i++) {
            content.add(i);
        }

        Result<C0> r0 = lookup.lookupResult(C0.class);

        final AtomicInteger cnt = new AtomicInteger();
        r0.addLookupListener(new LookupListener() {
            @Override
            public void resultChanged(LookupEvent ev) {
                cnt.incrementAndGet();
                LOG.fine("r0 notified");
            }
        });
        
        C0 o0 = new C0();
        C1 o1 = new C1();

        LOG.fine("Add o0");
        content.add(o0);
        assertEquals("One change", 1, cnt.getAndSet(0));

        LOG.fine("Remove o0");
        content.remove(o0);
        assertEquals("Another change change", 1, cnt.getAndSet(0));

        LOG.fine("Add o1");
        content.add(o1);
        assertEquals("No change", 0, cnt.getAndSet(0));

        LOG.fine("Remove o1");
        content.remove(o1);
        assertEquals("No change", 0, cnt.getAndSet(0));

        LOG.fine("Add o0");
        content.add(o0);
        LOG.fine("Line before should read 'r0 notified' ?");
        assertEquals("One change", 1, cnt.getAndSet(0));

    }
 
Example 14
Source File: ServerInstanceLookupTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@RandomlyFails
public void testListener() throws DeploymentManagerCreationException {
    ServerInstance instance = ServerRegistry.getInstance().getServerInstance(URL);

    // because of this - instance is connected
    ServerTarget target = instance.getServerTarget("Target 1"); // NOI18N
    ServerInstanceLookup lookup = new ServerInstanceLookup(instance,
            instance.getServer().getDeploymentFactory(), target.getTarget());

    DeploymentManager manager = instance.getDeploymentManager();
    Result<DeploymentManager> resultManager =
            lookup.lookup(new Template<DeploymentManager>(DeploymentManager.class));
    assertResultContainsInstance(DeploymentManager.class, manager, resultManager);

    // test listeners
    TestLookupListener listener = new TestLookupListener(resultManager);
    resultManager.addLookupListener(listener);
    instance.reset();
    // invoked by reset
    assertEquals(1, listener.getCount());

    manager = instance.getDisconnectedDeploymentManager();
    // invoked by getter
    assertEquals(2, listener.getCount());
    assertResultContainsInstance(DeploymentManager.class, manager, resultManager);

    manager = instance.getDisconnectedDeploymentManager();
    // count must remain same
    assertEquals(2, listener.getCount());
    assertResultContainsInstance(DeploymentManager.class, manager, resultManager);

    manager = instance.getDeploymentManager();
    // invoked by getter
    assertEquals(3, listener.getCount());
    assertResultContainsInstance(DeploymentManager.class, manager, resultManager);

    manager = instance.getDeploymentManager();
    // count must remain same
    assertEquals(3, listener.getCount());
    assertResultContainsInstance(DeploymentManager.class, manager, resultManager);
}
 
Example 15
Source File: LanguagesNavigator.java    From netbeans with Apache License 2.0 4 votes vote down vote up
MyLookupListener (Result<DataObject> result) {
    this.result = result;
    result.addLookupListener (this);
}
 
Example 16
Source File: PathLookup.java    From netbeans with Apache License 2.0 4 votes vote down vote up
PathLookupResult(Class<T> clazz, Result<T> orig, String path) {
    this.clazz = clazz;
    this.orig = orig;
    this.path = path;
    orig.addLookupListener(WeakListeners.create(LookupListener.class, ll, orig));
}