SOURCECODE |
How to... define a MenuSheet
Description:
A MenuSheet displays a menu, either as a JMenuBar, a JMenu or a JPopUpMenu.
It is important to know that a MenuSheet can be displayed in both ways, horizontally and vertically. In the first case you see the added MenuSheetItems as dropdownmenues (like File, Edit, Help) or submenues (like About) and in the other case as the menuItems in the submenus (like new, load, save).
You can define MenuSheets wherever you need them / want them to be. But in most cases you might want them to be just a Menubar at top of the Shop window or the ones of the Salespoints. To put them right where they belong, you overwrite the method getDefaultMenuSheet() of your Salespoint or the createMenuSheet() method of your Shop
As mentioned above, the MenuItems or MenuSheets contained by the first defined MenuSheet will be displayed horizontally. All the other MenuSheets and Items appear in the dropdown menu of the first MenuSheet. You can "group" the Items with MenuSheetSeparators.
If you want to improve the look of your menu, feel free to use JComponents. For further explanations on JComponents in menus try "Using Swing Components" of java.sun.com.
Adding JComponents to a MenuSheet is not as easy as adding framework MenuItems, because you can only add MenuSheetObjects with the add method. To add a JComponent anyway, you have to acces the JMenuBar in which the MenuSheet is being displayed by calling the getMenuBar method. To this you add the JMenu with all the JMenuItems like CheckBoxes or RadioButtons added to it and which you initialize before.
ToDo's:
I- Locate the method needed. Depending on the FrameworkClass you are working on, it is getDefaultMenuSheet in Salespoint or createShopMenuSheet in the Shop-class.
- Initialize the toplevel-MenuSheet (the one displayed horizontally)
- Initialize another MenuSheet (will be a dropdown-menu)
- Create the needed MenuItems with the referring action and add them to the dropdown-MenuSheet
- add the dropdownMenuSheet to the toplevel-MenuSheet
For more dropdown-menues, begin with 3 again.
In order to add a JComponents to the menubar do the following:
II- After initializing the toplevel-MenuSheet initialize a JMenu (remember to import javax.swing)
- Initialize the JMenuItem and add it to the JMenu
- Get the JMenuBar the toplevel-MenuSheet is being displayed in by calling getMenuBar() and add the JMenu to it
Don't forget to return the finished toplevel-MenuSheet at the end of the method.
Uses:
Shop SalesPoint MenuSheet MenuSheetItem MenuSheetSeparator
import sale.*;
import sale.events.*;
import sale.stdforms.*;
import data.events.*;
import data.ooimpl.*;
import log.*;
import log.stdforms.*;
import users.*;
import users.swing.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;
//...
I.1
//in order to define a new MenuSheet in a SalesPoint you have to
//overwrite the getDefaultMenuSheet method
public MenuSheet getDefaultMenuSheet() {
I.2
//create a new MenuSheet to be the horizontal menu
MenuSheet msMenu = new MenuSheet("office menu");
I.3
//create a new MenuSheet to be a submenu
MenuSheet msSubMenu = new MenuSheet("Maintenance");
I.4
//create a new MenuSheetItem to be displayed in the submenu above
MenuSheetItem msi1 = new MenuSheetItem ("Advance time", //the caption
new sale.Action() {
//the action to be performed when the MenuSheetItem is being chosen
public void doAction(SaleProcess p, SalesPoint sp) {
Shop.getTheShop().getTimer().goAhead();
}
});
//add the MenuSheetItem to the msSubMenu
msSubMenu.add(msi1);
I.4
//create a new MenuSheetItem to be displayed in the submenu
MenuSheetItem msi2 = new MenuSheetItem (
"See log file", //the caption
new sale.Action() {
//the action to be performed when the MenuSheetItem is being chosen
//as you can see, such an action can also be quite complex and
//even contain FormSheets and dialogs
public void doAction (SaleProcess p, SalesPoint sp) {
try {
//create a FileInputStream
FileInputStream fis = new FileInputStream ("machine.log");
//and finally have the stream being displayed on the LogTableForm
LogTableForm ltf2 = new LogTableForm ("View log file", lis2);
//no logfile specific action. just a removal of the cancelbutton
ltf2.removeButton (FormSheet.BTNID_CANCEL);
//setup the window with the LogTableForm
setFormSheet (null, ltf2);
2
//hand it over to a LogInputStream
LogInputStream lis = new LogInputStream (fis);
//and finally have the stream being displayed on the LogTableForm
LogTableForm ltf = new LogTableForm ("View log file", lis);
//no logfile specific action. just a removal of the cancelbutton
ltf.removeButton (FormSheet.BTNID_CANCEL);
//setup the window with the LogTableForm
setFormSheet (null, ltf);
//looks like lots of exceptions to be caught,
//but only the three of them refer to the InputStreams,
//the InterruptedExceptions (except number 8) belong to the setFormSheet(...) method
//this
} catch (FileNotFoundException fnfexc) {
//this
try {
setFormSheet(null, new MsgForm("Error", "Log file not found."));
} catch (InterruptedException iexc) {
System.out.println(iexc);
}
//this
} catch (IOException ioexc) {
try {
setFormSheet( null,
new MsgForm("Error",
"Log file corrupt. It might be empty."));
} catch (InterruptedException iexc) {
System.out.println(iexc);
}
//and this
} catch (InterruptedException iexc) {
try {
setFormSheet (null, new MsgForm ("Error", iexc.toString()));
} catch (InterruptedException iexc2) {
System.out.println(iexc2);
}
}
}
});
//add the MenuSheetItem to the msSubMenu
msSubMenu.add(msi2);
I.4
//create a MenuSheetSeperator
MenuSheetSeparator mss = new MenuSheetSeparator();
msSubMenu.add(mss);
I.4
//create a new MenuSheetItem first
MenuSheetItem msi3 = new MenuSheetItem ("Edit Video-Stock", new sale.Action() {
public void doAction(SaleProcess p, SalesPoint sp) {
sp.runProcess(new SeeVideoStockProcess());
}
});
//and then add it to msSubMenu
msSubMenu.add(msi3);
I.4
//add a new MenuItem to msSubMenu
msSubMenu.add(new MenuSheetItem ("Edit Customers", //caption
new sale.Action() { //action to perform
public void doAction(SaleProcess p, SalesPoint sp) { //method of the new action
sp.runProcess(new EditCustomersProcess());
}
}));
I.4
//add a new MenuItem to msSubMenu
msSubMenu.add(new MenuSheetItem ("See Videos", //caption
new sale.Action() { //action to perform
public void doAction(SaleProcess p, SalesPoint sp) { //method of the new action
sp.runProcess(new SeeVideos());
}
}));
I.5
// add the msSubMenu to the first MenuSheet
msMenu.add(msSubMenu);
II.1
//create a new JMenu where we display some JMenuItems
//remember, they cannot be added to normal MenuSheets
JMenu jm = new JMenu("JMenuItems");
II.2
//here we try a JCheckBoxMenuItem with just a caption, but it is also combinable
//with an Icon and of course an action
//remember, JMenuItems belong to the javax.swing package
JCheckBoxMenuItem jcb = new JCheckBoxMenuItem("JCheckBox");
//add the CheckBox to the JMenu
jm.add(jcb);
II.2
//you can also add a JRadioButton, here again with just a caption
JRadioButtonMenuItem jrmi = new JRadioButtonMenuItem("JRadioButtonMenuItem");
//add it again to the JMenu
jm.add(jrmi);
II.3
//add the JMenu to the msMenu
//remember to add it to the JMenu which displays the horizontal MenuSheet
//in order to create an effective menu, you may also group items like RadioButtons
//for further information refer to the tutorial mentioned above
msMenu.getMenuBar().add(jm);
//return the first MenuSheet
return msMenu;
}