jgraph in action(三)

来源:互联网 发布:c语言 宏 编辑:程序博客网 时间:2024/06/06 06:52
 
 
JGraph document Model
library是预定义的cell
 
示例:
int number = model.getChildCount(file) + 1;
JGraphpadDiagram newDiagram = new JGraphpadDiagram(
getString("Diagram") + number);
model.addChild(newDiagram, (JGraphpadFile) file);
 
model.removeNodeFromParent(mutableTreeNode);
 
 
All JGraphpadFile and JGraphpadLibrary objects are roots, and
all JGraphpadDiagram objects are direct children of JGraphpadFile objects.
The root contains one or more files (documents or libraries), and the documents contain one or more diagrams.
 
 
factory methods定制之Main Window
[1] 主窗口结构如下:
JGraphpadPane.FactoryMethod创建主窗口,
负责整个JFrame实例的创建(包括menubar, toolbar, toolbox, statusbar and main area
main area包括:leftcontentbottom三部分。
Leftbottom由各自的factory methods创建,返回JTabbedPane.
 
示例:创建toolboxNode为参数创建
Node toolboxConfig = JGraphEditorSettings.getNodeByName(
configuration.getChildNodes(), NODENAME_TOOLBOX);
JGraphEditorToolbox toolbox = editor.getFactory()
.createToolbox(toolboxConfig);
configuration对象代表xml格式的ui描述中的最外层结点(例如下面的xml),
getNodeByName获取名为toolbox的结点。
<ui>
<toolbox>
<item key="selectTool"/>
<separator/>
<item key="createDashPatternCombo"/>
</toolbox>
</ui>
menubartoolbar也是类似:
Node menuBarConfig = JGraphEditorSettings.getNodeByName(
configuration.getChildNodes(), NODENAME_MENUBAR);
frame.setJMenuBar((JMenuBar) editor.getFactory().createMenuBar(
menuBarConfig));
[...]
Node toolbarConfiguration = JGraphEditorSettings.getNodeByName(
configuration.getChildNodes(), NODENAME_TOOLBAR);
JToolBar toolbar = editor.getFactory().createToolBar(
toolbarConfiguration);
注意下面,没有以Node做参数而是直接创建:
Component statusBar = editor.getFactory().executeMethod(
JGraphpadStatusBar.FactoryMethod.NAME);
frame.getContentPane().add(statusBar, BorderLayout.SOUTH);
 
JGraphpadPane.DocumentTracker这个内部类是在document modelpane之间的中间者,在model变化的时候,更新pane
 
 
[2] 主窗口的组件
 
 
窗口的main areaJGraphPane,分为3部分,
leftbottom是由factory method创建,而contentJDeskTopPane)部分在constructor中创建。
Component leftTab = editor.getFactory().executeMethod(
LeftTabFactoryMethod.NAME);
Component bottomTab = editor.getFactory().executeMethod(
BottomTabFactoryMethod.NAME);
三部分的位置信息注册到settings对象,供显示和退出保存。
 
Content被注册了一个鼠标监听器:
desktopPane.addMouseListener(new JGraphpadMouseAdapter(editor,
NODENAME_DESKTOPPOPUPMENU));
这个监听器根据配置文件处理右键弹出窗口问题。
 
JGraphpadPane另外有些method,其用途是
DocumentTracker调用,以同步model的各种变化。
在其中有个configureDiagramPane hook which is called when a new diagram pane is created for a diagram
 
 
 
 
[3] left tab
left是一个JSplitPane
factory.executeMethod(JGraphEditorNavigator.FactoryMethod.NAME);
factory.executeMethod(JGraphpadLibraryPane.FactoryMethod.NAME);
JSplitPane navigatorSplit = editor.getFactory().createSplitPane(
navigator, libraryPane, JSplitPane.VERTICAL_SPLIT);
editor.getSettings().putObject(KEY_NAVIGATORSPLIT, navigatorSplit);
 
为了记住JSplitPane的当前位置信息,添加shut down hook
editor.getSettings().addShutdownHook(
new JGraphEditorSettings.ShutdownHook() {
public void shutdown() {
editor.getSettings().storeSplitPane(
JGraphpad.NAME_USERSETTINGS,
KEY_NAVIGATORSPLIT);
}
});
 
 
[4] bottom tab
bottom是一个JTabbedPane,如下:
JTabbedPane tabPane = editor.getFactory().createTabbedPane(
JTabbedPane.TOP);
Component console = editor.getFactory().executeMethod(
JGraphpadConsole.FactoryMethod.NAME);
if (console != null)
tabPane.addTab(JGraphEditorResources.getString("Errors"),
editor.getFactory().createScrollPane(console));
 
 
factory methods定制之Library Pane
The library pane is inserted into the main area of the JGraphpadPane and displays the entries in the library.
The library supports drag and drop and copy, paste, cut and delete, and has a reference to the library
in the document model.
待续。。。
 
 
factory methods定制之Window Menu
 
创建并注册到settings
JMenu menu = new JGraphpadWindowMenu();
editor.getSettings().putObject(KEY_WINDOWMENU, menu);
 
JGraphpadWindowMenu静态动态entities两部分,后者是保存在List
当内部的frame(多窗口程序)变化时(removeadd),后者会相应变化。 
 
factory methods定制之Open Recent Menu
JGraphpadOpenRecentMenu实现,也是动态的,
 
通过读取保存在一个property对象中的List
tmp = props.getProperty(KEY_RECENTFILES + i++);
 
更新“最近的文档”的记录:
editor.getSettings().pushListEntryProperty(
NAME_USERSETTINGS, KEY_RECENTFILES, filename,
MAX_RECENTFILES);
 
factory methods定制之Status bar
JGraphpadStatusBarJPanel的扩展,
 
constructor中用工厂方法创建,并且注册所有关心的listeners,即
焦点的传递,和模型的事件,当前graph的鼠标移动事件,
 
factory methods定制之console
com.jgraph.pad.factory.JGraphpadConsole继承JTextArea
连接到outerr系统流。
 
 
factory methods定制之combo boxes
 
 
除去framework提供的combo box外,pad提供了关于颜色和shape的combo box。
 
factory methods定制之custom tools
framework只对tools支持名字和isAlwaysActive属性。而继承的
JGraphpadVertexToolJGraphpadEdgeTool支持更多,并且后者用从前缀继承。
两者都使用了prototype
 
The vertex tool is used to insert cells with bounds,
the edge tool is used to insert cells with points
 
添加tool,使用
createVertexTool
createEdgeTool
下面以一个tree作为user object为例:
JTree tree = new JTree();
tree.setRootVisible(false);
kit.addTool(createVertexTool("treeTool", tree,
JGraphpadVertexRenderer.SHAPE_RECTANGLE, null));
边框会在插入时刻被添加。
kit.addTool(createEdgeTool("orthogonalEdgeTool",
"", GraphConstants.ROUTING_SIMPLE));
最后,添加到配置文件
# treeTool
treeTool.tooltip=Tree
treeTool.icon=/com/jgraph/pad/images/tree.gif
和ui的xml
<toolbox>
[...]
<item key="treeTool"/>
</toolbox>
 
factory methods定制之Dialogs
内置对话框:
文件对话框:
使用如下:
JGraphpadDialogs.getSharedInstance().editorFileDialog(
getActiveFrame(), getString("OpenJGraphpadFile"),
null, true, ".csv",lastDirectory)
文件对话框会以上次目录为当前目录。
上面的参数true指明对话框类型:open
 
字体对话框
 
about对话框:
 
授权对话框
JGraphpadAuthenticator继承java.net. Authenticator
用来获取基于网络的授权
 
不直接使用,而是作为默认的对话框,以参数形式传递给Authenticator
Authenticator.setDefault(new JGraphpadAuthenticator());
 
 
工具类