Javaone2008 Bof 5102 Groovybuilders

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    5 Favorites & 1 Event

    Javaone2008 Bof 5102 Groovybuilders - Presentation Transcript

    1. Cooking your own Groovy Builder: A step forward into Domain-Specific Languages Ixchel Ruiz, PSE CCA Andres Almiray, PSE Oracle BOF-5102
        • Learn to empower your code with the expressiveness of Groovy’s Builders
    2. Agenda
      • Builders and Groovy
      • Life without builders
        • Swing demo
        • Java2D demo
      • FactoryBuilderSupport
      • Resources
    3. 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
    4. Builders
      • Provide an abstraction layer
      • Increase Expressiveness
      • Better suited to express hierarchical data
      • Simplify Development
    5. Why builders for DSLs?
      • Domain-Specific Languages can be written with Groovy in many ways.
        • Metaprogramming
        • Fluent Interface, static imports, closures
        • Builders
    6. 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
    7. The Dark Side
      • Life without builders
    8. Swing
      • How many lines of code may this little program require in plain Java?
    9. 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 )
      • })
      • }
      • }
    10. Java2D This is a Java2D drawing that started as a rounded rectangle
    11. 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
    12. 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()
      • }
      • }
    13. 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
    14. 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
    15. 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
    16. 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
    17. 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
    18. 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
    19. 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
    20. 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' )
      • }
      • }
    21. Live code session
    22. 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
    23. Andres Almiray, PSE Oracle Ixchel Ruiz, PSE CCA BOF-5102

    + Andres AlmirayAndres Almiray, 2 years ago

    custom

    4438 views, 5 favs, 2 embeds more stats

    Cooking Your Own Groovy Builder: A Step Forward int more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 4438
      • 4433 on SlideShare
      • 5 from embeds
    • Comments 0
    • Favorites 5
    • Downloads 161
    Most viewed embeds
    • 3 views on http://localhost
    • 2 views on http://tjpablog.blogspot.com

    more

    All embeds
    • 3 views on http://localhost
    • 2 views on http://tjpablog.blogspot.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Groups / Events