Team LiB
Previous Section Next Section

Hack 90. Identify Reusable Toolkits

Don't code everything from scratch: reuse bits and pieces of Firefox.

The chrome files supplied with FirefoxXUL, CSS, JavaScript, and otherscan be reused by your extension or chrome application project. If you're familiar with these pieces, you can possibly save yourself a lot of time. At least you'll save yourself a lot of confusion.

8.8.1. Poking Around Inside toolkit.jar

In the install area, inside the chrome directory, lies the toolkit.jar archive. This is the starting point for all XUL pages and therefore for all extensions and chrome applications. It consists of a number of pieces.

This toolkit.jar file changed a small amount when it was updated from the Mozilla Application Suite (1.x) to Firefox. Because that change involved renaming and was advertised in a number of influential places, some people assumed that there would be significant compatibility issues. This is not the case. The most important files in toolkit.jar have changed very little, so there is substantial and extensive compatibility with other Mozilla-based products. There is 100 percent compatibility with Thunderbird and Nvu, at least. Furthermore, the toolkit.jar file is just a file, and it is easy for extensions to specialize or override portions of it with their own tailored files. There is therefore no reason to consider this file an immovable requirement. It is just a well-tested and convenient starting point.

8.8.1.1 The global package

The global package has special status in the chrome. It provides default functionality that XUL pages can pick up, regardless of which theme or locale is the current one. Each new theme that is created must implement a skin for the special global package.

The global package also holds locale and content information. The locale information is delivered in the default locale JAR file, such as en-US.jar, but the content information is in toolkit.jar. This content information includes all the ultra-generic XUL dialog boxes that an XUL application might want. Examples include print dialog boxes, file pickers and savers, and the XUL content that's displayed when the JavaScript alert() method is called.

These dialogs can also be called from XPCOM components [Hack #82] .


8.8.1.2 The xul.css master XBL stylesheet

A special part of toolkit.jar is the xul.css file. This is the place to look for any and all details about the definition and behavior of XUL tags. This stylesheet is applied to every XUL document that Firefox displays. It uses the -moz-binding style property [Hack #69] to define every XUL tag (and therefore the whole XUL vocabulary).

This file is therefore used in place of a DTD, an XML Schema definition, or a hardcoded solution (there is also a little bit of XUL hardcoding). It is therefore the ultimate authority and reference on XUL. All of the XBL bindings attached to tags using this stylesheet are also located in toolkit.jar.

Modifying xul.css modifies the look and/or behavior of all XUL-based extensions and applications. Don't modify this file directly; extend or override it with your own if you need to make changes.

8.8.1.3 The mozapps package

Another source of common dialog boxes appears in the mozapps package, which is also located in toolkit.jar. Rather than contain generic dialog boxes, this package contains functionality that represents "The Mozilla Way." If your chrome application wants to offer an extension interface, a themes interface, an update manager, or XPInstall functionality, then you'll find some generic pieces you can reuse.

8.8.2. Scavenging Application Pieces

The toolkit.jar archive is not the only place to find useful application pieces. Firefox's browser.jar chrome archive also contains some reusable bits. These include the Bookmark Manager and the Cookie Viewer. There's also the Help system, which is stored separately in help.jar. In general, any and all chrome content can contain gems that you can reuse, but as your application becomes more specific and more specialized, reusable pieces are harder to find.

8.8.3. Finding Embedded Components

Separate from the chrome proper are the XPCOM components that are used extensively in chrome scripts. It's not so easy to determine from scratch which components support which interfaces.

The cview extension (http://www.hacksrus.com/~rginda/) goes a little way toward listing all these components and their interfaces. For a complete set of reports, try the downloadable files from the book Rapid Application Development with Mozilla (Prentice Hall PTR). You can find them at the author's web site, http://www.nigelmcfarlane.com/books/radmoz_support.html.

8.8.4. Reusing Script Libraries

In addition to user-interface pieces, you can speed up your application by using existing JavaScript libraries. Here are two examples.


nsDragAndDrop.js

This is a script library supplied with toolkit.jar that eases the development of mouse-driven drag-and-drop functionality. Instead of having to housekeep complex state systems, this script provides a predefined manager object that you can climb on top of. This particular example is of medium difficulty to learn, but it's easy to use.


The jsLib library

The jsLib library (http://jslib.mozdev.org) provides wrapper objects that seek to make common tasks like file handling and RDF datasources easier to use. Although some simplicity is achieved, the library presents a number of separate objects, and fitting them all together might not be what you had in mind as a time-saver. Nevertheless, it provides a medium-level abstraction that several extensions have deemed worthy of use.

    Team LiB
    Previous Section Next Section