Groovy Scripting

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

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

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.