Java Code Examples for org.openide.util.Lookup#getDefault()

The following examples show how to use org.openide.util.Lookup#getDefault() . 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: RequestProcessorLookupGetDefaultTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testChangeOfDefaultLookupAppliedToRPTask() throws Exception {
    Lookup prev = Lookup.getDefault();
    final Lookup my = new AbstractLookup(new InstanceContent());
    final Thread myThread = Thread.currentThread();
    final RequestProcessor.Task[] task = { null };
    final boolean[] ok = { false };
    
    Lookups.executeWith(my, new Runnable() {
        @Override
        public void run() {
            assertSame("Default lookup has been changed", my, Lookup.getDefault());

            if (task[0] == null) {
                assertSame("We are being executed in the same thread", myThread, Thread.currentThread());
                // once again in the RP
                task[0] = RequestProcessor.getDefault().post(this, 500);
            } else {
                ok[0] = true;
            }
        }
    });
    assertNotNull("In my lookup code executed OK", task[0]);
    assertEquals("Current lookup back to normal", prev, Lookup.getDefault());
    task[0].waitFinished();
    assertTrue("Even RP task had the right lookup", ok[0]);
}
 
Example 2
Source File: GlobalContextImplTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUp () throws Exception {
    tc = new TopComponent ();
    tc.getActionMap ().put (KEY, sampleAction);
    tc.requestActive();
    
    
    Lookup global = Lookup.getDefault();
    
    Object p = global.lookup (org.openide.util.ContextGlobalProvider.class);
    assertNotNull ("There is one", p);
    assertEquals ("Action context provider is our as well", org.netbeans.modules.openide.windows.GlobalActionContextImpl.class, p.getClass ());
    
    
    lookup = org.openide.util.Utilities.actionsGlobalContext();
    result = lookup.lookup (new Lookup.Template<Node> (Node.class));
    result.addLookupListener (this);
    result.allItems();
}
 
Example 3
Source File: TestBase.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void setUp() throws Exception {
    super.setUp();
   
    clearWorkDir();
    
    DerbyDatabasesTest.SampleDatabaseLocator sdl = new DerbyDatabasesTest.SampleDatabaseLocator();
    sampleDBLookup = new ProxyLookup(Lookup.getDefault(), Lookups.singleton(sdl));
    
    Lookups.executeWith(sampleDBLookup, new Runnable() {
        @Override
        public void run() {
            // Force initialization of JDBCDrivers
            JDBCDriverManager jdm = JDBCDriverManager.getDefault();
            JDBCDriver[] registeredDrivers = jdm.getDrivers();
        }
    });
}
 
Example 4
Source File: LookupGetDefaultTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testCanChangeDefaultLookup() throws Exception {
    Lookup prev = Lookup.getDefault();
    final Lookup my = new AbstractLookup();
    final Thread myThread = Thread.currentThread();
    final boolean[] ok = { false };
    
    Lookups.executeWith(my, new Runnable() {
        @Override
        public void run() {
            assertSame("Default lookup has been changed", my, Lookup.getDefault());
            assertSame("We are being executed in the same thread", myThread, Thread.currentThread());
            ok[0] = true;
        }
    });
    assertTrue("In my lookup code executed OK", ok[0]);
    assertEquals("Current lookup back to normal", prev, Lookup.getDefault());
}
 
Example 5
Source File: XMLUpdateDocumentAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void performAction (final Node[] activatedNodes) {
    
    Lookup lookup = Lookup.getDefault();
    Lookup.Template<Class> template = new Lookup.Template(Performer.class);
    final Lookup.Result result = lookup.lookup(template);
    
    RequestProcessor.getDefault().postRequest(new Runnable() {
        public void run() {        
            for (int i = 0; i < activatedNodes.length; i++) {
                UpdateDocumentCookie rc = activatedNodes[i].getCookie
                                               (UpdateDocumentCookie.class);
                if (rc != null) {
                    rc.updateDocumentRoot();
                }

                //??? unfortunatelly there can be only one cookie per node
                // use lookup to emulate N-cookies - performers

                // delegate to all registered performers
                Iterator it = result.allInstances().iterator();
                while (it.hasNext()) {
                    Performer next = (Performer) it.next();
                    next.perform(activatedNodes[i]);
                }
            }
        }
    });
}
 
Example 6
Source File: PathInLookupTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    Lookup.getDefault();
    Field f = Lookup.class.getDeclaredField("defaultLookup");
    f.setAccessible(true);
    f.set(null, null);
    System.setProperty("org.openide.util.Lookup.paths", "MyServices:YourServices");
    MockServices.setServices(P.class);
    Lookup.getDefault();
}
 
Example 7
Source File: ContextProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@NonNull
private static Iterable<? extends ContextProvider> getImpls() {
    Lookup.Result<ContextProvider> res = impls.get();
    if (res == null) {
        final Lookup lkp = new ProxyLookup(
            Lookup.getDefault(),
            Lookups.singleton(new DefaultContextProvider()));
        res = lkp.lookupResult(ContextProvider.class);
        if (!impls.compareAndSet(null, res)) {
            res = impls.get();
        }
    }
    return res.allInstances();
}
 
Example 8
Source File: GephiSolver.java    From ServiceCutter with Apache License 2.0 5 votes vote down vote up
private GraphModel bootstrapGephi() {
	// boostrap gephi
	Lookup lookup = Lookup.getDefault();
	ProjectController pc = lookup.lookup(ProjectController.class);
	pc.newProject();
	@SuppressWarnings("unused")
	Workspace workspace = pc.getCurrentWorkspace();
	GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
	return graphModel;
}
 
Example 9
Source File: Deadlock60917Test.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    System.setProperty("org.openide.util.Lookup", Deadlock60917Test.class.getName() + "$Lkp");
    
    super.setUp();
    
    Lookup l = Lookup.getDefault();
    if (!(l instanceof Lkp)) {
        fail("Wrong lookup: " + l);
    }
    
    clearWorkDir();
}
 
Example 10
Source File: LoggingTestCaseHid.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void run(TestResult result) {
    Lookup l = Lookup.getDefault();
    assertEquals("We can run only with our Lookup", Lkp.class, l.getClass());
    Lkp lkp = (Lkp)l;
    lkp.reset();
    
    super.run(result);
}
 
Example 11
Source File: LoggingTestCaseHid.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Allows subclasses to register content for the lookup. Can be used in 
 * setUp and test methods, after that the content is cleared.
 */
protected final void registerIntoLookup(Object instance) {
    Lookup l = Lookup.getDefault();
    assertEquals("We can run only with our Lookup", Lkp.class, l.getClass());
    Lkp lkp = (Lkp)l;
    lkp.ic.add(instance);
}
 
Example 12
Source File: TabbedHandler.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Possibly invokes popup menu. */
public static void handlePopupMenuShowing(MouseEvent e, int idx) {
    Component c = (Component) e.getSource();
    while (c != null && !(c instanceof Tabbed.Accessor))
        c = c.getParent();
    if (c == null) {
        return;
    }
    final Tabbed tab = ((Tabbed.Accessor)c).getTabbed();

    final Point p = SwingUtilities.convertPoint((Component) e.getSource(), e.getPoint(), c);

    final int clickTab = idx;
    Lookup context = null;
    Action[] actions = null;
    if (clickTab >= 0) {

        TopComponent tc = tab.getTopComponentAt(clickTab);
        if(tc != null) {
            // ask also tabbed to possibly alter actions
            actions = tab.getPopupActions(tc.getActions(), clickTab);
            if (actions == null) { 
                actions = tc.getActions();
            }
            if (actions == null || actions.length == 0 )
                return;
            context = tc.getLookup();
        }
    }
    if( null == context ) {
        actions = tab.getPopupActions(new Action[0], -1);
        if (actions == null || actions.length == 0 )
            return;
        context = Lookup.getDefault();
    }

    showPopupMenu(Utilities.actionsToPopup(actions, context), p, c);
}
 
Example 13
Source File: ContextProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
@CheckForNull
protected Lookup findContext(@NonNull final FileObject file) {
    return Lookup.getDefault();
}
 
Example 14
Source File: MIMESupportLoggingTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected @Override void setUp() throws Exception {
    lookup = (MIMESupportLoggingTest.TestLookup)Lookup.getDefault();
    lookup.init();
}
 
Example 15
Source File: MIMESupport69049Test.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testProblemWithRecursionInIssue69049() throws Throwable {
    Lkp lkp = (Lkp)Lookup.getDefault();
    @SuppressWarnings("unchecked")
    class Pair extends AbstractLookup.Pair implements Runnable {
        public MIMEResolver[] all;
        public MIMEResolver[] all2;
        public Throwable ex;
        public RequestProcessor.Task wait;
        
        
        protected boolean instanceOf(Class c) {
            LOG.info("instanceOf: " + c);
            return c.isAssignableFrom(getType());
        }

        protected boolean creatorOf(Object obj) {
            LOG.info("creatorOf: " + obj);
            return false;
        }

        public Object getInstance() {
            LOG.info("getInstance: " + all);
            if (all == null) {
                all = MIMESupport.getResolvers();
                assertNotNull("Computed", all);
            } else {
                all2 = MIMESupport.getResolvers();
                assertNotNull("Computed", all2);
            }
            LOG.info("after getInstance: " + all + " and " + all2);
            return null;
        }

        public Class getType() {
            return MIMEResolver.class;
        }

        public String getId() {
            return getType().getName();
        }

        public String getDisplayName() {
            return getId();
        }
        
        public void run() {
            try {
                LOG.info("running");
                if (wait != null) wait.waitFinished();
                all = MIMESupport.getResolvers();
                LOG.info("finishing");
            } catch (Throwable e) {
                LOG.log(Level.INFO, "ending with exception", e);
                ex = e;
            }
        }
        
        public void assertResults() throws Throwable {
            if (ex != null) {
                throw ex;
            }

            MIMESupportHid.assertNonDeclarativeResolver("c1 is there", Lkp.c1, all);
        }
    }
    
    lkp.turn(Lkp.c1);
    Pair p = new Pair();
    lkp.ic.addPair(p);

    Pair run1 = new Pair();
    Pair run2 = new Pair();


    LOG.info("Starting the tasks");
    RequestProcessor.Task t2 = new RequestProcessor("t2").post(run2, 20, Thread.NORM_PRIORITY);
    run1.wait = t2;
    RequestProcessor.Task t1 = new RequestProcessor("t1").post(run1);
    
    t1.waitFinished();
    t2.waitFinished();
    
    LOG.info("Waiting for the tasks to finish");
    
    assertTrue("t1 done", t1.isFinished());
    assertTrue("t2 done", t2.isFinished());
    
    run1.assertResults();
    run2.assertResults();

    assertNotNull("Been in the query", p.all);
    assertEquals("In query we cannot do better than nothing", 0, p.all.length);
}
 
Example 16
Source File: StatusLineElementProviderTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected void setUp() {
    impl1 = new Impl ("First");
    impl2 = new Impl ("Second");
    statusLine = new JPanel ();
    Lookup.getDefault ();
}
 
Example 17
Source File: MIMESupportTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected @Override void setUp() throws Exception {
    lookup = (MIMESupportTest.TestLookup)Lookup.getDefault();
    lookup.init();
}
 
Example 18
Source File: ObjectEditor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** A lookup to work on.
 * @return a lookup.
 */
protected Lookup lookup () {
    Lookup l = lookup;
    return l == null ? Lookup.getDefault () : l;
}
 
Example 19
Source File: DataObjectEnvFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Lookup getContextLookup() {
    return Lookup.getDefault();
}
 
Example 20
Source File: ContextProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
@CheckForNull
protected Lookup findContext(@NonNull final URL url) {
    return Lookup.getDefault();
}