Eclipse Rich Client Platform Second Edition The Eclipse Series Eric McAffer, Erich Gamma, John Wiegand, Series Editors Visit informit.com/series/eclipse for a complete list of available publications. Eclipse is a universal, multilanguage software development environment— an open, extensible, integrated development environment (IDE)—that can be used for anything. Eclipse represents one of the most exciting initiatives to come from the world of application development, and it has the support of leading companies and organizations in the technology sector. Eclipse is gaining widespread acceptance in both commercial and academic arenas. The Eclipse Series is the definitive collection of publications dedicated to the Eclipse platform. Books in this series bring you key technical information, critical insight, and the practical advice you need to build tools to support this revolutionary open-source platform. Eclipse Rich Client Platform Second Edition Jeff McAffer Jean-Michel Lemieux Chris Aniszczyk Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • 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 the publisher 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 excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 [email protected] For sales outside the United States please contact: International Sales [email protected] Visit us on the Web: informit.com/aw Library of Congress Cataloging-in-Publication Data McAffer, Jeff. Eclipse Rich Client Platform / Jeff McAffer, Jean-Michel Lemieux, Chris Aniszczyk.—2nd ed. p. cm. Includes index. ISBN 0-321-60378-8 (pbk. : alk. paper) 1. Computer software—Development. 2. Java (Computer program language) 3.Application software—Development. I. Lemieux, Jean-Michel. II. Aniszczyk, Chris. III. Title. QA76.76.D47M383 2010 005.13’3—dc22 2010006689 Copyright © 2010 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to: Pearson Education, Inc. Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax: (617) 671-3447 ISBN-13: 978-0-321-60378-4 ISBN-10: 0-321-60378-8 Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana. First printing, May 2010 Contents Foreword xxi Preface xxv Acknowledgments xxix About the Authors xxxi ❍ Part I Introduction 1 Chapter 1 Eclipse as a Rich Client Platform 3 1.1 Eclipse 5 1.2 The Eclipse Rich Client Platform 5 1.3 Eclipse RCP over the Years 6 1.4 Uses of RCP 7 1.4.1 IBM Lotus and Eclipse RCP 7 1.4.2 NASA and the Eclipse RCP 10 1.5 Summary 12 1.6 Pointers 13 Chapter 2 Eclipse RCP Concepts 15 2.1 A Community of Plug-ins 15 2.2 Inside Plug-ins 18 2.3 Putting a System Together 19 2.4 OSGi Framework 20 2.5 Equinox 21 2.5.1 Applications 22 v vi Contents 2.5.2 Products 22 2.5.3 Extension Registry 23 2.6 Standard Widget Toolkit (SWT) 25 2.7 JFace 25 2.8 UI Workbench 25 2.8.1 Contribution-Based Extensibility 26 2.8.2 Perspectives, Views, and Editors 26 2.9 Summary 27 2.10 Pointers 27 ❍ Part II RCP by Example 29 Chapter 3 Tutorial Introduction 31 3.1 What Is Hyperbola? 31 3.2 The Evolution of Hyperbola 32 3.3 Development Environment Installation 33 3.4 Sample Code 34 3.4.1 Moving from Chapter to Chapter 35 3.4.2 Comparing 35 3.5 Target Platform Setup 36 3.5.1 The Predefined Target 37 3.5.2 Defining Target Platforms 38 3.6 Learning by Example 42 3.7 Summary 44 3.8 Pointers 44 Chapter 4 The Hyperbola Application 45 4.1 Hyperbola “Hello, World” 45 4.2 Tour of the Code 51 4.2.1 Application 51 4.2.2 WorkbenchAdvisor 53 4.2.3 Perspective 53 4.2.4 WorkbenchWindowAdvisor 54 4.2.5 ActionBarAdvisor 55 4.2.6 Summary 55 Contents vii 4.3 Running and Debugging 55 4.3.1 Debugging 56 4.3.2 Launch Configurations 60 4.4 Summary 62 4.5 Pointers 62 Chapter 5 Starting the Hyperbola Prototype 63 5.1 Continuing from the Shell 64 5.1.1 Saving Window Location and Size 65 5.2 Adding a Contacts View 65 5.2.1 Adding the Contacts View to a Perspective 67 5.3 The Chat Model 70 5.4 Filling in the Contacts View 72 5.4.1 The ContactsView 72 5.4.2 Content Providers Overview 74 5.4.3 The Label Provider 77 5.5 Adding Images 78 5.6 Summary 81 5.7 Pointers 82 Chapter 6 Adding Actions 83 6.1 Adding to the Menus and Toolbar 84 6.1.1 Create a Top-Level Menu 85 6.1.2 Menu Managers 87 6.1.3 The Add Contact Action 88 6.1.4 Adding the Add Contact Action 91 6.1.5 Customizable Toolbars 93 6.2 Adding to the Status Line 93 6.2.1 Status Line—A Shared Resource 95 6.3 System Tray Integration 96 6.3.1 Obtaining a Display 97 6.3.2 Creating the Tray Item 98 6.4 Summary 100 6.5 Pointers 101 viii Contents Chapter 7 Adding a Chat Editor 103 7.1 Views and Editors 104 7.2 Defining the Chat Editor 105 7.2.1 Editor Input 109 7.2.2 The Chat Action 110 7.3 Checkpoint 113 7.4 Summary 114 7.5 Pointers 114 Chapter 8 Branding Hyperbola 115 8.1 Defining the Hyperbola Product 115 8.2 Window Images 120 8.3 Customizing the Launcher 121 8.4 Splash Screen 122 8.5 About Information 124 8.5.1 Product About Information 124 8.5.2 Installation Details About Information 126 8.6 Summary 127 8.7 Pointers 127 Chapter 9 Packaging Hyperbola 129 9.1 Exporting Hyperbola 129 9.2 Exporting for Other Platforms 132 9.3 Summary 134 9.4 Pointers 135 Chapter 10 Messaging Support 137 10.1 Integrating a Third-Party Library 138 10.1.1 Bundling Smack 138 10.1.2 Testing the Bundling 141 10.2 Refactoring the Model 143 10.2.1 Introduction to Smack 144 10.2.2 Design Objectives 145 10.2.3 Deleting Prototype Classes 146 10.2.4 Adding Chats 147 Contents ix 10.3 Updating the UI 149 10.3.1 The Content Provider 150 10.3.2 The Label Provider 151 10.4 Chatting with Eliza 152 10.5 Summary 153 10.6 Pointers 154 Chapter 11 Adding a Login Dialog 155 11.1 Adding the Login Dialog 155 11.1.1 Branding the Dialog 159 11.2 Remembering Login Settings 161 11.2.1 The Basics 161 11.2.2 Using Preferences 164 11.2.3 Storing Preferences Securely 168 11.3 Adding Auto-login Preferences 170 11.3.1 Creating a Preference Page 170 11.3.2 Adding the Action 172 11.3.3 Accessing Preferences 173 11.3.4 Default Preference Values 173 11.3.5 Preferences on the Login Dialog 174 11.4 Summary 175 11.5 Pointers 175 Chapter 12 Adding Key Bindings 177 12.1 Defining Commands 177 12.2 Checkpoint 182 12.3 Adding Key Bindings for Workbench Actions 182 12.4 Key Schemes 184 12.5 Keys Preference Page 185 12.6 Summary 186 12.7 Pointers 186 Chapter 13 Adding Help 187 13.1 Adding to the Target Platform 187 13.1.1 Getting Plug-ins 188 13.1.2 Adding Plug-ins 188