ラベル JGraphX の投稿を表示しています。 すべての投稿を表示
ラベル JGraphX の投稿を表示しています。 すべての投稿を表示

2014年7月16日水曜日

JGraphXでユーザー情報を含んだ図形の保存・ロードを行う

JGraphXでユーザー情報を含んだ図形の保存・ロードを行うには、以下のコードのようにmxXmlUtils, mxCodecクラスを使用します。また、mxGraphのconvertValueToStringメソッド・cellLabelChangedメソッドもオーバーライドします。

サンプルコード
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のダウンロードページ

2014年7月14日月曜日

JGraphXで図形の上でポップアップメニューを表示する

JGraphXで図形の上でポップアップメニューを表示するには、以下のコードのようにmxMouseAdapterクラスを使用します。

サンプルコード
import java.awt.*
import java.awt.event.*
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.swing.util.*
import com.mxgraph.view.*
import com.mxgraph.util.mxConstants


class MyMouseAdapter extends mxMouseAdapter
{
  def swingbuilder
  def graphcomp
  public MyMouseAdapter(asb, amxgc)
  {
    swingbuilder = asb
    graphcomp = amxgc
  }
  @Override
  public void mouseClicked(MouseEvent ev)
  {
    if( SwingUtilities.isRightMouseButton(ev) && graphcomp.getCellAt(ev.getX(), ev.getY()) != null ){
      swingbuilder.popup.show(graphcomp, ev.getX(), ev.getY())
    }
  }
}

def graph = new mxGraph()
def mxgc = new mxGraphComponent(graph)

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - popup menu on cells",
  visible: true,
  size: [400, 150],
  resizable: true,
  show:true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){
  popupMenu(id:"popup"){
    menuItem(text:"show cell label", mnemonic: "S", actionPerformed:{
      def sm = graph.getSelectionModel()
      for(cell in sm.getCells()){
        println "selected:${graph.getLabel(cell)}"
      }
    })
  }

  parent = graph.getDefaultParent()
  graph.model.beginUpdate()
  try
  {

    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 50, 30, "style1")
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 50, 30, "style2")
    def v3 = graph.insertVertex(parent, null, "処理3",
      320, 20, 50, 30, "style3")

    graph.insertEdge(parent, null, "正常終了", v1, v2)
    graph.insertEdge(parent, null, "正常終了", v2, v3)

    mxgc.getGraphControl().addMouseListener(new MyMouseAdapter(sb, mxgc))

  }
  finally
  {
    graph.model.endUpdate()
  }
}
実行結果

関連情報
JGraphxのダウンロードページ

2014年7月12日土曜日

JGraphXで選択されている図形・線を削除する

JGraphXで選択されている図形・線を削除するには、以下のコードのようにGraphSelectionModelのgetCellsとremoveCellsメソッドを使用します。
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.view.*
import com.mxgraph.util.mxConstants

def graph = new mxGraph()
def mxgc = new mxGraphComponent(graph)

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - deleting selected cells",
  visible: true,
  size: [400, 150],
  resizable: true,
  show:true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){
  menuBar(){
    menu(text:"Test", mnemonic: "T"){
      menuItem(text:"Delete selected cells", mnemonic: "S", actionPerformed:{
        def sm = graph.getSelectionModel()
        graph.removeCells(sm.getCells(), false /*=include edges*/)
      })
    }
  }

  parent = graph.getDefaultParent()
  graph.model.beginUpdate()
  try
  {

    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 50, 30, "style1")
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 50, 30, "style2")
    def v3 = graph.insertVertex(parent, null, "処理3",
      320, 20, 50, 30, "style3")

    graph.insertEdge(parent, null, "正常終了", v1, v2)
    graph.insertEdge(parent, null, "正常終了", v2, v3)

  }
  finally
  {
    graph.model.endUpdate()
  }
}
実行結果

関連情報
JGraphxのダウンロードページ

2014年7月10日木曜日

JGraphXで選択されている図形・線を取得する

JGraphXで選択されている図形・線を取得するには、以下のコードのようにGraphSelectionModelのgetCellsメソッドを使用します。
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.view.*
import com.mxgraph.util.mxConstants

def graph = new mxGraph()
def mxgc = new mxGraphComponent(graph)

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - getting selected cells",
  visible: true,
  size: [400, 150],
  resizable: true,
  show:true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){
  menuBar(){
    menu(text:"Test", mnemonic: "T"){
      menuItem(text:"Show selected cells", mnemonic: "S", actionPerformed:{
        def sm = graph.getSelectionModel()
        for(cell in sm.getCells()){
          println "selected:${graph.getLabel(cell)}"
        }
      })
    }
  }

  parent = graph.getDefaultParent()
  graph.model.beginUpdate()
  try
  {

    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 50, 30, "style1")
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 50, 30, "style2")
    def v3 = graph.insertVertex(parent, null, "処理3",
      320, 20, 50, 30, "style3")

    graph.insertEdge(parent, null, "正常終了", v1, v2)
    graph.insertEdge(parent, null, "正常終了", v2, v3)

  }
  finally
  {
    graph.model.endUpdate()
  }
}
実行結果

関連情報
JGraphxのダウンロードページ

2014年7月8日火曜日

JGraphXで単一選択モードに設定する

JGraphXで単一選択モードに設定するには、以下のコードのようにGraphSelectionModelのsetSingleSelectionメソッドを使用します。
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.view.*
import com.mxgraph.util.mxConstants

def graph = new mxGraph()
def mxgc = new mxGraphComponent(graph)

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - single selection mode",
  visible: true,
  size: [400, 150],
  resizable: true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){

  parent = graph.getDefaultParent()
  graph.model.beginUpdate()
  try
  {

    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 50, 30, "style1")
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 50, 30, "style2")
    def v3 = graph.insertVertex(parent, null, "処理3",
      320, 20, 50, 30, "style3")

    graph.insertEdge(parent, null, "正常終了", v1, v2)
    graph.insertEdge(parent, null, "正常終了", v2, v3)

    def sm = graph.getSelectionModel()
    sm.setSingleSelection(true)

  }
  finally
  {
    graph.model.endUpdate()
  }
}
実行結果

関連情報
JGraphxのダウンロードページ

2014年7月6日日曜日

JGraphXで全ての図形・線をプログラムで選択する

JGraphXで全ての図形・線をプログラムで選択するには、以下のコードのようにmxGraphのselectAllメソッドを使用します。
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.view.*
import com.mxgraph.util.mxConstants

def graph = new mxGraph()
def mxgc = new mxGraphComponent(graph)

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - selecting all vertices and edges",
  visible: true,
  size: [400, 150],
  resizable: true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){

  parent = graph.getDefaultParent()
  graph.model.beginUpdate()
  try
  {

    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 50, 30, "style1")
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 50, 30, "style2")
    def v3 = graph.insertVertex(parent, null, "処理3",
      320, 20, 50, 30, "style3")

    graph.insertEdge(parent, null, "正常終了", v1, v2)
    graph.insertEdge(parent, null, "正常終了", v2, v3)

    // 図形と線をすべて選択
    graph.selectAll()
  }
  finally
  {
    graph.model.endUpdate()
  }
}
実行結果

関連情報
JGraphxのダウンロードページ

2014年7月4日金曜日

JGraphXで図形をプログラムで選択する

JGraphXで図形をプログラムで選択するには、以下のコードのようにGraphSelectionModelのaddCellを使用します。
サンプルコード
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.view.*
import com.mxgraph.util.mxConstants

def graph = new mxGraph()
def mxgc = new mxGraphComponent(graph)

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - selecting a vertex",
  visible: true,
  size: [400, 150],
  resizable: true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){

  parent = graph.getDefaultParent()
  graph.model.beginUpdate()
  try
  {

    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 50, 30, "style1")
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 50, 30, "style2")
    def v3 = graph.insertVertex(parent, null, "処理3",
      320, 20, 50, 30, "style3")

    graph.insertEdge(parent, null, "正常終了", v1, v2)
    graph.insertEdge(parent, null, "正常終了", v2, v3)

    def sm = graph.getSelectionModel()
    sm.clear()
    sm.addCell(v2)

  }
  finally
  {
    graph.model.endUpdate()
  }
}
実行結果

関連情報
JGraphxのダウンロードページ

2014年7月2日水曜日

JGraphXでテキストを太字・イタリックで表示する

JGraphXでテキストを太字・イタリックで表示するにはmxConstantsの以下の定数を使用します。
  • STYLE_FONTSTYLE:テキストのスタイル
  • FONT_BOLD:太字
  • FONT_ITALIC:イタリック
サンプルコード
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.view.*
import com.mxgraph.util.mxConstants

def graph = new mxGraph()
def mxgc = new mxGraphComponent(graph)

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - bold and italic",
  visible: true,
  size: [400, 150],
  resizable: true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){

  parent = graph.getDefaultParent()
  graph.model.beginUpdate()
  try
  {
    def stylesheet = graph.getStylesheet()

    def style1 = [
      (mxConstants.STYLE_FONTSTYLE): mxConstants.FONT_BOLD
    ]
    stylesheet.putCellStyle("style1", style1)
    def style2 = [
      (mxConstants.STYLE_FONTSTYLE): mxConstants.FONT_ITALIC
    ]
    stylesheet.putCellStyle("style2", style2)

    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 50, 30, "style1")
    def v2 = graph.insertVertex(parent, null, "Process2",
      170, 20, 50, 30, "style2")
    def v3 = graph.insertVertex(parent, null, "処理3",
      320, 20, 50, 30, "style3")

    graph.insertEdge(parent, null, "正常終了", v1, v2)
    graph.insertEdge(parent, null, "正常終了", v2, v3)
  }
  finally
  {
    graph.model.endUpdate()
  }
}
実行結果

関連情報
JGraphxのダウンロードページ

2014年6月30日月曜日

JGraphXでマウスオーバー時の色を設定する

JGraphXでマウスオーバー時の色を設定するには、以下のコードのようにmxCellTrackerクラスを使用します。

サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.swing.handler.*
import com.mxgraph.swing.util.mxSwingConstants
import com.mxgraph.view.*

def graph = new mxGraph()
def mxgc = new mxGraphComponent(graph)
def mxct = new mxCellTracker(mxgc, new Color(0x77, 0x99, 0xdd))

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - hover color for vertices and edges",
  visible: true,
  size: [300, 200],
  resizable: true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){

  parent = graph.getDefaultParent()
  graph.model.beginUpdate()
  try
  {
    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 80, 30)
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 80, 30)

    graph.insertEdge(parent, null, "正常終了", v1, v2)
  }
  finally
  {
    graph.model.endUpdate()
  }
}
実行結果

関連情報
JGraphxのダウンロードページ