2014年5月31日土曜日

JGraphXで画像アイコンを表示する

JGraphXで画像アイコンを表示するにはmxConstantsの以下の定数を使用します。
  • STYLE_SHAPE:図形の指定
  • SHAPE_IMAGE:画像の図形タイプ
  • STYLE_IMAGE:画像のURI
サンプルコード
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 - icons",
  visible: true,
  size: [320, 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_VERTICAL_LABEL_POSITION): mxConstants.ALIGN_BOTTOM,
      (mxConstants.STYLE_SHAPE): mxConstants.SHAPE_IMAGE,
      (mxConstants.STYLE_IMAGE): "file:/e:/icons/basket-empty.png"
    ]
    stylesheet.putCellStyle("style1", style1)

    def style2 = [
      (mxConstants.STYLE_VERTICAL_LABEL_POSITION): mxConstants.ALIGN_BOTTOM,
      (mxConstants.STYLE_SHAPE): mxConstants.SHAPE_IMAGE,
      (mxConstants.STYLE_IMAGE): "file:/e:/icons/basket-full.png"
    ]
    stylesheet.putCellStyle("style2", style2)

    def style3 = [
      (mxConstants.STYLE_VERTICAL_LABEL_POSITION): mxConstants.ALIGN_BOTTOM,
      (mxConstants.STYLE_SHAPE): mxConstants.SHAPE_IMAGE,
      (mxConstants.STYLE_IMAGE): "file:/e:/icons/checkout.png"
    ]
    stylesheet.putCellStyle("style3", style3)

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

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

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

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

2014年5月30日金曜日

SwingBuilderでフローレイアウトを使用する

SwingBuilderでフローレイアウトを使用するには、以下のコードの様にflowLayoutを使用します。
  • alignment:アライメントの指定。(LEFT, RIGHT, CENTER, LEADING, TRAILING)
  • hgap:水平方向ギャップ
  • vgap:垂直方向ギャップ
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "flow layout example",
    show: true,
    resizable: true,
    size: [300, 100],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    flowLayout(alignment: FlowLayout.LEADING, hgap:10, vgap:2)
    button(text:"button1")
    button(text:"button2")
    button(text:"button3")
    button(text:"button4")
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年5月29日木曜日

JGraphXでフォントサイズを変更する

JGraphXでフォントサイズを変更するにはmxConstantsの以下の定数を使用します。
  • STYLE_FONTSIZE:フォントサイズ
サンプルコード
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 - font size",
  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_FONTSIZE): 20
    ]
    stylesheet.putCellStyle("style1", style1)

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

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

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

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

2014年5月28日水曜日

SwingBuilderでエッチングの枠を使用する

SwingBuilderでエッチングの枠を使用するには、以下のコードの様にetchedBorder/raisedEtchedBorderを使用します。
  • highlight:ハイライトの色
  • shadow:影の色
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "etched border example",
    show: true,
    resizable: true,
    size: [200, 100],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    label(text:"etched border example1",
      border: etchedBorder(),
      constraints:BorderLayout.NORTH
    )
    label(text:"etched border example2",
      border: raisedEtchedBorder(highlight: new Color(0xff, 0x00, 0x00), shadow: new Color(0x00, 0x00, 0xff)),
      constraints:BorderLayout.SOUTH
    )
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年5月27日火曜日

JGraphXで図形に影を付ける

JGraphXで図形に影を付けるにはmxConstantsの以下の定数を使用します。
  • STYLE_SHADOW:影の表示(true/false)
サンプルコード
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 - shadow",
  visible: true,
  size: [150, 200],
  resizable: true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){

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

    def style1 = [
      // 影の表示(true/false)
      (mxConstants.STYLE_SHADOW): true
    ]
    stylesheet.putCellStyle("style1", style1)

    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 100, 30, "style1")
    def v2 = graph.insertVertex(parent, null, "処理2",
      20, 90, 100, 30)

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

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

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

2014年5月26日月曜日

SwingBuilderでメニューを使用する

SwingBuilderでメニューを使用するには、以下のコードの様にmenuBar/menu/menuItemを使用します。separatorで区切り線を表示します。
サンプルコード
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "menu example",
    show: true,
    resizable: true,
    size: [300, 200],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    menuBar(){
      menu(text:"File", mnemonic: "F"){
        menuItem(text:"Open", mnemonic: "O", actionPerformed:{
          println "open"}
        )
        separator()
        menuItem(){
          action(name:"Save", mnemonic:"S",
            closure: {
              println "save"
            }
          )
        }
      }
    }
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年5月25日日曜日

JGraphXで図形をグラデーション表示する

JGraphXで図形をグラデーション表示するにはmxConstantsの以下の定数を使用します。
  • STYLE_FILLCOLOR:塗りつぶし色
  • STYLE_GRADIENTCOLOR:グラデーション色
  • STYLE_GRADIENT_DIRECTION:グラデーションの方向
  • DIRECTION_EAST:東向き
  • DIRECTION_WEST:西向き
  • DIRECTION_NORTH:北向き
  • DIRECTION_SOUTH:南向き
サンプルコード
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 - gradient",
  visible: true,
  size: [150, 200],
  resizable: true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){

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

    def style1 = [
      // 塗りつぶし色
      (mxConstants.STYLE_FILLCOLOR): "#7799dd",
      // グラデーション色
      (mxConstants.STYLE_GRADIENTCOLOR): "#ffffff",
      // グラデーションの方向
      //(DIRECTION_EAST, DIRECTION_WEST, DIRECTION_NORTH, DIRECTION_SOUTH)
      (mxConstants.STYLE_GRADIENT_DIRECTION): mxConstants.DIRECTION_EAST
    ]
    stylesheet.putCellStyle("style1", style1)

    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 100, 30, "style1")
    def v2 = graph.insertVertex(parent, null, "処理2",
      20, 90, 100, 30)

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

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

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

2014年5月24日土曜日

SwingBuilderでツリーを使用する

SwingBuilderでツリーを使用するには、以下のコードの様にtreeを使用します。DefaultMutableTreeNodeを拡張して使用することで、任意の情報をノード/リーフに追加することができます。 サンプルコード
import javax.swing.*
import javax.swing.tree.*
import groovy.swing.*

class MyNode extends DefaultMutableTreeNode
{
  MyNode(params)
  {
    super(params)
  }
  public String toString()
  {
    return userObject?.name
  }
}

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "tree example",
    show: true,
    resizable: true,
    size: [300, 200],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    scrollPane(){
      tree(id:"tree", rootVisible: false)
      tree.model.root.removeAllChildren()
      def node1 = new MyNode([name:"Node1"])
      node1.add(new MyNode([name:"Leaf1"]))
      node1.add(new MyNode([name:"Leaf2"]))
      tree.model.root.add(node1)
      tree.model.root.add(new MyNode([name:"Node2"]))
      tree.model.reload(tree.model.root)
      tree.expandRow(0)
    }
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年5月23日金曜日

JGraphXでグリッドを表示する

JGraphXでグリッドを表示するには以下のメソッドを使用します。
  • mxGraph.setGridSize:グリッドのサイズ
  • mxGraphComponent.setGridVisible:グリッドの表示指定
  • mxGraphComponent.setGridColor:グリッドの色
  • mxGraphComponent.setGridStyle:グリッドのスタイル設定
    • mxGraphComponent.GRID_STYLE_DOT:点のグリッドスタイル
    • mxGraphComponent.GRID_STYLE_CROSS:十字のグリッドスタイル
    • mxGraphComponent.GRID_STYLE_LINE:線のグリッドスタイル
    • mxGraphComponent.GRID_STYLE_DASHED:点線のグリッドスタイル
サンプルコード
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()
graph.setGridSize(10)
def mxgc = new mxGraphComponent(graph)
mxgc.setGridVisible(true)
mxgc.setGridColor(new Color(0x77, 0x99, 0xdd))
//mxgc.setGridStyle(mxgc.GRID_STYLE_DOT)
//mxgc.setGridStyle(mxgc.GRID_STYLE_CROSS)
//mxgc.setGridStyle(mxgc.GRID_STYLE_LINE)
mxgc.setGridStyle(mxgc.GRID_STYLE_DASHED)

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - grid",
  visible: true,
  size: [150, 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, 100, 30)
    def v2 = graph.insertVertex(parent, null, "処理2",
      20, 90, 100, 30)

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

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

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

2014年5月22日木曜日

SwingBuilderでスライダーを使用する

SwingBuilderでスライダーを使用するには、以下のコードの様にsliderを使用します。
  • value:スライダーの値
  • minimum:スライダーの最小値
  • maximum:スライダーの最大値
  • majorTickSpacing:スライダーのメジャー目盛幅
  • minorTickSpacing:スライダーのマイナー目盛幅
  • paintTicks:目盛表示
  • paintLabels:ラベル表示
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "Slider example",
    show: true,
    resizable: true,
    pack: true,
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    slider(id:"slider1", value:50, minimum:0, maximum:100,
      majorTickSpacing:10, minorTickSpacing:5, paintTicks:true, paintLabels:true,
      constraints: BorderLayout.NORTH
    )
    slider(id:"slider2", value:50, minimum:0, maximum:100,
      majorTickSpacing:10, minorTickSpacing:5, paintTicks:true, paintLabels:true,
      orientation: JSlider.VERTICAL,
      constraints: BorderLayout.CENTER
    )
    button(text:"OK", constraints: BorderLayout.SOUTH,
      actionPerformed: {
        sb.optionPane(
          message:"slider1=${sb.slider1.value}, slider2=${sb.slider2.value}",
          messageType:JOptionPane.INFORMATION_MESSAGE)
          .createDialog("slider").visible = true
      }
    )
  }
}

実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年5月21日水曜日

JGraphXで線のスタイルを指定する

JGraphXで線のスタイルを指定するにはmxConstantsの以下の定数を使用します。
  • STYLE_EDGE:線のスタイル指定
  • EDGESTYLE_SIDETOSIDE:左右で接続
  • EDGESTYLE_TOPTOBOTTOM:上下で接続
  • EDGESTYLE_ENTITY_RELATION:リレーションで接続
サンプルコード
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()

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - line styles",
  visible: true,
  size: [400, 400],
  resizable: true,
  contentPane: new mxGraphComponent(graph),
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){

  parent = graph.getDefaultParent()
  graph.model.beginUpdate()
  try
  {
    def stylesheet = graph.getStylesheet()
    // カスタムスタイル
    def style1 = [
      (mxConstants.STYLE_EDGE):mxConstants.EDGESTYLE_SIDETOSIDE
    ]
    stylesheet.putCellStyle("style1", style1)

    def v1a = graph.insertVertex(parent, null, "処理1A",
      20, 90, 100, 30)
    def v1b = graph.insertVertex(parent, null, "処理1B",
      220, 20, 100, 30)

    graph.insertEdge(parent, null, "正常終了", v1a, v1b, "style1")

    def style2 = [
      (mxConstants.STYLE_EDGE):mxConstants.EDGESTYLE_TOPTOBOTTOM
    ]
    stylesheet.putCellStyle("style2", style2)

    def v2a = graph.insertVertex(parent, null, "処理2A",
      20, 200, 100, 30)
    def v2b = graph.insertVertex(parent, null, "処理2B",
      220, 130, 100, 30)

    graph.insertEdge(parent, null, "正常終了", v2a, v2b, "style2")

    def style3 = [
      (mxConstants.STYLE_EDGE):mxConstants.EDGESTYLE_ENTITY_RELATION
    ]
    stylesheet.putCellStyle("style3", style3)

    def v3a = graph.insertVertex(parent, null, "処理3A",
      20, 310, 100, 30)
    def v3b = graph.insertVertex(parent, null, "処理3B",
      220, 240, 100, 30)

    graph.insertEdge(parent, null, "正常終了", v3a, v3b, "style3")


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

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

2014年5月20日火曜日

SwingBuilderでテーブルを編集不可にする

SwingBuilderでテーブルを編集不可にするには、以下のコードの様にカラム定義時にeditable:falseを指定します。 サンプルコード
import javax.swing.*
import groovy.swing.*

data = [
  [country:'Japan', prefecture:'Tokyo'],
  [country:'Japan', prefecture:'Osaka']
]

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "Read-only table example",
    show: true,
    resizable: true,
    size: [200, 200],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    scrollPane(){
      table(){
        tableModel(list: data){
          propertyColumn( header:"country", propertyName: "country", editable: false )
          propertyColumn( header:"prefecture", propertyName: "prefecture", editable: false )
        }
      }
    }
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

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