重点:
1.创建NetBeans Plaform Application项目
2.可以生成多个模块,设置依赖,降低耦合
选择文件| 新建项目,然后选择NetBeans模块。选择“ NetBeans平台应用程序”
右键单击“ WordEditorCore”模块,然后选择“新建” |“新建”。其他。在“模块开发”类别中,选择“窗口”:
本教程涵盖了两个重要的概念。
- 该应用程序包含四个模块。如果(1)第一个模块显式公开包,并且(2)第二个模块设置对第一个模块的依赖性,则一个模块的代码只能由另一个模块使用。这样,NetBeans平台有助于在严格的模块化体系结构中组织代码,从而确保不会随机重用代码,而仅在提供代码的模块之间设置了合同时才可重用。
- 其次,作为JDK 6 ServiceLoader方法的扩展,引入了Lookup类作为模块之间进行通信的机制。通过其接口加载实现。无需使用实现中的任何代码,“ WordEditorCore”模块就可以显示实现者提供的服务。以此方式向NetBeans平台应用程序提供了松散耦合。
NetBeans Selection Management Tutorial I-Using a TopComponent’s Lookup
So, What's the Point?
You might be wondering what the point of this exercise is—you've just shown that you can handle selection—big deal! The key to the importance of this is the way the code is split into three modules—the My Viewer module knows nothing about the My Editor module—either one can run by itself. They only share a common dependency on My API. That's important—it means two things: 1. My Viewer and My Editor can be developed and shipped independently, and 2. Any module that wants to provide a different sort of editor than My Editor can do so, and the viewer component will work perfectly with it, as long as the replacement editor offers an instance of Event
from its Lookup.
To really picture the value of this, imagine Event
were something much more complex; imagine that MyEditor
is an image editor, and Event
represents an image being edited. The thing that's powerful here is that you could replace MyEditor
with, say, an SVG vector-based editor, and the viewer component (presumably showing attributes of the currently edited image) will work transparently with that new editor. It is this model of doing things that is the reason you can add new tools into the NetBeans IDE that work against Java files, and they will work in different versions of NetBeans, and that you can have an alternate editor (such as the form editor) for Java files and all the components and actions that work against Java files still work when the form editor is used.
This is very much the way NetBeans works with Java and other source files—in their case, the thing that is available from the editor's Lookup is a DataObject
, and components like Navigator and the Property Sheet are simply watching what object is being made available by the focused TopComponent
.
Another valuable thing about this approach is that often people are migrating existing applications to the NetBeans Platform. The object that is part of the data model, in that case, is probably existing, working code that should not be changed in order to integrate it into NetBeans. By keeping the data model's API in a separate module, the NetBeans integration can be kept separate from the core business logic.