|
Compiling and Running
To compile and run the examples and your own applications, the following jar files must be added to the classpath:
- smaker.jar,
- only for JDK 1.3: the JAXP archives, see Installation
- the directory containing the XML layout files, but only if the SMaker method load(String s) is used,
- the directory and/or jar file(s) containing images,
- and if a look and feel is used that is not included in JDK: the jar files of that look and feel.
Loading the Layouts
Working with XML files
To make the screen layouts available to a Java application, the XML files are loaded by the XMLSMaker. Create an instance of XMLSMaker for each XML file you want to load.
XMLSMaker smaker = new XMLSMaker();
smaker.load("JExample1and3.xml");
There are 3 methods to load a file, each of them having just one parameter. If the parameter is a string like in the above example, the file is searched in the classpath. The other two methods have a parameter of type File and URL to be able to load the XML file from any place in the filesystem or from the Web.
Working with Java files
The SEditor also is able to generate Java files which may be loaded instead of the XML files. The file name and the class name of such an export will be "SMJ" + (xml-filename lowercase) + ".java". For example, the class name for JExample1and3.xml will become SMJjexample1and3. Ensure that this will be a valid Java identifier, so do'nt use filenames like "A+B.xml"! The generated code (don't care about details, it mainly contains an array) will be similar to the following:
package mypackage; // inserted by the developer
import vnas.smaker.*;
public class SMJjexample1and3 implements INodeArray {
public Object[][] getArray() { return DEF; }
private static Object[][] DEF = {
{ "-1","notesFrame","frame", AdTframe.getInstance()
,"dim","300,300","id","notesFrame","text","Example 1"
},
...
Copy the Java file into a package directory of your application, add a package statement to the code and compile it together with your application source files. The code lines below show how to integrate the generated class into the application:
INodeArray nodeArray = new SMJjexample1and3();
JavaSMaker smaker = new JavaSMaker();
smaker.load(nodeArray);
If you prefer to work with generated Java code instead of XML files, an instance of the JavaSMaker class must be allocated. The XMLSMaker and the JavaSMaker are both instances of the ISMaker interface, so the following programming steps will be the same for both.
Accessing the Swing and AWT Objects
After loading the XML or Java layout into the ISMaker object, a creation step is required to construct the GUI components. After this step the SMaker object may be regarded as a collection of GUI components.
XMLSMaker smaker = new XMLSMaker();
// Loading
smaker.load("JExample1and3.xml");
// Now invoking constructors:
smaker.create();
The GUI components are now ready for use and can be retrieved by the getTarget() method:
JFrame frame = (JFrame)smaker.getTarget("notesFrame");
For the retrieval the component id ("notesFrame") is used as a key. When designing the layout in SEditor, this id must be specified (as the "id" property) in the Property Dialog.
As soon as a reference to the component is available, the usual Swing/AWT programming can be done. For example, set the frame to visible, add listeners etc. Usually, properties like size, colors, and others are already specified in the SEditor but there is no problem to change these settings in the application by calling methods of the component. In addition to this, those properties which are not supported by the editor can be set in the application at any time.
Multiple Layouts
An XML file (or its Java equivalent) may contain more than one layout (for example a login dialog, an address panel etc.). In a first step such a file is loaded in the same way as a single layout file by calling the load() method of XMLSMaker or JavaSMaker. In the next step, for single layout files the create() method will be called which instantiates all components contained in the ISMaker object. However, for multiple layout files it's not necessary and not recommended to instantiate all components of all panels at the same time. Use sub-SMaker objects instead as shown in the following example:
// Load the XML-File into the SMaker
smaker.load("myApplication.xml");
// Get the child SMaker for the main JFrame
ISMaker smakerFrame = smaker.newSMaker("mainFrame");
// Get the child SMaker for the login dialog content pane
ISMaker smakerLogin = smaker.newSMaker("loginDialog");
// Get the child SMaker for the address panel
ISMaker smakerAddress = smaker.newSMaker("addressPanel");
These sub-SMaker objects are obtained from the parent SMaker by the newSMaker() method. As a parameter, the id of the subtree root ("mainFrame", etc.) is used as a key. For each of the sub-SMakers, the create() method must be called only before the references to the Swing/AWT objects are needed.
For a better understanding of this concept, please look at JExample2.java.
Integration of Specific GUI Components
Some applications are working with GUI components that are closely bound to their "environment" and cannot exist outside, for example in the environment of the SEditor. Such specific components are for example subclasses of Swing components working with data structures that only available in the application but not to the SEditor.
Example:
Investments investments = ......
InvestmentTree tree = new InvestmentTree(investments);
Assume that the InvestmentTree is a subclass of JTree. For this specific tree, use a JTree node as a substitute in the SEditor and replace it in the application by the InvestmentTree, before the construction of the GUI components, this means before calling the SMaker create() method. Example:
XMLSMaker smaker = new XMLSMaker();
smaker.load("investments.xml");
Investments investments = ......
InvestmentTree tree = new InvestmentTree(investments);
// replace the "investTree" node
smaker.setTarget("investTree", tree);
// construct the Swing components
smaker.create();
See also JExample3.java for a complete example of this component replacement.
|