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月13日日曜日

SwingBuilderでテーブルにポップアップメニューを表示する

SwingBuilderでテーブルにポップアップメニューを表示するには、以下のコードのようにpopupMenuでポップアップメニューを作成しcomponentPopupMenuで設定します。

サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

data = [
  [category:'City', city:'Tokyo'],
  [category:'City', city:'Osaka'],
  [category:'City', city:'Sapporo']
]

sb = new SwingBuilder()
def popupMenu = sb.popupMenu() {
  menuItem() {
    action( name:'Information',
      closure:{
        if( sb.table.getSelectedRowCount() != 0 ){
          sb.optionPane(
            message:(sb.table.getSelectedRows() as ArrayList<Integer>)
              .collect {sb.table.model.getValueAt(it,1)}.join(","),
            messageType:JOptionPane.INFORMATION_MESSAGE)
            .createDialog("table").visible = true
        } else {
          sb.optionPane(
            message:"no row selected",
            messageType:JOptionPane.INFORMATION_MESSAGE)
            .createDialog("table").visible = true
        }
      }
    )
  }
}

sb.edt {
  frame(
    title: "example - table with popup menu",
    show: true,
    resizable: true,
    size: [300, 100],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    scrollPane(){
      table(id:"table", componentPopupMenu:popupMenu){
        tableModel(list: data){
          propertyColumn( header:"category", propertyName: "category", editable: false )
          propertyColumn( header:"city", propertyName: "city", editable: false )
        }
      }
    }
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

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月11日金曜日

SwingBuilderでラベルのアイコンとテキスト間の幅を設定する

SwingBuilderでラベルのアイコンとテキスト間の幅を設定するには、以下のコードのようにiconTextGapに幅を指定します。

サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example : gap between label and icon",
    show: true,
    resizable: true,
    pack: true,
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    label(text:"example label1", icon:imageIcon(file:"e:/icons/home.png"),
      iconTextGap:10, constraints: BorderLayout.NORTH
    )
    label(text:"example label2", icon:imageIcon(file:"e:/icons/plane.png"),
      iconTextGap:20, constraints: BorderLayout.CENTER
    )
    label(text:"example label3", icon:imageIcon(file:"e:/icons/mobile2.png"),
      iconTextGap:30, constraints: BorderLayout.SOUTH
    )
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

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月9日水曜日

SwingBuilderでテーブルを単一行選択モードに設定する

SwingBuilderでテーブルを単一行選択モードに設定するには、以下のコードのようにselectionModeにListSelectionModel.SINGLE_SELECTIONを指定します。

サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

data = [
  [category:'Apache', product:'HTTPD'],
  [category:'Apache', product:'Tomcat']
]

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example - single row selection",
    show: true,
    resizable: true,
    size: [300, 100],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    scrollPane(){
      table(selectionMode:ListSelectionModel.SINGLE_SELECTION){
        tableModel(list: data){
          propertyColumn( header:"category", propertyName: "category", editable: false )
          propertyColumn( header:"product", propertyName: "product", editable: false )
        }
      }
    }
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

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月7日月曜日

SwingBuilderでテーブルのデフォルトの選択色を設定する

SwingBuilderでテーブルのデフォルトの選択色を設定するには、以下のUIManagerのキーを設定します。
  • Table.selectionBackground : テーブル選択時の背景色
  • Table.selectionForeground : テーブル選択時の文字色

サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

// テーブルのデフォルトの選択色を設定
UIManager.put("Table.selectionBackground", new Color(0xff, 0xff, 0xdd))
UIManager.put("Table.selectionForeground", new Color(0x77, 0x99, 0xdd))

data = [
  [category:'Browser', product:'Firefox'],
  [category:'Browser', product:'IE']
]

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example - default colors for selected rows",
    show: true,
    resizable: true,
    size: [300, 100],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    scrollPane(){
      table(){
        tableModel(list: data){
          propertyColumn( header:"category", propertyName: "category", editable: false )
          propertyColumn( header:"product", propertyName: "product", editable: false )
        }
      }
    }
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

○テーブルに関するその他のエントリ

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