该Action 能够控制编辑区中显示网格或隐藏网格。定义该Action
/**
* 网格Action (可隐藏网格,显示网格)
* @author lww
*
*/
public class GridAction extends Action {
GraphicalViewer viewer;
public GridAction(GraphicalViewer viewer) {
super();
this.viewer = viewer;
if ((Boolean) viewer.getProperty(SnapToGrid.PROPERTY_GRID_VISIBLE) == true)
this.setText("隐藏网格");
else
this.setText("显示网格");
}
@Override
public void run() {
if ((Boolean) viewer.getProperty(SnapToGrid.PROPERTY_GRID_VISIBLE) == true)
viewer.setProperty(SnapToGrid.PROPERTY_GRID_VISIBLE, false);
else
viewer.setProperty(SnapToGrid.PROPERTY_GRID_VISIBLE, true);
}
}
在定义上下文菜单的时候,在方法中将该方法加入上下文菜单中
@Override
public void buildContextMenu(IMenuManager menu) {
GEFActionConstants.addStandardActionGroups(menu);
IAction action;
action = getActionRegistry().getAction(ActionFactory.UNDO.getId());//撤销
menu.appendToGroup(GEFActionConstants.GROUP_UNDO, action);
action = getActionRegistry().getAction(ActionFactory.REDO.getId());//重做
menu.appendToGroup(GEFActionConstants.GROUP_UNDO, action);
action = getActionRegistry().getAction(ActionFactory.DELETE.getId());//删除
menu.appendToGroup(GEFActionConstants.GROUP_EDIT, action);
action = getActionRegistry().getAction(ActionFactory.COPY.getId());//复制 lww
menu.add(action);
action = getActionRegistry().getAction(ActionFactory.PASTE.getId());//粘贴
menu.add(action);
menu.add(new Separator()); //分割线
action = new GridAction((GraphicalViewer) this.getViewer());//显示网格或隐藏网格
menu.add(action);
}