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