Register and Lookup Serivces (Globol Lookup)
1) In service module, create a service interface
public interface TaskIdGenerator {
public String generateID();
}
In order to register a service, you only need to annotate a class with the @ServiceProvider annotation to fulfill the Java extension mechanism requirement, as this annotation causes the META-INF/services folder to be created at compile time, together with the required service provider registration.
@ServiceProvider(service = TaskIdGenerator.class)
public class TaskIdGeneratorImpl implements TaskIdGenerator {
2) Lookup the service from another module
The default Lookup finds the files created at compile time in the META-INF/services folder, evaluates them, and gives back instances of registered implementing classes.
TaskIdGenerator idGen = Lookup.getDefault().lookup(TaskIdGenerator.class);
this.id = idGen.generateID();
or get all registered implementations of the service:
Collection<? extends TaskIdGenerator> idGen =
Lookup.getDefault().lookupAll(TaskIdGenerator.class);
Exchange Data (Local Lookup)
To make loose coupling possible, an information provider is able to administer its own Lookup, providing it to anyone interested in its information. Into its Lookup, the provider can put objects that the consumer might find useful. The consumer only needs to find the Lookup and get the objects, based on their type.
The ability to administer and provide a Lookup is defined via the Lookup.Provider interface:
Lookup getLookup()
The TopComponent class already implements Lookup.Provider and provide a method for resetting the Lookup.
Instead of implementing your own Lookup, you use the Lookup class, which creates a Lookup with one single object:
associateLookup(Lookups.singleton(taskEditorPanel1.task));
You can get the Task instance in another TopComponent subclass:
Task task = WindowManager.getDefault().
findTopComponent("TaskEditorTopComponent").
getLookup().lookup(Task.class);
- Usage 2 (dynamic changes and notifications):
1) On data provider side:
InstanceContent class enables us to dynamically change the content of the Lookup.
InstanceContent content = new InstanceContent();
associateLookup(new AbstractLookup(content));
set the lookup data whenever you want to update it:
Mp3FileObject mp3 = ...;
content.set(Collections.singleton(mp3), null);
2) On data consumer side:
The NetBeans window system already provides an instance of the central
proxy component, the class Global-ActionContextImpl. This class provides
a proxy Lookup, which accesses the local Lookup of the
focused TopComponent. This Lookup can be obtained by calling the static utility method Utilities. actionsGlobalContext().
To get notification of changes in the Lookup, the LookupListener interface is implemented.
A Lookup.Result, which enables us to react to changes for a specific type (in our case Mp3FileObject), is used as a private member.
public class Mp3DetailsView extends TopComponent implements LookupListener {
private Lookup.Result<Mp3FileObject> result = null;
public void componentOpened() {
result = Utilities.actionsGlobalContext().lookupResult(Mp3FileObject.class);
result.addLookupListener(this);
resultChanged(null);
}
public void resultChanged(LookupEvent event) {
Collection<? extends Mp3FileObject> mp3s = result.allInstances();
if(!mp3s.isEmpty()) {
Mp3FileObject mp3 = mp3s.iterator().next();
Hidden Lookup API to Consumers (Global Services)
the services manage the implementation on their own and provide an additional
trivial implementation (as an inner class) in case there is no other implementation registered
in the system. This has the advantage that the user always gets a valid reference to a service and
never a null value.
The advantage of this pattern is that there is no need for users of the service to
know anything about the Lookup API.
public abstract class Mp3Player {
public abstract void play(Mp3FileObject mp3);
public abstract void stop();
public static Mp3Player getDefault() {
Mp3Player player = Lookup.getDefault().lookup(Mp3Player.class);
if(player == null) {
player = new DefaultMp3Player();
}
return(player);
}
private static class DefaultMp3Player extends Mp3Player {
public void play(Mp3FileObject mp3) {
}
public void stop() {}
}
}
References
分享到:
相关推荐
5. 下载完成后,在“插件”对话框的“下载”标签页中,点击“添加按钮”,然后导航到保存.nbm文件的目录,选择并导入所有必要的Python插件文件,例如:org-netbeans-modules-python-editor.nbm、org-netbeans-...
Apache-NetBeans-11.2-bin-windows-x64.exeApache-NetBeans-11.2-bin-windows-x64.exeApache-NetBeans-11.2-bin-windows-x64.exeApache-NetBeans-11.2-bin-windows-x64.exeApache-NetBeans-11.2-bin-windows-x64....
Netbeans8.2的 codeigniter插件 org-nbphpcouncil-modules-php-ci-0.6.0.nbm和org-nbphpcouncil-modules-php-ci-repository-0.6.0.nbm 资源分开上传的
Netbeans8.2的 codeigniter插件 org-nbphpcouncil-modules-php-ci-0.6.0.nbm和org-nbphpcouncil-modules-php-ci-repository-0.6.0.nbm
org-netbeans-modules-web-frameworks-struts2lib-v2_2_3.nbm
NetBeans包括开源的开发环境和应用平台,NetBeans IDE可以使开发人员利用Java平台能够快速创建Web、企业、桌面以及移动的应用程序,NetBeans IDE已经支持PHP、Ruby、JavaScript、Groovy、Grails和C/C++等开发语言。
Apache-NetBeans-11.1-bin-windows-x64 WIDNOWS 64位系统安装文件,java编程IDE。
Apache-NetBeans-12.4-bin-windows-x64.exe
Start the IDE at the command line C:\> netbeans-install-directory\bin\netbeans.exe. To start the IDE (Solaris OS or Linux): Type at the command prompt: /netbeans-install-directory/bin/netbeans To ...
NetBeans IDE 8.0.2 是一个广泛使用的开源集成开发环境(IDE),专为Java开发者设计,尤其针对Java企业版(Java EE)的...通过下载并安装`netbeans-8.0.2-javaee-windows.exe`文件,你就能开始你的Java EE开发之旅了。
Java JDK 7学习笔记,附源代码...-NetBeans:IDE netbeans-7.0-ml-javase-windows.exe -MySQL:mysql-5.5.11-win32.msi -MySQL:驱动程式 mysql-connector-java-5.1.16.zip videos文件夹:提供相关章节操作的教学视频。
netbeans-7.4-javase-windows
Apache NetBeans 12.1 was released on September 1, 2020.
由于公司外网限制。需要的朋友可以搜索NetBeans-7.4-JavaSE-Windows。一起两个分卷。(仅Java)
【标题】"Java实验工具2(netbeans-6.8m2-ml-javase-windows)" 是一个针对Windows平台的Java开发环境,基于NetBeans IDE的特定版本,即6.8 Milestone 2。这个工具包专为进行Java编程实验而设计,适合初学者和专业...
NetBeans Rich Client Platform (RCP) 是一个强大的框架,用于构建桌面应用程序,它利用Java Swing组件和NetBeans的模块化特性。在这个特定的项目中,"NetBeans RCP - 使用 Node & ExplorerManager 结合 ...
org-netbeans-modules-editor-lib.jar
netbeans-8.2-windows.exe 全能开发工具
org-netbeans-api-debugger-jpda.jar netbeans 文件
标题“netbeans-7.3-javase-windows”指的是NetBeans IDE的一个特定版本,即7.3版,专为Windows操作系统设计,用于Java SE(标准版)开发。NetBeans IDE是一款开源的集成开发环境(IDE),它支持多种编程语言,包括...