Your SlideShare is downloading. ×
0
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Javaone2008 Bof 5102 Groovybuilders
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Javaone2008 Bof 5102 Groovybuilders

4,974

Published on

Cooking Your Own Groovy Builder: A Step Forward into Domain-Specific Languages

Cooking Your Own Groovy Builder: A Step Forward into Domain-Specific Languages

Published in: Technology, News & Politics
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,974
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
176
Comments
0
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Cooking your own Groovy Builder: A step forward into Domain-Specific Languages Ixchel Ruiz, PSE CCA Andres Almiray, PSE Oracle BOF-5102
  • 2.
      • Learn to empower your code with the expressiveness of Groovy’s Builders
  • 3. Agenda
    • Builders and Groovy
    • Life without builders
      • Swing demo
      • Java2D demo
    • FactoryBuilderSupport
    • Resources
  • 4. Why Groovy?
    • It is so Cool !
    • No impedance mismatch with Java
    • Groovy is Java
    • Flat learning curve
    • If you are a Java programmer, congratulations! you are also a Groovy programmer
  • 5. Builders
    • Provide an abstraction layer
    • Increase Expressiveness
    • Better suited to express hierarchical data
    • Simplify Development
  • 6. Why builders for DSLs?
    • Domain-Specific Languages can be written with Groovy in many ways.
      • Metaprogramming
      • Fluent Interface, static imports, closures
      • Builders
  • 7. Builders for DSLs
    • Builders are a great choice to begin with
      • Pretty straight forward structure
      • No heavy metaprogramming required
      • Groovy provides 2 base classes to get you started
        • BuilderSupport
        • FactoryBuilderSupport
  • 8. The Dark Side
    • Life without builders
  • 9. Swing
    • How many lines of code may this little program require in plain Java?
  • 10. Groovy’s answer
    • import groovy.swing.SwingBuilder
    • import java.awt.BorderLayout as BL
    • import javax.swing.JOptionPane
    • SwingBuilder.build {
    • frame( id: 'frame', title: 'Hello World', show: true, pack: true ){
    • borderLayout()
    • label( 'Enter a message:', constraints: BL.WEST )
    • textField( id: 'name', columns: 20, constraints: BL.CENTER )
    • button( 'Click', constraints: BL.EAST, actionPerformed: { e ->
    • if( !name.text ) return
    • JOptionPane.showMessageDialog( frame, name.text )
    • })
    • }
    • }
  • 11. Java2D This is a Java2D drawing that started as a rounded rectangle
  • 12. How many lines of code would it take in plain Java?
    • Short answer: didn’t even bother to measure them
    • The complete example has a little animation, applying each filter at a time, wonder how much code would be needed
  • 13. Groovy’s answer
    • antialias on
    • rect( x: 20, y: 20, width: 180, height: 160,
    • arcWidth: 50, arcHeight: 50, borderColor: no, id: 'base' ){
    • linearGradient {
    • stop( offset: 0.5, color: 'white' )
    • stop( offset: 1, color: 'blue' )
    • }
    • filters( offset: 50 ){
    • weave()
    • kaleidoscope( sides: 4 )
    • shapeBurst( merge: true, type: 'circle up' )
    • lights(){
    • ambientLight( color: color('lime').rgb(),
    • centreX: 0, centreY: 0 )
    • }
    • dropShadow()
    • }
    • }
  • 14. What is going on?
    • As with other builders, each node is actually a “pretended” method
    • Pretended methods are routed through the build cycle, any other methods are handled as usual
    • SwingBuilder node names are easy to remember, most of the time just remove the leading ‘J’ from a Swing class and that’s it
    • Both SwingBuilder and GraphicsBuilder extend from FactoryBuilderSupport
  • 15. FactoryBuilderSupport at a glance
    • Factories have the responsibility to handle pretended methods
    • Each factory hooks into the build cycle
    • The builder will notify the current building factory whenever an action on the node should be taken
    • Each factory has it own context for storing useful information while building nodes
  • 16. What factories may control
    • Node creation
    • Setting attributes on the node
    • If the node accepts children
    • Adding the node to its parent node
    • Processing the closure by themselves or let the builder handle it
    • Node completion
  • 17. Available context properties (1)
    • context : retrieves the current context
    • current : reference to the current node
    • currentName : the “pretended” method name
    • currentFactory : reference to the factory that built the current node
  • 18. Available context properties (2)
    • Similar properties if a parent node is available
    • parentContext : retrieves the parent’s context
    • parentNode : reference to the parent node
    • parentName : the parent’s node name
    • parentFactory : reference to the parent’s factory
  • 19. Extension Points
    • FactoryBuilderSupport exposes the following extension points via closure delegates
      • Pre node creation
      • Post node creation
      • Attribute handling
      • Node completion
      • End of build cycle
  • 20. Adding css to Swing, how?
    • We can’t extend every swing component available, that’s overkill
    • We can’t also extend every factory for the same reason
    • But we can use an attribute delegate that handles css attributes
  • 21. This is one way to do it
    • import groovy.swing.*
    • import com.u2d.css4swing.CSSEngine
    • import com.u2d.css4swing.style.ComponentStyle
    • CSSEngine.initialize()
    • SwingBuilder.build {
    • addAttributeDelegate { builder, node, attributes ->
    • def cssClass = attributes.remove("cssClass")
    • if( cssClass ) ComponentStyle.addClass( node, cssClass )
    • }
    • frame( id: 'fame', size: [300,100], locationRelativeTo: null,
    • title: 'CSS+ Swing + Groovy', show: true ){
    • gridLayout( cols: 1, rows: 2 )
    • label( 'This is not important' )
    • label( 'This is really important', cssClass: 'important' )
    • }
    • }
  • 22. Live code session
  • 23. For More Information
    • http:// groovy.codehaus.org
    • The builders
      • http:// groovy.codehaus.org/FactoryBuilderSupport
      • http:// groovy.codehaus.org/ObjectGraphBuilder
      • http:// groovy.codehaus.org/Swing+Builder
      • http:// groovy.codehaus.org/SwingXBuilder
      • http:// groovy.codehaus.org/JideBuilder
      • http://groovy.codehaus.org/GraphicsBuilder
      • http://groovy.codehaus.org/Using+Ant+from+Groovy
      • http://groovy.codehaus.org/Creating+XML+using+Groovy's+MarkupBuilder
    • http:// jroller.com/aalmiray
  • 24. Andres Almiray, PSE Oracle Ixchel Ruiz, PSE CCA BOF-5102

×