CONTRIBUTING TO ECLIPSE the eclipse series SERIESEDITORS Erich Gamma n Lee Nackman n John Wiegand Eclipse is a universal tool platform, an open extensible integrated development environment (IDE) for anything and nothing in particu- lar. Eclipse represents one of the most exciting initiatives hatched from the world of application development in a long time, and it has the considerable support of the leading companies and organizations in the technology sector. Eclipse is gaining widespread acceptance in both the commercial and academic arenas. The Eclipse Series from Addison-Wesley is the definitive series of books dedicated to the Eclipse platform. Books in the series promise to bring you the key technical information you need to analyze Eclipse, high-quality insight into this powerful technology, and the practical advice you need to build tools to support this evolutionary Open Source platform. Leading experts Erich Gamma, Lee Nackman, and John Wiegand are the series editors. Titles in the Eclipse Series Kent Beck and Erich Gamma,Contributing to Eclipse:Principles, Patterns,and Plug-Ins, 0-321-20575-8 Frank Budinsky,David Steinberg,Ed Merks,Ray Ellersick,and Timothy J.Grose,Eclipse Modeling Framework, 0-131-42542-0 Eric Clayberg and Dan Rubel, Eclipse:Building Commercial-Quality Plug- Ins, 0-321-22847-2 For more information on books in this series visit www.awprofessional.com/series/eclipse CONTRIBUTING TO ECLIPSE Principles, Patterns, and Plug-Ins Erich Gamma Kent Beck Boston • San Francisco • New York London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Addison-Wesley was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers discounts on this book when ordered in quantity for bulk purchases and special sales. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 [email protected] For sales outside of the U.S., please contact: International Sales (317) 581-3793 [email protected] Visit Addison-Wesley on the Web: www.awprofessional.com Library of Congress Cataloging-in-Publication Data Gamma, Erich. Contributing to Eclipse : principles, patterns, and plug-ins / Erich Gamma, Kent Beck. p. cm. ISBN 0-321-20575-8 (alk. paper) 1. Computer software—Development. I. Beck, Kent. II. Title. QA76.76.D47G355 2003 005.1—dc22 2003020914 Copyright © 2004 by Pearson Education, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or other- wise, without the prior consent of the publisher. Printed in the United States of America. Published simultaneously in Canada. For information on obtaining permission for use of material from this work, please submit a written request to: Pearson Education, Inc. Rights and Contracts Department 75 Arlington Street, Suite 300 Boston, MA 02116 Fax: (617) 848-7047 ISBN 0-321-20575-8 Text reprinted on recycled paper 2 3 4 5 6 7 8 9 10—CRS—0807060504 Second printing, February 2004 To my sunshine Karin, Jeremy, and Jill —E.G. Behind every book is a network of people and relationships that are invisible to the reader except as they affect the quality of what you are reading. My most important relationship is with my wife, Cindee. She gave me love and support when it was most difficult, encouraged me, worked many hours on the book itself, appreciated the good and pointed out opportunities for improvement, and supported our kids while I worked. You’ll never see what she did, but without her, I couldn’t have contributed to this book. Thank you, Cindee. —K.B. This page intentionally left blank Contents Foreword xiii Preface xv Chapter 1 The Big Picture 1 1.1 Book Goals 3 1.2 Plug-In 5 1.3 Eclipse in a Nutshell 5 Part I ❍ Circle Zero: Hello World 9 Chapter 2 Setting Up Eclipse for Plug-In Development 11 2.1 Setting Up a Workspace 11 2.2 Browsing and Searching Source 12 Chapter 3 Hello World 15 3.1 Declaration/Implementation Split 16 3.2 Hello Button 18 3.3 Saying “Hello” 25 Part II ❍ Circle One: Basic Plug-In 33 Chapter 4 Der Plan 35 4.1 JUnit by Example 35 4.2 JUnit Integration 37 vii viii Contents Chapter 5 Contributing a Menu Item to Run Tests 39 Chapter 6 Implementing the Menu Item Behavior 47 Chapter 7 Displaying the Results 59 Chapter 8 Defining an Extension Point 65 Chapter 9 Notifying Extensions 77 Chapter 10 Publishing 79 10.1 Package the Plug-In 79 10.2 Bundling the Plug-In into a Feature 83 10.3 Contributing 86 Chapter 11 Closing Circle One 89 Interlude Chapter 12 Test-Driven Plug-In Development 91 12.1 PDE JUnit 92 12.2 A Test Project Fixture 98 12.3 Testing the Contributed JUnit Plug-In 100 12.4 And Now… 104 Part III ❍ Circle Two: The Rest of the Plug-In 105 Chapter 13 Viewing Results 107 13.1 Contributing a View 107 13.2 Listening to Testing Progress 111 13.3 Changing Colors 113 Chapter 14 Menu Contributions 119 14.1 Creating Context Menus 119 14.2 Contributing Menu Items 124 Chapter 15 Failed Tests Are Compile Errors 129 Chapter 16 Test Failures as Markers 131 16.1 Test for a Marker 131 16.2 Passing the Project 134 16.3 Creating Markers 136 Contents ix 16.4 Deleting Markers 140 16.5 Marker Images 141 16.6 Marker Resolution 142 Chapter 17 Finding Tests 147 Chapter 18 Builders and Natures 151 18.1 Resource Listeners versus Builders 152 18.2 Using Natures to Configure Builders 154 Chapter 19 Auto-Test Property 161 Chapter 20 Exception Handling 171 20.1 IStatus, CoreException 172 20.2 Presenting Exceptions in an Error Dialog 173 20.3 Logging Errors 175 Chapter 21 Tracing—Instrumenting a Plug-In 179 Chapter 22 Marker Resolution—Invoking a Long-Running Operation 185 22.1 Testing Marker Resolution 186 22.2 Build and Rerun 186 22.3 Showing Progress 188 22.4 Rerunning the Test 191 Chapter 23 Test Report View—Using JFace 193 23.1 TestResult 195 23.2 The Test 196 23.3 The View 197 23.4 TestReportLabelProvider 201 23.5 TestReportContentProvider 204 23.6 Handling Events 210 Chapter 24 A Simple Editor to Exclude Tests 217 24.1 Contributing an Editor 219 24.2 Contributing a Contributor 224 Chapter 25 ResultView Revisited—Observing Changes 231 25.1 Testing Color 231 25.2 Observing Changes 232 25.3 Reacting to Changes 235