Tài liệu Griffon in Action - Pdf 10

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: [email protected]
©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


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

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


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

“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


Initializing group members 147
Advanced techniques 148
6.3 Using and managing MVC groups 151
Accessing multiple MVC groups 151
Destroying MVC groups 153
6.4 Creating custom artifact templates 155
Templates, templates, templates 156

It’s alive! 158
6.5 Summary 159
7
Multithreaded applications 160
7.1 The bane of Swing development 161
Java Swing without threading 161

Java Swing
with threading 163
7.2 SwingBuilder alternatives 166
Groovy Swing without threading 166

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

the UI thread 189

Executing code outside of the UI thread 189
Is this the UI thread? 189

Executing code asynchronously 189
7.7 Summary 190
8
Listening to notifications 191
8.1 Working with build events 192
Creating a simple script 192

Handling an event with
the events script 193

Publishing build events 195
8.2 Working with application events 196
E is for events 196

Additional application event handlers 198
Firing application events 201
8.3 Your class as an event publisher 205
A basic Marco-Polo game 206

Running the application 209
8.4 Summary 210
9
Testing your application 211
9.1 Griffon testing basics 212
Creating tests 213

10.1 Understanding the common packaging options 243
10.2 Using Griffon’s standard packaging targets 244
The jar target 244

The zip target 246

The applet and
webstart targets 247

Customizing the manifest 247
Customizing the templates 248
10.3 Using the Installer plugin 250
Building a distribution 251

The izpack target 252
The rpm target 253

The deb target 254
The mac target 255

The jsmooth target 255
The windows target 255

Tweaking a distribution 255
10.4 Summary 257
11
Working with plugins 258
11.1 Working with plugins 259
Getting a list of available plugins 259


12.2 Builder delegates under the hood 285
Acting before the node is created 286

Tweaking the
node before properties are set 286

Handling node properties
your way 287

Cleaning up after the node is built 287
www.it-ebooks.info
CONTENTS
xiii
12.3 Quick tour of builder extensions in Griffon 288
SwingXBuilder 288

JideBuilder 291

CSSBuilder 293
GfxBuilder 296

Additional builders 300
12.4 Summary 301
13
Griffon in front, Grails in the back 302
13.1 Getting started with Grails 303
13.2 Building the Grails server application 304
Creating domain classes 304

Creating the controllers 305

Griffon and Maven 340
14.3 The Griffon wrapper 340
14.4 Summary 341
appendix Porting a legacy application 342
index 350
www.it-ebooks.info
www.it-ebooks.info
xv
foreword
As soon as I heard about Griffon in Action, I was eager to get it into my hands. What I
expected was a typical Manning In Action book: providing an easy jump start, working
from actionable examples, and providing lots of insight about the technology at hand.
It turned out that this book not only lived up to my expectations, it exceeded them in
many ways.
First, the authors’ knowledge is indisputable. This is obvious for the technology,
because we’re talking about main Griffon contributors. But beyond that comes experi-
ence about all aspects of developing desktop applications based on Swing, ranging
from how to set up your project, through proper separation of concerns, threading,
building, testing, visual composition, and code metrics, down to how to deliver the
final application to the customer.
Second, the book goes beyond giving simple recipes. It explains the underlying
constraints and considerations that enable readers to make informed decisions about
their projects.
Third, Griffon in Action is a great reference. I have it open whenever I write Griffon
applications so I can quickly look up an example or a list of available goodies. It is
such a thorough source of information that I consider it the definitive guide.
Writing such a book is a huge effort—especially when aiming for approachability
and completeness at the same time. Additionally, the authors pushed the Griffon proj-
ect forward while writing this book, and one or the other may even have an additional
day job.

moment: we agreed that creating a desktop framework that stuck as closely as possible
to Grails would be the way to go—although we didn’t have a name for it yet.
Danno went back to his batcave after the conference and in a matter of weeks
bootstrapped the framework by forking Grails and removing all the webby stuff that
was not needed. Then he grafted in the most important pieces of Griffon’s architec-
ture: the UberBuilder, the
MVC
group conventions, and the application life cycle.
We finally had something tangible. James picked the name and we went public
with the project on September 2008. The initial reaction from the community was so
www.it-ebooks.info
PREFACE
xviii
positive than in a matter of months work on the book began. And this is where both
projects got intertwined.
Together with Danno and Geertjan Wielenga, we wrote the first part of the book.
We went to work on the framework, and then we came back to the book when we
stopped to rethink where we were going with the framework. This kept going for
months: hacking some code, writing a few pages. In the meantime, we received plenty
of feedback about both projects. A particular advantage of this setup was that we were
able to address the needs of users and readers and thus save time, the most precious
resource for an open source effort.
Eventually Geertjan and Danno reduced their contributions, and my coauthor and
good friend Jim Shingler joined the project. Being an early adopter of the technology
plus a seasoned Swing developer meant he was the right person for the job. And he
didn’t disappoint. Thank you, Jim!
All this leads to where we are now, with you reading these pages. During the time
it took to get the book into your hands, we painstakingly revised its goals and the
framework, making sure both were kept as accurate and fresh as possible. Despite
what the naysayers have said for years—that Java on the desktop is no longer rele-

thank Dierk for contributing the foreword to our book.
Thanks to all those who have contributed to the Groovy, Griffon, and Grails projects,
especially Guillaume Laforge, Graeme Rocher, Jochen Theodoru, Alex Tkachman, Paul
www.it-ebooks.info
ACKNOWLEDGMENTS
xx
King, Hans Dockter, Peter Niederwiser, Luke Daley, Spring Source, and VMWare. We
would also like to thank other Groovy, Griffon, and Grails community contributors,
including James Williams for SwingXBuilder, Alexander Klein for bringing new ideas
to the framework, and René Gröschke and his build-bending Gradle powers. They
have created some great stuff and should be proud of themselves. Thanks to Sven
Haiges, Glen Smith, and Peter Ledbrook for their informative Grails podcast, where
Griffon was present on several occasions. Other special mentions go to Peter for the
countless exchanges we had regarding Grails and Griffon; Dick “I loooove the Groovy”
Wall, Tor Norbye, Carl Quinn, and Joe Nuxoll for the Java Posse podcast; and Michael
Kimsal for Groovy Mag.
ANDRES ALMIRAY
First and foremost, I would like to thank my wife, Ix-chel, for being my rock, anchor,
companion, and soul mate. You wouldn’t be holding this book in your hands without
her patience, understanding, and driving force. I’d like to thank my parents for bringing
me into this world and for all their love through the years. Patricia and Astrud: where
would I be without all your help? A very special and warm thank you to Christianne,
Joseph, and Didier Muelemans, dear mentors and beacons of hope. We had a group
of professors back in college who shaped our professional lives and led us to where we
are. Bruno Guardia, Enrique Espinoza, Carlos Guerra, Angel Kuri, and Barbaro Ferro,
I’m grateful for all your lessons and your words of encouragement.
Danno Ferrin is the man with the plan. He wrote the initial pieces that eventually
led us to bring forth the Griffon framework. You rock!
Geertjan Wielenga started the book with us; sadly, he had to let it go after a while.
Still, his contributions in the early stages are deeply engrained in the book. Thank

JIM SHINGLER
I would like to thank my wife, Wendy, and son, Tyler, for their support and patience
during the writing of the book and in our journey together through life. I would like
to thank all those who have contributed to my personal and professional growth over
the years: Wendy Shingler, Tyler Shingler, James L. Shingler Sr., Linda Shingler,
George Ramsayer, Chris Judd, Andres Almiray, Danno Ferrin, Tom Posival, Ken
Heintz, Bryce Kerlin, Rick Burchfield, David Lucas, Chris Nicholas, Tim Resch,
BJ
Allmon, Kevin Smith, Jeff Brown, Dave Klein, Paul King, Soren Berg Glasius, Michael
Kimsal, Joseph Nusairat, Brian Sam-Bodden. Steve Swing, Brian Campbell, Greg
Wilmer, Rick Fannin, Kunal Bajaj, Mukund Chandrasekar, Seth Flory, Frank
Neugebauer, David Duhl, Jason Gilmore, Teresa Whitt, Jay Johnson, Gerry Wright, and
the many other people who have touched my life. I’d also like to thank Jay Zimmerman,
Andrew Glover, Dave Thomas, Venkat Subramaniam, Scott Davis, Neal Ford, Ted
Neward, and the other great speakers and influencers on the “No Fluff Just Stuff” tour.
DANNO FERRIN
I would like to thank K.D., S.R., C.B, J.C., H.G., and H.F. for their support and patience.
www.it-ebooks.info
xxii
about this book
Griffon in Action is a comprehensive introduction to the Griffon framework that covers
the basic building blocks such as
MVC
groups, binding, threading, services, plugins,
and addons. But don’t let this quick summary fool you into thinking the topics are
covered lightly. The book provides deep dives into the topics at hand, following a
practical approach to get you started as quickly as possible.
Who should read this book
This book is for anyone interested in writing desktop applications for the Java virtual
machine (

In part 2 of the book, we go deep into the Griffon’s lair and explore the
MVC
com-
ponents found in every Griffon application. Our first stop is modeling data and estab-
lishing automatic updates via binding. We hope that by the end of chapter 3, you’ll
agree that binding makes life much easier that manually wiring up triggers; and event
listeners will be a task you cross off your list permanently.
Walking further into the den of the beast in chapter 4, we’ll discuss several tech-
niques for building a
UI
. Declarative programming is certainly within your reach, and
the fact that Griffon uses Groovy—a real programming language—makes things
much sweeter. You’ll find that the relationships between the different components
emerge naturally as you progress.
Closer to the nest, in chapter 5, are the components that form the logic of an
application: controllers and services. They’re responsible for routing events and data,
as well as responding to user events.
All the pieces will have fallen into place at this point, but you may have some unan-
swered questions regarding the relationships between components. Chapter 6 covers
in great detail how the platform manages its components and the facilities it puts at
your disposal to make the most out of them.
In part 3, we progress to more advanced topics. Building a responsive application
can be a daunting task, but in chapter 7 we’ll show you a few options that will help you
sort out multithreading obstacles with ease. Dealing with highly coupled components
is equally intimidating; but, fortunately, Griffon lets you react to well-timed events
depending on the application’s life cycle. You can even trigger your own events. And
did we mention that the event system is also useful for the command line? Events are
essential to building an application, and we’ll show you how to use them.
Chapter 8 offers complete coverage of notifications. Then, we’ll move to an often-
neglected aspect of desktop applications: proper testing, involving the

like
this
to separate it from the ordinary text. In addition, class and method names,
object properties, and other code-related terms and content in text are presented
using the same
fixed-width

font
.
Code and command-line input/output can be verbose. In some cases, the original
source code (available online) has been reformatted; we’ve added line breaks and
reworked indentation to accommodate the page space available in the book. In rare
cases, when even this was not enough, line-continuation markers were added to show
where longer lines had to be broken.
Code annotations accompany many of the listings, highlighting important con-
cepts. In some cases, numbered cueballs link to additional explanations that follow
the listing.
Source code downloads
You can access the source code for all examples in the book from the publisher’s website:
www.manning.com/GriffoninAction. All source code for the book is hosted at GitHub
(github.com), a commercial Git hosting firm. We’ll maintain the current
URL
via the
publisher’s website, also mirrored at https://github.com/aalmiray/griffoninaction. To
simplify finding your way, the source code is maintained by chapter.
Software requirements
All you need to get started is a working version of Oracle’s
JDK6
(available from http://
java.oracle.com) that matches your platform and operating system plus the latest sta-


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status