CIT 597 Assignment 8: GUI Generator
Fall 2007, David Matuszek

Purposes of this assignment:

General idea of the assignment:

Read in an XML file that describes a GUI, and generate a Java program that implements that GUI.

Details:

Your program will implement only a very limited set of GUI features. Specifically, it will create a class that extends JFrame, and within that class you may have:

All of the above will be in the constructor for the class. In addition, your program should create a main method to create the GUI, make it visible, and provide a means to quit the program. The purpose of this main method is to simplify testing.

The DTD:

Use this DTD. I believe it is correct; let me know if you find any problems with it.

<!ELEMENT JFrame (JMenuBar?, JPanel+)>
<!ELEMENT JMenuBar (JMenu+)>
<!ELEMENT JMenu (JMenuItem+)>
<!ELEMENT JMenuItem EMPTY>
<!ELEMENT JPanel ((JPanel|JTextArea|JButton)+)>
<!ELEMENT JTextArea EMPTY>
<!ELEMENT JButton EMPTY>

<!ATTLIST JFrame name CDATA #REQUIRED
layout (flow|grid|border) "border"
rows CDATA #IMPLIED
columns CDATA #IMPLIED>

<!ATTLIST JPanel name CDATA #REQUIRED
position CDATA #IMPLIED
layout (flow|grid|border) "border"
rows CDATA #IMPLIED
columns CDATA #IMPLIED>

<!ATTLIST JTextArea name CDATA #REQUIRED
position CDATA #IMPLIED
rows CDATA #REQUIRED
columns CDATA #REQUIRED>

<!ATTLIST JMenuBar name CDATA "myMenuBar">
<!ATTLIST JMenuItem name CDATA #REQUIRED>
<!ATTLIST JMenu name CDATA #REQUIRED>

<!ATTLIST JButton name CDATA #REQUIRED
position CDATA #IMPLIED
text CDATA #REQUIRED>

In the above, a JPanel will use a position of NORTH, SOUTH, EAST, WEST, or CENTER if it is inside a JFrame or JPanel that uses a BorderLayout manager. If a JFrame or JPanel specifies grid as a layout, it will use the rows and columns attributes, but not otherwise. These have to be optional (#IMPLIED) because DTDs are too weak to require them in some places and disallow them in others.

The XML:

Here is an example XML file for creating a GUI. Your program may be tested with a different XML file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE JFrame SYSTEM
 "http://www.cis.upenn.edu/~matuszek/cit597-2007/Examples/gui.dtd">
<JFrame name="SimpleTranslatorGui" layout="border">
<JMenuBar name="myMenuBar">
<JMenu name="File">
<JMenuItem name="Load..."/>
<JMenuItem name="Save As..."/>
</JMenu>
</JMenuBar>
<JPanel name="mainPanel" position="CENTER"
layout="grid" rows="2" columns="1">
<JTextArea name="inputArea" rows="10" columns="30"/>
<JTextArea name="outputArea" rows="10" columns="30"/>
</JPanel>
 <JPanel name="controlPanel" position="SOUTH" layout="flow">
<JButton name="translateButton" text="Translate"/>
<JButton name="quitButton" text="Quit"/>
</JPanel>
</JFrame>

The result:

Important note: The following is not a result of running my version of this program (because I haven't written it yet). Instead, it's my impression of the kind of result I expect. Your generated program may vary considerably from this, but it should behave the same.

import java.awt.*;
import javax.swing.*;

public class SimpleTranslatorGui extends JFrame {

    public SimpleTranslatorGui() {
        setLayout(new BorderLayout());
        
        JMenuBar myMenuBar = new JMenuBar();
        setJMenuBar(myMenuBar);
        
        JMenu menu1 = new JMenu("File");
        myMenuBar.add(menu1);
        
        JMenuItem menuItem1 = new JMenuItem("Load...");
        menu1.add(menuItem1);
        
        JMenuItem menuItem2 = new JMenuItem("Save As...");
        menu1.add(menuItem2);
        
        JPanel mainPanel = new JPanel();
        add(mainPanel, BorderLayout.CENTER);
        mainPanel.setLayout(new GridLayout(2, 1));

        JTextArea inputArea = new JTextArea(10, 30);
        mainPanel.add(new JScrollPane(inputArea));

        JTextArea outputArea = new JTextArea(10, 30);
        mainPanel.add(new JScrollPane(outputArea));

        JPanel controlPanel = new JPanel();
        add(controlPanel, BorderLayout.SOUTH);
        controlPanel.setLayout(new FlowLayout());

        JButton translateButton = new JButton("Translate");
        controlPanel.add(translateButton);

        JButton quitButton = new JButton("Quit");
        controlPanel.add(quitButton);

        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SimpleTranslatorGui gui = new SimpleTranslatorGui();
        gui.pack();
        gui.setVisible(true);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Hints:

Use Swing's JFileChooser to select the input file to be read and the output file to be written.

This type of program can be very long, or you can come up with some good methods that do most of the work for you. For example, you could easily write a method that generates (that is, writes all the code for) the main method; all it needs is a String parameter that tells the name given to the JFrame.

Due date:

Monday, November 26, before midnight. Zip the folder containing your complete program, including javadoc documentation, and submit via Blackboard.