|
|
< Day Day Up > |
|
SWT OverviewThe SWT component home page on eclipse.org states the following.
This is as strong an embodiment of the design philosophy of SWT as can be found anywhere. Certainly, part of the reason why SWT is efficient is because it is predominantly a compatibility layer on top of the user interface facilities of the underlying operating system. Most of the "real" work is done by the highly optimized, platform-specific code that the operating system vendors have implemented. There is more to it than that, however. To the greatest extent possible, anything that would put extra overhead between the running program and the operating system has been avoided. For example, SWT does not have a separate internal implementation hierarchy, as is found in the AWT peer layer. This removes a layer of indirection from every public entry point and significantly reduces the size of the library. The trade-off with this strategy is that SWT implementations (called ports) have the same implementation hierarchy shape on every platform,[1] and there is very little sharing within the internals of the ports. This made the initial design of SWT somewhat challenging but in practice has not turned out to be a significant issue since then.
Implementations of SWT are available for all of the major desktop operating systems and some handheld devices.[2] Over time, areas of missing platform functionality that would have prevented particular operating systems from being targets for full SWT implementations (and thus, candidate Eclipse platforms) have been covered by emulated versions of that functionality. Using the emulated versions simplifies the task of getting an initial port of SWT running, making it possible to fill in the details incrementally.
Another way that SWT maintains efficiency is by avoiding the trap of attempting to "fix" fundamental limitations in the underlying operating system. There are three significant ways that this is visible in SWT.
Despite these potential sources of platform-specific behavior, writing SWT applications that run well on multiple operating systems is not difficult. Certainly, the star example of this is the Eclipse universal tools platform. SWT has also been used for many other applications ranging from simple text editors to development environments and large vertical market applications. By allowing access to platform-specific functionality but not requiring that you use it, SWT allows you to choose whether to optimize for a platform that is particularly important to you or to provide the same capabilities everywhere. Look and FeelApplications built with SWT native widgets inherently take on the appearance and behavior (the look and feel) of the platforms they run on because the widget implementations are provided by platform-specific code that the operating system vendors have written. Although the SWT API makes it simple to create applications that run on any supported platform, it is important to test your applications on each platform on which you intend to ship. You should expect the look and feel of the application to change to match the norm for the platform in any or all of the following ways.
In general, platform-specific differences in behavior are handled by the SWT implementation, either by modeling the platform-specific aspects in terms of the portable API (as was done in the Windows radio button selection case described above) or by introducing new abstractions to describe the differences. For example, the SWT.MenuDetect event was introduced to indicate that the user had performed the appropriate action to request a pop-up menu. The sequence of button and/or key presses and releases that are required to do this varies from platform to platform, but portable SWT application code can be written by simply watching for the SWT.MenuDetect event. To handle platform-specific differences in appearance—the size differences, size constraints, and location differences described above—SWT provides a powerful framework for dynamically positioning and sizing widgets based on layouts. By associating a layout with a collection of widgets, it is possible to arrange them in terms of various kinds of abstract constraints. Within these constraints, the layouts and widgets will collaborate to ensure that the expected platform appearance is maintained without requiring platform-specific code to be written. Layouts are described in detail in the Layout chapter. Custom WidgetsDuring the creation of Eclipse, two issues were uncovered that have affected the nature of SWT.
Neither of these issues could be addressed by the native widget-based capabilities of SWT, so a set of support classes, called the custom widgets package, was designed for the specific needs of Eclipse. These classes are implemented entirely in terms of existing SWT API, requiring no new native routines. They can be found in the package org.eclipse.swt.custom. It is important to note that the custom widgets package is not the basis of the Eclipse user interface. Most of what you see in Eclipse—shells, trees, tables, lists, text fields, labels, menu bars and menus, tool bars, dialogs, scroll bars, progress bars, etc.—is created from native widgets. Thus, Eclipse generally takes on the look and feel of the platform. The custom widgets package provides the "icing" on the user interface that makes it distinctly Eclipse. Despite the fact that the custom widgets classes were created for Eclipse, they have been designed to be generically useful. If you are looking for a feature that the native widgets do not provide and can live with the fact that the appearance of the result may not necessarily match the platform look and feel, it is worth checking out the classes in the custom widget package. Some of the things you will find there include the following.
As stated above, the custom widgets package is implemented entirely in terms of the existing SWT API. The widget and event handling support they use is described in the Widgets part of the book. The graphics routines that were used to draw them are covered in the Graphics part. HistorySWT was created by the group within the Rational Division of IBM—then Object Technology International, Inc. (OTI)—that had been responsible for implementing the user interface for IBM's VisualAge/Smalltalk product. Much of the design philosophy of SWT has its roots in the VisualAge/Smalltalk work, so a brief discussion of this history is in order. At the time of VisualAge/Smalltalk's creation, most Smalltalk implementations used user interfaces that were drawn entirely by simple graphics primitives (BitBlt). By doing this, the Smalltalk environments had complete control over the presentation of the user interface and provided what were at the time state-of-the-art capabilities. However, the other side of the coin was that Smalltalk programs did not look or interact like native platform applications. Attempts were made to extend the standard Smalltalk user interface to be "pluggable," so that Smalltalk code could be written to emulate the appearance of native widgets on each platform. These were successful to some extent but never to the point where users were unable to tell the difference. Applications that were built using this user interface failed in at least three ways.
There was a growing sense among Smalltalk developers that these user interface issues significantly limited the acceptance of Smalltalk within the larger software developer community, despite the many advanced capabilities that the environment provided: pure object-oriented programming, garbage collection, pointer safety, cross-platform portability, and integrated development and debugging (with hot code replace support), to name just a few. Solving the problem would require the construction of a Smalltalk user interface that used the native widget code rather than emulating it. The first incarnation of this was done at OTI to implement a simple, native widget-based user interface for the Digitalk Smalltalk/V Mac product. Although the implementation strategy used was closely tied to the Macintosh operating system, and the set of native widgets and their API was limited to the requirements of the existing Smalltalk/V GUI, it clearly showed that building Smalltalk user interfaces from native widgets was practical. Digitalk produced similar native widget-based user interfaces for OS/2 and Windows. The VisualAge/Smalltalk user interface, called CommonWidgets, was implemented using a strategy that was similar to the one used by Smalltalk/V, but it differed from it in at least two interesting ways.
VisualAge/Smalltalk provided excellent portability across all of the supported platforms: Microsoft Windows 3.11, 95, NT3.51 and NT4.0, OS/2 Warp 3.0 and 4.0, and X/Motif. Implementations for MacOS and OpenLook were also started but not completed. At the time, OTI did not have a business case that made it worth the effort to "productize" them. To simplify the task of creating the various non-X/Motif implementations, another layer, called the OSWidget layer, was constructed that more closely matched the programming model of Windows and OS/2. The CommonWidgets API was implemented in terms of this layer, and it in turn was implemented on each of the platforms. By the time Java began its massive rise to fame, VisualAge/Smalltalk was a robust, mature product. However, design decisions that had seemed to be perfectly reasonable at the inception were now definitely showing their age. Most notably, selecting X/Motif as the basis for the API now seemed positively arcane, given that Microsoft Windows had become the overwhelmingly dominant desktop platform. In any case, it was clear that the world was moving to Java. OTI continued to use Smalltalk for its internal development long enough to create the award-winning VisualAge/Java development environment (with a GUI built from CommonWidgets), but beyond that, any new development would almost certainly be done with Java. When work began on the next programming environment from OTI—a tool for developing embedded systems in Java that became IBM VisualAge/MicroEdition—Java was, as expected, chosen as the implementation language. The developers initially built a prototype of the user interface using AWT/Swing but in the process of doing so, they discovered a number of appearance and performance problems. They attempted to work around the problems but eventually came to the conclusion that at that time, AWT/Swing was not ready to be used as the basis for commercial application development. In addition, it seemed that Swing's use of emulated widgets was going to be prone to all of the negatives that the "emulated look and feel" Smalltalk user interfaces had been shown to exhibit. At this point, a question needed to be answered: Would AWT and Swing mature sufficiently quickly that applications built using them would be commercially acceptable by the time VisualAge/MicroEdition went to market? And even if that happened, would an emulated approach effectively enable the implementation of tools that would appeal to demanding developers? After considerable internal discussion, it was decided that a different approach was required. Given the amount of effort that had already gone into CommonWidgets, an obvious choice of action would have been to translate the existing Smalltalk code into Java and take that as the basis for a new toolkit. However, using X/Motif as the model for an API no longer seemed reasonable. The OSWidget layer that had been used to provide portability between the various non-X/Motif implementations was equally powerful and had a simpler API. This, then, filtered through the constraints of the new programming language and with the opportunity taken to clean up a few of the suboptimal design decisions but keeping the original philosophy and overall structure, became the basis for SWT. Since then, SWT has continued to advance, with the conversion to open source; its use by Eclipse and the addition of the custom widgets package; new ports available for Windows CE, GTK 2, and Macintosh OS X; and many other new features. Despite the new capabilities, the philosophy remains the same: Make the platform capabilities available, keep the layers as thin as possible, and give people the tools they need to make real platform applications in a portable way. |
|
|
< Day Day Up > |
|