Skip to content

Instantly share code, notes, and snippets.

@y-sumida
Created January 26, 2013 12:39
Show Gist options
  • Save y-sumida/4642093 to your computer and use it in GitHub Desktop.
Save y-sumida/4642093 to your computer and use it in GitHub Desktop.
yokohama.groovy #11の成果 簡易電卓
package calc
import groovy.util.Eval
import java.awt.event.ActionEvent
class CalcController {
def model
def view
def eval = new Eval()
def calculateExpression = {evt = null ->
doOutside {
def result = 0
try {
result = eval.me(model.expression)
} finally {
edt {
model.result += model.expression + "=" + result + "\n"
model.expression = ""
}
}
}
}
def clearExpression = {evt = null ->
doOutside {
model.expression = ""
}
}
def assembleExpression = { evt ->
String cmd = evt.getActionCommand();
doOutside {
edt {
model.expression += cmd
}
}
}
def deleteCharacter = {evt = null ->
doOutside {
edt {
model.expression -= model.expression[-1]
}
}
}
}
package calc
import groovy.beans.Bindable
class CalcModel {
@Bindable String expression = ""
@Bindable String result = ""
}
package calc
import static java.awt.BorderLayout.*
application(title: 'Calc',
preferredSize: [400, 400],
pack: true,
locationByPlatform:true,
iconImage: imageIcon('/griffon-icon-48x48.png').image,
iconImages: [imageIcon('/griffon-icon-48x48.png').image,
imageIcon('/griffon-icon-32x32.png').image,
imageIcon('/griffon-icon-16x16.png').image]) {
panel(constraints:SOUTH) {
vbox {
textField columns:15, text: bind(target:model, 'expression', mutual:true)
hbox {
button '7', actionPerformed: controller.&assembleExpression
button '8', actionPerformed: controller.&assembleExpression
button '9', actionPerformed: controller.&assembleExpression
button '+', actionPerformed: controller.&assembleExpression
}
hbox {
button '4', actionPerformed: controller.&assembleExpression
button '5', actionPerformed: controller.&assembleExpression
button '6', actionPerformed: controller.&assembleExpression
button '-', actionPerformed: controller.&assembleExpression
}
hbox {
button '1', actionPerformed: controller.&assembleExpression
button '2', actionPerformed: controller.&assembleExpression
button '3', actionPerformed: controller.&assembleExpression
button '*', actionPerformed: controller.&assembleExpression
}
hbox {
button '0', actionPerformed: controller.&assembleExpression
button '(', actionPerformed: controller.&assembleExpression
button ')', actionPerformed: controller.&assembleExpression
button '/', actionPerformed: controller.&assembleExpression
}
hbox {
button '.', actionPerformed: controller.&assembleExpression
button 'del', actionPerformed: controller.&deleteCharacter
button 'clr', actionPerformed: controller.&clearExpression
button '=', actionPerformed: controller.&calculateExpression
}
}
}
scrollPane(constraints:CENTER) {
textArea editable:false, columns:15, rows:10, text:bind{model.result}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment