Mais conteúdo relacionado Semelhante a Groovy's Builder (20) Mais de Yasuharu Nakano (9) Groovy's Builder4. Ad::
@GrabConfig(systemClassLoader=true) // for GroovyServ
@GrabResolver(name="kobo-repo", root="http://github.com/kobo/maven-repo/raw/master/release")
@Grab("org.jggug.kobo:gexcelapi:0.2")
import org.jggug.kobo.gexcelapi.GExcel
def book = GExcel.open(args[0])
def sheet = book[0]
println sheet.A1.value
sheet.A1.value = "New value of A1"
sheet.A_.each{ cell -> println cell.value }
sheet._1.each{ cell -> println cell.value }
sheet.A1_B6.each{ row -> row.each { cell -> println it.value } }
5. Sample of import
javafx.stage.*;
import
javafx.scene.*;
import
javafx.scene.shape.*;
import
javafx.scene.paint.*;
Stage
{
title:
"Sample
by
JavaFX
Script"
width:
600
height:
450
scene:
Scene
{
fill:
Color.LIGHTSKYBLUE
content:
Rectangle
{
x:
25,
y:
40
width:
100,
height:
50
fill:
Color.RED
}
}
}
9. import
groovy.swing.SwingBuilder
Sample of import
java.awt.BorderLayout
as
BL
int
count
=
0
new
SwingBuilder().edt
{
frame(
title:'Hello',
size:[600,
450],
show:true
)
{
borderLayout()
textLabel
=
label(
text:'Sample
of
SwingBuilder',
constraints:
BL.NORTH
)
button(
text:
'Click
Me!',
actionPerformed:
{
count++
textLabel.text
=
"Clicked
${count}
time(s)."
println
"clicked"
},
constraints:
BL.SOUTH
)
}
}
13. from Roadmap
http://www.flickr.com/photos/33678919@N07/5239012324/
15. import
griffon.builder.fx.FxBuilder
Sample of import
javafx.stage.*
import
javafx.scene.*
import
javafx.scene.shape.*
import
javafx.scene.paint.*
new
FxBuilder().edt
{
stage(
title:
"Sample
by
FxBuilder
of
Groovy",
width:
600,
height:
450,
scene:
scene(fill:
Color.$LIGHTSKYBLUE)
{
rectangle(
x:
25,
y:
40,
width:
100,
height:
50,
fill:
Color.$RED
)
}
)
}
http://griffon.codehaus.org/FxBuilder+Plugin
17. import
griffon.builder.fx.FxBuilder
import
javafx.stage.*
import
javafx.stage.*; import
javafx.scene.*
import
javafx.scene.*; import
javafx.scene.shape.*
import
javafx.scene.shape.*; import
javafx.scene.paint.*
import
javafx.scene.paint.*;
new
FxBuilder().edt
{
Stage
{
stage(
title:
"Sample
by
JavaFX
Script",
title:
"Sample
by
FxBuilder
of
Groovy",
width:
600
width:
600,
height:
450
height:
450,
scene:
Scene
{
fill:
Color.LIGHTSKYBLUE
content:
Rectangle
{
x:
25,
y:
40
≒
width:
100,
height:
50
fill:
Color.RED
scene:
scene(fill:Color.$LIGHTSKYBLUE){
rectangle(
x:
25,
y:
40,
width:
100,
height:
50,
fill:
Color.$RED
}
)
}
}
}
)
}
FxBuilder
18. JavaFX Script
Welcome to
http://www.flickr.com/photos/anirudhkoul/3734360895/
25. groovy.lang.Closure
closure
=
{
String
arg
-‐>
return
"hoge:${arg}"
} Java8
#
//
closure.call(“foo”)
//=>
“hoge:foo”
//
closure(“foo”)
//=>
“hoge:foo”
26. Args of
//
( )
{
-‐>
"hoge"
}.call()
//=>
"hoge"
//
( )
{
num
-‐>
"hoge:${num}"
}.call(123)
//=>
"hoge:123"
//
( )
{
num,
String
id,
option
-‐>
return
"hoge:${num}:${id}:${option}"
}.call(123,
“0A”,
“X”)
//=>
"hoge:123:0A:X"
//
it
{
"hoge:${it}"
}.call(123)
//=>
"hoge:123"
27. as Args
//
( )
someMethod({
num
-‐>
num
*
2
})
//
someMethod
{
num
-‐>
num
*
2
}
//
someMethod(arg1,
arg2,
{
num
-‐>
num
*
2
})
someMethod(arg1,
arg2)
{
num
-‐>
num
*
2
}
//
29. MOP::
class
MopSample
{
def
methodMissing(String
name,
args)
{
println
"Method:
$name($args)"
}
}
def
sample
=
new
MopSample()
sample.hoge()
//
=>
“Method:
hoge([])”
sample.foo(123,
456)
//
=>
“Method:
foo([123,
456])”
30. MOP::
class
MopSample
{
def
propertyMissing(String
name)
{
//
for
getter
println
"Property:
$name"
}
def
propertyMissing(String
name,
value)
{
//
for
setter
println
"Property:
$name
=
$value"
}
}
sample
=
new
MopSample2()
sample.hoge
//=>
"Property:
hoge"
sample.bar
//=>
"Property:
bar"
sample.baz
=
"!!"
//=>
"Property:
baz
=
!!"
32. ex.
def
writer
=
new
StringWriter()
def
builder
=
new
groovy.xml.MarkupBuilder(writer)
builder.books
{
book(published:"2010-‐12-‐20")
{
author
"nobeans"
title
"Groovy
←Describe here
"
}
3.times
{
book
"Groovy
${it+1} "
} <books>
}
<book
published='2010-‐12-‐20'>
println
writer.toString()
<author>nobeans</author>
<title>Groovy </title>
</book>
<book>Groovy
1 </book>
Format as Xml→
<book>Groovy
2 </book>
<book>Groovy
3 </book>
</books>
35. Classification of
Dynamically Statically
Defined Method Defined Method
extends extends
BuilderSupport FactoryBuilderSupport
Extends MarkupBuilder SwingBuilder
Abstract NodeBuilder FxBuilder
Class DOMBuilder ObjectGraphBuilder
AntBuilder JmxBuilder
SAXBuilder
ConfigSlurper
Independent CliBuilder
36. public
abstract
class
BuilderSupport
...
{
//
...snip...
protected
abstract
void
setParent(Object
parent,
Object
child);
protected
abstract
Object
createNode(Object
name);
protected
abstract
Object
createNode(Object
name,
Object
value);
protected
abstract
Object
createNode(Object
name,
Map
attributes);
protected
abstract
Object
createNode(Object
name,
Map
attributes,
Object
value);
//
...snip...
}
37. public
class
SwingBuilder
extends
FactoryBuilderSupport
{
//
...snip...
def
registerSupportNodes()
{
registerFactory("action",
new
ActionFactory())
registerFactory("actions",
new
CollectionFactory())
registerFactory("map",
new
MapFactory())
registerFactory("imageIcon",
new
ImageIconFactory())
registerFactory("buttonGroup",
new
ButtonGroupFactory())
addAttributeDelegate(ButtonGroupFactory.&buttonGroupAttributeDelegate)
//
...snip...
}
//
...snip...
}
40. def
writer
=
new
StringWriter()
new
DdlBuilder(writer).scheme
{
book
{ CREATE
TABLE
book
(
id
type:int
id
INTEGER,
title
type:String
title
TEXT,
published
type:Date
published
TIMESTAMP,
author
type:int
author
INTEGER
} )
author
{ CREATE
TABLE
author
(
id
type:int
id
INTEGER,
name
type:String
name
TEXT
} )
}
println
writer.toString()
41. class
DdlBuilder
{
def
writer
DdlBuilder(writer)
{
this.writer
=
writer
}
def
scheme(Closure
cls)
{
new
NodeBuilder().scheme(cls).each
{
table
-‐>
writer.println
"CREATE
TABLE
${table.name()}
("
writer.println
table.collect
{
col
-‐>
"
${col.name()}
${type(col.attribute('type'))}"
}.join(",n")
writer.println
")"
}
}
private
type(clazz)
{
switch
(clazz)
{
case
String:
return
"TEXT"
case
int:
return
"INTEGER"
case
Date:
return
"TIMESTAMP"
default:
throw
new
RuntimeException("unsupported
type:
$clazz")
}
}
}