|
Inside RelativeLayoutby James Elliott, coauthor of Java Swing, 2nd Edition11/27/2002 |
As promised in my first article, "RelativeLayout: A Constraint-Based Layout Manager," here's a look inside the RelativeLayout package. This article explains how the layout manager works, and discusses how to extend it to support new kinds of constraints. Readers should be familiar with the original article, which introduces RelativeLayout and explains how to use it as a tool.
What's Inside?
Once you download and expand the source archive, you'll find the following items inside of it (Figure 1 shows everything it will contain once you're ready to build and run RelativeLayout):

Figure 1: RelativeLayout Source
The File build.xml
This is a build file for the Ant tool from Apache's Jakarta project. It is used to compile and test RelativeLayout. Once you have installed Ant on your system (which you have likely done already, since it has rapidly and deservedly become the build tool of choice for Java projects) you can compile RelativeLayout simply by moving to the top-level source directory and typing ant compile (after you've set up the lib directory as described below).
Other interesting build targets you can run include:
ant ex1: runs the first example program discussed in the first article. Similarly, the targetsex2andex3run the second and third examples.ant doc: builds the JavaDoc forRelativeLayout. You may want to refer to this documentation from time to time as you read the overview of how the classes work, below.ant dist: builds the distribution fileRelativeLayout.jarso you can easily useRelativeLayoutwith other projects.ant clean: cleans up any generated files and removes thedocandclassesdirectories.
The Files example2.xml and example3.xml
These files are used by the XML-based examples in the first article. They contain the layout constraints used by the second and third example programs.
The Directory lib
Contains libraries used by RelativeLayout. It's empty when you first download and expand the source archive, because these libraries are available from separate organizations. In order to compile and use RelativeLayout, you'll need the JDOM library and (if you're using a Java SDK earlier than version 1.4) an XML parser such as Apache Xerces, as discussed in the first article. Once you've downloaded any libraries you need (which you likely did in order to run the examples when reading Part 1), copy their library jars (e.g. jdom.jar and xerces.jar) into the lib directory, and RelativeLayout will compile and run properly.
The File test.xml
I used this file along with a test program while I was developing RelativeLayout. It's not too useful now, unless you want to study and play with that test program. Note that the current configuration of the program (invoked through ant test) and this file are inconsistent and cause an over-constraint error to be reported. If you're into that sort of thing, debugging and fixing the problem could be an interesting exercise.
The Directory src
The rest of the source is organized under the src directory, so let's move in there and see what we find.
|
Related Reading
Java Swing |
- The files
src/Example1.java,src/Example2.java, andsrc/Example3.javaThese are the three example programs discussed in Part 1.
The File
src/Test.javaThis is the test program that works with
test.xmlas described above. It's no longer of much interest except for software archaeology, in that it provides a little insight into the development of the package.The File
src/overview.htmlThis package overview document is used by JavaDoc to provide introductory information on the starting page.
The Directory
src/comThe Java source for
RelativeLayoutitself is grouped under this directory. To be precise, it's in the nested directory src/com/brunchboy/util/swing/relativelayout, corresponding to the package in which the classes themselves are organized,com.brunchboy.util.swing.relativelayout. The classes that make upRelativeLayoutare explained in the next few sections. You'll best understand how everything works if you can examine the source itself while you read the descriptions below, perhaps by printing one or the other.The
relativelayoutdirectory also contains the filepackage.html, used by JavaDoc to provide an introductory explanation for the classes in the directory, andconstraint-set.dtd, the XML document type definition (described below), used byXmlConstraintBuilderto parse constraint specifications expressed as XML.
