jgraph in action(二)

本文深入剖析了JGraphPad编辑框架的结构与工作流程,包括资源管理、组件功能及国际化支持等内容。介绍了如何通过资源包实现多语言支持,详细解释了核心组件DiagramPane的功能特性,如撤销重做、背景设置等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
com.jgraph.editor.JGraphEditorResources
com.jgraph.editor.JGraphEditorResources 对一组资源包 Resource bundles 建模,得到一组文件,每个文件对应一种语言。
注意:Resource bundlesjava中有类java.util.ResourceBundle
资源文件 resouce files classpath
 
使用 com.jgraph.editor.JGraphEditorResources
在启动时,添加 Resource bundles
JGraphEditorResources.addBundle("com.jgraph.pad.resources.actions");
    将添加属于 actions 的资源包
在运行时,使用 Resource
注意:实际添加资源包时,会根据本地语言来选择适当的资源包。
 
从资源包中获取资源和资源文件,有些静态方法:
• getString: Returns the resource for the specified key and replaces the placeholders with the corresponding
values
• getImage: Tries to load the specified image using the classloader (no resource bundles used)
• getInputStream: Tries to open the specified filename using the classloader (no resource bundles
used)
 
 
com.jgraph.editor.factory.JGraphEditorDiagramPane
com.jgraph.editor.factory.JGraphEditorDiagramPane
继承 JScrollPane
JGraph 组件的 wrapper
并且具有下例扩展:
Undo Support — Creates an undo manager for the contained graph
Backgrounds — Paints background colors, images and the page background (aka. layout view)
Autoscaling — Automatic zooming of the contained graph according to a scaling policy
Printing — Implements the Printable interface for printing the graph
Rulers — Acts as a scrollpane with optional rulers
 
 
这个 com.jgraph.editor.factory.JGraphEditorDiagramPane 唯一具有
com.jgraph.editor.JGraphEditorDiagram( 见上 com.jgraph.editor.JGraphEditorModel 部分 ) 引用的组件。
因此在 JGraphEditorAction 中有 com.jgraph.editor.factory.JGraphEditorDiagramPane
获取 com.jgraph.editor.JGraphEditorDiagram 的代码 如下
public static JGraphEditorDiagram getPermanentFocusOwnerDiagram() {
JGraphEditorDiagramPane diagramPane =
getPermanentFocusOwnerDiagramPane();
if (diagramPane != null)
return diagramPane.getDiagram();
return null;
}
 
所以要使用 diagram page ,就要用在调用 factory 方法创建 diagram pane 时用 diagram 在参数,
这样才能在 diagram pane 中保持 diagram 的引用,如:
public void treeNodesInserted(TreeModelEvent event) {
TreeModel source = (TreeModel) arg0.getSource();
Object[] children = arg0.getChildren();
for (int i = 0; i < children.length; i++) {
if (children[i] instanceof JGraphEditorDiagram)
add(editor.getFactory().createDiagramPane(
(JGraphEditorDiagram) diagram));
}
}  
 
undo 支持:
To implement the respective undo and redo actions,
the undo manager in the diagram pane can be used as follows:
GraphUndoManager manager = diagramPane.getGraphUndoManager();
GraphLayoutCache cache = diagramPane.getGraph().getGraphLayoutCache();
if (manager.canUndo(cache))
manager.undo(cache);
redo
GraphUndoManager manager = diagramPane.getGraphUndoManager();
GraphLayoutCache cache = diagramPane.getGraph().getGraphLayoutCache();
if (manager.canRedo(cache))
manager.redo(cache);
 
 
backgrounds
setBackgroundImage, setBackground and setPageVisible
 
 
autoscaling
setAutoScalePolicy 设置不同的 autoscaling 策略,策略包括:
 
 
printing :
为了支持 printing ,实现了 Pritable 接口:
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(diagramPane);
if (printJob.printDialog())
printJob.print();
The default print method in the diagram pane supports printing diagrams across multiple pages.
 
com.jgraph.editor.factory package.
定义了创建某些UI组件的工厂方法,
定义了一些UI组件
 
com.jgraph.editor.factory.JGraphEditorToolbox
但是 com.jgraph.editor.factory.JGraphEditorToolbox是tools,不是actions。
 
对toolbox上的每个tool,the factory
creates a toggle button and updates the selection tool of the toolbox when the toggle button is pressed.
Since the toolbox installs the selection tool as a marquee handler in the current JGraph, there must also
be a way to set that current graph on a toolbox.
 
Toolbox没有注册监听器来更新graph,但为此提供setGraph方法。
 
The toolbox uses an inner class to redirect events to either the current selection tool or the previous marquee
handler of the graph based on the return values of the isForceMarqueeEvent methods of the two
 
com.jgraph.editor.factory.JGraphEditorComboBox
 
com.jgraph.editor.factory.JGraphEditorComboBox
使用 cell view render 来显示元素。
使用 HashMap 数组来保存信息, contain the attributes
and values for the individual entries and a boolean indicating whether to use an edge view or
vertex view to render the entries 如下:
Map[] attrs = new Hashtable[defaultShapes.length];
for (int i = 0; i < defaultShapes.length; i++) {
attrs[i] = new Hashtable(2);
JGraphpadGraphConstants.setVertexShape(attrs[i],
defaultShapes[i]);
}
 
com.jgraph.editor.factory.JGraphEditorNavigator
 
com.jgraph.editor.factory.JGraphEditorNavigator
通过一个 backing graph 来实现 bird-eye 视图。
 
JGraph  Editor Framework 总结
 
The JGraph Editor Framework provides
a simple document model and defines
a way of dealing with actions, tools and add-on methods to construct a user interface. A factory constructs
the UI based on external configuration files and resource bundles by using these objects.
 
1.      In the startup phase, objects are added to the kit and factory, and files are added to the settings and resources.
2.      Then, optional plugins are initialized to add additional objects and files.
3.      The user interface is then created, typically by calling a sequence of factory methods.
4.      In the shutdown phase, after having displayed all dialogs, the exit method of the editor object should be
invoked, which in turn calls the shutdown method and -hooks in the editor settings. Subclassers should
make sure to call the superclass when overriding this method.
 
The framework also provides a toolbox, diagram pane and navigator component.
The toolbox displays a set of marquee handlers to be used in a graph.
The diagram pane is a scrollpane with add-on functionality,and
the navigator provides a birds-eye view for graphs.
 
 
JGraphpad简介
基于 JGraph Editor Framework
没有引入新的概念,
而是应用已有概念,即
actions and tools to implement the functionality, factory methods to create the
user interface and serializable objects to implement the document model
 
com.jgraph.pad.resources UI 描述文件
com.jgraph.pag.imges :图片
com.jgraph.pad: Defines the elements of the document model
com.jgraph.pad.action: Provides actions to be used in the kit
com.jgraph.pad.dialog: Contains a set of customizable dialogs
com.jgraph.pad.factory: Implements factory methods for constructing the UI
com.jgraph.pad.graph: Provides a custom JGraph implementation
com.jgraph.pad.tool: Implements the tools to be added to the kit
com.jgraph.pad.util: Offers various utility classes
 
JGraphpad 有桌面版和 applet 版。
com.jgraph.JGraphpad
com.jgraph.JGraphpadApplet
 
applet 版只有一个 start 按钮,
button actionPerformed 实现与桌面版的 main 一样,即
new JGraphpad().createApplication(arguments);
 
the JGraphpad class only
creates an editor object ,
starts the application and
defines a shutdown mechanism .
该类的代码大部分是关于 IOC 的,
This is also the reason why JGraphpad does not require
to extend the settings, model, kit or factory objects as these are created and configured in the main
class using dependency injection.
 
启动顺序:
1. Static Initialization — Adds resource bundles and modifies bean information for encoding
2. Editor Construction — Creates and configures objects
ui factory editor kit document model settings 等) that make up the editor
3. Plugins Initialization — Initializes plugins which may alter the existing configuration
4. UI Construction ui factory Creates the user interface based on the final configuration
 
Static Initialization
static 初始化块中,
JGraphEditorResources.addBundles(new String[] {
"com.jgraph.pad.resources.actions",
"com.jgraph.pad.resources.menus",
"com.jgraph.pad.resources.strings",
"com.jgraph.pad.resources.tools" });
 
 
国际化信息
src/plugins/i18nplugin
 
To avoid encoding redundant properties with the XMLEncoder, the static initializer also issues a series
of calls to make fields transient. For example, the code makes all irrelevant fields of the JGraphpadVertexView
transient:
JGraphEditorModel
.makeCellViewFieldsTransient(JGraphpadVertexView.class);
JGraphEditorModel.makeTransient(JGraphpadFile.class, "modified");
JGraphEditorModel.makeTransient(JGraphpadFile.class, "filename");
 
Editor Construction
1.      The createSettings method creates a new JGraphEditorSettings object and passes it to the
configureSettings method. The latter adds the ui.xml configuration file and the user settings file, if it exists. If it
does not exist, it adds the default.ini settings file from the resources package.
Additionally, it installs a shutdown hook to store the user settings file back to the filesystem when the app terminates.
Note that the shutdown hooks are processed in reverse order, thus making sure that this will be the last
shutdown hook to be invoked.
Finally, it installs the prototypes cells( 见前文的 一些设计策略 ) which are created using the createVertex, createGroup and createEdge
methods. The prototypes are placed in the map using this code:
settings.putObject( KEY _GROUPPROTOTYPE, createGroup());
settings.putObject( KEY _VERTEXPROTOTYPE, createVertex());
settings.putObject( KEY _EDGEPROTOTYPE, createEdge());
prototypes factory methods action bundles 中使用 ,比如:
Object groupPrototype = editor.getSettings().getObject(
JGraphpad. KEY _GROUPPROTOTYPE);
actionGroup.putValue( KEY _PROTOTYPE, groupPrototype);
 
 
 
2.      The createModel method passes a new instance of JGraphEditorModel to the
configureModel method,which adds persistence delegates for all objects that the document model may contain, such as documents,
libraries, diagrams, layout caches, graph models, cells, user objects and attributes.
 
可利用 getConnectionSet 方法,减小需要保存的数据量。
使用该方法,可以消除这些冗余: port edge 集合和 edge source 之间的冗余。
To do this, the model's persistence delegate must be changed to
fetch the connection set from the respective method and pass it to the constructor at construction time:
model.addPersistenceDelegate(JGraphpadGraphModel.class,
new DefaultPersistenceDelegate(new String[] { "roots",
"attributes", "connectionSet" }));
To avoid storing the respective properties of the cells, they must be made transient (which is done in the
static initializer in the preceding step):
JGraphEditorModel.makeTransient(DefaultPort.class, "edges");
JGraphEditorModel.makeTransient(DefaultEdge.class, "source");
JGraphEditorModel.makeTransient(DefaultEdge.class, "target");
 
3.      createKit
 
the create method passes a new instance to the
configure method. The kit is configured using the addActions  and addTools helper methods, which add the actions and tools respectively.
The rest of the method creates various listeners to invoke the update method of the kit, namely for all graph changes,
focus traversal and selection changes in the library.
 
4.      createfactory
The construction of the factory completes the creation of the editor object.
In createFactory,
the createGraph method of the factory is redirected to the local createGraph method for ease of use. (It is assumed
that the createGraph method will be the most frequent overridden method of the factory.)
In the configureFactory method, the various factory methods are added to create the user interface.
 
Plugins Initialization
可选
JGraphPad里的 createPlugins 中的片断:
JGraphEditorPlugin plugin = (JGraphEditorPlugin) Class
.forName(defaultPlugins[i]).newInstance();
plugin.initialize(editor, null);
 
UI Construction
Window mainWindow = createMainWindow(editor,
JGraphpadPane.FactoryMethod.NAME);
The createMainWindow method calls the respective factory method and configures the main window
 
The createMainWindow method
calls the respective factory method and configures the main window.(For example by installing the exit action in the window's close icon.)
The rest of the createApplication method deals with
post-initializing the user interface , eg. restoring window bounds and divider locations,wire up factory methods or open default files and libraries. Note that in Swing, the divider locations
need to be restored after the window has been made visible.
 
Shut down
The application shutdown is implemented with the
global exit hook which is to be called after all dialogs have been displayed
 
In the case of an applet it is not allowed to call System.exit. The method above overrides the parent method with an empty implementation to avoid the call.
protected void exit(int code) {
// empty
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值