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

The following examples show how to use org.openide.util.lookup.InstanceContent#set() . 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: GlobalManagerTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testActionsUpdatedWhenActionMapChanges() throws Exception {
    ContextAwareAction a = Actions.callback("ahoj", null, true, "Ahoj!", "no/icon.png", true);
    final InstanceContent ic = new InstanceContent();
    Lookup lkp = new AbstractLookup(ic);

    ActionMap a1 = new ActionMap();
    ActionMap a2 = new ActionMap();
    a2.put("ahoj", new Enabled());

    ic.add(a1);
    Action clone = a.createContextAwareInstance(lkp);
    class L implements PropertyChangeListener {
        int cnt;
        public void propertyChange(PropertyChangeEvent evt) {
            cnt++;
        }
    }
    L listener = new L();
    clone.addPropertyChangeListener(listener);
    assertFalse("Not enabled", isEnabled(clone));

    ic.set(Collections.singleton(a2), null);

    assertTrue("Enabled now", isEnabled(clone));
    assertEquals("one change", 1, listener.cnt);
}
 
Example 2
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 3
Source File: ContextActionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testContextInstancesAreIndependent() throws Exception {
    System.out.println("testContextInstancesAreIndependent");
    A a = new A();
    assertNull(Utilities.actionsGlobalContext().lookup(String.class)); //sanity check
    InstanceContent ic = new InstanceContent();
    Lookup l = new AbstractLookup(ic);
    Action a3 = a.createContextAwareInstance(l);
    assertFalse(a3.isEnabled());
    PCL pcl = new PCL();
    a3.addPropertyChangeListener(pcl);
    setContent("fuddle");
    a.assertNotPerformed();
    assertTrue(a.isEnabled());
    assertFalse(a3.isEnabled());
    synchronized (a3) {
        //should time out if test is going to pass
        a3.wait(TIMEOUT);
    }
    synchronized (pcl) {
        pcl.wait(TIMEOUT);
    }
    pcl.assertNotFired();
    ic.set(Collections.singleton("boo"), null);
    synchronized (a3) {
        a3.wait(TIMEOUT);
    }
    synchronized (pcl) {
        pcl.wait(TIMEOUT);
    }
    pcl.assertEnabledChangedTo(true);
    clearContent();
    assertTrue(a3.isEnabled());
    assertFalse(a.isEnabled());
}
 
Example 4
Source File: NodeFactorySupportTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Test of createCompositeChildren method, of class org.netbeans.spi.project.ui.support.NodeFactorySupport.
 */
public void testCreateCompositeChildren() throws InterruptedException, InvocationTargetException {
    InstanceContent ic = new InstanceContent();
    final Children dels = new TestDelegates(new AbstractLookup(ic));
    final Node node1 = new AbstractNode(Children.LEAF);
    final Node node2 = new AbstractNode(Children.LEAF);
    final Node node3 = new AbstractNode(Children.LEAF);
    final Node node4 = new AbstractNode(Children.LEAF);
    node1.setName("node1");
    node2.setName("node2");
    node3.setName("node3");
    node4.setName("node4");
    NodeFactory fact1 = new TestNodeFactory(node1);
    NodeFactory fact2 = new TestNodeFactory(node2);
    NodeFactory fact3 = new TestNodeFactory(node3);
    NodeFactory fact4 = new TestNodeFactory(node4);
    List<NodeFactory> col = new ArrayList<NodeFactory>();
    col.add(fact1);
    col.add(fact2);
    ic.set(col, null);

    assertEquals(Arrays.asList(node1, node2), Arrays.asList(dels.getNodes(true)));
    
    col.add(0, fact4);
    col.add(fact3);
    col.remove(fact2);
    ic.set(col, null);
    //#115995, caused by fix for #115128
    SwingUtilities.invokeAndWait(new Runnable() {
        public void run() {
            Node[] nds = dels.getNodes();
            assertEquals(nds[0], node4);
            assertEquals(nds[1], node1);
            assertEquals(nds[2], node3);
        }
    });
    
}
 
Example 5
Source File: NodeDataLookup.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * This private constructor is used by the public constructor
 * so that the InstanceContent can be captured.
 * 
 * @param content the InstanceContent to construct the object with
 */
private NodeDataLookup(InstanceContent content, Lookup lookup) {
    super(content);
    this.content = content;

    if (lookup != null) {
        Collection<? extends Object> objects = lookup.lookupAll(Object.class);
        for (Object obj : objects) {
            dataInstances.add(obj);
        }
        
        content.set(dataInstances, null);
    }
}
 
Example 6
Source File: JavaViewHierarchyRandomTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
static void add(String mimePath, Object... instances) {
    InstanceContent content = new InstanceContent();
    content.set(Arrays.asList(instances), null);
    Lookup lkp = new AbstractLookup(content);
    mime2Lookup.put(MimePath.get(mimePath), lkp);
}
 
Example 7
Source File: ContextActionTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testContextDelegate() throws Exception {
    TestData data = new TestData();
    data.setStateValue(true);
    
    TestData otherData = new TestData();
    
    
    Action baseA = Actions.forID("Test", "CheckedTest");

    // must change main action Lookup after the main delegate exists,
    // so it gets property change and changes its Action.SELECTED_KEY
    // otherwise the .map is null and action just delegates to Fallback
    activate(Lookups.fixed(data));
    
    assertSame(data, Utilities.actionsGlobalContext().lookup(TestData.class));

    InstanceContent ic = new InstanceContent();
    Lookup context = new AbstractLookup(ic);
    ic.add(data);
    assertTrue(getIsEnabled(baseA));
    assertTrue(getIsChecked(baseA));
    
    Action actionA = ((ContextAwareAction)baseA).createContextAwareInstance(context);
    
    assertTrue(getIsEnabled(actionA));
    assertTrue(getIsChecked(actionA));
    
    // let's have completely different local context:
    Lookup context2 = Lookups.fixed(otherData);
    Action actionB = ((ContextAwareAction)baseA).createContextAwareInstance(context2);
    assertTrue(getIsEnabled(actionB));
    // in this context, action should be enabled, but UNchecked.
    assertFalse(getIsChecked(actionB));

    class PCL implements PropertyChangeListener {
        Set<String> propChanges = Collections.synchronizedSet(new HashSet<>());
        
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            String n = evt.getPropertyName();
            if (n != null) {
                propChanges.add(n);
            }
        }
    }
    
    PCL listenerBase = new PCL();
    PCL listenerA = new PCL();
    PCL listenerB = new PCL();
    baseA.addPropertyChangeListener(listenerBase);
    actionA.addPropertyChangeListener(listenerA);
    actionB.addPropertyChangeListener(listenerB);
    
    TestData data3 = new TestData();
    // the data has property false, so actionA should fire & change, not the other ones
    ic.set(Collections.singleton(data3), null);

    // also potentially replans to AWT, so the pending change event from lookup 
    // will be probably processed.
    assertFalse(getIsChecked(actionA));
    assertTrue(getIsChecked(baseA));

    assertTrue(listenerBase.propChanges.isEmpty());
    assertTrue(listenerB.propChanges.isEmpty());
    
    assertTrue(listenerA.propChanges.contains(Action.SELECTED_KEY));
    
    listenerA.propChanges.clear();
    
    otherData.setStateValue(true);

    // again sync with AWT
    assertTrue(getIsChecked(baseA));
    assertTrue(getIsChecked(actionB));

    assertTrue(listenerBase.propChanges.isEmpty());
    assertTrue(listenerA.propChanges.isEmpty());
    
    assertTrue(listenerB.propChanges.contains(Action.SELECTED_KEY));
}
 
Example 8
Source File: SettingsProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private MyLookup(MimePath mimePath, String profile, InstanceContent ic) {
    super(ic);

    this.mimePath = mimePath;
    
    if (profile == null) {
        // Use the selected current profile
        String currentProfile = EditorSettings.getDefault().getCurrentFontColorProfile();
        this.fcsProfile = FontColorSettingsImpl.get(mimePath).getInternalFontColorProfile(currentProfile);
        this.specialFcsProfile = false;
    } else {
        // This is the special test profile derived from the mime path.
        // It will never change.
        this.fcsProfile = profile;
        this.specialFcsProfile = true;
    }
    
    this.ic = ic;
    
    // Start listening
    EditorSettings es = EditorSettings.getDefault();
    es.addPropertyChangeListener(WeakListeners.propertyChange(this, es));
    
    this.kbsi = KeyBindingSettingsImpl.get(mimePath);
    this.kbsi.addPropertyChangeListener(WeakListeners.propertyChange(this, this.kbsi));

    // in fact this could probably be turned into 'assert preferences == null;'
    if (preferences == null) {
        preferences = PreferencesImpl.get(mimePath);
        preferences.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, this, preferences));
    }

    fontColorSettings = new CompositeFCS(mimePath, fcsProfile, preferences);
    keyBindingSettings = this.kbsi.createInstanceForLookup();

    ic.set(Arrays.asList(new Object [] {
        fontColorSettings,
        keyBindingSettings,
        preferences
    }), null);
}
 
Example 9
Source File: GlobalContextImplTest.java    From netbeans with Apache License 2.0 3 votes vote down vote up
public void testComponentChangeActionMapIsPropagatedToGlobalLookup() throws Exception {
    assertEquals("test1", 0, cnt);

    
    InstanceContent ic = new InstanceContent();
    AbstractLookup al = new AbstractLookup(ic);
    tc = new TopComponent (al);

    assertEquals("test2", 0, cnt);
    
    
    ActionMap myMap = new ActionMap();
    myMap.put (KEY, sampleAction);
    assertEquals("test3", 0, cnt);

    tc.requestActive();
    
    assertEquals("test4", 1, cnt);
    
    result = lookup.lookup (new Lookup.Template<ActionMap> (ActionMap.class));
    result.addLookupListener (this);
    result.allItems();
    
    assertEquals("test5", 1, cnt);
    
    ic.set(Collections.singleton(new ActionMap()), null);
    
    assertEquals("One change in ActiomMap delivered", 2, cnt);
}