Appendix B. Useful Utility Classes

In any piece of software, developers will find there are some common tasks that need to be done in widely differing areas of the code, but none of which are of such significance that they merit their own API definition or package. This is as true of NetBeans as any other large software project. Here we’ll try to cover some of the convenient bits of utility code that are available to you as a module author.

XML Utilities

Increasing numbers of modules deal with XML, either for their own internal use or to support an XML-based user data format. There are a number of useful utilities you can use if your module deals in any way with XML data.

org.openide.xml.XMLUtil

The XMLUtil class consists entirely of static utility methods. Some are purely convenience methods that provide easier ways to access JAXP functionality to parse documents, create DOM documents, and so on. You can also serialize a DOM document to XML text—this functionality is provided in some parser implementations, but with XMLUtil you do not need to know which you are using. Finally, various methods help escape XML metacharacters, useful when manually generating XML markup from a data model.

org.openide.xml.EntityCatalog

EntityCatalog provides an entity resolver that is able to look in a local catalog for DTDs and other entities without having to connect to a network to download them. Modules can register DTDs in the system filesystem via their XML layers, and these local copies will be used in preference to downloading. The catalog implements the standard org.xml.sax.EntityResolver interface.

Visual Components and Extensions to Standard Swing Components

Visual components in NetBeans, outside the Window System API, fall generally into two categories: extensions to Swing components designed to hook into NetBeans’ infrastructure and hacks or workarounds to either bugs or limitations in Swing. The former category can be useful; the latter category will ideally eventually be eliminated as Swing matures.

In the package org.openide.awt, you can find such classes as MenuBar, Toolbar, and so forth. Generally these are components that know how to create themselves from a NetBeans filesystem folder. For example, the main menu is essentially a component that, given a filesystem folder, will create menu item subcomponents for the contents and subfolders of that folder.

Lookup Implementations

FolderLookup, technically in the loaders package, is a specialized FolderInstance that makes it possible to look up object instances declared in .instance (or .settings) files. If one or more .instance files in the folder are an implementations of Lookup, the FolderLookup will delegate to them, splicing in their lookup results. Including subinstances of Lookup inside a folder that is searchable using the Lookup API is generally useful if your module creates many objects on the fly that you need to add to lookup.

This elegant approach bridges the declarative power of the XML filesystems specification to NetBeans at runtime; not everything you get back in a Lookup.Result has to be directly declared in a module’s XML layer.

The org.openide.util.lookup package also contains a variety of base classes useful for creating certain kinds of in-memory lookups. You can easily make a Lookup implementation providing just a fixed set of instances.

Threading

org.openide.util.Mutex provides the classic mutex design pattern: a read-many, write-one lock.

Task is a frequently used and subclassed class representing a task that can be executed and monitored for its status. In particular, it goes hand-in-hand with the RequestProcessor.

RequestProcessor can execute Tasks or other Runnables in its own thread. It offers scheduling and priority options for posted tasks.

Miscellaneous Utility Classes inorg.openide.util

The utilities package and its subclasses are a general grab-bag of classes and methods that have proven useful in one place or another as NetBeans has evolved. NetBeans APIs written before the availability of Java 2 Collections make use of java.util.Enumeration, and the org.openide.util.enum package contains some convenience classes for aggregating enumerations, enumerating arrays, and such. If you find yourself needing to do something complex with enumerations, check the Javadoc for this package—the class names are fairly self-explanatory, and you may save yourself some coding.

WeakListener

Allows attaching listeners without creating strong references between objects. For details on its use, see Chapter 26.

Utilities

This class is a smorgasbord of unrelated static methods for performing various random tasks, including for example:

  • Determining the OS on which NetBeans is running

  • Loading images with a cache

  • Merging images for icon badging

  • Comparing arrays

  • Converting arrays of objects such as Integer to equivalent arrays of primitive types and vice versa

  • Determining the screen size and centering windows, taking into account the Windows task bar and multi-screen displays

  • Breaking a string with newlines into an array of strings

For a complete overview of this package, refer to the Javadoc.

Serialization Helpers

The package org.openide.util.io contains various classes helpful when working with serialization. NbMarshalledObject serves as a unitary wrapper for a serialized data stream that can be unpacked on demand. It is often used when there is a “dangerous” object that might include arbitrary classes from foreign module code and that could easily throw exceptions when serialized or deserialized due to programming errors. Rather than embedding the dangerous object directly into a larger serialized data stream, you can wrap it in an NbMarshalledObject, which will isolate any problems without affecting the rest of the stream.

NbObjectOutputStream and the matching NbObjectInputStream are commonly used for serialization in NetBeans. Besides automatically using a classloader that can load classes from any NetBeans module, they make it easy to isolate dangerous serialized objects (using NbMarshalledObject). SafeException is thrown from these classes when serializing or deserializing one object to or from the stream fails but the rest of the stream stays intact.

Note

From time to time, you might need to look at the structure of a serialized object that NetBeans has saved in the user directory (for example). Perhaps a SystemOption does not seem to be storing what you expect, and you are not sure what is really on disk.

Let the IDE help you! If you have a .ser file in the Explorer in NetBeans 3.4, you can expand it to see the detailed structure of its serial data stream. If you have a .settings file with serialdata inside it, right-click it and choose Customize Bean, then Serialize As.... Pick any location and name, then browse to the new .ser file and look at what was really saved.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.217.187