Java Code Examples for org.openide.util.lookup.InstanceContent#remove()

The following examples show how to use org.openide.util.lookup.InstanceContent#remove() . 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: MultiViewProcessorTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testLookupInitializedForCloneable() {
    InstanceContent ic = new InstanceContent();
    Lookup lookup = new AbstractLookup(ic);
    ic.add(10);

    CloneableTopComponent cmv = MultiViews.createCloneableMultiView("text/context", new LP(lookup));
    assertEquals("10 now", Integer.valueOf(10), cmv.getLookup().lookup(Integer.class));
    
    assertNotNull("MultiViewComponent created", cmv);
    TopComponent mvc = cmv.cloneTopComponent();
    
    assertNotNull("MultiViewComponent cloned", mvc);
    MultiViewHandler handler = MultiViews.findMultiViewHandler(mvc);
    assertNotNull("Handler found", handler);
    
    assertEquals("10 now", Integer.valueOf(10), mvc.getLookup().lookup(Integer.class));
    ic.remove(10);
    ic.add(1);
    assertEquals("1 now", Integer.valueOf(1), mvc.getLookup().lookup(Integer.class));
}
 
Example 2
Source File: SaveAsActionTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testActionStatusUpdatedOnLookupChange() throws Exception {
    final InstanceContent content = new InstanceContent();
    Lookup lkp = new AbstractLookup( content );
    final SaveAsCapable saveAsImpl = new SaveAsCapable() {
        public void saveAs(FileObject folder, String name) throws IOException {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };
    
    TopComponent tc = new TopComponent( lkp );
    editorMode.dockInto( tc );
    tc.open();
    tc.requestActive();
    assertTrue(Arrays.asList(WindowManager.getDefault().getOpenedTopComponents(editorMode)).contains(tc));
    
    ContextAwareAction action = SaveAsAction.create();
    assertFalse( "action is disabled for editor windows without SaveAsCapable in their Lookup", action.isEnabled() );
    
    Action a = action.createContextAwareInstance( tc.getLookup() );
    
    content.add( saveAsImpl );
    assertTrue( "action is enabled for editor windows with SaveAsCapable in their Lookup", a.isEnabled() );
    content.remove( saveAsImpl );
    assertFalse( "action is disabled for editor windows without SaveAsCapable in their Lookup", a.isEnabled() );
}
 
Example 3
Source File: ActionProcessorTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testContextAction() throws Exception {
    Action a = Actions.forID("Tools", "on.int");
    assertTrue("It is context aware action", a instanceof ContextAwareAction);

    InstanceContent ic = new InstanceContent();
    AbstractLookup lkp = new AbstractLookup(ic);
    Action clone = ((ContextAwareAction) a).createContextAwareInstance(lkp);
    NumberLike ten = new NumberLike(10);
    ic.add(ten);

    assertEquals("Number lover!", clone.getValue(Action.NAME));
    clone.actionPerformed(new ActionEvent(this, 300, ""));
    assertEquals("Global Action not called", 10, Context.cnt);

    ic.remove(ten);
    clone.actionPerformed(new ActionEvent(this, 200, ""));
    assertEquals("Global Action stays same", 10, Context.cnt);
}
 
Example 4
Source File: ContextActionInjectTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testOwnContextAction() throws Exception {
    MultiContext.cnt = 0;
    
    FileObject fo = FileUtil.getConfigFile(
        "actions/support/test/testOwnContext.instance"
    );
    assertNotNull("File found", fo);
    Object obj = fo.getAttribute("instanceCreate");
    assertNotNull("Attribute present", obj);
    assertTrue("It is action", obj instanceof Action);
    assertFalse("It is not context aware action: " + obj, obj instanceof ContextAwareAction);
    Action a = (Action)obj;

    InstanceContent ic = contextI;
    ic.add(10);

    assertEquals("Number lover!", a.getValue(Action.NAME));
    a.actionPerformed(new ActionEvent(this, 300, ""));
    assertEquals("Global Action not called", 10, MultiContext.cnt);

    ic.remove(10);
    a.actionPerformed(new ActionEvent(this, 200, ""));
    assertEquals("Global Action stays same", 10, MultiContext.cnt);
}
 
Example 5
Source File: NodeLookupTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void checkInstanceInGetLookup (Object obj, InstanceContent ic, Node node, boolean shouldBeThere) {
    Listener listener = new Listener ();
    Lookup.Result res = node.getLookup().lookupResult(obj.getClass());
    Collection ignore = res.allItems ();
    res.addLookupListener(listener);

    ic.add (obj);
    if (shouldBeThere) {
        listener.assertEvents ("One change in node's lookup", -1, 1);
        assertEquals ("Can access object in content via lookup", obj, node.getLookup ().lookup (obj.getClass ()));
    } else {
        assertNull ("Cannot access object in content via lookup", node.getLookup ().lookup (obj.getClass ()));
    }
        
    
    ic.remove (obj);
    if (shouldBeThere) {
        listener.assertEvents ("One change in node's lookup", -1, 1);
    }
    assertNull ("Cookie is removed", node.getLookup ().lookup (obj.getClass ()));
}
 
Example 6
Source File: ContextActionInjectTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testMultiContextAction() throws Exception {
    MultiContext.cnt = 0;
    
    FileObject fo = FileUtil.getConfigFile(
        "actions/support/test/testInjectContextMulti.instance"
    );
    assertNotNull("File found", fo);
    Object obj = fo.getAttribute("instanceCreate");
    assertNotNull("Attribute present", obj);
    assertTrue("It is context aware action", obj instanceof ContextAwareAction);
    ContextAwareAction a = (ContextAwareAction)obj;

    InstanceContent ic = new InstanceContent();
    AbstractLookup lkp = new AbstractLookup(ic);
    Action clone = a.createContextAwareInstance(lkp);
    ic.add(10);
    ic.add(3L);

    assertEquals("Number lover!", clone.getValue(Action.NAME));
    clone.actionPerformed(new ActionEvent(this, 300, ""));
    assertEquals("Global Action not called", 13, MultiContext.cnt);

    ic.remove(10);
    clone.actionPerformed(new ActionEvent(this, 200, ""));
    assertEquals("Adds 3", 16, MultiContext.cnt);

    ic.remove(3L);
    assertFalse("It is disabled", clone.isEnabled());
    clone.actionPerformed(new ActionEvent(this, 200, ""));
    assertEquals("No change", 16, MultiContext.cnt);
}
 
Example 7
Source File: ContextActionInjectTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testMultiContextActionLookup() throws Exception {
    FileObject fo = FileUtil.getConfigFile(
        "actions/support/test/testInjectContextLookup.instance"
    );
    assertNotNull("File found", fo);
    Object obj = fo.getAttribute("instanceCreate");
    assertNotNull("Attribute present", obj);
    assertTrue("It is context aware action", obj instanceof ContextAwareAction);
    ContextAwareAction a = (ContextAwareAction)obj;

    InstanceContent ic = new InstanceContent();
    AbstractLookup lkp = new AbstractLookup(ic);
    Action clone = a.createContextAwareInstance(lkp);
    ic.add(10);
    ic.add(3L);

    assertEquals("Number lover!", clone.getValue(Action.NAME));
    clone.actionPerformed(new ActionEvent(this, 300, ""));
    assertEquals("Global Action not called", 13, LookupContext.cnt);

    ic.remove(10);
    clone.actionPerformed(new ActionEvent(this, 200, ""));
    assertEquals("Adds 3", 16, LookupContext.cnt);

    ic.remove(3L);
    assertFalse("It is disabled", clone.isEnabled());
    clone.actionPerformed(new ActionEvent(this, 200, ""));
    assertEquals("No change", 16, LookupContext.cnt);
}
 
Example 8
Source File: MultiViewProcessorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testLookupInitialized() {
    InstanceContent ic = new InstanceContent();
    Lookup lookup = new AbstractLookup(ic);
    ic.add(10);

    TopComponent mvc = MultiViews.createMultiView("text/context", new LP(lookup));
    assertEquals("10 now", Integer.valueOf(10), mvc.getLookup().lookup(Integer.class));
    ic.remove(10);
    ic.add(1);
    assertEquals("1 now", Integer.valueOf(1), mvc.getLookup().lookup(Integer.class));
}
 
Example 9
Source File: ActionProcessorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testSurviveFocusChangeBehavior() throws Exception {
    class MyAction extends AbstractAction {
        public int cntEnabled;
        public int cntPerformed;
        
        @Override
        public boolean isEnabled() {
            cntEnabled++;
            return true;
        }
        
        @Override
        public void actionPerformed(ActionEvent ev) {
            cntPerformed++;
        }
    }
    MyAction myAction = new MyAction();
    
    ActionMap disable = new ActionMap();
    ActionMap enable = new ActionMap();
    
    InstanceContent ic = new InstanceContent();
    AbstractLookup al = new AbstractLookup(ic);
    
    ContextAwareAction temp = (ContextAwareAction) Actions.forID("Windows", "my.survival.action");
    Action a = temp.createContextAwareInstance(al);
    
    enable.put(SURVIVE_KEY, myAction);
    
    ic.add(enable);
    assertTrue("MyAction is enabled", a.isEnabled());
    ic.set(Collections.singletonList(disable), null);
    assertTrue("Remains enabled on other component", a.isEnabled());
    ic.remove(disable);
}
 
Example 10
Source File: NodeLookupTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void checkInstanceInGetCookie (Node.Cookie obj, InstanceContent ic, Node node) {
    assertNull("The node does not contain the object yet", node.getCookie(obj.getClass()));
    
    Listener listener = new Listener ();
    node.addNodeListener(listener);
    
    ic.add (obj);
    listener.assertEvents ("One change in node", 1, -1);

    assertEquals("Can access cookie in the content", obj, node.getCookie(obj.getClass()));

    ic.remove (obj);
    listener.assertEvents ("One change in node", 1, -1);
}
 
Example 11
Source File: LookupProviderSupportTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testSourcesMerger() {
    SourcesImpl impl0 = createImpl("group0");
    SourcesImpl impl1 = createImpl("group1");
    SourcesImpl impl2 = createImpl("group2");
    SourcesImpl impl3 = createImpl("group3");
    
    Lookup base = Lookups.fixed(impl0, LookupProviderSupport.createSourcesMerger());
    LookupProviderImpl2 pro1 = new LookupProviderImpl2();
    LookupProviderImpl2 pro2 = new LookupProviderImpl2();
    LookupProviderImpl2 pro3 = new LookupProviderImpl2();
    
    InstanceContent provInst = new InstanceContent();
    Lookup providers = new AbstractLookup(provInst);
    provInst.add(pro1);
    provInst.add(pro2);
    
    pro1.ic.add(impl1);
    pro2.ic.add(impl2);
    pro3.ic.add(impl3);
    
    DelegatingLookupImpl del = new DelegatingLookupImpl(base, providers, "<irrelevant>");
    
    Sources srcs = del.lookup(Sources.class); 
    assertNotNull(srcs);
    SourceGroup[] grps = srcs.getSourceGroups("java");
    assertEquals(3, grps.length);
    
    //now let's add another module to the bunch and see if the new SG appears
    provInst.add(pro3);
    
    srcs = del.lookup(Sources.class); 
    assertNotNull(srcs);
    grps = srcs.getSourceGroups("java");
    assertEquals(4, grps.length);
    
    //now let's remove another module to the bunch and see if the SG disappears
    provInst.remove(pro2);
    
    srcs = del.lookup(Sources.class); 
    assertNotNull(srcs);
    grps = srcs.getSourceGroups("java");
    assertEquals(3, grps.length);
    
    //lets remove one and listen for changes...
    srcs = del.lookup(Sources.class); 
    MockChangeListener ch = new MockChangeListener();
    srcs.addChangeListener(ch);
    provInst.remove(pro1);
    
    ch.assertEvent();
    grps = srcs.getSourceGroups("java");
    assertEquals(2, grps.length);
    
    provInst.add(pro2);
    
    ch.assertEvent();
    grps = srcs.getSourceGroups("java");
    assertEquals(3, grps.length);
    
}
 
Example 12
Source File: NavigatorTCTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** 
 */
@RandomlyFails // NB-Core-Build #9367: Still Unstable
public void test_118082_ExplorerView () throws Exception {
    System.out.println("Testing #118082, Explorer view integration...");

    InstanceContent ic = getInstanceContent();
    
    TestLookupHint explorerHint = new TestLookupHint("explorerview/tester");
    ic.add(explorerHint);
        
    NavigatorTC navTC = NavigatorTC.getInstance();
    NavigatorTCHandle navTCH = new NavigatorTCHandle(navTC);
    try {
        navTCH.open();
        waitForProviders(navTC);
        List<? extends NavigatorPanel> panels = navTC.getPanels();
        assertNotNull("Selected panel should not be null", navTC.getSelectedPanel());
        assertTrue("Expected 1 provider panel, but got " + panels.size(), panels != null && panels.size() == 1);
        assertTrue("Panel class not expected", panels.get(0) instanceof ListViewNavigatorPanel);
        ListViewNavigatorPanel provider = (ListViewNavigatorPanel)panels.get(0);

        // wait for selected node change to be applied, because changes are
        // reflected with little delay
        waitForChange();

        Node[] selNodes = provider.getExplorerManager().getSelectedNodes();
        Node[] actNodes = navTC.getActivatedNodes();
        Action copyAction = provider.getCopyAction();
        assertTrue("Copy action should be enabled", copyAction.isEnabled());
        assertNotNull("Activated nodes musn't be null", actNodes);
        assertNotNull("Explorer view selected nodes musn't be null", selNodes);
        assertTrue("Expected 1 activated node, but got " + actNodes.length, actNodes.length == 1);
        assertTrue("Nodes from explorer view not propagated correctly, should be the same as activated nodes, but got: \n"
                + "activated nodes: " + Arrays.toString(actNodes) +"\n"
                + "explorer view selected nodes: " + Arrays.toString(selNodes),
                Arrays.equals(actNodes, selNodes));

        // test if action map can be found in NavigatorTC lookup
        Collection<? extends ActionMap> result = navTC.getLookup().lookupResult(ActionMap.class).allInstances();
        boolean found = false;
        for (Iterator<? extends ActionMap> it = result.iterator(); it.hasNext();) {
            ActionMap map = it.next();
            Action a = map.get(DefaultEditorKit.copyAction);
            if (a != null) {
                found = true;
                assertSame("Different action instance the expected", a, copyAction);
            }
        }
        assertTrue("Action " + DefaultEditorKit.copyAction + " not found in action map", found);
    } finally {        
    // cleanup
        navTCH.close();
        ic.remove(explorerHint);
    }
}
 
Example 13
Source File: NavigatorTCTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@RandomlyFails // NB-Core-Build #8071: Expected 3 provider panels, but got 2
public void testBugfix93123_RefreshCombo () throws Exception {
    System.out.println("Testing bugfix 93123, correct refreshing of combo box with providers list...");

    InstanceContent ic = getInstanceContent();
    
    TestLookupHint ostravskiHint = new TestLookupHint("ostravski/gyzd");
    ic.add(ostravskiHint);
    TestLookupHint prazskyHint = new TestLookupHint("prazsky/pepik");
    ic.add(prazskyHint);
    TestLookupHint prazskyHint2 = new TestLookupHint("moravsky/honza");
        
    NavigatorTC navTC = NavigatorTC.getInstance();
    NavigatorTCHandle navTCH = new NavigatorTCHandle(navTC);
    try {
        navTCH.open();
        waitForProviders(navTC);
        List<? extends NavigatorPanel> panels = navTC.getPanels();

        assertNotNull("Selected panel should not be null", navTC.getSelectedPanel());
        assertTrue("Expected 2 provider panels, but got " + panels.size(), panels.size() == 2);

        final NavigatorPanel panel = panels.get(1);
        Mutex.EVENT.readAccess(new Mutex.ExceptionAction() {
            @Override
            public Object run() throws Exception {
                NavigatorHandler.activatePanel(panel);
                return null;
            }
        });

        NavigatorPanel selPanel = navTC.getSelectedPanel();
        int selIdx = panels.indexOf(selPanel);

        assertTrue("Expected selected provider #2, but got #1", selIdx == 1);

        ic.add(prazskyHint2);

        // wait for selected node change to be applied, because changes are
        // reflected with little delay
        waitForChange();

        panels = navTC.getPanels();
        assertTrue("Expected 3 provider panels, but got " + panels.size(), panels.size() == 3);

        JComboBox combo = navTC.getPanelSelector();
        assertTrue("Expected 3 combo items, but got " + combo.getItemCount(), combo.getItemCount() == 3);

        assertTrue("Expected the same selection", selPanel.equals(navTC.getSelectedPanel()));

        selIdx = panels.indexOf(selPanel);
        assertTrue("Expected the same selection in combo, sel panel index: "
                + selIdx + ", sel in combo index: " + 
                combo.getSelectedIndex(), selIdx == combo.getSelectedIndex());
    } finally {
        // cleanup
        navTCH.close();
        ic.remove(ostravskiHint);
        ic.remove(prazskyHint);
        ic.remove(prazskyHint2);
    }
}
 
Example 14
Source File: LookupSensitiveActionBase.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void doTestRefreshAfterBeingHidden(boolean clone, boolean menu) throws IOException {
    InstanceContent ic = new InstanceContent();
    Lookup context = new AbstractLookup(ic);
    
    
    Action instance;
    if (clone) {
        Action a = create(Lookup.EMPTY);
        instance = ((ContextAwareAction)a).createContextAwareInstance(context);
    } else {
        instance = create(context);
    }
    
    if (!(instance instanceof Presenter.Popup)) {
        // cannot test, skipping
        return;
    }
    
    
    CharSequence log1 = Log.enable("org.netbeans.modules.project.ui.actions", Level.FINER);
    assertFalse("Disabled", instance.isEnabled());
    if (!log1.toString().contains("Refreshing")) {
        fail("Should be refreshing: " + log1);
    }
    
    JMenuItem item = item(instance, menu);
    JMenu jmenu = new JMenu();
    jmenu.addNotify();
    assertTrue("Peer created", jmenu.isDisplayable());
    jmenu.getPopupMenu().addNotify();
    assertTrue("Peer for popup", jmenu.getPopupMenu().isDisplayable());
    
    item.addPropertyChangeListener(this);
    jmenu.add(item);
    assertEquals("anncessor properly changes, this means the actions framework is activated", 1, ancEvent);
    
    
    assertFalse("Not enabled", item.isEnabled());
    FileObject pfo = TestSupport.createTestProject(FileUtil.createMemoryFileSystem().getRoot(), "yaya");
    FileObject pf2 = TestSupport.createTestProject(FileUtil.createMemoryFileSystem().getRoot(), "blabla");
    MockServices.setServices(TestSupport.TestProjectFactory.class);
    Project p = ProjectManager.getDefault().findProject(pfo);
    Project p2 = ProjectManager.getDefault().findProject(pf2);
    if (p instanceof TestSupport.TestProject) {
        enhanceProject((TestSupport.TestProject)p);
    }
    if (p2 instanceof TestSupport.TestProject) {
        enhanceProject((TestSupport.TestProject)p2);
    }
    
    assertNotNull("Project found", p);
    assertNotNull("Project2 found", p2);
    OpenProjects.getDefault().open(new Project[] { p }, false);
    ic.add(p);
    assertTrue("enabled", item.isEnabled());
    assertEquals("One change", 1, change);

    if (menu) {
        item.removeNotify();
        CharSequence log2 = Log.enable("org.netbeans.modules.project.ui.actions", Level.FINER);
        ic.remove(p);
        ic.add(p2);
        if (log2.length() > 0) {
            fail("Nothing shall happen:\n" + log2);
        }
    } // irrelevant for popups
}
 
Example 15
Source File: TopComponentGetLookupTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void doTestNoChangeWhenSomethingIsChangedOnNotActivatedNode (int initialSize) {
    Object obj = new OpenCookie() { public void open() {} };
    
    Lookup.Result res = lookup.lookup(new Lookup.Template(OpenCookie.class));
    Lookup.Result nodeRes = lookup.lookup (new Lookup.Template(Node.class));
    
    InstanceContent ic = new InstanceContent ();
    CountingLookup cnt = new CountingLookup (ic);
    AbstractNode ac = new AbstractNode(Children.LEAF, cnt);
    for (int i = 0; i < initialSize; i++) {
        ic.add (new Integer (i));
    }
    
    top.setActivatedNodes(new org.openide.nodes.Node[] { ac });
    assertEquals ("One node there", 1, get.getActivatedNodes ().length);
    assertEquals ("It is the ac one", ac, get.getActivatedNodes ()[0]);
    ic.add (obj);
    
    L listener = new L ();
    
    res.allItems();
    nodeRes.allItems ();
    res.addLookupListener (listener);
    
    Collection allListeners = cnt.listeners;
    
    assertEquals ("Has the cookie", 1, res.allItems ().size ());
    listener.check ("No changes yet", 0);

    ic.remove (obj);
    
    assertEquals ("Does not have the cookie", 0, res.allItems ().size ());
    listener.check ("One change", 1);
    
    top.setActivatedNodes (new N[0]);
    assertEquals("The nodes are empty", 0, get.getActivatedNodes ().length);
    listener.check ("No change", 0);
    
    cnt.queries = 0;
    ic.add (obj);
    ic.add (ac);
    listener.check ("Removing the object or node from not active node does not send any event", 0);
    
    nodeRes.allItems ();
    listener.check ("Queriing for node does generate an event", 0);
    assertEquals ("No Queries to the not active node made", 0, cnt.queries);
    assertEquals ("No listeneners on cookies", allListeners, cnt.listeners);
}
 
Example 16
Source File: TopComponentGetLookupTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void doTestNoChangeWhenSomethingIsChangedOnNotActivatedNode(int initialSize) {
    Object obj = new OpenCookie() { public void open() {} };
    
    Lookup.Result<OpenCookie> res = lookup.lookup(new Lookup.Template<OpenCookie>(OpenCookie.class));
    Lookup.Result<Node> nodeRes = lookup.lookup(new Lookup.Template<Node>(Node.class));
    
    InstanceContent ic = new InstanceContent();
    CountingLookup cnt = new CountingLookup(ic);
    AbstractNode ac = new AbstractNode(Children.LEAF, cnt);
    for (int i = 0; i < initialSize; i++) {
        ic.add(new Integer(i));
    }
    
    top.setActivatedNodes(new Node[] { ac });
    assertEquals("One node there", 1, get.getActivatedNodes().length);
    assertEquals("It is the ac one", ac, get.getActivatedNodes()[0]);
    ic.add(obj);
    
    L listener = new L();
    
    res.allItems();
    nodeRes.allItems();
    res.addLookupListener(listener);
    
    Collection allListeners = cnt.listeners;
    
    assertEquals("Has the cookie", 1, res.allItems().size());
    listener.check("No changes yet", 0);
    
    ic.remove(obj);
    
    assertEquals("Does not have the cookie", 0, res.allItems().size());
    listener.check("One change", 1);
    
    top.setActivatedNodes(new N[0]);
    assertEquals("The nodes are empty", 0, get.getActivatedNodes().length);
    listener.check("No change", 0);
    
    cnt.queries = 0;
    ic.add(obj);
    ic.add(ac);
    listener.check("Removing the object or node from not active node does not send any event", 0);
    
    nodeRes.allItems();
    listener.check("Queriing for node does generate an event", 0);
    assertEquals("No Queries to the not active node made", 0, cnt.queries);
    assertEquals("No listeneners on cookies", allListeners, cnt.listeners);
}
 
Example 17
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 18
Source File: NavigatorTCTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@RandomlyFails // NB-Core-Build #8071: instanceof PrazskyPepikProvider
public void testCorrectCallsOfNavigatorPanelMethods () throws Exception {
    System.out.println("Testing correct calls of NavigatorPanel methods...");
    InstanceContent ic = getInstanceContent();

    TestLookupHint ostravskiHint = new TestLookupHint("ostravski/gyzd");
    //nodesLkp.setNodes(new Node[]{ostravskiNode});
    ic.add(ostravskiHint);
    TestLookupHint prazskyHint = new TestLookupHint("prazsky/pepik");

    NavigatorTC navTC = NavigatorTC.getInstance();
    NavigatorTCHandle navTCH = new NavigatorTCHandle(navTC);
    try {
        navTCH.open();
        waitForProviders(navTC);
        NavigatorPanel selPanel = navTC.getSelectedPanel();

        assertNotNull("Selected panel is null", selPanel);
        assertTrue("Panel class not expected", selPanel instanceof OstravskiGyzdProvider);
        OstravskiGyzdProvider ostravak = (OstravskiGyzdProvider)selPanel;
        assertEquals("panelActivated calls count invalid: " + ostravak.getPanelActivatedCallsCount(),
                        1, ostravak.getPanelActivatedCallsCount());
        assertEquals(0, ostravak.getPanelDeactivatedCallsCount());

        ic.add(prazskyHint);
        ic.remove(ostravskiHint);

        // wait for selected node change to be applied, because changes are
        // reflected with little delay
        waitForChange();

        selPanel = navTC.getSelectedPanel();
        assertNotNull(selPanel);
        assertTrue(selPanel instanceof PrazskyPepikProvider);
        PrazskyPepikProvider prazak = (PrazskyPepikProvider)selPanel;

        assertEquals(1, ostravak.getPanelDeactivatedCallsCount());
        assertTrue(ostravak.wasGetCompBetween());
        assertFalse(ostravak.wasActCalledOnActive());
        assertFalse(ostravak.wasDeactCalledOnInactive());

        assertEquals(1, prazak.getPanelActivatedCallsCount());
        assertEquals(0, prazak.getPanelDeactivatedCallsCount());

        ic.remove(prazskyHint);
        ic.add(ostravskiHint);
        // wait for selected node change to be applied, because changes are
        // reflected with little delay
        waitForChange();

        selPanel = navTC.getSelectedPanel();
        assertNotNull("Selected panel is null", selPanel);

        assertEquals(1, prazak.getPanelDeactivatedCallsCount());
        assertTrue(prazak.wasGetCompBetween());
        assertFalse(prazak.wasActCalledOnActive());
        assertFalse(prazak.wasDeactCalledOnInactive());

        navTCH.close();

        selPanel = navTC.getSelectedPanel();
        assertNull("Selected panel should be null", selPanel);
        assertNull("Set of panels should be null", navTC.getPanels());
    } finally {
        // clean
        navTCH.close();
        ic.remove(ostravskiHint);
        ic.remove(prazskyHint);
    }
}
 
Example 19
Source File: NavigatorTCTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void test_112954_LastSelected () throws Exception {
    System.out.println("Testing feature #112954, remembering last selected panel for context type...");

    InstanceContent ic = getInstanceContent();
    
    URL url = NavigatorControllerTest.class.getResource("resources/lastsel/file.lastsel_mime1");
    assertNotNull("url not found.", url);

    FileObject fo = URLMapper.findFileObject(url);
    assertNotNull("File object for test node not found.", fo);
    DataObject dObj = DataObject.find(fo);
    assertNotNull("Data object for test node not found.", dObj);
    Node mime1Node = dObj.getNodeDelegate();
    
    TestLookupHint mime2Hint = new TestLookupHint("lastsel/mime2");
    
    ic.add(mime1Node);
        
    NavigatorTC navTC = NavigatorTC.getInstance();
    NavigatorTCHandle navTCH = new NavigatorTCHandle(navTC);
    try {
        navTCH.open();
        waitForProviders(navTC);
        List<? extends NavigatorPanel> panels = navTC.getPanels();
        assertNotNull("Selected panel should not be null", navTC.getSelectedPanel());
        assertTrue("Expected 3 provider panels, but got " + panels.size(), panels != null && panels.size() == 3);
        assertTrue("Panel class not expected", panels.get(0) instanceof LastSelMime1Panel1);
        assertTrue("Panel class not expected", panels.get(1) instanceof LastSelMime1Panel2);
        assertTrue("Panel class not expected", panels.get(2) instanceof LastSelMime1Panel3);

        // selecting 3rd panel, this should be remembered
        final NavigatorPanel p3 = panels.get(2);
        Mutex.EVENT.readAccess(new Mutex.ExceptionAction() {
            @Override
            public Object run() throws Exception {
                NavigatorHandler.activatePanel(p3);
                return null;
            }
        });

        ic.remove(mime1Node);
        ic.add(mime2Hint);
        
        // wait for selected node change to be applied, because changes are
        // reflected with little delay
        waitForChange();
        waitForProviders(navTC);
        
        panels = navTC.getPanels();
        assertNotNull("Selected panel should not be null", navTC.getSelectedPanel());
        assertTrue("Expected 3 provider panels, but got " + panels.size(), panels != null && panels.size() == 3);
        assertTrue("Panel class not expected", panels.get(0) instanceof LastSelMime2Panel1);
        assertTrue("Panel class not expected", panels.get(1) instanceof LastSelMime2Panel2);
        assertTrue("Panel class not expected", panels.get(2) instanceof LastSelMime2Panel3);
        
        // selecting 2nd panel, this should be remembered
        final NavigatorPanel p2 = panels.get(1);
        Mutex.EVENT.readAccess(new Mutex.ExceptionAction() {
            @Override
            public Object run() throws Exception {
                NavigatorHandler.activatePanel(p2);
                return null;
            }
        });
        
        ic.remove(mime2Hint);
        ic.add(mime1Node);
        
        // wait for selected node change to be applied, because changes are
        // reflected with little delay
        waitForChange();
        waitForProviders(navTC);
        
        // third panel should be selected
        panels = navTC.getPanels();
        assertNotNull("Selected panel should not be null", navTC.getSelectedPanel());
        assertTrue("Expected 3 provider panels, but got " + panels.size(), panels != null && panels.size() == 3);
        assertTrue("Expected LastSelMime1Panel3 panel to be selected, but selected is "
                + navTC.getSelectedPanel().getClass().getSimpleName(), navTC.getSelectedPanel() instanceof LastSelMime1Panel3);
        
        ic.remove(mime1Node);
        ic.add(mime2Hint);
        
        // wait for selected node change to be applied, because changes are
        // reflected with little delay
        waitForChange();
        waitForProviders(navTC);

        
        // third panel should be selected
        panels = navTC.getPanels();
        assertNotNull("Selected panel should not be null", navTC.getSelectedPanel());
        assertTrue("Expected 3 provider panels, but got " + panels.size(), panels != null && panels.size() == 3);
        assertTrue("Expected LastSelMime2Panel2 panel to be selected, but selected is "
                + navTC.getSelectedPanel().getClass().getSimpleName(), navTC.getSelectedPanel() instanceof LastSelMime2Panel2);
    } finally {
        navTCH.close();
        ic.remove(mime1Node);
        ic.remove(mime2Hint);
    }
}
 
Example 20
Source File: OSGILookupProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void checkContent(Project prj, InstanceContent ic, AccessQueryImpl access, ForeignClassBundlerImpl bundler, RecommendedTemplates templates) {
    NbMavenProject nbprj = prj.getLookup().lookup(NbMavenProject.class);
    String effPackaging = nbprj.getPackagingType();
    
    boolean needToCheckFelixProjectTypes = true;
    if(!nbprj.isMavenProjectLoaded()) { 
        // issue #262646 
        // due to unfortunate ProjectManager.findPorjetc calls in awt, 
        // speed is essential during project init, so lets try to avoid
        // maven project loading if we can get the info faster from raw model.
        needToCheckFelixProjectTypes = false;
        Model model;
        try {
            model = nbprj.getRawModel();
        } catch (ModelBuildingException ex) {
            // whatever happend, we can't use the model, 
            // lets try to follow up with loading the maven project
            model = null;
            Logger.getLogger(OSGILookupProvider.class.getName()).log(Level.FINE, null, ex);
        }
        Build build = model != null ? model.getBuild() : null;
        List<Plugin> plugins = build != null ? build.getPlugins() : null;
        if(plugins != null) {
            for (Plugin plugin : plugins) {
                if(OSGiConstants.GROUPID_FELIX.equals(plugin.getGroupId()) && OSGiConstants.ARTIFACTID_BUNDLE_PLUGIN.equals(plugin.getArtifactId())) {
                    needToCheckFelixProjectTypes = true;
                    break;
                }
            }
        } 
    }
    if(needToCheckFelixProjectTypes) {
        String[] types = PluginPropertyUtils.getPluginPropertyList(prj, OSGiConstants.GROUPID_FELIX, OSGiConstants.ARTIFACTID_BUNDLE_PLUGIN, "supportedProjectTypes", "supportedProjectType", /*"bundle" would not work for GlassFish parent POM*/null);
        if (types != null) {
            for (String type : types) {
                if (effPackaging.equals(type)) {
                    effPackaging = NbMavenProject.TYPE_OSGI;
                }
            }
        }
    }
    if (NbMavenProject.TYPE_OSGI.equals(effPackaging)) {
        ic.add(access);
        ic.add(bundler);
        ic.add(templates);
    } else {
        ic.remove(access);
        ic.remove(bundler);
        ic.remove(templates);
    }
}