Become a programming Picasso with JHotDraw

Use the highly customizable GUI framework to simplify draw application development

Page 3 of 3
private void readObject(ObjectInputStream s)
    // call superclass' private readObject() indirectly
    s.defaultReadObject();
    // Create popup menu after it has been read
    setAttribute(Figure.POPUP_MENU, createPopupMenu());
}

Moreover, reading and writing drawings with their contained graphical figures can also simply use the serialization mechanism. JHotDraw also has its own mechanism for storing objects to disk and restoring them. Each figure implements the CH.ifa.draw.util.Storable interface and provides its own implementation of how to write itself to disk and reread from it. The information written contains the class name and all attribute values as strings. Obviously, the order in which attribute values are written and read is important. With the class name, JHotDraw creates a new Drawing instance using Java's reflection mechanism. In this case, the default constructor with an empty parameter list is called.

The main() routine

Finally, the class diagram editor must be instantiated and the editor window opened. This is done in the main() method of JModellerApplication, which is derived from the skeleton application with support for several internal frames:

/**
 * Start the application by creating an instance and open
 * the editor window.
 */
public static void main(String[] args) {
    JModellerApplication window = new JModellerApplication();
    window.open();
}

Using JModeller

Using JModeller should be rather straightforward. There are only a few obstacles that require a short description. First, each ClassFigure has a context menu that lets you add attributes and methods. By double-clicking on the class name, an attribute, or a method, you can edit the text. An empty attribute or method name deletes an attribute or method, while an empty class name deletes the whole class. The AssociationLineConnection also has a context menu, which changes the association into an aggregation and switches between bidirectional and unidirectional associations. You can attach text comments to any class relationship with the help of the ConnectedTextTool. You can insert additional handle points into any class relationship line by clicking on the connection line with the appropriate connection tool selected. Finally, make sure that jhotdraw.jar is included in your classpath when you compile or run JModeller. For example, under Windows you can compile JModeller with the following command line:

javac -classpath "%CLASSPATH%;<JHotDraw-Directory>/jhotdraw.jar;." *.java

You can start it with:

java -classpath "%CLASSPATH%;<JHotDraw-Directory>/jhotdraw.jar;." JModellerApplication

<JHotDraw-Directory> must be replaced with the path in which you unzipped JHotDraw.

Figure 9. Drawing a class diagram with JModeller. Click on thumb- nail to view full-size image. (13 KB)

Conclusion

You must understand a framework to use it correctly. Design patterns are a way of achieving a highly reusable software architecture, and are also very suitable for documenting that architecture in a language intelligible to other developers. JHotDraw is a good example of a framework that is composed of fundamental design patterns, such as Composite, State, Template method, Factory method, and Strategy. Knowing the basic concepts behind those patterns, and their applications within JHotDraw, helps you decide how to customize and adapt the framework to meet your application's requirements. Although learning a framework requires extra effort and may initially slow you down, a framework like JHotDraw often reduces development time and improves the quality of your software. Of course, JModeller is just a basic example of JHotDraw's capabilities, but I hope it will serve as a starting point for your own hands-on experience.

JHotDraw has evolved into an open-source project. From this project, you can download the latest releases and contribute to the project as well: http://jhotdraw.sourceforge.net

Wolfram Kaiser has a master's degree in human-computer systems. At Poet Software, Wolfram works on Poet's Java database binding and combines his interest in object-oriented techniques like frameworks and design patterns with technology like XML and Java. One of his research projects required the porting of JHotDraw to JFC Swing.

Learn more about this topic

| 1 2 3 Page 3