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