サンプルコード
import java.awt.* import java.awt.event.* import javax.swing.* import groovy.swing.* import com.mxgraph.swing.* import com.mxgraph.view.* import com.mxgraph.model.* import com.mxgraph.util.mxXmlUtils import com.mxgraph.util.mxConstants import com.mxgraph.io.* import org.w3c.dom.* def graph = new mxGraph() def mxgc = new mxGraphComponent(graph) parent = graph.getDefaultParent() graph.model.beginUpdate() try { def v1 = graph.insertVertex(parent, null, [label:"処理1", lang:"groovy"], 20, 20, 50, 30, "style1") def v2 = graph.insertVertex(parent, null, [label:"処理2", lang:"SQL"], 170, 20, 50, 30, "style2") def v3 = graph.insertVertex(parent, null, [label:"処理3", lang:"shell"], 320, 20, 50, 30, "style3") graph.insertEdge(parent, null, "正常終了", v1, v2) graph.insertEdge(parent, null, "正常終了", v2, v3) } finally { graph.model.endUpdate() } // モデルの書き込み def codec = new mxCodec() def xml = mxXmlUtils.getXml(codec.encode(graph.getModel())) new File("output.xml").write(xml, "UTF-8") // モデルの読み込みと表示 def mxgc2 def graph2 = new mxGraph(){ @Override public String convertValueToString(Object cell) { if( cell instanceof mxCell ){ if( cell.value instanceof Map ){ return cell.value.label } } return super.convertValueToString(cell) } @Override public void cellLabelChanged(Object cell, Object newVal, boolean autoSize) { if( cell instanceof mxCell ){ if( cell.value instanceof Map ){ cell.value.label = newVal.toString() println cell.value.label mxgc2.refresh() return } } super.cellLabelChanged(cell, newVal, autoSize) } } mxgc2 = new mxGraphComponent(graph2) sb = new SwingBuilder() def frm = sb.frame( title: "JGraphX - save and load", visible: true, size: [400, 150], resizable: true, show:true, contentPane: mxgc2, defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE ){ def document = mxXmlUtils.parseXml(new File("./output.xml").getText("UTF-8")) codec.decode(document.getDocumentElement(), graph2.getModel()) // 読み込んだ図形の値を表示 for(vertex in graph2.getChildVertices(graph2.getDefaultParent())){ println "value=${vertex.getValue()}" } }実行結果
関連情報
JGraphxのダウンロードページ
0 件のコメント:
コメントを投稿