HowTos - Display: MenuSheet

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 dropdownmenus (like File, Edit, Help) or submenus (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.

Used classes:

Related topics:

ToDo:

  1. Locate the method where you want to define the MenuSheet. In this case a SalesPoint is chosen and its getDefaultMenuSheet() method overwritten.
  2. Create the top-level MenuSheet, it's the one that will be displayed horizontally and where all other MenuSheets in this case will be added to.
  3. Create another MenuSheet, it will the dropdown menu of the currently defined horizontal MenuSheet.
  4. Create one MenuSheetItem, name it and add the Action that should be performed when the item is clicked.
    Add the MenuSheetItem to the dropdown menu.
  5. Create a MenuSheetSeperator and add it to the menu.
  6. Create and add another MenuSheetItem to the menu.
  7. Add the newly created dropdown menu to the horizontal menu.
  8. Return the horizontal menu.
  9. Note: You can nest as many MenuSheets as you like and create really complex MenuSheet structures.

Example Source Code:

1
    public MenuSheet getDefaultMenuSheet()
    {
			
        2
        MenuSheet msMenu = new MenuSheet("HorizontalMenuSheet");
			
        3
        MenuSheet msSubMenu = new MenuSheet("myMenu");
			
        4
        MenuSheetItem msiSubItem1 = new MenuSheetItem("Perform an action",
                new DisplayCustomAction());
        msSubMenu.add(msiSubItem1);
			
        5
        MenuSheetSeparator mssSeperator = new MenuSheetSeparator();
        msSubMenu.add(mssSeperator);
			
        6
        MenuSheetItem msiSubItem2 = new MenuSheetItem("Perform another action",
                new DisplayCustomAction());
        msSubMenu.add(msiSubItem2);
			
        // add as many MenuSheetItems or MenuSheet as you like
        .
        .
        .
        
        7
        msMenu.add(msSubMenu);
			
        8
        return msSubMenu;
			
    }
		

Back to:


Define a MenuSheet with JComponents

Description:
If you want to improve the look of your menu, feel free to use JComponents. For further explanations on JComponents in menus "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.

Used classes:

Related topics:

ToDo:

  1. Locate the method where you want to define the MenuSheet. In this case a new method getJComponentMenuSheet() was created.
  2. Create the top-level MenuSheet, the one that is displayed horizontally.
  3. Initialize a JMenu using the javax.swing package.
  4. Initialize chosen JMenuItems and add them to the JMenu. Refer to "Using Swing Components" about possible item types.
    In this case a JCheckBoxMenuItem and a JRadioButtonMenuItem where added.
  5. Get the JMenuBar of the top-level MenuSheet and add the newly created JMenu to it.
  6. Finally return the MenuSheet.

Example Source Code:

1
    public MenuSheet getJComponentMenuSheet()
    {
			
        2
        MenuSheet msMenu = new MenuSheet("JComponentMenu");
			
        3
        JMenu jmMenu = new JMenu("JMenuItems");
			
        4
        JCheckBoxMenuItem jcbItem1 = new JCheckBoxMenuItem("JCheckBox");
        jmMenu.add(jcbItem1);
        JRadioButtonMenuItem jrbmItem2 = new JRadioButtonMenuItem("JRadioButtonMenuItem");
        jmMenu.add(jrbmItem2);
			
        5
        msMenu.getMenuBar().add(jmMenu);
			
        6
        return msMenu;
    }
		

Back to:


Define a MenuSheet for a Shop

Description:
Menus are essential for users interacting with an application. To create a menu in a Shop you will need to redefine the method createShopMenuSheet() of your instance of Shop.

Used classes:

Related topics:

ToDo:

  1. In your Shop class create the method createShopMenuSheet().
  2. Create your desired MenuSheet and return it. Therefore refer to Define a MenuSheet.

Example Source Code:

1
public class DisplayShop extends Shop
{
			
    .
    .
    .
			
    public MenuSheet createShopMenuSheet()
    {
			
        2
        MenuSheet msMenu = new MenuSheet("ShopMenu");
			
        // add your MenuSheets and MenuSheetItems as needed
            
        .
        .
        .
            
        return msMenu;
    }
            
    .
    .
    .
            
}
		

Back to:


Define a MenuSheet for a SalesPoint

Description:
Normally you display a SalesPoint in a new window. To add a menu to it, you simply have to redefine the method getDefaultMenuSheet() of your instance of SalesPoint.

Used classes:

Related topics:

ToDo:

  1. In your SalesPoint class create the method getDefaultFormSheet().
  2. Create your desired MenuSheet and return it. Therefore refer to Define a MenuSheet.

Example Source Code:

1
public class DisplaySalesPoint extends SalesPoint
{
			
    .
    .
    .
			
    public MenuSheet getDefaultMenuSheet()
    {
			
        2
        MenuSheet msMenu = new MenuSheet("HorizontalMenuSheet");
			
        // add your MenuSheets and MenuSheetItems as needed
            
        .
        .
        .
            
        return msSubMenu;
    }
            
    .
    .
    .
            
}
		

Back to:


Define a StatusMenuSheet for a SalesPoint

Description:
SalesPoints are being displayed in a separate window but also have a StatusDisplay at the Shop, which is the TabbedPane in the Shop's Frame, labled the name of the SalesPoint. By bringing it on top, it shows what is defined as the StatusDisplay in your SalesPoint instance and also adds the Menu defined as StatusMenuSheet in the SalesPoint instance to the Shop's MenuSheet. By default, both, the StatusFormSheet and the StatusMenuSheet are empty.
Feel free to use the StatusDisplay and MenuSheet, which are equally handled to the DefaultFormSheet and the DefaultMenuSheet except that due to the strong division of the Shop and it's SalesPoints it is not possible to have processes running on it. You may trigger a Processes on it, but they will always be displayed by the SalesPoint's window. Therefor a more suitable name would be "Statical Display".

Used classes:

Related topics:

ToDo:

  1. In your SalesPoint class create the method getDefaultStatusMenuSheet().
  2. Create your desired MenuSheet and return it. Therefore refer to Define a MenuSheet.

Example Source Code:

1
public class DisplaySalesPoint extends SalesPoint
{
			
    .
    .
    .
			
    public MenuSheet getDefaultStatusMenuSheet()
    {
			
        2
        MenuSheet msMenu = new MenuSheet("StatusMenu");
			
        // add your MenuSheets and MenuSheetItems as needed
            
        .
        .
        .
            
        return msMenu;
    }
            
    .
    .
    .
            
}
		

Back to:


Alter a Shop's MenuSheet during runtime

Description:
Sometimes there are reasons to change a Shop's MenuSheet during runtime. But in the class Shop a method setMenuSheet(MenuSheet ms) like the one in the class SalesPoint doesn't exists. This makes it a little more complicate. It is necessary to get the Shop's frame, which is actually a MultiWindow. The class MultiWindow provides the necessary public void setMenuSheet(MenuSheet newMenuSheet) method.
Like this it is possible to change the MenuSheet, whenever wanted.

Used classes:

Related topics:

ToDo:

  1. Get the Shop's frame, while casting it as a MultiWindow.
  2. Create the new MenuSheet for the Shop.
    (See also: Define a MenuSheet).
  3. Set the new MenuSheet on the MultiWindow.

Example Source Code:

 
    1
        DisplayShop displayShop = new DisplayShop();
        MultiWindow multiWindow = (MultiWindow) displayShop.getShopFrame();
			
    2
        MenuSheet menuShop = super.createShopMenuSheet();
        // get the first menu bar
        MenuSheet menuBar = (MenuSheet)menuShop.getTaggedItem(SHOP_MENU_TAG, false);
        // create new MenuSheetItem
        MenuSheetItem menuItemNew = new MenuSheetItem("Important", "Important_TAG",
                new sale.Action()
                {
                   public void doAction (SaleProcess p, SalesPoint sp)
                   {
                      System.out.println("very important action!");
                   }
                });
        // add it to the MenuSheet
        menuBar.add(menuItemNew);
			
    3
        multiWindow.setMenuSheet(menuShop);
		

Back to:


Create your own MenuSheet type

Description:
If you regularly need your own MenuSheet types, it may seem a bit inconvenient to create the item and afterwards manually change it's peer as described in "Define a MenuSheet with JComponents". In these cases you can create your own MenuSheet type by extending SalesPoint's MenuSheetObject according to your needs.
This example describes how to create a quite simple radion button menusheet item. You can also refer to the MenuSheetItem if you wish extra functionality. Some omitted features are:

For the first two features, have a look at MenuSheetItem (link above), where they are already implemented. The third one could be realized using maps and Integers.

Used classes:

Related topics:

ToDo:

  1. Subclass the MenuSheetObject. Since we wish to react on clicks on the item afterwards, we also implement the ActionListener interface.
  2. Create a new constructor and explicitely invoke the super constructor with the given name. While doing so, we can already think of any parameters the constructor could need later. In our case these are (besides the caption) the intial checked state.
  3. Add the members the item we will need. These are, in our case a reference to the swing peer radio menuitem and a menu and the checked state.
  4. Implement the constructor. You could also add additional constructors that use standard values for some of the parameters.
  5. Override the neccessary methods from MenuSheetObject, such that they reflect the changes in the correspondend peers:
    1. getPeer:
      Returns a JMenuItem (or a subclass of it) that represents the MenuSheetItem in a JMenu.
    2. getMenuPeer:
      Returns a JMenu that has the same caption as this MenuSheetItem and has this item as single entry.
    3. setCaption:
      Sets the caption of both the menu item peer and the menu peer.
    4. setVisible:
      Set the visibility as needed. If it is set to false, you can invalidate the peers (e.g. by setting them to null) and then recreate them when needed.
  6. Add methods according to the needs of the new MenuSheetItem type. In our case we need a getter and a setter for the checked status.
  7. Implement the method from the ActionListener interface. Since we added the item itself as ActionListener for the menu item peer, we can react on clicks on it and synchronize it's checked state with the checked member of the MenuSheetItem itself.

Example Source Code:

// needed imports
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;

import sale.MenuSheetObject;
1
public class DisplayMenuSheetItem extends MenuSheetObject implements ActionListener {
    3
    protected transient JRadioButtonMenuItem m_jrbmiPeer;

    protected transient JMenu m_jmMenuPeer;

    private boolean  m_fChecked;
    2
    public DisplayMenuSheetItem(String sCaption, boolean checked) {
        super(sCaption);
        4
        m_jrbmiPeer = null;
        m_jmMenuPeer = null;
        m_fChecked = checked;
    }
    5 a
    public JMenuItem getPeer() {
        if (m_jrbmiPeer == null) {
            m_jrbmiPeer = new JRadioButtonMenuItem(getCaption(), m_fChecked);
            m_jrbmiPeer.addActionListener(this);
            m_jmMenuPeer = null;
        }
        return m_jrbmiPeer;
    }
    5 b
    public JMenu getMenuPeer() {
        if (m_jmMenuPeer == null) {
            m_jmMenuPeer = new JMenu(getCaption());
            JRadioButtonMenuItem jmi = new JRadioButtonMenuItem(getCaption(), m_fChecked);
            jmi.addActionListener(this);
            m_jmMenuPeer.add(jmi);
        }
        return m_jmMenuPeer;
    }
    5 c
    public void setCaption(String caption) {
        super.setCaption(caption);

        if (m_jrbmiPeer != null) {
            m_jrbmiPeer.setText(getCaption());
        }

        if (m_jmMenuPeer != null) {
            m_jmMenuPeer.setText(getCaption());
            m_jmMenuPeer.getItem(0).setText(getCaption());
        }
    }
    5 d
    public void setVisible(boolean fVisible) {
        super.setVisible(fVisible);

        if (!fVisible) {
            m_jrbmiPeer = null;
            m_jmMenuPeer = null;
        }
    }
    6
    public boolean isChecked() {
        return m_fChecked;
    }

    public void setChecked(boolean checked) {
        m_fChecked = checked;
        getPeer().setSelected(m_fChecked);
    }
    7
    public void actionPerformed(ActionEvent e) {
        m_fChecked = m_jrbmiPeer.isSelected();
    }
}
		

Back to:


previous Display: FormSheetDisplay: LogFile Management next



by Thomas Ryssel