next up previous contents
Next: Saving and Loading, Exporting Up: Editing Previous: Attribute Customize Editor   Contents


An example for a user-defined Java class

The HTML Browser grammar is an example for visualizing a directory tree and browsing HTML files. This grammar performs a recursive scan of a directory of your filesystem for HTML files and represents the directory tree as a graph. Afterwards, it starts up an HTML browser to inspect the files that have been found. The files already visited are marked in the graph. This example demonstrates how you can access system resources like the file system using user-defined Java classes and their methods in graph rules. It also shows how you can control an external Java application (here: the browser) by graph transformation rules.
Figure 8 shows rule initDir being applied to the start graph of grammar HTML_Browser. The rule launchs a dialog asking for the directory to browse.

Figure 8: The initDir rule
\begin{figure}\def
\epsfsize  ...

Here the user-defined Java class Entry (Entry.java) is used for attribute name in the right-hand side of the rule.

This class defines operations for:

Also rules showDir, prepare2ndPath and showFile invoke methods of this class.
Rule openBrowser in Figure 9 uses another user-defined Java class Browser (Browser.java) to create a file browser.

Figure 9: The openBrowser rule
\begin{figure}\def
\epsfsize  ...

Finally, rule viewPage in Figure 10 uses class Browser to view a HTML file. Please pay attention to the attribution of ref in the Browser node in the RHS of the rule. The method setPage of class Browser returns no object (is void) but we want to return. In this case we use $;$ (semicolon) to return the changed Browser object ( $br;setPage(''file:''+page.toString())$ ).

Figure 10: The viewPage rule
\begin{figure}\def
\epsfsize  ...

Please note: A user-defined class has to contain the default class constructor without parameters. If a class rewrites the default class constructor, the first line has to contain super(); entry. Moreover, setter and getter methods have to be defined for class parameters like this: for a parameter with the name abc the setter and getter methods will get the names setAbc(...) and getAbc(). The method toString() can be rewritten to get a sufficient string representation of this class.


next up previous contents
Next: Saving and Loading, Exporting Up: Editing Previous: Attribute Customize Editor   Contents
Olga Runge 2006-08-16