Java Code Examples for org.openide.util.Lookup#Result

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: FileEntityResolverDeadlock54971Test.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testAskForEnvironmentFromLookup () throws Exception {
    // this is going to clear the cache in URLMapper
    Lkp.INSTANCE.add (new MyURLMapper ());

    Lkp.query = true;
    
    Lookup.Result r = Lookup.getDefault().lookupResult(ComplexPair.class);
    
    java.util.Collection c = r.allInstances();
    if (c.size () != 1 || ComplexPair.IC != c.iterator().next()) {
        fail ("Wrong instances. Should be just " + ComplexPair.IC + " but was: " + c);
    }
    
    assertEquals ("One request for environment. If this fails" +
            " then the obj.getCookie in ComplexPair failed and was not " +
            " fully executed, wrong it should be", 1, Env.howManyTimesWeHandledRequestForEnvironmentOfOurObject);
}
 
Example 2
Source File: MultiViewActionMapTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testSimplifiedActionMapChanges81117() {
    MultiViewTopComponentLookup.InitialProxyLookup lookup = new MultiViewTopComponentLookup.InitialProxyLookup(new ActionMap());
    Lookup.Result res = lookup.lookup(new Lookup.Template(ActionMap.class));
    LookListener list = new LookListener();
    list.resetCount();
    res.addLookupListener(list);
    assertEquals(1, res.allInstances().size());
    assertEquals(0, list.getCount());
    lookup.refreshLookup();
    assertEquals(1, list.getCount());
    assertEquals(1, res.allInstances().size());
    
    MultiViewTopComponentLookup lookup2 = new MultiViewTopComponentLookup(new ActionMap());
    res = lookup2.lookup(new Lookup.Template(ActionMap.class));
    list = new LookListener();
    list.resetCount();
    res.addLookupListener(list);
    assertEquals(1, res.allInstances().size());
    assertEquals(0, list.getCount());
    lookup2.setElementLookup(Lookups.fixed(new Object[] {new Object()} ));
    assertEquals(1, list.getCount());
    assertEquals(1, res.allInstances().size());
    
}
 
Example 3
Source File: JavaKit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void afterInsert(Context context) throws BadLocationException {
    if (isJavadocTouched) {
        Lookup.Result<TextAction> res = MimeLookup.getLookup(MimePath.parse("text/x-javadoc")).lookupResult(TextAction.class); // NOI18N
        ActionEvent newevt = new ActionEvent(context.getComponent(), ActionEvent.ACTION_PERFORMED, "fix-javadoc"); // NOI18N
        for (TextAction action : res.allInstances()) {
            action.actionPerformed(newevt);
        }
        isJavadocTouched = false;
    }
}
 
Example 4
Source File: SimpleProxyLookup.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Checks state of the result
 */
private Result<T> checkResult() {
    Lookup.Result lkp;
    synchronized (this) {
        lkp = getDelegate();
    }
    checkLookup();
    Lookup.Result[] used = { null };
    updateLookup(lkp, used);
    return used[0];
}
 
Example 5
Source File: AbstractLookupBaseHid.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Test of firing events. */
public void testLookupListener() {
    Object inst = 10;
    Lookup.Result<?> res = lookup.lookupResult(inst.getClass());
    res.allInstances ();
    
    LL listener = new LL(res);
    res.addLookupListener(listener);
    
    ic.add(inst);
    if (listener.getCount() == 0) {
        fail("None event fired during NbLookup.addPair()");
    }
    
    ic.remove(inst);
    if (listener.getCount() == 0) {
        fail("None event fired during NbLookup.removePair()");
    }
    
    ic.add(inst);
    if (listener.getCount() == 0) {
        fail("None event fired during second NbLookup.addPair()");
    }
    
    ic.remove(inst);
    if (listener.getCount() == 0) {
        fail("None event fired during second NbLookup.removePair()");
    }
}
 
Example 6
Source File: ProxyLookup173975Test.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void registerLookupListenerAndAddSomething(boolean useProxy, boolean callAllInstances, boolean inheritanceTree) {
    called = false;
    InstanceContent aInstanceContent = new InstanceContent();
    Storage<?> s = inheritanceTree ? new InheritanceTree() : new ArrayStorage();
    Lookup aLookup = new AbstractLookup(aInstanceContent, s);
    if (useProxy) {
        aLookup = new ProxyLookup(aLookup);
    }
    Lookup.Result<ObjectInLookup> result = aLookup.lookupResult(ObjectInLookup.class);
    if (callAllInstances) {
        result.allInstances(); // TO GET SUCCESS
    }
    result.addLookupListener(new LookupListener() {

        public void resultChanged(LookupEvent ev) {
            Lookup.Result aResult = (Lookup.Result) ev.getSource();
            Collection c = aResult.allInstances();
            if (!c.isEmpty()) {
                called = true;
            }
        }
    });

    aInstanceContent.set(Collections.singleton(
            new ObjectInLookup("Set Object in Lookup)")), null);
    Assert.assertTrue("Listener was notified", called);
}
 
Example 7
Source File: MultiViewTopComponentLookup.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public Lookup.Result lookup(Lookup.Template template) {
    
    if (template.getType() == ActionMap.class || (template.getId() != null && template.getId().equals("javax.swing.ActionMap"))) {
        return initial.lookup(template);
    }
    Lookup.Result retValue;
    retValue = proxy.lookup(template);
    retValue = new ExclusionResult(retValue);
    return retValue;
}
 
Example 8
Source File: SearchMetadataValueAction.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
public SearchMetadataValueAction(Lookup lkp) {
    super(Bundle.CTL_SearchMetadataValueAction_MenuText());
    this.lkp = lkp;
    Lookup.Result<ProductNode> lkpContext = lkp.lookupResult(ProductNode.class);
    lkpContext.addLookupListener(WeakListeners.create(LookupListener.class, this, lkpContext));
    setEnableState();
    putValue(Action.SHORT_DESCRIPTION, Bundle.CTL_SearchMetadataValueAction_ShortDescription());
}
 
Example 9
Source File: HighlightingManagerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testCachedInstancesGCed() {
    MemoryMimeDataProvider.reset(null);
    
    // Hold MimePath instance and lookup result; the highlighting container should still
    // be GCed
    final MimePath mimePath = MimePath.parse("text/plain");
    final Lookup.Result<FontColorSettings> lookupResult = MimeLookup.getLookup(mimePath).lookupResult(FontColorSettings.class);
    Collection<? extends FontColorSettings> fcs = lookupResult.allInstances();
    assertTrue("There should be FontColorSettings for " + mimePath.getPath(), fcs.size() > 0);
    
    JEditorPane pane = new JEditorPane();
    HighlightingManager hm = HighlightingManager.getInstance(pane);
    pane.setContentType("text/plain");
    assertEquals("The pane has got wrong mime type", "text/plain", pane.getContentType());

    HighlightsContainer hc = hm.getHighlights(HighlightsLayerFilter.IDENTITY);
    assertNotNull("Can't get HighlightsContainer", hc);

    WeakReference<JEditorPane> refPane = new WeakReference<JEditorPane>(pane);
    WeakReference<HighlightsContainer> refHc = new WeakReference<HighlightsContainer>(hc);
    
    // reset hard references
    pane = null;
    hm = null;
    hc = null;
    
    assertGC("JEP has not been GCed", refPane);
    assertGC("HC has not been GCed", refHc);
}
 
Example 10
Source File: ExcludingLookupTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testWeCanGetInstanceOfSerializableEvenItIsExcludedIfWeAskForClassNotExtendingIt () throws Exception {
    Lookup lookup = Lookups.exclude (this.instanceLookup, new Class[] { java.io.Serializable.class });
    Lookup.Template t = new Lookup.Template (Object.class);
    Lookup.Result res = lookup.lookup (t);
    
    LL ll = new LL ();
    res.addLookupListener (ll);
    assertEquals ("Nothing is there", 0, res.allItems ().size ());
    
    Object inst = new Integer (3);
    ic.add (inst);
    
    assertEquals ("Not Filtered out", inst, lookup.lookup (Object.class));
    assertEquals ("Not Filtered out2", inst, lookup.lookupItem (t).getInstance ());
    assertEquals ("One is there - 2", 1, res.allItems ().size ());
    assertEquals ("One is there - 2a", 1, res.allInstances ().size ());
    assertEquals ("One is there - 2b", 1, res.allClasses ().size ());
    assertEquals ("Right # of events", 1, ll.getCount ());
    
    ic.remove (inst);
    assertEquals ("Filtered out3", null, lookup.lookupItem (t));
    assertEquals ("Nothing is there - 3", 0, res.allItems ().size ());
    assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ());
    assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ());
    assertEquals ("Of course it is not there", null, lookup.lookup (Object.class));
    assertEquals ("Right # of events", 1, ll.getCount ());
}
 
Example 11
Source File: RecognizeInstanceObjectsOnModuleEnablementTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testEnableModuleWithEntry() throws Exception {
    ModuleSystem ms = Main.getModuleSystem();
    final ModuleManager man = ms.getManager();
    
    CharSequence log = Log.enable("org.openide.loaders.FolderInstance.ui.ch.my", Level.FINE);
    
    final Lookup.Result<JComponent> res = Lookups.forPath("ui").lookupResult(JComponent.class);
    assertTrue("No component registered yet", allWithoutPanel(res).isEmpty());
    class L implements LookupListener {
        int cnt;
        @Override
        public void resultChanged(LookupEvent ev) {
            allWithoutPanel(res);
            cnt++;
        }
    }
    L l = new L();
    res.addLookupListener(l);
    
    doEnableModuleWithEntry(man, res);
    
    assertTrue("Listener notified about change: " + l.cnt, l.cnt >= 1);
    
    int first = log.toString().indexOf("new org.openide.loaders.FolderLookup");
    int none = log.toString().indexOf("new org.openide.loaders.FolderLookup", first + 1);
    
    assertTrue("One instance created: " + first, first >= 0);
    assertEquals("No other instance created: " + none + "\n" + log, -1, none);
}
 
Example 12
Source File: EditorSettingsStorageTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void disabledTestEvents() {
    MimePath mimePath = MimePath.parse("text/x-type-A");
    Lookup lookup = MimeLookup.getLookup(mimePath);
    Lookup.Result<FontColorSettings> result = lookup.lookup(new Lookup.Template<FontColorSettings>(FontColorSettings.class));
    Listener listener = new Listener();
    
    result.addLookupListener(listener);

    Collection<? extends FontColorSettings> instances = result.allInstances();
    assertEquals("Wrong number of FontColorSettings instances", 1, instances.size());
    
    FontColorSettings fcs = instances.iterator().next();
    assertNotNull("Can't find FontColorSettings", fcs);
    
    // Check preconditions
    AttributeSet attribs = fcs.getTokenFontColors("test-events");
    assertNotNull("Can't find test-events coloring", attribs);
    assertEquals("Wrong background color", 
        new Color(0x0A0B0C), attribs.getAttribute(StyleConstants.Background));
    assertEquals("There should be no events yet", 0, listener.eventsCnt);
    
    // Prepare new coloring
    SimpleAttributeSet newAttribs = new SimpleAttributeSet();
    newAttribs.addAttribute(StyleConstants.NameAttribute, "test-events");
    newAttribs.addAttribute(StyleConstants.Background, new Color(0xFFFFF0));

    // Change the coloring
    setOneColoring("text/x-type-A", newAttribs);

    // Check that the event was fired
    assertEquals("Wrong number of events", 1, listener.eventsCnt);
    
    // Check that the new attributes were set
    fcs = lookup.lookup(FontColorSettings.class);
    assertNotNull("Can't find FontColorSettings", fcs);
    attribs = fcs.getTokenFontColors("test-events");
    assertNotNull("Can't find test-events coloring", attribs);
    assertEquals("Wrong background color", 
        new Color(0xFFFFF0), attribs.getAttribute(StyleConstants.Background));
}
 
Example 13
Source File: J2SEPlatformImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private List<URL> defaultJavadoc(final boolean listen) {
    final JavaPlatform safePlatform = new ForwardingJavaPlatform(this) {
        @Override
        public List<URL> getJavadocFolders() {
            return Collections.<URL>emptyList();
        }
    };
    final Set<URI> roots = new LinkedHashSet<>();
    final Lookup.Result<? extends J2SEPlatformDefaultJavadoc> res = getJ2SEPlatformDefaultJavadoc();
    if (listen) {
        synchronized (this) {
            if (jdocListener == null) {
                jdocListener = new LookupListener[2];
                jdocListener[0] = new LookupListener() {
                    @Override
                    public void resultChanged(LookupEvent ev) {
                        javadoc = null;
                    }
                };
                jdocListener[1] = WeakListeners.create(LookupListener.class, jdocListener[0], res);
                res.addLookupListener(jdocListener[1]);
            }
        }
    }
    for (J2SEPlatformDefaultJavadoc jdoc : res.allInstances()) {
        roots.addAll(jdoc.getDefaultJavadoc(safePlatform));
    }
    final List<URL> result = new ArrayList<>(roots.size());
    for (URI root : roots) {
        try {
            result.add(root.toURL());
        } catch (MalformedURLException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    return Collections.unmodifiableList(result);
}
 
Example 14
Source File: ProxyLookup.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private Lookup.Result<T>[] getResults() {
    return results;
}
 
Example 15
Source File: ContextManager.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private <T> boolean isEnabledOnData(Lookup.Result<T> result, Class<T> type, ContextSelection selectMode) {
    boolean res = isEnabledOnDataImpl(result, type, selectMode);
    LOG.log(Level.FINE, "isEnabledOnData(result, {0}, {1}) = {2}", new Object[]{type, selectMode, res});
    return res;
}
 
Example 16
Source File: PrereqCheckerMerger.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public Impl(Lookup.Result<PrerequisitesChecker> res) {
    checkers = res;
}
 
Example 17
Source File: CustomJAXWSSupportProviderTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testProviders() throws Exception {
    Lookup.Result<JAXWSSupportProvider> res = Lookup.getDefault().lookup(new Lookup.Template<JAXWSSupportProvider>(JAXWSSupportProvider.class));
    assertEquals("there should be 2 instances - one from websvc/jaxwsapi and one from tests", 2, res.allInstances ().size ());
}
 
Example 18
Source File: TopComponentGetLookupTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testNodesAreThereEvenIfTheyAreNotContainedInTheirOwnLookup() {
    Lookup.Result<Node> res = lookup.lookup(new Lookup.Template<Node>(Node.class));
    
    AbstractNode n1 = new AbstractNode(Children.LEAF, Lookup.EMPTY);
    
    InstanceContent content = new InstanceContent();
    AbstractNode n2 = new AbstractNode(Children.LEAF, new AbstractLookup(content));
    
    assertNull("Not present in its lookup", n1.getLookup().lookup(n1.getClass()));
    assertNull("Not present in its lookup", n2.getLookup().lookup(n2.getClass()));
    
    top.setActivatedNodes(new AbstractNode[] { n1 });
    assertEquals("But node is in the lookup", n1, lookup.lookup(n1.getClass()));
    
    assertEquals("One item there", 1, res.allInstances().size());
    
    L listener = new L();
    res.addLookupListener(listener);
    
    top.setActivatedNodes(new AbstractNode[] { n2 });
    assertEquals("One node there", 1, get.getActivatedNodes().length);
    assertEquals("n2", n2, get.getActivatedNodes()[0]);
    
    //MK - here it changes twice.. because the setAtivatedNodes is trigger on inner TC, then lookup of MVTC contains old activated node..
    // at this monent the merged lookup contains both items.. later it gets synchronized by setting the activated nodes on the MVTC as well..
    // then it contains only the one correct node..
    listener.check("Node changed", 1);
    
    Collection addedByTCLookup = res.allInstances();
    assertEquals("One item still", 1, addedByTCLookup.size());
    
    content.add(n2);
    assertEquals("After the n2.getLookup starts to return itself, there is no change",
            addedByTCLookup, res.allInstances());
    
    // this could be commented out if necessary:
    listener.check("And nothing is fired", 0);
    
    content.remove(n2);
    assertEquals("After the n2.getLookup stops to return itself, there is no change",
            addedByTCLookup, res.allInstances());
    // this could be commented out if necessary:
    listener.check("And nothing is fired", 0);
    
    content.add(n1);
    // this could be commented out if necessary:
    listener.check("And nothing is fired", 0);
    // Change from former behavior (#36336): we don't *want* n1 in res.
    Collection one = res.allInstances();
    assertEquals("Really just the activated node", 1, one.size());
    Iterator it = one.iterator();
    assertEquals("It is the one added by the TC lookup", n2, it.next());
}
 
Example 19
Source File: XMLDataObjectInfoParser.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private Lookup updateLookup(XMLDataObject realXML, String previousID, String id) {
    if (realXML == null) {
        return lookup;
    }
    
    synchronized (this) {
        if (previousID != null && previousID.equals(id) && lookup != null) {
            if (XMLDataObject.ERR.isLoggable(Level.FINE)) {
                XMLDataObject.ERR.fine("No need to update lookup: " + id + " for " + realXML);
            }
            return lookup;
        }
    }
    Lookup newLookup;
    @SuppressWarnings("deprecation")
    XMLDataObject.Info info = XMLDataObject.getRegisteredInfo(id);
    if (info != null) {
        newLookup = XMLDataObject.createInfoLookup(realXML,info);
        if (XMLDataObject.ERR.isLoggable(Level.FINE)) {
            XMLDataObject.ERR.fine("Lookup from info: " + newLookup + " for " + realXML);
        }
    } else {
        newLookup = Environment.findForOne(realXML);
        if (XMLDataObject.ERR.isLoggable(Level.FINE)) {
            XMLDataObject.ERR.fine("Lookup from env: " + newLookup + " for " + realXML);
        }
        if (newLookup == null) {
            newLookup = Lookup.EMPTY;
        }
    }
    synchronized (this) {
        Lookup.Result prevRes = result;
        lookup = newLookup;
        if (XMLDataObject.ERR.isLoggable(Level.FINE)) {
            XMLDataObject.ERR.fine("Shared lookup updated: " + lookup + " for " + realXML);
        }
        result = lookup.lookupResult(Node.Cookie.class);
        result.addLookupListener(this);
        if (prevRes != null) {
            prevRes.removeLookupListener(this);
            if (XMLDataObject.ERR.isLoggable(Level.FINE)) {
                XMLDataObject.ERR.fine("Firing property change for " + realXML);
            }
            realXML.firePropertyChange(DataObject.PROP_COOKIE, null, null);
            if (XMLDataObject.ERR.isLoggable(Level.FINE)) {
                XMLDataObject.ERR.fine("Firing done for " + realXML);
            }
        }
        return newLookup;
    }
}
 
Example 20
Source File: CustomWebServicesClientViewProviderTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testProviders() throws Exception {
    Lookup.Result res = Lookup.getDefault().lookup(new Lookup.Template(WebServicesClientViewProvider.class));
    assertEquals("there should be 1 instance - from websvc/clientapi", 1, res.allInstances().size());
}