org.openide.util.lookup.InstanceContent Java Examples

The following examples show how to use org.openide.util.lookup.InstanceContent. 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: ContextManagerTest.java    From netbeans with Apache License 2.0 7 votes vote down vote up
public void testSurviveFocusChange() throws Exception {
    InstanceContent ic = new InstanceContent();
    Lookup lkp = new AbstractLookup(ic);

    Action clone = ((ContextAwareAction) Actions.forID("cat", "survive")).createContextAwareInstance(lkp);
    L listener = new L();
    clone.addPropertyChangeListener(listener);

    assertFalse("Disabled", clone.isEnabled());
    Object val = Integer.valueOf(1);
    ic.add(val);
    assertTrue("Enabled now", clone.isEnabled());
    assertEquals("One change", 1, listener.cnt);
    ic.remove(val);
    assertTrue("Still Enabled", clone.isEnabled());

    Survival.value = 0;
    clone.actionPerformed(new ActionEvent(this, 0, ""));
    assertEquals("Added one", 1, Survival.value);
}
 
Example #2
Source File: FilterNode.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private FilterNode(Filter filter, InstanceContent content) {
    super(Children.LEAF, new AbstractLookup(content));
    content.add(filter);

    content.add(filter.getEditor());
    this.filter = filter;
    filter.getChangedEvent().addListener(new ChangedListener<Filter>() {

        public void changed(Filter source) {
            update();
        }
    });

    update();

    Lookup.Template<FilterChain> tpl = new Lookup.Template<FilterChain>(FilterChain.class);
    result = Utilities.actionsGlobalContext().lookup(tpl);
    result.addLookupListener(this);

    FilterTopComponent.findInstance().getFilterSettingsChangedEvent().addListener(this);
    resultChanged(null);
}
 
Example #3
Source File: NbLookupBenchmark.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Fills the lookup with instances */
protected void setUp () {
    Integer integer = (Integer)getArgument ();
    int cnt = integer.intValue ();
    
    boolean reverse = cnt < 0;
    if (reverse) cnt = -cnt;
    
    InstanceContent iContent = new InstanceContent();
    
    lookup = new AbstractLookup(iContent);
    
    while (cnt-- > 0) {
        for (int i = 0; i < INSTANCES.length; i++) {
            if (reverse) {
                iContent.add (INSTANCES[INSTANCES.length - i - 1]);
            } else {
                iContent.add (INSTANCES[i]);
            }
        }
    }
}
 
Example #4
Source File: CMPFieldNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private CMPFieldNode(CmpField field, EntityMethodController controller, FileObject ddFile, InstanceContent instanceContent) {
        super(Children.LEAF, new AbstractLookup(instanceContent));
        instanceContent.add(this); // for enabling Open action
        
        //TODO: RETOUCHE
//        try {
//            ic.add(DataObject.find(JavaModel.getFileObject(controller.getBeanClass().getResource()))); // for enabling SafeDelete action
//        } catch (DataObjectNotFoundException ex) {
//            // ignore
//        }
//        Method getterMethod = controller.getGetterMethod(controller.getBeanClass(), field.getFieldName());
//        if (getterMethod != null) {
//            ic.add(getterMethod); // for SafeDelete refactoring to find Method element
//        }
        this.field = field;
        this.ddFile = ddFile;
        this.controller = controller;
        field.addPropertyChangeListener(WeakListeners.propertyChange(this, field));
    }
 
Example #5
Source File: ActionInvocationTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testMoveAction() throws InterruptedException, InvocationTargetException, DataObjectNotFoundException, IOException {
    DataObject testdo = DataObject.find(f);
    final Node node = testdo.getNodeDelegate();

    InstanceContent ic = new InstanceContent();
    Lookup lookup = new AbstractLookup(ic);
    ic.add(node);
    final Action move = RefactoringActionsFactory.moveAction().createContextAwareInstance(lookup);
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            if (move.isEnabled()) {
                move.actionPerformed(RefactoringActionsFactory.DEFAULT_EVENT);
                if (!((MoveRefactoring) DD.rui.getRefactoring()).getRefactoringSource().lookup(FileObject.class).equals(f))
                    fail("MoveClass was opened with wrong data");
            } else {
                fail("Action is not enabled.");
            }
        }
    });
}
 
Example #6
Source File: CloneableEditorSupportCOSRedirectorTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected void setUp () {
    ic = new InstanceContent ();
    CES support = new CES (this, new AbstractLookup(ic));
    
    MockServices.setServices(Redirector.class, DD.class);
    red = Lookup.getDefault().lookup(Redirector.class);
    assertNotNull(red);
    dd = Lookup.getDefault().lookup(DD.class);
    assertNotNull(dd);

    CloneableEditorSupportCOSRedirectorTest t = new CloneableEditorSupportCOSRedirectorTest(this.content);
    red.master = support;
    InstanceContent slave = new InstanceContent();
    red.slave = new CES(t, new AbstractLookup (slave));
    slave.add(red.master);
}
 
Example #7
Source File: FilterNode.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private FilterNode(Filter filter, InstanceContent content) {
    super(Children.LEAF, new AbstractLookup(content));
    content.add(filter);

    content.add(filter.getEditor());
    this.filter = filter;
    filter.getChangedEvent().addListener(new ChangedListener<Filter>() {

        public void changed(Filter source) {
            update();
        }
    });

    update();

    Lookup.Template<FilterChain> tpl = new Lookup.Template<FilterChain>(FilterChain.class);
    result = Utilities.actionsGlobalContext().lookup(tpl);
    result.addLookupListener(this);

    FilterTopComponent.findInstance().getFilterSettingsChangedEvent().addListener(this);
    resultChanged(null);
}
 
Example #8
Source File: GlobalActionContextProxy.java    From nb-springboot with Apache License 2.0 6 votes vote down vote up
public GlobalActionContextProxy() {
    try {
        this.content = new InstanceContent();
        // The default GlobalContextProvider
        this.globalContextProvider = new GlobalActionContextImpl();
        this.globalContextLookup = this.globalContextProvider.createGlobalContext();
        // Monitor the activation of the Projects Tab TopComponent
        TopComponent.getRegistry().addPropertyChangeListener(this.registryListener);
        // Monitor the existance of a Project in the principle lookup
        this.resultProjects = globalContextLookup.lookupResult(Project.class);
        this.resultProjects.addLookupListener(this.resultListener);
    } catch (Exception e) {
        Exceptions.printStackTrace(e);
    }
    WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
        @Override
        public void run() {
            // Hack to force the current Project selection when the application starts up
            TopComponent tc = WindowManager.getDefault().findTopComponent(PROJECT_LOGICAL_TAB_ID);
            if (tc != null) {
                tc.requestActive();
            }
        }
    });
}
 
Example #9
Source File: FolderNode.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private FolderNode(String name, GroupOrganizer organizer, List<String> oldSubFolders, final List<Group> groups, FolderChildren children, InstanceContent content) {
    super(children, new AbstractLookup(content));
    children.setParent(this);
    this.content = content;
    this.children = children;
    content.add(new RemoveCookie() {

        public void remove() {
            for (Group g : groups) {
                if (g.getDocument() != null) {
                    g.getDocument().removeGroup(g);
                }
            }
        }
    });
    init(name, organizer, oldSubFolders, groups);
}
 
Example #10
Source File: ExternalChangeOfModifiedContentTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUp() throws Exception {
    MockServices.setServices(DD.class);


    clearWorkDir();
    LocalFileSystem fs = new LocalFileSystem();
    fs.setRootDirectory(getWorkDir());

    FileObject fo = fs.getRoot().createData("Ahoj", "txt");

    ic = new InstanceContent();
    support = new CES(this, new AbstractLookup(ic));
    edit = support;
    assertNotNull("we have editor", edit);

    DD.type = -1;
    DD.toReturn = new Stack<Object>();
}
 
Example #11
Source File: StatefulActionProcessorTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testCustomContextAwareInstance() {
    Action a = Actions.forID("Foo", "test.ListAction");
    DefaultListSelectionModel model = new DefaultListSelectionModel();
    
    InstanceContent localContent1 = new InstanceContent();
    AbstractLookup localLookup1 = new AbstractLookup(localContent1);
    
    Action la = ((ContextAwareAction)a).createContextAwareInstance(localLookup1);
    
    assertFalse(a.isEnabled());
    assertFalse(la.isEnabled());
    
    localContent1.add(model);
    
    assertFalse(a.isEnabled());
    assertTrue(la.isEnabled());
    assertFalse((Boolean)la.getValue(Action.SELECTED_KEY));
    
    // checks that the context-bound instance changes its selected state
    // if the model changes (relevant property change event is fired)
    model.setSelectionInterval(1, 2);
    assertTrue((Boolean)la.getValue(Action.SELECTED_KEY));
}
 
Example #12
Source File: FolderNode.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private FolderNode(String name, GroupOrganizer organizer, List<String> oldSubFolders, final List<Group> groups, FolderChildren children, InstanceContent content) {
    super(children, new AbstractLookup(content));
    children.setParent(this);
    this.content = content;
    this.children = children;
    content.add(new RemoveCookie() {

        public void remove() {
            for (Group g : groups) {
                if (g.getDocument() != null) {
                    g.getDocument().removeGroup(g);
                }
            }
        }
    });
    init(name, organizer, oldSubFolders, groups);
}
 
Example #13
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 #14
Source File: ExternalDeleteOfModifiedContentTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUp () throws Exception {
    MockServices.setServices(DD.class);
    
    
    clearWorkDir();
    LocalFileSystem fs = new LocalFileSystem();
    fs.setRootDirectory(getWorkDir());
    
    FileObject fo = fs.getRoot().createData("Ahoj", "txt");

    ic = new InstanceContent();
    support = new CES(this, new AbstractLookup(ic));
    edit = support;
    assertNotNull("we have editor", edit);

    DD.type = -1;
    DD.toReturn = new Stack<Object>();
}
 
Example #15
Source File: XMLCESTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    MockLookup.init();
    final InstanceContent ic = new InstanceContent();
    lkp = new AbstractLookup(ic);
    ic.add("", this);
}
 
Example #16
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 #17
Source File: AngularJsDelcarationFinderTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public TestProject(FileObject dir, ProjectState state, ClassPathProvider classpathProvider, Sources sources) {
    this.dir = dir;
    this.state = state;

    InstanceContent ic = new InstanceContent();
    ic.add(classpathProvider);
    ic.add(sources);

    this.lookup = new AbstractLookup(ic);

}
 
Example #18
Source File: GlobalSharabilityQueryImplTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Lookup getInstanceLookup() {
    InstanceContent instanceContent = new InstanceContent();
    instanceContent.add(sq);
    instanceContent.add(vq);
    Lookup instanceLookup = new AbstractLookup(instanceContent);
    return instanceLookup;
}
 
Example #19
Source File: FilterNodeTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void testNoClass2Cast89329() throws Exception {
    InstanceContent ic = new InstanceContent();
    AbstractLookup lookup = new AbstractLookup(ic);
    AbstractNode a = new AbstractNode(Children.LEAF, lookup);
    
    class F extends FilterNode implements OpenCookie {
        public F(Node n) {
            super(n);
        }
        @Override
        public Node.Cookie getCookie(Class type) {
            if (OpenCookie.class.isAssignableFrom(type)) return this;
            else return super.getCookie(type);
        }

        public void open() {
        }
    }
    
    FilterNode f = new F(a);
    
    ic.add("Kuk");
    
    Class what = String.class;
    assertNull("Indeed null, string is not a cookie", f.getCookie(what));
    assertEquals("Kuk", f.getLookup().lookup(String.class));
}
 
Example #20
Source File: DatabaseConnectionConvertor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
private DatabaseConnectionConvertor(XMLDataObject object) {
    holder = new WeakReference<>(object);
    InstanceContent cookies = new InstanceContent();
    cookies.add(this);
    lookup = new AbstractLookup(cookies);
}
 
Example #21
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 #22
Source File: HistoryComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public HistoryComponent() {
    activatedNodesContent = new InstanceContent();
    lookup = new ProxyLookup(new Lookup[] {
        new AbstractLookup(activatedNodesContent)
    });
    init();
}
 
Example #23
Source File: DependenciesEditorTopComponent.java    From BART with MIT License 5 votes vote down vote up
public DependenciesEditorTopComponent() {
    setName(Bundle.CTL_DependenciesEditorTopComponent());
    setToolTipText(Bundle.HINT_DependenciesEditorTopComponent());
    setLayout(new BorderLayout());
    init();
    content = new InstanceContent();
    lookup = new AbstractLookup(content);
    associateLookup(lookup);
}
 
Example #24
Source File: JAXBWizardRootNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private JAXBWizardRootNode(Project prj, InstanceContent content) {
    super(new JAXBWizardRootNodeChildren(prj), 
            new AbstractLookup(content));
    // adds the node to our own lookup
    content.add (this);
    // adds additional items to the lookup
    content.add (prj);
}
 
Example #25
Source File: SettingsProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private MyLookup(MimePath mimePath, InstanceContent ic) {
    super(ic);

    this.mimePath = mimePath;
    this.ic = ic;
    
    // Start listening
    List<MimePath> allPaths = mimePath.getIncludedPaths();
    this.allCtsi = new CodeTemplateSettingsImpl[allPaths.size()];
    
    for(int i = 0; i < allPaths.size(); i++) {
        this.allCtsi[i] = CodeTemplateSettingsImpl.get(allPaths.get(i));
        this.allCtsi[i].addPropertyChangeListener(WeakListeners.propertyChange(this, this.allCtsi[i]));
    }
}
 
Example #26
Source File: PlatformNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private PlatformNode(PlatformProvider pp, ClassPathSupport cs) {
    super (new PlatformContentChildren (cs), new ProxyLookup(new Lookup[]{
        Lookups.fixed(new PlatformEditable(pp), new JavadocProvider(pp),  new PathFinder()),
                new PlatformFolderLookup(new InstanceContent(), pp)
        }));
    this.pp = pp;
    this.pp.addChangeListener(this);
    setIconBaseWithExtension(PLATFORM_ICON);
}
 
Example #27
Source File: ChangeParametersFix.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void doFullChangeMethodParameters(TreePathHandle tph, ParameterInfo[] newParameterInfo) {
    InstanceContent ic = new InstanceContent();
    ic.add(tph);
    ic.add(new AbstractNode(Children.LEAF, Lookups.singleton(tph)));
    for (ParameterInfo parameterInfo : newParameterInfo) {
        ic.add(parameterInfo);
    }
    Lookup actionContext = new AbstractLookup(ic);
    final Action a = JavaRefactoringActionsFactory.changeParametersAction().createContextAwareInstance(actionContext);
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            a.actionPerformed(RefactoringActionsFactory.DEFAULT_EVENT);
        }
    });
}
 
Example #28
Source File: ElementNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@NonNull
private static Lookup prepareLookup(@NonNull final Description d) {
    final InstanceContent ic = new InstanceContent();
    ic.add(d, ConvertDescription2FileObject);
    ic.add(d, ConvertDescription2DataObject);
    if (d.handle != null) {
        ic.add(d, ConvertDescription2TreePathHandle);
    }
    return new AbstractLookup(ic);
}
 
Example #29
Source File: FeatureManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static synchronized Lookup featureTypesLookup() {
    if (featureTypesLookup != null) {
        return featureTypesLookup;
    }

    String clusters = System.getProperty("netbeans.dirs");
    if (clusters == null) {
        featureTypesLookup = Lookup.EMPTY;
    } else {
        InstanceContent ic = new InstanceContent();
        AbstractLookup l = new AbstractLookup(ic);
        String[] paths = clusters.split(File.pathSeparator);
        for (String c : paths) {
            int last = c.lastIndexOf(File.separatorChar);
            String clusterName = c.substring(last + 1).replaceFirst("[0-9\\.]*$", "");
            String basename = "/org/netbeans/modules/ide/ergonomics/" + clusterName;
            String layerName = basename + "/layer.xml";
            String bundleName = basename + "/Bundle.properties";
            URL layer = FeatureManager.class.getResource(layerName);
            URL bundle = FeatureManager.class.getResource(bundleName);
            if (layer != null && bundle != null) {
                FeatureInfo info;
                try {
                    info = FeatureInfo.create(clusterName, layer, bundle);
                    ic.add(info);
                } catch (IOException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
        featureTypesLookup = l;
    }
    return featureTypesLookup;
}
 
Example #30
Source File: JavaRefactoringGlobalAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected Lookup getLookup(Node[] n) {
    InstanceContent ic = new InstanceContent();
    for (Node node:n) {
        ic.add(node);
    }
    if (n.length>0) {
        EditorCookie tc = getTextComponent(n[0]);
        if (tc != null) {
            ic.add(tc);
        }
    }
    ic.add(new Hashtable(0));
    return new AbstractLookup(ic);
}