Having defined our business objects we should set up some icons and
write a simple class that will make these classes available to the user.
The icons are used to represent our objects on-screen and are just
images, stored in files, that the framework can display. Each image file
name is based on the name of the class and the file itself must be
located in a directory called images and should have an
extension of .gif.
Configuration
To set up the icon images for this application, create a
directory in the project directory called images.
Then copy six suitable images, from the icon-library
directory in the nakedobjects0.8.0 directory, into the new
images directory and rename them. The filenames given, must
correspond to the names of the classes with an image size appended
(16 or 32 pixels).
The suggested images are HouseUnderConstruction,
Hammer, and Man. These should be renamed as
Project, Role, and Employee
respectively. For example Hammer16.gif will become
Role16.gif and Hammer32.gif will become
Role32.gif.
The window below shows the new directory with the six new image files
within it.
In Unix the cp command can be used to copy and rename
each image file, as shown below:
mkdir images
cd /nakedobjects0.8.0/icon-library
cp HouseUnderConstruction16.gif /project/images/Project16.gif
cp HouseUnderConstruction32.gif /project/images/Project32.gif
cp Hammer16.gif /project/images/Role16.gif
cp Hammer32.gif /project/images/Role32.gif
cp Man16.gif /project/images/Employee16.gif
cp Man32.gif /project/images/Employee32.gif
The test application
To show these classes in a test environment we need to write a simple
class that will load the framework, set it up, and then make the classes
available to the user. Please note that this class is used only to test
our application; when we make the application available to the end user
it is setup differently.
The following class is used to load our three classes into the
framework, which will then make them available to the user.
Run.java
import org.nakedobjects.Exploration;
import org.nakedobjects.object.NakedClassList;
public class Run extends Exploration {
public static void main(String args[]){
new Run();
}
public void classSet(NakedClassList classes){
classes.addClass(Project.class);
classes.addClass(Role.class);
classes.addClass(Employee.class);
}
}
Compilation
To compile both the business object classes and the
Run class we need to invoke the Java compiler, specifying
the framework library (nakedobjects.jar) in the classpath.
(Remember that the directory name used, due to the version, could be different.)
To do this under Windows, start an MS-DOS window and enter the commands as shown below.
After this the project directory should contain the
.java and .class files and the
images directory as shown below.
In Unix the command is very similar:
javac -classpath /nakedobjects0.8.0/lib/nakedobjects.jar:. *.java
Listing the files shows the same set as in Windows:
Employee.class
Employee.java
Project.class
Project.java
Role.class
Role.java
Run.class
Run.java
images
Starting the application
To run the application (the Run class) invoke the Java
Virtual Machine (JVM) from within the project directory.
The classpath must refer to the framework, the Apache Log4j library and
the current directory where your class files resides. For Windows:
java -classpath
c:\nakedobjects0.8.0\lib\nakedobjects.jar;c:\nakedobjects0.8.0\lib\log4j.jar;.
Run
(This command should all be on a single line.)
And in Unix:
java -classpath
/nakedobjects0.8.0/lib/nakedobjects.jar:/nakedobjects0.8.0/lib/log4j.jar:.
Run
(This command should all be on a single line.)
A small window will appear with the three classes showing. The
following screenshots show these classes in use.
1. The classes window pops up showing the three classes we
specified:
2. Right-clicking the Employees class and selecting
New Employee... creates a new employee object:
3. Entering a name into the Name field gives the
object a title. The title, as defined by the title()
method returns the value of the Name field:
4. Similarly, right-clicking the Roles class and
selecting New Role... creates a new role object:
5. Entering a name into the Name field also gives the
Role object a title:
6. Now dragging the employee John Barry onto the
Employee field sets up a relationship between the two
objects: the role knows the employee:
7. As discussed a project is the normal starting point and here one
has been created and a name assigned:
8. Dragging the Programmer role onto the
Roles field adds our programmer to the project:
9. Right-clicking on the Project object shows the
action we added: Add Project Leader...:
10. Selecting that option creates a new Role object,
labels it 'Project Leader' and adds it to the Roles
field:
11. The same pop-up menu now shows the option as disabled as the about
method found a role containing 'project leader':
12. The final state of the project once all the roles have been
added:
|