MANNING Andres Almiray Danno Ferrin James Shingler FOREWORD BY Dierk König www.it-ebooks.info Griffon in Action www.it-ebooks.info www.it-ebooks.info Griffon in Action ANDRES ALMIRAY DANNO FERRIN JAMES SHINGLER MANNING S HELTER I SLAND www.it-ebooks.info For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: orders@manning.com ©2012 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Cynthia Kane 20 Baldwin Road Technical proofreader: Al Scherer PO Box 261 Copyeditors: Tiffany Taylor, Andy Carroll Shelter Island, NY 11964 Proofreader: Melody Dolab Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781935182238 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 18 17 16 15 14 13 12 www.it-ebooks.info v brief contents P ART 1 G ETTING STARTED 1 1 ■ Welcome to the Griffon revolution 3 2 ■ A closer look at Griffon 36 P ART 2 E SSENTIAL G RIFFON 57 3 ■ Models and binding 59 4 ■ Creating a view 92 5 ■ Understanding controllers and services 117 6 ■ Understanding MVC groups 138 7 ■ Multithreaded applications 160 8 ■ Listening to notifications 191 9 ■ Testing your application 211 10 ■ Ship it! 242 11 ■ Working with plugins 258 12 ■ Enhanced looks 277 13 ■ Griffon in front, Grails in the back 302 14 ■ Productivity tools 322 www.it-ebooks.info www.it-ebooks.info vii contents foreword xv preface xvii acknowledgments xix about this book xxii about the cover illustration xxvi P ART 1 G ETTING STARTED 1 1 Welcome to the Griffon revolution 3 1.1 Introducing Griffon 4 Setting up your development environment 5 ■ Your first Griffon application 7 1.2 Building the GroovyEdit text editor in minutes 9 Giving GroovyEdit a view 9 ■ Making the menu items behave: the controller 14 ■ How about a tab per file? 16 Making GroovyEdit functional: the FilePanel model 18 Configuring the FilePanel controller 19 1.3 Java desktop development: welcome to the jungle 22 Lots of boilerplate code (ceremony vs. essence) 23 ■ UI definition complexity 24 ■ Lack of application life cycle management 26 No built-in build management 27 www.it-ebooks.info CONTENTS viii 1.4 The Griffon approach 27 At the core: the MVC pattern 28 ■ The convention-over- configuration paradigm 31 ■ Groovy: a modern JVM language 33 1.5 Summary 35 2 A closer look at Griffon 36 2.1 A tour of the common application structure 37 2.2 The ABCs of configuration 39 A is for Application 40 ■ B is for Builder 41 C is for Config 43 2.3 Using Griffon’s command line 47 Build command targets 49 ■ Run command targets 50 Miscellaneous command targets 50 2.4 Application life cycle overview 51 Initialize 52 ■ Startup 53 ■ Ready 53 Shutdown 54 ■ Stop 55 2.5 Summary 56 P ART 2 E SSENTIAL G RIFFON 57 3 Models and binding 59 3.1 A quick look at models and bindings 60 Creating the project 60 ■ Creating the model 61 Creating the view 62 ■ Creating the controller 63 3.2 Models as communication hubs 64 MVC in the age of web frameworks 65 Rethinking the pattern 66 3.3 Observable beans 66 JavaBeans bound properties: the Java way 67 ■ JavaBeans bound properties: the Groovy way 69 ■ Handy bound classes 72 3.4 Have your people call my people: binding 74 A basic binding call 75 ■ The several flavors of binding 76 Finding the essence 77 ■ Other binding options 80 3.5 The secret life of BindingUpdatable 83 Keeping track of bindings with the BindingUpdatable object 83 Managing the bindstorm: bind(), unbind(), and rebind() 84 www.it-ebooks.info CONTENTS ix Manually triggering a binding: update() and reverseUpdate() 85 Grouping bindings together 85 3.6 Putting it all together 86 Setting up the model 87 ■ Defining a view 87 Adding the missing validations to the model 89 3.7 Summary 91 4 Creating a view 92 4.1 Java Swing for the impatient 93 “Hello World” the Swing way 94 ■ Extending “Hello World”: “Hello Back” 95 ■ Swing observations 96 4.2 Groovy SwingBuilder: streamlined Swing 97 “Hello World” the SwingBuilder way 98 “Hello Back” with SwingBuilder 99 4.3 Anatomy of a Griffon view 100 Builders are key to views 101 Nodes as building blocks 102 4.4 Using special nodes 104 Container 104 ■ Widget 104 ■ Bean 105 Noparent 105 ■ Application 106 4.5 Managing large views 106 Rounding up reusable code 107 ■ Breaking a large view into scripts 107 ■ Organize by script type 109 4.6 Using screen designers and visual editors 110 Integrating with the NetBeans GUI builder (formerly Matisse) 110 ■ Integrating with Abeille Forms Designer 114 4.7 Summary 116 5 Understanding controllers and services 117 5.1 Dissecting a controller 118 Quick tour of injected properties and methods 118 Using the post-initialization hook 121 Understanding controller actions 122 5.2 The need for services 124 Creating a simple service 125 ■ Creating a Spring-based service 126 www.it-ebooks.info [...]... Getting plugin-specific information 260 Installing a plugin 261 Uninstalling a plugin 262 ■ ■ 11.2 Understanding plugin types Build-time plugins 11.3 263 ■ 262 Runtime plugins 265 Creating the Tracer plugin and addon 267 Bootstrapping the plugin/addon 268 Intercepting property updates 269 Using the plugin 270 Intercepting action calls 272 Running the plugin again 273 ■ ■ ■ ■ 11.4 11.5 12 Releasing... pointing to your Griffon installation folder Finally, add GRIFFON_ HOME/bin (or %GRIFFON_ HOME%\bin on Windows) to your path: www.it-ebooks.info 6 CHAPTER 1 Welcome to the Griffon revolution ■ OS X and Linux—This is normally done by editing your shell configuration file (such as ~/.profile) by adding the following lines: export GRIFFON_ HOME=/opt /griffon export PATH=$PATH: $GRIFFON_ HOME/bin ■ Windows—Go to... treasures and riches; in our case, Griffon is the key to a productive experience when writing desktop applications Let’s begin our journey by looking the Griffon directly in the eye www.it-ebooks.info www.it-ebooks.info Welcome to the Griffon revolution This chapter covers ■ What Griffon is all about ■ Installing Griffon ■ Building your first Griffon application ■ Understanding how Griffon simplifies... SwingXBuilder and threading support 177 Installing SwingXBuilder 177 The withWorker() node 178 www.it-ebooks.info CONTENTS 7.5 xi Putting it all together 179 Defining the application’s outline 180 Setting up the UI elements 181 Defining a tab per loading technique Adding the loading techniques 184 FileViewer: the aftermath 187 ■ ■ 182 ■ 7.6 Additional threading options 188 Synchronous calls in the UI thread... standard Griffon distribution contains all the files and tools you need to get going, including libraries, executables, and documentation If you’re working on a Windows platform, avoid installing Griffon in the special Program Files directory, because the operating system may impose special restrictions that hinder Griffon s setup CAUTION Next, set an environment variable called GRIFFON_ HOME, pointing to... Building the Griffon frontend Setting up the view 312 13.5 305 ■ ■ Pointing to resources 311 Updating the model Querying the Grails backend 314 315 Creating a service 315 Injecting an instance of the service Configuring the Bookstore application 318 ■ 13.6 13.7 14 Alternative networking options Summary 321 320 Productivity tools 322 14.1 Getting set up in popular IDEs 323 Griffon and Eclipse 323 Griffon. .. Groovy Swing with threading 167 Synchronous calls with edt 170 Asynchronous calls with doLater 171 Outside calls with doOutside 171 ■ ■ ■ 7.3 Multithreaded applications with Griffon 172 Threading and the application life cycle 172 Threading support the Griffon way 173 Controller actions and multithreading: a quick guide 173 Fine-tuning threading injection 175 What about binding? 176 ■ ■ 7.4 SwingXBuilder... and NetBeans IDE 327 Griffon and IDEA 331 Griffon and TextMate 334 ■ ■ 14.2 Command-line tools 336 Griffon and Ant 336 Griffon and Gradle 338 Griffon and Maven 340 ■ 14.3 14.4 appendix The Griffon wrapper Summary 341 340 Porting a legacy application index 342 350 www.it-ebooks.info 317 www.it-ebooks.info foreword As soon as I heard about Griffon in Action, I was eager to get it into my hands What I... language comes naturally Roadmap Griffon in Action gives a quick, accessible, no-fluff introduction to writing desktop applications in the Java universe The book is divided into four parts: ■ ■ Part 1 Getting started Part 2 Essential Griffon xxii www.it-ebooks.info ABOUT THIS BOOK ■ ■ xxiii Part 3 Advanced Griffon Part 4 Extending Griffon s reach We cover what Griffon is in chapter 1: where did it come... entry point for other commands, such as the help command you just used It’s a good idea to familiarize yourself with the additional commands because they’re useful when you’re developing Griffon applications Using Griffon s command line will be explored further in chapter 2 Figure 1.1 Updating variable settings on Windows www.it-ebooks.info Introducing Griffon 7 Now you’re ready to start building your . people: binding 74 A basic binding call 75 ■ The several flavors of binding 76 Finding the essence 77 ■ Other binding options 80 3.5 The secret life of BindingUpdatable. available plugins 259 ■ Getting plugin-specific information 260 ■ Installing a plugin 261 Uninstalling a plugin 262 11.2 Understanding plugin types 262 Build-time