Javaone2008 Bof 5102 Groovybuilders


Published on

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

Published in: Technology, News & Politics
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Javaone2008 Bof 5102 Groovybuilders

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

    Clipping is a handy way to collect important slides you want to go back to later.