Groovy Scripting

Revision as of 5 September 2020 21:00 by RobM (Comments | Contribs)

Groovy documentation can be found here Note, however, the documentation does not include much on building a GUI using SwingBuilder

From https://github.com/apache/groovy-examples/blob/3bd1e1811c5306b0d8dbf65c9bde6091596a1307/src/main/groovy/swing/Widgets.groovy

Copy the code below and paste it into the System console, choose Groovy and then evaluate. It may take a few seconds for the GUI to appear. Closing the GUI window will close jAlbum too.

/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

import java.awt.Color
import javax.swing.SwingConstants
import javax.swing.WindowConstants
import groovy.swing.SwingBuilder

def swing = new SwingBuilder()
def unownedDialog
def ownedDialog

def showUnownedDialog(event) {
	unownedDialog.show();
}

def showOwnedDialog(event) {
	ownedDialog.show();
}

	unownedDialog = swing.dialog(
		title:'unrooted dialog',
		location: [200, 200],
		pack:true,
		defaultCloseOperation:WindowConstants.DISPOSE_ON_CLOSE
		) {
			label("I am unowned, but not unwanted");
		}

	def frame = swing.frame(
		title:'FrameTitle',
		location:[100,100],
		size:[800,400],
		defaultCloseOperation:WindowConstants.EXIT_ON_CLOSE) {

		menuBar {
			menu(text:'File') {
				menuItem() {
					action(name:'New', closure:{ println("clicked on the new menu item!") })
				}
				menuItem() {
					action(name:'Open', closure:{ println("clicked on the open menu item!") })
				}
				separator()
				menuItem() {
					action(name:'Save', enabled:false, closure:{ println("clicked on the Save menu item!") })
				}
			}
			menu(text:'Dialogs') {
				menuItem() {
					action(name:'Owned Dialog', closure: this.&showOwnedDialog)
				}
				menuItem() {
					action(name:'Unowned Dialog', closure: this.&showUnownedDialog)
				}
				def deeplyOwnedDialog = swing.dialog(
					title:'rooted dialog #2',
					location: [200, 200],
					pack:true,
					defaultCloseOperation:WindowConstants.DISPOSE_ON_CLOSE
					) {
					label("ownership is deep");
				}
				menuItem() {
					action(name:'Deeply Owned Dialog', closure: {deeplyOwnedDialog.show()} )
				}
			}
		}

		tabbedPane() {

			//colorChooser(
			//    name:"Color Chooser",
			//    color: 0xfeed42)

			panel(name:"Formatted Text Fields") {
				gridLayout(columns: 2, rows: 0)
				label("Simple Constructor:")
				formattedTextField()
				label("Date Value")
				formattedTextField(value: new java.util.Date())
				label("Integer Value")
				formattedTextField(value: new java.lang.Integer(42))
				label("Date Format")
				formattedTextField(format: java.text.DateFormat.getDateInstance())
				label("Currency Format ")
				formattedTextField(format: new java.text.DecimalFormat("¤###.00;(¤###.00)"))
			}

			panel(name:"Sliders") {
				flowLayout()
				slider(minimum:-100, 
					maximum:100, 
					majorTickSpacing: 50,
					orientation: SwingConstants.VERTICAL, 
					paintLabels:true)
				slider(minimum:-100, 
					maximum:100, 
					orientation: SwingConstants.VERTICAL, 
					paintLabels:true,
					paintTicks:true,
					majorTickSpacing: 50,
					minorTickSpacing: 10,
					snapToTicks:true,
					paintTrack:true)
			}

			panel(name:"Spinners") {
				gridBagLayout()
				label(
					text:"Tempuature in London:",
					insets:[12, 12, 2, 2],
					anchor: EAST,
					gridx: 0)
				spinner(
					model:spinnerNumberModel(minimum:-10, 
						maximum: 40, 
						value:20,
						stepSize:5),
					insets:[12, 3, 2, 12],
					anchor: WEST,
					gridx: 1,
					fill: HORIZONTAL)
				label(
					text:"Baseball Leagues:",
					insets:[3, 12, 2, 2],
					anchor: EAST,
					gridx: 0)
				spinner(
					model:spinnerListModel(
						list: ["Major League", "AAA", "AA", "A", "Rookie", "Semi-Pro", "Rec A", "Rec B"],
						value: "AA"),
					insets:[3, 3, 2, 12],
					anchor: WEST,
					gridx: 1,
					fill: HORIZONTAL)
				label(
					text:"Today's Date:",
					insets:[3, 12, 2, 2],
					anchor: EAST,
					gridx: 0)
				spinner(
					model:spinnerDateModel(calendarField: Calendar.HOUR_OF_DAY),
					insets:[3, 3, 2, 12],
					anchor: WEST,
					gridx: 1,
					fill: HORIZONTAL)
			}

			panel(name:"Border Layout") {
				borderLayout()
				label(text:"Border Layout", 
					  constraints:NORTH,
					  horizontalAlignment:SwingConstants.CENTER)
				label(text:"South", 
					  constraints:SOUTH,
					  background:Color.YELLOW,
					  opaque:true,
					  horizontalAlignment:SwingConstants.CENTER,
					  toolTipText:"Tooltip on south")
				label(text:"West", 
					  constraints:WEST,
					  background:Color.ORANGE,
					  opaque:true,
					  horizontalAlignment:SwingConstants.CENTER,
					  toolTipText:"Tooltip on west")
				label(text:"East", 
					  constraints:EAST,
					  background:Color.GREEN,
					  opaque:true,
					  horizontalAlignment:SwingConstants.CENTER,
					  toolTipText:"Tooltip on east")
				label(text:"Center", 
					  constraints:CENTER,
					  background:Color.WHITE,
					  opaque:true,
					  horizontalAlignment:SwingConstants.CENTER,
					  toolTipText:"<html>This is not the tooltip you are looking for.<br><i>*waves hand*</i>")
			}
		}

		ownedDialog = swing.dialog(
			title:'rooted dialog',
			location: [200, 200],
			pack:true,
			defaultCloseOperation:WindowConstants.DISPOSE_ON_CLOSE
			) {
			label("j00 h4v3 b33n 0wn3xed");
		}
	}        
	frame.show()

Below is some code examples, found on the internet, to help you get started. From sourceforge.net/p/freeplane

import groovy.swing.SwingBuilder
import java.awt.FlowLayout as FL
import javax.swing.BoxLayout as BXL
import javax.swing.JFrame

//def dial  diallocationRelativeTo:ui.frame and owner:ui.frame fails as ui. is not rcognised, need to ref jAlbum window, or just remove them?
def s = new SwingBuilder()
s.setVariable('myDialog-properties',[:])
def vars = s.variables
def dial = s.dialog(title:'Dialog 1', id:'myDialog', modal:true, locationRelativeTo:ui.frame, owner:ui.frame, defaultCloseOperation:JFrame.DISPOSE_ON_CLOSE, pack:true, show:true) {
    panel() {
        boxLayout(axis:BXL.Y_AXIS)
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            checkBox(id:'check', text:'Checkbox')    // append 'selected: true' to have the checkbox already selected
        }
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            label('Text')
            textArea(id:'textArea', columns:10, rows:2)
        }
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            label('Completion:')
            buttonGroup().with { group ->  
                radioButton(id: '0', text: '0%', selected: true, buttonGroup: group)  
                radioButton(id: '25', text: '25%', buttonGroup: group)  
                radioButton(id: '50', text: '50%', buttonGroup: group)  
                radioButton(id: '75', text: '75%', buttonGroup: group)  
                radioButton(id: '100', text: '100%', buttonGroup: group)  
            }  
        }
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            label('Combo')
            comboBox(id:'combo', items:['Option 1', 'Option 2'])
        }
        panel(alignmentX:0f) {
            flowLayout(alignment:FL.LEFT)
            button('Submit', preferredSize:[80, 24],
                   actionPerformed:{
                       vars.dialogResult = 'ok'
                       dispose()
            })
            button('Cancel', preferredSize:[80, 24],
                   actionPerformed:{
                       vars.dialogResult = 'cancel'
                       dispose()
            })
        }
    }
}

if (vars.dialogResult == 'ok') {
    def child = node.createChild(vars.textArea.text)
    def percent = ["0", "25", "50", "75", "100"].find{ vars[it].selected }
    child.icons.add(percent + "%")
    child["combo"] = vars.combo.selectedItem
    child["checkbox"] = (vars.check.selected ? 'checked' : 'not checked')
}

From josh-in-antarctica.blogspot.com A nicer way to do it using Groovy's with keyword: from

buttonGroup().with {
    add radioButton(text: 'Option 1')
    add radioButton(text: 'Option 2')
} 

Spinners take the form of

 spinner (id:'spinnerName', model:spinnerNumberModel(minimum:0, maximum: 40,  value:20,  stepSize:5))
 spinner(id:'spinnerName', model:spinnerListModel( list: ["Text one", "two", "three", "four", "five"], value: "three")
 spinner(id:'spinnerName', model:spinnerDateModel(calendarField: Calendar.HOUR_OF_DAY)

There is a Groovy discussion forum at nabble.com with question and answers which may have useful tips. For example: nabble.com groovy-examples