CS 3340 OOP
Exercise 7c: Term Project: Recipes: GUI Model-View-Controller
Chapter Readings: TIJ [14]

Goal: To build a GUI for the recipe application using the Model-View-Controller architecture.


Tutorial

Some source code has already been provided:

Source Purpose
planner/Planner.java To run PlannerApplet as an application
planner/gui/PlannerApplet.java The GUI of Buttons, Fields, etc. (this Ex7c)
planner/gui/domain/MenuBook.java The first Domain Logic class, seen in Ex7a
Planner.txt View source of html file
Planner.html To run PlannerApplet from a browser - TRY ME!

If you run Planner.html, you will see a GUI come up with just a few "Widgets". If the GUI doesn't come up, then your browser doesn't have access to Java 1.5 (you can verify this by selecting Java console in the Tool menu).

The source code has been placed in Java packages in a hierarchical structure: planner/gui/domain.

Here is a: Java Packages Tutorial and another Tutorial.

and the specific package assignments for the recipe application: ex7c1.pdf

To build the recipe application, we're going to divide it into Domain Objects (our business records as seen in Ex7a), GUI (this Ex7c), and Domain Logic (which responds to events such as Button pushes as seen in Ex7d). This particular division is called the Model (Domain Objects) - View (GUI) - Controller (Domain Logic).

Here is a MVC tutorial.

In Java, the View and the Controller are often put together in the Applet/Frame: ex7c2.pdf, and that is what you will do in this project.

To understand how to build your GUI, you will need the following Widgets. You do not need to read this in detail now but you can use it as a resource as necessary. But these will be the exact building blocks you will use.

Widget                     Help                   
JApplet API
  Tutorial
JLabel API
  Tutorial
JTextField API
  Tutorial
JTextArea API
  Tutorial
JButton API
  Tutorial
JComboBox API
  Tutorial
JRadioButton API
  Tutorial
ButtonGroup API
  Tutorial

In order to position any Widgets on a screen, you use a Layout Manager. Java comes with many Managers and the major ones are listed below. In this project, you will only use the GridBagLayout but you are responsible for understanding - at a conceptual level - the other Layout Managers.

Layout Manager                    Help                   
FlowLayout API
  Tutorial
BorderLayout API
  Tutorial
GridLayout API
  Tutorial
GridBagLayout API
  Tutorial
BoxLayout API
  Tutorial
Absolute Positioning No Layout Manager
  Tutorial

An absolute positioning demo [push the Test Button] and the source code.

Your project will use GridBagLayout: planner/gui/PlannerApplet.java

This JApplet shows how do to one JLabel, one JTextField, one JTextArea, one JButton, one JComboBox, one JRadioButton, one ButtonGroup, and one GridLayout. You will have to add more Widgets to your JApplet and place them in a good location using GridBagLayout.


SPECIAL NOTE ON ENVIRONMENT:

Options a. and b. below are obsolete. So do Option c: Run on your PC.

a. If you run on your Unix account, and are sitting in front of a Unix machine in the Sun Lab, the GUI will display.

b. If you run on your Unix account, but you have done an SSH from home, then the GUI will NOT display. An error will arise:

"Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable."

c. If you run your Java program from your PC - say from Netbeans - then the GUI will display.

d. If you want to run on your Unix account - from home - then you then you need to run an X11 server so that your home PC looks like a Unix terminal. This describes how X11 can tunnel through your SSH connection:

http://www.uic.edu/depts/accc/software/exceed/sshexceed.html

This is one possible site for a free X11 server for Windows:

http://freedesktop.org/wiki/Xming

There are some other details: turn on X11 packet forwarding and run "xterm &" at the command prompt.

Bottom Line: best to run from your home PC (option c).


Assignment

Your current assignment is to populate your JApplet with the necessary Widgets. Below is a table of the necessary actions your application must perform. Each of these 10 actions is initiated with its own JButton. Place these on the GridLayout. In addition, the Actions suggest other JComboBoxes, JTextFields, etc. that you will need. Create and populate these Widgets.

Your JApplet does not have to actually perform these Actions yet, just display the necessary Widgets. The Actions will be implemented in Ex7d: Domain Logic

It is a good idea to actually layout a grid of rows and columns on paper, and write-in your Widgets.

Button Action
Show Book Display the entire MenuBook to the TextArea
New Menu TextField specifies new menu name, create Menu, add to ComboBox
  RadioButton specifies BREAKFAST, LUNCH, DINNER
Remove Menu ComboBox specifies old menu name, remove Menu from MenuBook, HashMap, ComboBox
Show Menu ComboBox specifies menu name, get from HashMap, display to TextArea
New Recipe similar to New Menu
Show Recipe similar to Show Menu
Add Recipe ComboBox specifies recipe name, get from HashMap
  ComboBox specifies menu name, get from HashMap, add Recipe to Menu
Remove Recipesimilar to Remove Menu but remove from specified Menu
Add IngredientComboBoxes specify Recipe, Item, Unit, get from respective HashMaps
  TextField specifies quantity of Unit, convert to Double
  add new Ingredient to Recipe
Add Step ComboBox specifies recipe name, get from HashMap
  TextField specifies Step text, add new Step to Recipe

Your GUI does not need to be able to remove Ingredients or Steps from a Recipe. Nor does it need to be able to make new (or remove) Units or Items, these can be created in MenuBook's constructor. Note: Although ComboBox for, say existing recipe names, is mentioned multiple times, you would only need one such ComboBox. And each ComboBox needs to be populated by the data from the associated HashMap.

Be sure that planner/Planner.java has a setSize() large enough to accomodate all of your widgets.

Also, you might need to examine your copy of planner/gui/PlannerApplet.java. The original has been modified to get rid of "narrow" fields:

//FIND THIS LINE OF CODE:
constraints = new GridBagConstraints();
//NOW ADD THIS NEXT LINE OF CODE:
constraints.fill = GridBagConstraints.HORIZONTAL;

This table summarizes the widgets. The grade will be partially based on appearance/layout/presentation.

Widget Quantity Purpose
JButton 10 actions (Show Menu, ...)
JTextField 4 menu, recipe, step, quantity (be sure to give default width)
JComboBox 4 menu, recipe, item, unit
JLabel 6? to help organize areas
JTextArea 1 to append strings
JRadioButton 3 breakfast, lunch, dinner
ButtonGroup 1 to add radio buttons
GridBagLayout 1 to manage widgets
GridBagConstraints1 position/scale widgets
JPanel 1 to accept scroll
JScrollPane 1 to accept text area

After editing PlannerApplet.java, compile and test:

cd ex7
rm planner/*.class
rm planner/gui/*.class
rm planner/gui/domain/*.class

javac planner/Planner.java
java planner/Planner

Note that it is necessary to be one directory above planner in order to do the compilation. This is because of the package structure. The alternative is to place the planner directory into your CLASSPATH (as suggested by the Java Packages Tutorial). If you do that, then you can be on any directory to compile.

Turn-In: Entire planner directory.


ex7a ex7b ex7c ex7d