Java Drawing DrawTop

Language

JP  US  UK

 

Popup Menu

 H. Jyounishi, Tokyo Japan
 

Frame (Index), No frame                 version:0.3(latest)  

Summary: This page describes the method to creates popup menues.

Classes on this page: PopupPulldownMenu, PopupMenuAction

1. Overview


1.1 The type of the popup menu

Type

Description

ShapePopupMenu

This popup menu is used for a single shape.
Selects a single shape by clicking the mouse button on the shape or its boundary and clicks the right mouse button, then the pop up menu appears.

This menu is created by the createShapePopupMenu method.

GroupPopupMenu

This popup menu is used for a group shape.
Selects a group shape by clicking the mouse button on the group shape or its boundary and clicks the right mouse button, then the pop up menu appears.

This menu is created by the createGrouprPopupMenu method.

TextBoxPopupMenu

This popup menu is used for editing a text box of a single shape or a group shape.
Selects a single shape or a group shape by clicking the mouse button inside the shape and clicks the right mouse button, then the pop up menu appears.
The selected text box is shown in the green frame and the selected text is shown with the pink background, and the possible menu items are shown on the pop up menu in the figure below.

This menu is created by the createTextBoxPopupMenu method.

DrawPanelPopupMenu

The popup menu for the DrawPanel. Clicks the right mouse button at the empty place of the drawing panel, then the popup menu appears.

This menu is created by the createDrawPanelPopupMenu method.



1.2 Triggering popup menus and executing the commands return=>page top

Step

Description

Step1 Detecting the mouse right button is pressed Whether the right button is pressed or not is detected in the MousePositionLS.mousePressed method of the MousePositionLS (or SelectionLS.mousePressed). If pressed, then the MousePosition.mouseClicked method calls the ExecPopupMenu.show method.
Step2 Showing the popup menu In the ExecPopupMenu.show method, the type of the popup menu will be determined according to the right button pressed position and shows the pulldown menu by the PopupPullownMenu.show method.
Step3 Action for selecting a popup menu If you select a menu in the popup menu, the PopupMenuAction.actionPerformed method creates the Command object for the menu and the ExecCommand.exec method executes the Command as follow;

case Command.SHAPE_FORMAT:{
Point point=(Point)params[0];
ShapeContainer container=(ShapeContainer)params[1];
DialogOfShapeFormat dialog=new DialogOfShapeFormat();
dialog.showDialog(container);
break;
}



2. Class PopupPulldownMenu return=>page top

public class PopupPulldownMenu extends JPopupMenu


Field

Description

menuAction

PopupMenuAction menuAction=new PopupMenuAction(this);

The ActionListener ( (PopupMenuAction)) to be added to this object.

point

public static Point point

The mouse pressed point for opening the popup menu. This field is set by the showMenu method of this class.

container

public static ShapeContainer container

The ShapeContainer object or null. This field is set by the show method of this class.


Method

Description

Constructor

public PopupPulldownMenu(String commandName, String[] menuItemNames, ImageIcon[] icons, String[] accelerators)

Parameters:

commandName - The command name.

menuItemNames - The array of the strings representing the names of the MenuItem objects.

imageIcons - The array of the ImageIcon objects to be displayed on the MenuItem objects.

accelerators - The array of the strings representing the accelerators on the MenuItem objects.

Processing:

Calls the following methods.

this.setName(commandName);
this.setMenuItems(menuItemNames, icons, accelerators);

setMenuItems

public void setMenuItems(String[] menuItemNames, ImageIcon[] icons, String[] accelerators)

Sets the MenuItem objects to the popup menu. The ImageIcons[i] and the menuItemNames[i] are displayed on the i-th MenuItem.

Paremeters:

menuItemNames - The array of the strings representing the names of the MenuItem objects.

imageIcons - The array of the ImageIcon objects to be displayed on the MenuItem objects.

accelerators - The array of the strings representing the accelerators on the MenuItem objects.

Processing:

(1) Calculates the maximum width of the menuItemNames by the getTextLayoutSize method.

(2) Creates MenuItem objects and adds them to this object.

The label of the menuItemNames[i] is add on the left side of the MenuItem[i] and the label of the imageIcons[i] is add on the right side of the MenuItem[i]. The label width of the menuItemNames[i] is determined by the result of step (1).

(3) Sets the action command and other attributes to the MenuItem[i] as follow.

menuItem.setPreferredSize(new Dimension(menuItemWidth, menuItemHeight));
menuItem.setActionCommand(menuItemNames[i]);
menuItem.addActionListener(this.menuAction);
menuItem.setName(menuItemNames[i]);

getTextLayoutSize

private Dimension getTextLayoutSize(String text, Font font)

Returns the size of the text on a MenuItem object.

Creates the TextLayout object using the text, font and a new FontRenderContext object, and gets the size of the text by the getAdvance, getAscent and getDescent methods.

getMenuItem

public JMenuItem getMenuItem(String menuItemName)

Parameters:
menuItemName - The name of the MenuItem object.

Returns:

The JMenuItem object which is added to the menu.

Processing:

The Component objects under the menu can be got by the JMenu.getMenuComponents method. The Command.compareStrings method is used to compare the specified menuItemName and the name of Component object.

setEnable
ToAllMenuItems

public void setEnableToAllMenuItems()

Sets enable to all the MenuItem objects.

show

public void showMenu(Point point, ShapeContainer container)

Parameters:
point - The mouse pressed point for opening the popup menu.
container - The ShapeContainer object or null.

Processing:

This method is called by the show method of the ExecPopupMenu to show the pulldown menu.

this.point=point;
this.container=container;
this.show(ObjectTable.getDrawPanel(), (int)point.getX(), (int)point.getY());

createSingleShapePopupMenu

public static void createSingleShapeContainerPopupMenu()

The PopupPulldownMenu for a single shape ShapeContainer. This popup menu is shown when the mouse right button is pressed on a shingle shape boundary.

Sets the parameters as follow and calls the constructor.

String commandName=Command.getCommandString(Command.SHAPE_POPUP_MENU);
String[] imageName={"", "", "", "", "", ""};
String[] menuItemNames={"cut", "copy", "delete", "add text box", "modify shape",
"shape format"};

createGroupShape
PopupMenu

public static void createGroupShapeContainerPopupMenu()

The PopupPulldownMenu for a group ShapeContainer. This popup menu is shown when the mouse right button is pressed on a group.

Sets the parameters as follow and calls the constructor.

String commandName=Command.getCommandString(Command.GROUP_POPUP_MENU);
String[] imageName={"", "", "", ""};
String[] menuItemNames={"cut", "copy", "delete", "shape format"};

createDrawPanel
PopupMenu

public static void createDrawPanelPopupMenu()

The PopupPulldownMenu for the DrawPanel. This popup menu is shown when the mouse right button is pressed at the empty place.

Sets the parameters as follow and calls the constructor.

String commandName=Command.getCommandString(Command.DRAWPANEL_POPUP_MENU);
String[] imageName={"", ""};
String[] menuItemNames={"paste", "draw panel information"};

createTextBoxPopupMenu public static void createTextBoxPopupMenu()


3. Class PopupMenuAction return=>page top

class PopupMenuAction extends AbstractAction


Field

Description

popupPulldownMenu

PopupPulldownMenu popupPulldownMenu

The PopupPulldownMenu object which created this object.


Method

Description

Constructor

PopupMenuAction(PopupPulldownMenu popupPulldownMenu)
Sets the parameter to the field.

actionPerformed

public void actionPerformed(ActionEvent e)

Sets the commandId and command parameters (params) as follow and calls the exec method of the ExecCommand.

∙ commandId

int commandId
=Command.getCommandId(this.buttonOfPulldownMenu.menu.getActionCommand());

∙ Command parameters:

args[0]=this.popupPulldownMenu.point;
args[1]=this.popupPulldownMenu.container;



Copyright (c) 2009-2013
All other trademarks are property of their respective owners.