blob: 0fb676a029c2934d598c7f95dc71f7d6d61cb5d4 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
--><mediawiki xmlns="http://www.mediawiki.org/xml/export-0.3/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.3" xml:lang="en" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.3/ http://www.mediawiki.org/xml/export-0.3.xsd">
<siteinfo>
<sitename>NetBeans Wiki</sitename>
<base>http://wiki.netbeans.org/Main_Page</base>
<generator>MediaWiki 1.15.1</generator>
<case>first-letter</case>
<namespaces>
<namespace key="-2">Media</namespace>
<namespace key="-1">Special</namespace>
<namespace key="0"/>
<namespace key="1">Talk</namespace>
<namespace key="2">User</namespace>
<namespace key="3">User talk</namespace>
<namespace key="4">NetBeans Wiki</namespace>
<namespace key="5">NetBeans Wiki talk</namespace>
<namespace key="6">File</namespace>
<namespace key="7">File talk</namespace>
<namespace key="8">MediaWiki</namespace>
<namespace key="9">MediaWiki talk</namespace>
<namespace key="10">Template</namespace>
<namespace key="11">Template talk</namespace>
<namespace key="12">Help</namespace>
<namespace key="13">Help talk</namespace>
<namespace key="14">Category</namespace>
<namespace key="15">Category talk</namespace>
</namespaces>
</siteinfo>
<page>
<title>DevFaqLookupHowToOverride</title>
<id>7377</id>
<revision>
<id>40173</id>
<timestamp>2010-07-24T20:15:37Z</timestamp>
<contributor>
<username>Jtulach</username>
<id>526</id>
</contributor>
<text xml:space="preserve">__NOTOC__
===How can I override an instance in the Default Lookup?===
As a result of NetBeans design for extensibility, you'll find a lot of code like this:
&lt;source lang="java"&gt;
DialogDisplayer displayer = DialogDisplayer.getDefault();
&lt;/source&gt;
in which an API is defined &lt;tt&gt;DialogDisplayer&lt;/tt&gt; as an abstract class or interface and an implementation is indirectly made available through a static method like &lt;tt&gt;getDefault()&lt;/tt&gt;. This approach gives you a default implementation of &lt;tt&gt;DialogDisplayer&lt;/tt&gt;, but also lets you "plug in" a different one of your own design.
How do you do that? First, here's the implementation of the &lt;tt&gt;getDefault()&lt;/tt&gt; method:
&lt;source lang="java"&gt;
public static DialogDisplayer getDefault() {
DialogDisplayer dd = (DialogDisplayer) Lookup.getDefault().lookup(DialogDisplayer.class);
if (dd == null) {
dd = new Trivial();
}
return dd;
}
&lt;/source&gt;
As you see, it will attempt to find some instance of &lt;tt&gt;DialogDisplayer&lt;/tt&gt; from the default &lt;tt&gt;Lookup&lt;/tt&gt; (in other words, one that has been registered via &lt;tt&gt;META-INF/services/&lt;/tt&gt;). If it cannot find one, it will return the default implementation (an instance of &lt;tt&gt;Trivial&lt;/tt&gt;, which is an inner class of &lt;tt&gt;DialogDisplayer&lt;/tt&gt;).
Therefore, it seems that you could override the default simply by registering your own implementation of &lt;tt&gt;DialogDisplayer&lt;/tt&gt;). If you tried it, you'd find it doesn't work (or at least may not work consistently) because there are already other instances registered and they'll likely take precedence over yours.
So, how do you mask out any other implementations so that yours will be used? In the file where you register the new implementation (&lt;tt&gt;META-INF/services/org.openide.DialogDisplayer&lt;/tt&gt; in this case), you will prefix the other implementation with a pound sign and a minus sign before listing your own on a different line. For example, here's what the file should look like:
&lt;source lang="properties"&gt;
#-org.netbeans.core.windows.services.DialogDisplayerImpl
com.tomwheeler.example.SpecialDialogDisplayerImpl
&lt;/source&gt;
More information about this and other Lookup-related topics, including how to set the order of registered services, can be [http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/doc-files/api.html found in the Utilities API documentation].</text>
</revision>
</page>
</mediawiki>