Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Eclipse in action
Nội dung xem thử
Mô tả chi tiết
Eclipse in Action
Eclipse in Action
A GUIDE FOR JAVA DEVELOPERS
DAVID GALLARDO
ED BURNETTE
ROBERT MCGOVERN
With contributions to appendixes by
STEVEN HAINES
MANNING
Greenwich
(74° w. long.)
For electronic information and ordering of this and other Manning books,
go to 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.
209 Bruce Park Avenue Fax: (203) 661-9018
Greenwich, CT 06830 email: [email protected]
©2003 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 they publish printed on acid-free paper, and we exert our best efforts to that end.
Manning Publications Co. Copyeditor: Tiffany Taylor
209 Bruce Park Avenue Typesetter: Denis Dalinnik
Greenwich, CT 06830 Cover designer: Leslie Haimes
ISBN 1-930110-96-0
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – VHG – 08 07 06 05 04 03
To my wife Eni and my son Alejandro
D.J.G.
To Lisa, Michael, and Christopher
E.B.B.
To my wife Roberta for putting up with this insane trip
R.M.
vii
PART 1USING ECLIPSE............................................................. 1
1 ■ Overview 3
2 ■ Getting started with the Eclipse Workbench 13
3 ■ The Java development cycle: test, code, repeat 39
4 ■ Working with source code in Eclipse 79
5 ■ Building with Ant 103
6 ■ Source control with CVS 143
7 ■ Web development tools 177
PART 2EXTENDING ECLIPSE ................................................ 217
8 ■ Introduction to Eclipse plug-ins 219
9 ■ Working with plug-ins in Eclipse 249
brief contents
ix
foreword xvii
preface xxi
acknowledgments xxiii
about this book xxv
about the title xxix
about the cover illustration xxx
PART 1USING ECLIPSE........................................................... 1
1 Overview 3
1.1 Where Eclipse came from 4
A bit of background 5 ■ The Eclipse organization 5
Open source software 6
1.2 What is Eclipse? 7
The Eclipse architecture 8 ■ Language and platform
neutrality 10
1.3 What’s next 11
1.4 Summary 11
contents
x CONTENTS
2 Getting started with the Eclipse Workbench 13
2.1 Obtaining Eclipse 14
2.2 Eclipse overview 15
Projects and folders 15 ■ The Eclipse Workbench 16
2.3 The Java quick tour 20
Creating a Java project 20 ■ Creating a Java class 22
Running the Java program 25 ■ Debugging the Java
program 27 ■ Java scrapbook pages 30
2.4 Preferences and other settings 31
Javadoc comments 32 ■ Format style 33 ■ Code generation
templates 33 ■ Classpaths and classpath variables 35
Exporting and importing preferences 36
2.5 Summary 37
3 The Java development cycle: test, code, repeat 39
3.1 Java development tools methodology 40
Testing is job 1 41 ■ A sample application and working sets 41
3.2 The JUnit unit testing framework 43
Method stubs and unit tests 44 ■ Creating test cases 49
How much testing is enough? 54 ■ Implementing the public
methods 58
3.3 Further adventures in debugging 62
Setting breakpoint properties 64
Finding and fixing a bug 66
3.4 Logging with log4j 68
Loggers, appenders, and pattern layouts 69 ■ Configuring
log4j 73 ■ Using log4j with Eclipse 75
3.5 Summary 77
4 Working with source code in Eclipse 79
4.1 Importing an external project 80
4.2 Extending the persistence component 83
Creating a factory method 84 ■ Creating the unit test class 84
Working with the astronomy classes 85 ■ The Star test case 88
Creating a test suite 89 ■ Implementing the ObjectManager
class 90
CONTENTS xi
4.3 Refactoring 95
Renaming a class 96 ■ Extracting an interface 99
Future refactoring 101
4.4 Summary 102
5 Building with Ant 103
5.1 The need for an official build process 104
Creating the build directory structure 105
5.2 Make: A retrospective 109
5.3 The new Java standard: Ant 112
A very brief introduction to XML 113 ■ A simple Ant
example 115 ■ Projects 118 ■ Targets 119 ■ Tasks 119
Properties 126 ■ File sets and path structures 128
Additional Ant capabilities 131
5.4 A sample Ant build 131
Creating the build file, build.xml 132 ■ Performing a
build 136 ■ Debugging the build 138
5.5 Summary 140
6 Source control with CVS 143
6.1 The need for source control 144
6.2 Using CVS with Eclipse 146
Sharing a project with CVS 146 ■ Working with CVS 153
Versions and branches 170
6.3 Summary 174
7 Web development tools 177
7.1 Developing for the Web 178
The web, HTML, servlets, and JSP 178 ■ JSP overview 179
Servlet overview 181
7.2 Tomcat and the Sysdeo Tomcat plug-in 181
Installing and testing Tomcat 182 ■ Installing and setting up
the Sysdeo Tomcat plug-in 183 ■ Creating and testing a JSP
using Eclipse 185 ■ Creating and testing a servlet in
Eclipse 187 ■ Placing a Tomcat project under CVS
control 190
xii CONTENTS
7.3 Building a web application 191
The web application directory structure 191 ■ Web application
design and testing 192 ■ Programming with servlets and
JSPs 197
7.4 Wrapping up the sample application 210
7.5 Summary 215
PART 2EXTENDING ECLIPSE .............................................. 217
8 Introduction to Eclipse plug-ins 219
8.1 Plug-ins and extension points 220
Anatomy of a plug-in 220 ■ The plug-in lifecycle 221
Creating a simple plug-in by hand 222
8.2 The Plug-in Development Environment (PDE) 223
Preparing your Workbench 224 ■ Importing the SDK
plug-ins 224 ■ Using the Plug-in Project Wizard 226
8.3 The “Hello, World” plug-in example 228
The Plug-in Manifest Editor 230 ■ The Run-time
Workbench 231 ■ Plug-in class (AbstractUIPlugin) 233
Actions, menus, and toolbars
(IWorkbenchWindowActionDelegate) 237 ■ Plug-ins and
classpaths 241
8.4 The log4j library plug-in example 242
Attaching source 244 ■ Including the source zip in the plug-in
package 244
8.5 Deploying a plug-in 246
8.6 Summary 247
9 Working with plug-ins in Eclipse 249
9.1 The log4j integration plug-in example 250
Project overview 252 ■ Preparing the project 253
9.2 Editors (TextEditor) 254
Preparing the editor class 255 ■ Defining the editor
extension 255 ■ Adding an icon 259 ■ Adding color 261
Token manager 268 ■ Content assist
(IContentAssistProcessor) 271 ■ Putting it all together 275
CONTENTS xiii
9.3 Views (ViewPart) 279
Adding the view 280 ■ Modifying perspective defaults 281
View class 282 ■ Table framework 289 ■ Label providers
(LabelProvider) 296 ■ Models 298 ■ Receiver thread 300
9.4 Preferences (FieldEditorPreferencePage) 301
Main preference page 302 ■ Editor preference page 303
9.5 Plugin class 304
9.6 Summary 305
A Java perspective menu reference 307
B CVS installation procedures 323
B.1 Installing CVS on UNIX and Linux 324
Creating the CVS repository 325 ■ Setting up SSH remote
access 326 ■ Setting up pserver remote access 327
B.2 Installing CVS on Mac OS X 328
B.3 Installing CVSNT on Windows 329
B.4 Installing Cygwin CVS and SSH on Windows 330
B.5 Troubleshooting the CVS installation 332
B.6 Backing up the CVS repository 332
C Plug-in extension points 333
D Introduction to SWT 343
D.1 What is the Standard Widget Toolkit? 344
D.2 SWT architecture 345
Widget creation 346 ■ Resource disposal 346
D.3 SWT and events 347
D.4 SWT and threads 348
D.5 Building and running SWT programs 350
D.6 Using SWT 353
The BasicFramework class 353 ■ The MainApp class 356
Trying the example 359
xiv CONTENTS
E Introduction to JFace 361
E.1 Architecture 362
E.2 Building a JFace application 363
JFaceExample class 364 ■ ExitAction class 366
index 369
xv
foreword
Imagine my surprise when the editors asked me to write this foreword. I’m not
a guru, just a programmer who has used Eclipse every day for the last couple
of years. The biggest difference between you and me is that when I started,
there weren’t any books like this, so I had to dig a lot of the material you find
here out of the source code. A character-building exercise, to be sure, but I’d
much rather have had the book!
This book will help you come up to speed fast on a great, free Java development tool. The chapters on JUnit, Ant, and Team (CVS) integration in particular address areas where newcomers often have questions and need a little
boost to become productive. If you’re not already using these tools, you should
be. If you are, you’ll find out how Eclipse makes it easier to use them. The nuts
and bolts of programming—creating and maintaining projects, editing source
code, and debugging—are not neglected, and the section on refactoring will
introduce you to features that, if your previous tool didn’t have them, you will
soon wonder how you ever lived without.
Eclipse has its own GUI framework called the Standard Widget Toolkit
(SWT), which is portable across all major platforms, runs fast, and looks native.
You can use SWT to develop your own applications, the same way you might use
AWT/Swing. A smaller framework named JFace, built on top of SWT, adds dialogs, wizards, models, and other essentials to the basic SWT widgets. These are
discussed in useful but not excruciating detail in appendixes you can also use
for reference.
xvi FOREWORD
The book will be even more helpful if you have ambitions to go beyond using
Eclipse to extending it. I’m one of those people. For a long time there was no
good introduction to plug-in writing, which made it tough to get started; but
now there is. I hope you will give the chapters on extending Eclipse a fair chance
to excite you with the possibilities.
I don’t know about you, but I’m never quite satisfied with the development tools
I use. No matter how great they are, something is always missing. I think most
developers are like that. We tend to fall in love with our favorite editor or IDE,
defend it staunchly in the newsgroups, and evangelize our friends relentlessly.
Yet, in our fickle hearts, we realize its many blemishes and shortcomings. That’s
why most developers are latent tool-builders. As Henry Petroski observed in The
Evolution of Useful Things, the mother of invention is not necessity, it is irritation.
But usually our tool-building urges remain dormant, because of the great
effort required to duplicate the hundreds of things about our favorite tool that
are perfectly fine in order to fix the handful of things we find wanting. An open
source development tool like Eclipse, built from the ground up by extending a
very small nucleus with plug-ins, allows us to give vent to our frustrations in the
most productive way possible. By writing plug-ins, we can improve and extend
an already rich IDE, keeping all that is good about it. Moreover, we can readily
share our efforts with other users of the tool and take advantage of their efforts,
by virtue of the common platform that underlies all.
I speak of irritation, but I wouldn’t write a plug-in for a tool I didn’t like much.
Eclipse has a lot to like. I won’t rattle off features; you will discover these for yourself in the pages of this book or on your own. I’ll just mention one thing that strikes
me as extraordinary, even unique: the excellent technical support provided in the
Eclipse newsgroups by the actual people who wrote the code. I know of no commercial product whose support is nearly as good, and no other open source project
whose developers are so committed to answering any and all questions thrown at
them. In many cases, questions are answered with source code written and tested
for the occasion. For a programmer, it doesn’t get much better than that.
Taking advantage of these resources, people like you and me have written or
are in the process of writing a wide spectrum of plug-ins, ranging from hacks to
features to entire subsystems. One guy didn’t like the way the toolbar icons were
laid out, so he wrote a plug-in that arranged them as he preferred; it turned out
quite a few people agreed with him. I wrote an XML editor because there wasn’t a
decent one available at the time. Others are fitting in new programming languages, graphical editors, GUI builders. Developers in large companies are using
plug-ins to tailor Eclipse to corporate ways, like the source control system the VP
standardized on that no other tools seem to support. Graduate students are
FOREWORD xvii
using Eclipse as the platform for their thesis projects. The list goes on, and it is
always incomplete.
You can even make money extending Eclipse. Eclipse is free, but its license
allows you to charge for your Eclipse-based extensions. (For complete licensing
details, use the Legal Stuff link on the eclipse.org web page.) There are four ways
to do this:
■ You can take Eclipse as a whole, strip out the parts you don’t want, add the
extensions you do want, and sell the result as your own product.
■ You can select parts of Eclipse, such as SWT and JFace (described in this book),
and use them to build your own applications that don’t necessarily have
anything to do with development tools.
■ You can sell individual plug-ins to the Eclipse community. (As you might
imagine, it takes a lot of added value to get people to pay for extensions to
free software, but I know of several projects underway, including my own,
that intend to test the waters.)
■ You can, with little extra effort, target your Eclipse plug-ins at the IBM
WebSphere Application Developer (WSAD) add-on market. WSAD is based
on Eclipse and is intentionally very compatible with it. (WSAD is Enterprisewith-a-big-E software; customers are accustomed to paying.)
The only catch to all this generosity is you have to know how to take advantage of
it. This book will get you off and running. I heartily recommend it. I’ve been
writing Eclipse plug-ins since 2001, and this book taught me things I didn’t
know. The only negative thing I can think to say is that I’m a little envious that
readers will come up to speed so much faster than I did.
Bob Foster
http://www.xmlbuddy.com/