org.openide.util.Lookup.Result Java Examples

The following examples show how to use org.openide.util.Lookup.Result. 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: ContextManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public <T> void registerListener(Class<T> type, ContextAction<T> a) {
    synchronized (CACHE) {
        LSet<T> existing = findLSet(type);
        if (existing == null) {
            Lookup.Result<T> result = createResult(lookup.lookupResult(type));
            existing = new LSet<T>(result, type);
            listeners.put(type, existing);
        }
        existing.add(a);
        // TBD: a.updateState(new ActionMap(), actionMap.get());
        
        if (a.selectMode == ContextSelection.ALL) {
            initSelectionAll();
            selectionAll.add(a);
        }
    }
}
 
Example #2
Source File: MimeLookupPerformanceTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testTemplateLookuping() throws IOException{
    MimePath mp = MimePath.parse("text/x-java/text/html/text/xml");
    Lookup lookup = MimeLookup.getLookup(mp);
    Result result = lookup.lookup(new Template(PopupActions.class));
    Collection col = result.allInstances();
    checkPopupItemPresence(lookup, RenameAction.class, true);
    gc();
    int size = assertSize("", Arrays.asList( new Object[] {lookup} ), 10000000,  getFilter());
    for (int i=0; i<30; i++){
        result = lookup.lookup(new Template(PopupActions.class));
        col = result.allInstances();
        checkPopupItemPresence(lookup, RenameAction.class, true);
    }
    gc();
    assertSize("", size, lookup);
}
 
Example #3
Source File: ExplorerUtilsTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testAssertFilteringIsLazy() throws Exception {
    LazyOpenNode n = new LazyOpenNode();
    ExplorerManager em = new ExplorerManager();
    Lookup lkp = ExplorerUtils.createLookup(em, new ActionMap());
    em.setRootContext(n);
    em.setSelectedNodes(new Node[] { n });
    
    waitAWT();
    
    assertSame("My node", n, lkp.lookup(Node.class));
    assertEquals("One node", 1, lkp.lookupAll(Node.class).size());
    assertTrue("No savable", lkp.lookupAll(Savable.class).isEmpty());
    
    Result<Openable> res = lkp.lookupResult(Openable.class);
    assertEquals("One item", 1, res.allItems().size());
    n.assertNoOpen();
    assertFalse("There is some openable", res.allInstances().isEmpty());
    n.assertOpen(lkp.lookup(Openable.class));
}
 
Example #4
Source File: ProvidedExtensionsTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private ProvidedExtensionsImpl lookupImpl(boolean providesCanWrite) {
    Result<BaseAnnotationProvider> result = Lookup.getDefault().
                   lookup(new Lookup.Template(BaseAnnotationProvider.class));
    for (Item<BaseAnnotationProvider> item : result.allItems()) {
        if (!item.getId().contains(ProvidedExtensionsTest.class.getSimpleName())) {
            continue;
        }
        BaseAnnotationProvider ap = item.getInstance();
        InterceptionListener iil = ap.getInterceptionListener();
        if (iil instanceof ProvidedExtensionsImpl) {
            ProvidedExtensionsImpl extension = (ProvidedExtensionsImpl) iil;
            if (ProvidedExtensionsAccessor.IMPL.providesCanWrite(extension) == providesCanWrite) {
                return (ProvidedExtensionsImpl) iil;
            }
        }
    }
    return null;
}
 
Example #5
Source File: TopComponentGetLookupTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testNodesAreInTheLookupAndNothingIsFiredBeforeFirstQuery() {
    AbstractNode n1 = new AbstractNode(Children.LEAF, Lookup.EMPTY);
    top.setActivatedNodes(new Node[] { n1 });
    assertEquals("One node there", 1, get.getActivatedNodes().length);
    assertEquals("Is the right now", n1, get.getActivatedNodes()[0]);
    
    Lookup.Result<Node> res = lookup.lookup(new Lookup.Template<Node>(Node.class));
    L l = new L();
    res.addLookupListener(l);
    
    l.check("Nothing fired before first query", 0);
    res.allInstances();
    l.check("Nothing is fired on first query", 0);
    lookup.lookup(new Lookup.Template<Node>(Node.class)).allInstances();
    l.check("And additional query does not change anything either", 0);
}
 
Example #6
Source File: ServerInstanceLookupTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testResultLookup() 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());

    // test factory
    Result<DeploymentFactory> resultFactory =
            lookup.lookup(new Template<DeploymentFactory>(DeploymentFactory.class));
    assertResultContainsInstance(DeploymentFactory.class,
            instance.getServer().getDeploymentFactory(), resultFactory);

    // test target
    Result<Target> resultTarget = lookup.lookup(new Template<Target>(Target.class));
    assertResultContainsInstance(Target.class, target.getTarget(), resultTarget);

    // test manager
    DeploymentManager manager = instance.getDeploymentManager();
    Result<DeploymentManager> resultManager =
            lookup.lookup(new Template<DeploymentManager>(DeploymentManager.class));
    assertResultContainsInstance(DeploymentManager.class, manager, resultManager);
}
 
Example #7
Source File: ContextManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private <T> List<? extends T> listFromResult(Lookup.Result<T> result) {
    Collection<? extends T> col = result.allInstances();
    Collection<T> tmp = new LinkedHashSet<T>(col);
    if (tmp.size() != col.size()) {
        Collection<T> nt = new ArrayList<T>(tmp.size());
        nt.addAll(tmp);
        col = nt;
    }
    List<? extends T> all;
    if (col instanceof List) {
        all = (List<? extends T>)col;
    } else {
        ArrayList<T> arr = new ArrayList<T>();
        arr.addAll(col);
        all = arr;
    }
    return all;
}
 
Example #8
Source File: ProxyLookupTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testFrequentSwitching() {
    Object o1 = new Object();
    Object o2 = new Object();
    String s1 = new String("foo");
    String s2 = new String("bar");

    Lookup l1 = Lookups.fixed(o1, s1);
    Lookup l2 = Lookups.fixed(o2, s2);

    ProxyLookup lookup = new ProxyLookup(new Lookup[0]);

    Lookup.Result<Object> res1 = lookup.lookupResult(Object.class);
    Lookup.Result<String> res2 = lookup.lookupResult(String.class);

    assertSize("Lookup is small", 1500, lookup);

    for (int i = 0; i < 100; i++) {
        lookup.setLookups(l1);
        lookup.setLookups(l2);
    }
    assertSize("Lookup has grown too much", 1500, lookup);
}
 
Example #9
Source File: ProxyLookupTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testConfuseIterativeIterator() {
    InstanceContent ic1 = new InstanceContent();
    AbstractLookup l1 = new AbstractLookup(ic1);
    InstanceContent ic2 = new InstanceContent();
    AbstractLookup l2 = new AbstractLookup(ic2);
    InstanceContent ic3 = new InstanceContent();
    AbstractLookup l3 = new AbstractLookup(ic3);
    
    ProxyLookup pl = new ProxyLookup(l1, l2, l3);
    Result<Number> res = pl.lookupResult(Number.class);
    
    ic1.add(1);
    ic2.add(2f);
    ic3.add(3d);
    
    int cnt = 0;
    for (Number n : res.allInstances()) {
        cnt += n.intValue();
    }
    assertEquals("Six", 6, cnt);
    final Collection<? extends Number> all = res.allInstances();
    assertEquals("Three numbers: " + all, 3, all.size());
}
 
Example #10
Source File: ContextManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Checks whether a type is enabled.
 */
public <T> boolean isEnabled(Class<T> type, ContextSelection selectMode, ContextAction.Performer<? super T> enabler) {
    Lookup.Result<T> result = findResult(type);
    
    boolean e = isEnabledOnData(result, type, selectMode);
    if (enabler != null) {
        if (e) {
            List<? extends T> all = listFromResult(result);
            e = enabler.enabled(all, new LkpAE(all, type));
        } else if (enabler != null) {
            enabler.detach();
        }
    }
    
    return e;
}
 
Example #11
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 #12
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 #13
Source File: Lookup.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void fillInstances(List<String> l, Result<T> lr, Set<String> s) {
    for (String className : l) {
        if (className.endsWith(HIDDEN)) {
            continue;
        }
        if (s != null && s.contains (className)) {
            continue;
        }
        fillClassInstance(className);
    }
    for (Item<T> li : lr.allItems()) {
        // TODO: We likely do not have the Item.getId() defined correctly.
        // We have to check the ContextAwareService.serviceID()
        String serviceName = getServiceName(li.getId());
        //System.err.println("ID = '"+li.getId()+"' => serviceName = '"+serviceName+"'");
        // We do not recognize method calls correctly
        if (s != null && (s.contains (serviceName) || s.contains (serviceName+"()"))) {
            continue;
        }
        //add(new LazyInstance<T>(service, li));
        fillServiceInstance(li);
    }
    /*
    for (Object lri : lr.allInstances()) {
        if (lri instanceof ContextAwareService) {
            String className = ((ContextAwareService) lri).serviceName();
            if (s != null && s.contains (className)) continue;
            fillClassInstance(className);
        }
    }
     */
}
 
Example #14
Source File: ServerInstanceLookupTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static <T> void assertResultContainsInstance(Class<T> clazz, T instance, Result<T> result) {
    Set<Class<? extends T>> classes = result.allClasses();
    assertEquals(1, classes.size());
    assertTrue(clazz.isAssignableFrom(classes.iterator().next()));

    Collection<? extends T> instances = result.allInstances();
    assertEquals(1, instances.size());
    assertEquals(instance, instances.iterator().next());

    Collection<? extends Item<T>> items = result.allItems();
    assertEquals(1, items.size());
    assertEquals(instance, items.iterator().next().getInstance());
}
 
Example #15
Source File: Subversion.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @return registered hyperlink providers
 */
public List<VCSHyperlinkProvider> getHyperlinkProviders() {
    if (hpResult == null) {
        hpResult = (Result<? extends VCSHyperlinkProvider>) Lookup.getDefault().lookupResult(VCSHyperlinkProvider.class);
    }
    if (hpResult == null) {
        return Collections.emptyList();
    }
    Collection<? extends VCSHyperlinkProvider> providersCol = hpResult.allInstances();
    List<VCSHyperlinkProvider> providersList = new ArrayList<VCSHyperlinkProvider>(providersCol.size());
    providersList.addAll(providersCol);
    return Collections.unmodifiableList(providersList);
}
 
Example #16
Source File: History.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @return registered hyperlink providers
 */
public List<VCSHyperlinkProvider> getHyperlinkProviders() {
    if (hpResult == null) {
        hpResult = (Lookup.Result<? extends VCSHyperlinkProvider>) Lookup.getDefault().lookupResult(VCSHyperlinkProvider.class);
    }
    if (hpResult == null) {
        return Collections.emptyList();
    }
    Collection<? extends VCSHyperlinkProvider> providersCol = hpResult.allInstances();
    List<VCSHyperlinkProvider> providersList = new ArrayList<VCSHyperlinkProvider>(providersCol.size());
    providersList.addAll(providersCol);
    return Collections.unmodifiableList(providersList);
}
 
Example #17
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 #18
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 #19
Source File: Lookup.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private MetaInfLookupList(List<String> l, Result<T> lr, Set<String> s, Class<T> service) {
    super(s);
    assert service != null;
    this.service = service;
    fillInstances(l, lr, s);
    // Schedule lazily as this may take a while...
    listenOnDisabledModulesTask.schedule(100);
}
 
Example #20
Source File: Git.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @return registered hyperlink providers
 */
public List<VCSHyperlinkProvider> getHyperlinkProviders() {
    if (hpResult == null) {
        hpResult = (Result<? extends VCSHyperlinkProvider>) Lookup.getDefault().lookupResult(VCSHyperlinkProvider.class);
    }
    if (hpResult == null) {
        return Collections.<VCSHyperlinkProvider>emptyList();
    }
    Collection<? extends VCSHyperlinkProvider> providersCol = hpResult.allInstances();
    List<VCSHyperlinkProvider> providersList = new ArrayList<VCSHyperlinkProvider>(providersCol.size());
    providersList.addAll(providersCol);
    return Collections.unmodifiableList(providersList);
}
 
Example #21
Source File: ContextManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public NeverEmptyResult(Result<T> delegate, Result<Provider> nodes) {
    this.delegate = delegate;
    this.nodes = nodes;
    this.listeners = new CopyOnWriteArrayList<LookupListener>();
    // add weak listeners so this can be GCed when listeners are empty
    this.delegate.addLookupListener(WeakListeners.create(LookupListener.class, this, this.delegate));
    this.nodes.addLookupListener(WeakListeners.create(LookupListener.class, this, this.nodes));
    initValues();
}
 
Example #22
Source File: Lookup.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void refreshContent() {
    // Perform changes under a lock so that iterators reading this list
    // can sync on it
    synchronized(this) {
        clear();
        List<String> l = list(folder, service);
        Result lr = listLookup(folder, service);
        Set<String> s = getHiddenClassNames(l);
        hiddenClassNames = s;
        fillInstances(l, lr, s);
    }
    firePropertyChange();
}
 
Example #23
Source File: ContextManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Lookup.Result<Lookup.Provider> initSelectionAll() {
    assert Thread.holdsLock(CACHE);
    if (selectionAll == null) {
        Lookup.Result<Lookup.Provider> result = lookup.lookupResult(Lookup.Provider.class);
        selectionAll = new LSet<Lookup.Provider>(result, Lookup.Provider.class);
    }
    return selectionAll.result;
}
 
Example #24
Source File: PathLookup.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static <T> List<Item<T>> itemsJustForPath(Class<T> clazz, Result<T> result, String path) {
    int l = path.length() + 1;
    Collection<? extends Item<T>> allItems = result.allItems();
    List<Item<T>> pathItems = new ArrayList<Item<T>>(allItems.size());
    for (Item<T> it : allItems) {
        String filePath = it.getId();
        assert filePath.startsWith(path) : "File path '"+filePath+"' does not start with searched path '"+path+"'";
        if (filePath.indexOf('/', l) < l) {
            // This item is from current folder
            if (clazz.isInterface()) {
                // Check whether the lookup item is really declared as an instance of the class we search for:
                FileObject fo = FileUtil.getConfigFile(filePath+".instance");
                if (fo != null) {
                    Object io = fo.getAttribute("instanceOf"); // NOI18N
                    if (io != null) {
                        if (((String) io).indexOf(clazz.getName()) < 0) {
                            continue;
                        }
                    }
                }
            }
            pathItems.add(it);
        }
    }
    if (pathItems.size() == allItems.size()) {
        return (List<Item<T>>) ORIG_ITEMS;
    }
    return pathItems;
}
 
Example #25
Source File: ContextManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private <T> Lookup.Result<T> findResult(final Class<T> type) {
    LSet<T> lset = findLSet(type);
    Lookup.Result<T> result;
    if (lset != null) {
        result = lset.result;
    } else {
        result = lookup.lookupResult(type);
    }
    return result;
}
 
Example #26
Source File: ContextManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Checks whether a type is enabled.
 */
public <T> boolean runEnabled(Class<T> type, ContextSelection selectMode,  BiFunction<List<? extends T>, Lookup.Provider, Boolean> callback) {
    Lookup.Result<T> result = findResult(type);
    
    boolean e = isEnabledOnData(result, type, selectMode);
    if (e) {
        List<? extends T> all = listFromResult(result);
        e = callback.apply(all, new LkpAE(all, type));
    }
    
    return e;
}
 
Example #27
Source File: Mercurial.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @return registered hyperlink providers
 */
public List<VCSHyperlinkProvider> getHyperlinkProviders() {
    if (hpResult == null) {
        hpResult = (Result<? extends VCSHyperlinkProvider>) Lookup.getDefault().lookupResult(VCSHyperlinkProvider.class);
    }
    if (hpResult == null) {
        return Collections.<VCSHyperlinkProvider>emptyList();
    }
    Collection<? extends VCSHyperlinkProvider> providersCol = hpResult.allInstances();
    List<VCSHyperlinkProvider> providersList = new ArrayList<VCSHyperlinkProvider>(providersCol.size());
    providersList.addAll(providersCol);
    return Collections.unmodifiableList(providersList);
}
 
Example #28
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 #29
Source File: MavenQueryProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public MavenQueryProvider() {
    grammars = new ArrayList<GrammarFactory>();
    Result<GrammarFactory> result = Lookup.getDefault().lookupResult(GrammarFactory.class);
    Collection<? extends Item<GrammarFactory>> items = result.allItems();
    for (Item<GrammarFactory> item : items) {
        grammars.add(item.getInstance());
    }

}
 
Example #30
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);
}