20,73 €
The NetBeans Platform has many features provided out of the box for Swing desktop application developers. It can take you hours just to create menu bars, toolbars, a window system, and other typical desktop application infrastructural needs rather than you focusing on your domain knowledge. Imagine how much time you could save with a hands-on guide for using the NetBeans Platform, which relieves you from creating desktop functions for each of your applications.
This book guides you through the development of a complete Swing application built on the NetBeans Platform. Each chapter introduces a number of new concepts relating to a theme, such as the window system, and then shows you how to implement the concepts in the application you are creating. At the end of the book you have a task manager, which you can adapt to your own purposes. Or you can, of course, create your own applications, now that you have built up a solid basis of NetBeans Platform knowledge.
The NetBeans Platform is a framework for developing large distributed desktop applications. It aims to drastically simplify desktop application development by providing a number of techniques, patterns, and full-blown Swing components out of the box. Most desktop applications have very similar technical requirements, such as: a consistent user interface, extensibility, data display, configuration settings, a help system, distribution mechanisms, on-line update possibilities, and the ability to be deployed to multiple operating systems.
Fulfilling these technical requirements over and over again for each new application is expensive, superfluous, and boring. The NetBeans Platform gives developers a transparent, open source, extensible, and free framework that address all of these technical requirements. This book will guide you through all these topics and show you how you can apply the lessons learned in the context of a real application.
The central driver of the book is the creation of a complete sample application, chapter by chapter, throughout the length of this book. You will learn how to apply the key concepts in your own work environment, so that you will be able to build flexible, reliable, robust and scalable Swing applications yourself. At the end of the book, you will be comfortable creating similar applications yourself and you will know what to do when you get stuck.
This book helps you to create robust Swing applications by providing an in-depth coverage of all the features of the powerful NetBeans Platform 6.9
This is a step-by-step example-driven tutorial designed to be worked through chapter-wise. You'll be learning with the help of example code and screenshots that will ensure easier understanding. At the end, you will have developed a complete application on top of the NetBeans Platform.
This book is written for developers who are comfortable with Java and Swing and who would like to use a framework as the basis of their Swing application. Zero knowledge of the NetBeans Platform is assumed. The reader is typically a developer (or a group of developers) wanting to create large, distributed, flexible Swing applications. The development team is typically large and distributed and members need to work independently on distinct parts of the application. The end user of the application typically needs to have the ability to install additional features at runtime, after the application has already been installed, without reinstalling the application.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 302
Veröffentlichungsjahr: 2010
Copyright © 2010 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: August 2010
Production Reference: 1020810
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-849511-76-6
www.packtpub.com
Cover Image by Vinayak Chittar (<[email protected]>)
Author
Jürgen Petri
Special Thanks
Geertjan Wielenga
Acquisition Editor
Douglas Paterson
Development Editor
Rakesh Shejwal
Technical Editor
Gauri Iyer
Indexer
Rekha Nair
Editorial Team Leader
Aanchal Kumar
Project Team Leader
Priya Mukherji
Project Coordinator
Prasad Rai
Proofreader
Lynda Sliwoski
Production Coordinator
Shantanu Zagade
Cover Work
Shantanu Zagade
NetBeans RCP—Das Entwicklerheft, Jürgen's original German book about the NetBeans Platform, was published in August, 2008. What's happened with the NetBeans Platform since then? Well, quite a lot, actually! NetBeans Platform 6.9 has been released, sporting a large set of features which Jürgen might only have dreamed of while working on his book. Most significantly, the new NetBeans Platform support for integrating OSGi bundles needs mentioning in this regard. Apart from that, there are many other new features, including new and changed APIs, which to a greater or lesser extent have an impact on the text and the code that the original book provided. Wherever relevant, and always as seamlessly as possible, these new features have been introduced into this translated and updated version.
The spirit of Jürgen's original book remains intact: this is not a complete reference guide to each and every detail that the NetBeans Platform provides Java desktop developers. For those purposes, "The Definitive Guide to the NetBeans Platform" (Apress) and "Rich Client Programming: Plugging into the NetBeans Platform" (Prentice Hall) continue to be the best sources, together with the many NetBeans Platform tutorials (http://platform.netbeans.org/tutorials), of course. However, after reading those books and documents, many readers have asked themselves: "OK, that's all very interesting, but how do I get started?" That's what this book is all about, taking you by the hand and showing you many aspects of the NetBeans Platform in step-by-step instructions, within one coherent whole.
Because of this approach, some readers may feel somewhat disappointed. For example, Maven-based applications are not addressed at all in this book, simply because that was not relevant to the particular application that Jürgen set about to create. And, most topics in this book could deserve more pages, more explanation, and deeper analysis. However, a balance had to be made between providing a practical guide to the newbie, which this book tries to do, and providing a complete and thorough reference guide, various forms of which already exist. Hopefully the reader will forgive us for any errors made in this balancing act.
The NetBeans Platform community pitched in and translated this book together. The translation team was as follows: Jean-Marc Borer, Zane Cahill, Costantino Cerbo, Stefan Alexander Flemming, Michael Holste, Peti Horozoglu, Martin Klähn, Victor Ott, Christian Enrique Portilla Pauca, Christian Pervoelz, Sven Reimers, Peter Rogge, Johannes Strassmayr, Florian Vogler, and Fionn Ziegler. Many thanks for their enthusiasm and hard work! I recommend them all as translators.
Many thanks also to the many reviewers, in particular Jaroslav Tulach, Anton Epple, and Tom Wheeler. In fact, in the Pantheon of NetBeans, Tom Wheeler is at least a minor deity on the order of Eos.
From the team at Packt, many people deserve praise and thanks. In particular, Prasad Rai and Douglas Paterson, for making this book possible, as well as Rakesh Shejwal and Gauri Iyer, for their endless patience and support.
Happy reading and learning about the NetBeans Platform!
Geertjan Wielenga
Technical Trainer & Writer
NetBeans
Jürgen Petri is a Sun Certified Enterprise Architect with more than 12 years experience in developing enterprise Java applications.
He provides consulting services on Java and Java EE technology, and he also trains architects and developers on software engineering, enterprise architectures, and Java-related technologies. Although he is interested in many areas of software development, Jürgen has a distinct focus on great UI technologies like the NetBeans Platform.
Currently, he puts his knowledge in migrating the UI of a core banking system with a broad spectrum of technical cross cutting aspects into a web application using GWT.
Additionally, Jürgen is the author of NetBeans RCP - Das Entwicklerheft published in 2008 by O'Reilly.
First, I want to thank Geertjan Wielenga for his relentless work to publish an updated English version of my previous book NetBeans RCP - Das Entwicklerheft published in 2008 by O'Reilly. Without him this book would not exist. Geertjan is the author of several NetBeans Platform publications and author of a great and well-known blog (http://blogs.sun.com/geertjan). If you are starting to develop an application on this great platform, you should make sure to visit his blog. For me it has been a continuous source of inspiration. Geertjan mobilized members of the NetBeans community in order to translate and update the book. He did an excellent job in managing the translation and update.
I especially want to thank Christian Enrique Portilla Pauca, Sven Reimers, Peter Rogge, Johannes Strassmayr, Florian Vogler, Zane Cahill, Martin Klähn, Jean-Marc Borer, Victor Ott, Fionn Ziegler, Constantino Cerbo, Peti Horozoglu, Christian Pervoelz, Martin Klähn, and Stefan Alexander Flemming for contributing to the translation.
I want to thank Packt for giving me the opportunity to write this book and for putting up with me even though I had continuously missed upon the agreed dates.
Most importantly, I want to thank my wife who supported me in writing the book in the first place and my son Leonard for all the joy he brings to us.
The NetBeans Platform is the world's only modular Swing application framework. It aims to drastically simplify desktop application development by providing a number of techniques, patterns, and full-blown Swing components.
Most desktop applications have very similar technical requirements, such as the following:
Fulfilling these technical requirements over and over again for each new application is expensive, superfluous, and boring. The NetBeans Platform gives the developer a transparent, open source, extensible, and free framework that addresses all of these technical requirements.
This book doesn't aim to explicate all that the NetBeans Platform offers or to explore each and every corner of its many features. Rather, this book guides you through the development of a specific Java desktop application, while showing you everything that is relevant in the context of the particular application itself. That process, in turn, will lead you through the main features relevant to the development of most general applications on the NetBeans Platform.
The central driver of the book is, therefore, the creation of a complete Java desktop application, chapter by chapter, step-by-step, sequentially through the progression of chapters in this book.
Chapter 1, Module: A module is the basic building block of a NetBeans Platform application. In the chapter dealing with this theme, you learn why it makes sense to develop modular applications, while examining the features of modules, as well as their interdependencies. Moreover, you examine module versioning and the lifecycle of modules, as well as the entry points into that lifecycle.
Chapter 2, Forms: Almost every large desktop application needs to provide a number of forms that accept data from the user. You learn how forms are created for usage on the NetBeans Platform, how their layout is set, and how to implement the related handling of events.
Chapter 3, Window System: The NetBeans Window System, together with the API that it exposes, lets you arrange forms on the screen within a docking framework. You learn how to create windows, what their lifecycle looks like, and how to influence that lifecycle. In addition, you examine how a window is positioned within the layout of the application, how to influence the window layout, and how to create groups of related windows.
Chapter 4, Lookup: The Lookup API provides a communication mechanism, comparable to an event bus, which is of central significance in the creation of NetBeans Platform applications. You learn how to use the Lookup to find services so that loosely-coupled communication between modules can be established. You also learn how to listen to the Lookup so that content can be added dynamically to a NetBeans Platform application. You examine how a Lookup can act as a proxy for another Lookup and how this functions as the basis of context sensitivity, also known as "selection management", in NetBeans Platform applications.
Chapter 5, Actions: You learn how to create global actions and how to invoke them from menus and toolbars. You also examine how to connect actions to shortcuts, allowing them to be invoked from a keyboard.
Chapter 6, Nodes and Explorer Views: A sophisticated MVC implementation for displaying business objects is made available via a set of extensible Swing components, which you can use without very much work at all. You explore how generic hierarchical models, known as "nodes", can represent and display business objects in advanced Swing components called "explorer views". You use flat as well as hierarchical structures and are shown how easily one view can be exchanged for another and how they can be synchronized with each other.
You also spend some time learning about the asynchronous creation of nodes and how context sensitive actions are attached to a node. Last but not least, you learn how the properties of a node can be displayed in property views and how to create the related property editors.
Chapter 7, File System: The File System API lets you access a NetBeans Platform's virtual filesystem, which serves as the application's central registry. You learn how to access the configuration system, as well as other systems that can be created on top of the same API. Finishing up, you create new folders, files, and attributes in the filesystem.
Chapter 8, Data System: The Datasystems API gives you access to the content of files. You learn how to extend a NetBeans Platform application to provide support for custom data types. You also discover how the features available to data content can change in relation to the current status of the underlying file.
Chapter 9, Dialogs: The responsibilities of dialogs in an application extend from the display of simple messages to the management of step-by-step procedural wizards. In that context, you learn how to display simple messages, standard dialogs, and sophisticated multi-step wizards to the user.
Chapter 10, Settings: Large applications, such as those based on the NetBeans Platform, typically have many different kinds of users. Not all of them need all the application's features and not all of them use those features in the same way. As the application becomes larger, a centralized approach is needed, so that each module added to the application can contribute to a centralized Options window. That is the theme of this chapter, which introduces you to the most important APIs and the entry points into the centralized Options window.
Chapter 11, Help: HTML files constituting your documentation can be integrated into the application. When the user clicks on the Help buttons in the application, or when they invoke an Action to display the entire help content, your HTML files can be displayed to them. This chapter shows you how to configure the help system and gets you started with a few help topics, a table of contents, and an indexing system.
Chapter 12, Branding: Branding enables the application's ancillary details, such as icons and splash screens, to be customized. For example, you learn how to exchange the custom splash screen with your own.
Chapter 13, Distribution and Updates: To let you distribute applications, you examine the various distribution mechanisms for NetBeans Platform applications. You generate a distribution and an installer, with a special focus on how to let an application be updated online.
The Appendices are available for free at http://www.packtpub.com/sites/default/files/1766_Appendices.zip.
To use this book, you need the following software:
For details, visit the following URL, which provides the complete installation instructions for NetBeans IDE 6.9: http://netbeans.org/community/releases/69/install.html
This book is written for developers who are comfortable with Java and Swing and who would like to use a framework as the basis of their Swing applications. Zero knowledge of the NetBeans Platform is assumed.
The reader is typically a developer (or a group of developers) wanting to create large, distributed, and flexible Swing applications. The development team is typically large and distributed and members need to work independently on distinct parts of the application. The end user of the application typically needs to have the ability to install additional features at runtime, after the application has already been installed, without reinstalling the application.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail <[email protected]>.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book on, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code for this book
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
In this chapter, you will cover the following topics:
Modularization becomes more important as an application's complexity increases. The old and trusted programming methodologies struggle to keep pace with continually shortened production cycles, gradually increasing feature sets, and the simultaneously rising importance of software quality. In particular, the pressure of time to market demands a solid application design, providing clear extension points, loosely-coupled components, and a reliable versioning mechanism. Without a strong foundation based on a sustainable architecture, each version increase and new feature introduces a new level of chaos, leading to a scenario where enhancements and bug fixes become impossible to implement, as each change causes increasingly detrimental side effects. An application of this kind can become so problematic that it needs to be rewritten from scratch but, because the underlying application architecture remains unchanged, a rewrite of this kind fails to solve the application's structural defects.
A solution to this problem entails the introduction of modules. A module is a self-contained software component with which other modules can communicate via well-defined interfaces, behind which their implementations are hidden. Each coarsely-grained feature of an application is created within a module, giving each module a clearly defined responsibility within the application. The chaos described above can, of course, continue to exist within a module, exactly as it had existed in the pre-modular application. However, as the classes within a module exist to provide a very specific feature, the module, as a whole, needs to function interdependently with the other modules in the application, meaning that the chances of complete chaos in your code are smaller than they were before. The limited responsibility of a module within the application enables it to be more focused and the application as a whole to be more cleanly designed.
Moreover, the worsening chaos described above is less likely to arise within a modular application, as the implementation of features can only take place via publicly exposed interfaces. The dependencies in the whole application are well-defined and never accidental. Breaking the contract with the interfaces, thereby introducing the chaos described above, can only be done intentionally.
Besides these advantages to the developer, a modular application also benefits the end user. As the NetBeans module system is dynamic, modules (that is, features) can be loaded and unloaded dynamically at runtime. The adaptability and flexibility of applications and the ease with which features can be added and removed by the user at runtime is of particular relevance to enterprise applications, which typically provide support for a variety of user profiles.
Let's now take a more concrete look at modules and their relationship to NetBeans Platform applications.
Generically, a module needs to have certain characteristics to provide the desired advantages promised by modular application development.
It must be possible to make all resources required by a module available via a single deployment package. The closest concept in the Java world is the JAR archive. You can bundle an arbitrary number of resources into a JAR and provide metadata via the manifest file. NetBeans modules are packaged in a similar format, together with module-specific metadata in the related manifest file, into an NBM archive.
Each module must have its own unique identity. The NetBeans module system provides an additional key in the manifest, OpenIDE-Module, which you use to define the application-wide unique name of a module.
Each module must be able to specify its version. NetBeans modules differentiate between specification versions and implementation versions.
Each module defines public interfaces, via which features can be accessed. Accepted methods of accessing dependencies in software rely strongly on conventions. One of these is that types in packages can only be accessed indirectly via facades. However, typically, these well-intended conventions are abandoned in the interests of time. On the other hand, component systems with their own lifecycle environment tend to demand that conventions of this kind are followed to the letter. Within a module, any class can be declared public, and thereby, be accessed by any other class within that module. However, other modules can only access a module's exposed classes, that is, classes that are found within explicitly exposed packages.
The NetBeans Platform is one of the systems that provide a mechanism of this kind. The packages that are to be exposed to other modules in the application are declared via the OpenIDE-Module-Public-Packages manifest key. The NetBeans Platform provides a system of ClassLoaders that provide access to those classes that are within the module, as well as those classes that are found within the publicly exposed packages of other modules upon which the module has declared a dependency.
Each module must declare which, if any, of the other modules in the application are required for it to function. To this end, the NetBeans Platform provides the manifest key OpenIDE-Module-Dependencies. As mentioned previously, the NetBeans Platform module system lets other modules access only the explicitly exposed packages of modules within the application.
Each module must have its own lifecycle, managed by the application's runtime. The NetBeans module system handles the loading and configuration of modules. It also handles the processing of installation code, if any, and the unloading of modules when the application shuts down.
Now that you've been exposed to a lot of theory, let's create a new application with two modules. You then configure them so that they can interact with each other via their publicly exposed interfaces.
You start by creating a new NetBeans Platform Application project. The application project serves as a container for modules and represents a single application, while providing a number of NetBeans Platform modules out of the box. You're also able to configure the NetBeans Platform and brand the application via this project type.
Now you add your first new module to the application.
The Code Name Base is a string defining a unique ID by which the module is known to the rest of the application.
Let's take a look at what you've done so far. First, using a wizard in NetBeans IDE, you created a new module within a new NetBeans Platform application. The module contains various configuration files. You will later learn that the most important of these are the manifest.mf file and the layer.xml file. Both files are found within the Important Files node of the module. The layer.xml is discussed in detail in the later chapters. At the moment, the content of this file is not yet very interesting. The manifest file has been discussed already and, right now, has the following content:
Now, of course, you'd like to be able to use the HelloService class from another module.
The new class cannot be compiled yet, as the HelloService class cannot be found currently. It is not possible to add an import statement, as the module containing the required class is isolated from all the other modules via its module-specific classloader. Therefore, for a class in the HelloClient module to use a class in the HelloService module, two conditions must first be satisfied:
In this case, the package you want to expose is com.netbeansrcp.helloservice. Types in any other packages than those that have been exposed cannot be accessed by other modules.
At this point, you've used various tools in NetBeans IDE to define the public interface of the HelloService module. Next, in the HelloClient module, you set a dependency on the HelloService module. Now the classes in the HelloClient module can access the classes that are part of the public interface of the HelloService module.
Along with the information defining the dependencies between modules, you need to specify exactly which version of the module you are depending on. The NetBeans Platform makes a distinction between the version of the specification and the version of the implementation of the module as follows:
As an example, you set the HelloService module to version 2.1.5.
Try again to build the application. The build succeeds as the requested specification version now corresponds to the specification version of the HelloService module.
A module needs to be installed for the runtime container to manage its lifecycle. Installation takes place declaratively, with all the necessary files being generated by wizards in NetBeans IDE.
You might want to programmatically influence the lifecycle of a module. For that purpose, the ModuleInstall API class is provided by the NetBeans Platform. This class defines a module installer providing methods that interact with the runtime container at specific points in the lifecycle of a module. Generally, these methods are used to initialize settings in the application at the time when the module is installed or to uninitialize those settings when the application closes.
Method
Description
validate()
Override this method to prevent the module from being loaded. That might be something you want to do after failing to determine that certain preconditions, such as the presence of a license, have been met.
restored()
Override this method to initialize the module. Do this only when absolutely necessary, as performance is impacted by the initialization sequences.
closing()
Override this method to return false in order to prevent the application from closing.
close()
Override this method to provide information about the closed state of the application.
To create your own module installer, right-click the Project node of the HelloService project, choose New | Other | Module Installer, accept the default values, and complete the wizard (see figures 9 and 10).
Define the restored() method as follows:
Start the application by right-clicking the Application Project node and choosing Run. The application is started, together with all the modules that define it. In the Output window of the IDE, you should now see the following:
In other words, the module installer has been processed.
If you have changed a module, you do not need to restart the whole application. Instead, you can reload the changed module. Without stopping the application, change the definition of the restored() method, then right-click the
