| <?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: |
| |
| <source lang="java"> |
| DialogDisplayer displayer = DialogDisplayer.getDefault(); |
| </source> |
| |
| in which an API is defined <tt>DialogDisplayer</tt> as an abstract class or interface and an implementation is indirectly made available through a static method like <tt>getDefault()</tt>. This approach gives you a default implementation of <tt>DialogDisplayer</tt>, 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 <tt>getDefault()</tt> method: |
| |
| <source lang="java"> |
| public static DialogDisplayer getDefault() { |
| DialogDisplayer dd = (DialogDisplayer) Lookup.getDefault().lookup(DialogDisplayer.class); |
| |
| if (dd == null) { |
| dd = new Trivial(); |
| } |
| |
| return dd; |
| } |
| </source> |
| |
| As you see, it will attempt to find some instance of <tt>DialogDisplayer</tt> from the default <tt>Lookup</tt> (in other words, one that has been registered via <tt>META-INF/services/</tt>). If it cannot find one, it will return the default implementation (an instance of <tt>Trivial</tt>, which is an inner class of <tt>DialogDisplayer</tt>). |
| |
| Therefore, it seems that you could override the default simply by registering your own implementation of <tt>DialogDisplayer</tt>). 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 (<tt>META-INF/services/org.openide.DialogDisplayer</tt> 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: |
| |
| <source lang="properties"> |
| #-org.netbeans.core.windows.services.DialogDisplayerImpl |
| com.tomwheeler.example.SpecialDialogDisplayerImpl |
| </source> |
| |
| |
| 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> |