`
wok71wok
  • 浏览: 15171 次
社区版块
存档分类
最新评论

Flex右键菜单扫盲

阅读更多

  怎么添加右键?在 Flex 中,只有应用程序中的顶层组件才能拥有上下文菜单。例如,如果 DataGrid 控件是 TabNavigator 或 VBox 容器的子级,则 DataGrid 控件不能拥有其自己的上下文菜单。组件一般都有个"contextMenu"属性,对通过实例化 ContextMenu 类,可以控制上下文菜单中显示的项。为了向 ContextMenu 对象中添加新项,可以创建一个 ContextMenuItem 对象,然后将该对象添加到ContextMenu.customItems数组。对该对象进行侦听可以响应单击事件,例如:pasteMenuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELEC T, applMenuItemPasteHandler); 使该对象不可见可以设置其visiable属性,使其不可用可使用enable属性;
  separatorBefore指示指定的菜单项上方是否显示分隔条。
  相关使用代码如下: diagramBox.contextMenu=popupMenuFactory.getPopupMe nu(); ////////////////////////////////////////////////// ///////////////////////////// // Licensed Materials - Property of IBM // 5724-Z78 // ?? Copyright IBM Corporation 2007, 2010. All Rights Reserved. // // Note to U.S. Government Users Restricted Rights: // Use, duplication or disclosure restricted by GSA ADP Schedule // Contract with IBM Corp. ////////////////////////////////////////////////// ///////////////////////////// package { import bpm.graphic.SubProcess; import bpm.graphic.Task; import com.ibm.ilog.elixir.diagram.Diagram; import com.ibm.ilog.elixir.diagram.Node; import com.ibm.ilog.elixir.diagram.Renderer; import com.ibm.ilog.elixir.diagram.Subgraph; import com.ibm.ilog.elixir.diagram.editor.DiagramEditor; import flash.events.ContextMenuEvent; import flash.events.MouseEvent; import flash.external.ExternalInterface; import flash.geom.Point; import flash.ui.ContextMenu; import flash.ui.ContextMenuItem; import mx.core.FlexGlobals; import mx.resources.IResourceManager; /** * Creates the popup menu for the Diagram. */ public class DiagramMenuFactory { private var diageditor:DiagramEditor; private var resourceManager:IResourceManager; private var diagramPopupMenu:ContextMenu; // Custom menu items private var selectAllMenuItem:ContextMenuItem; private var deleteMenuItem:ContextMenuItem; private var cutMenuItem:ContextMenuItem; private var copyMenuItem:ContextMenuItem; private var pasteMenuItem:ContextMenuItem; private var groupSubMenuItem:ContextMenuItem; private var ungroupMenuItem:ContextMenuItem; private var layoutMenuItem:ContextMenuItem; private var connectMenuItem:ContextMenuItem; private var renameMenuItem:ContextMenuItem; private var conneMenuItem:ContextMenuItem; private var enterConnectMenuItem:ContextMenuItem; // Cache for labels used in popup menu private var deleteSelectedObjectsLabel:String; private var deleteObjectLabel:String; private var cutSelectedObjectsLabel:String; private var cutObjectLabel:String; private var copySelectedObjectsLabel:String; private var copyObjectLabel:String; private var layoutAllLabel:String; private var layoutObjectLabel:String; private var conneObjectLabel:String; private var enterConnectObjectLabel:String; // Renderer that is currently with the mouse public var currentObject:Object; private var currentMouseLocationX:Number=NaN; private var currentMouseLocationY:Number=NaN; private var menuLocationX:Number=NaN; private var menuLocationY:Number=NaN; public var projectId:String=""; /** * Factory to create the Diagram popup menu */ public function DiagramMenuFactory(resourceMgr:IResourceManager, editor:DiagramEditor, diagram:Diagram) { diageditor=editor; resourceManager=resourceMgr; diagram.addEventListener(MouseEvent.MOUSE_MOVE, diagramMouseMoveHandler); } private function diagramMouseMoveHandler(event:MouseEvent):void { currentMouseLocationX=event.stageX; currentMouseLocationY=event.stageY; currentObject=diageditor.graph.getHitRenderer(even t.target); } // -------------------------------------------------- --- // Popup Menu for the Diagram // -------------------------------------------------- --- private function populatePopupMenu():void { diagramPopupMenu=new ContextMenu(); diagramPopupMenu.hideBuiltInItems(); addCustomItems(diagramPopupMenu); diagramPopupMenu.addEventListener(ContextMenuEvent .MENU_SELECT, popupMenu_menuSelect); } /** * Enables or disables the menu items according to * the number of selected objects. */ private function popupMenu_menuSelect(evt:ContextMenuEvent):void { // Store the position of the mouse at the moment when the menu is opened menuLocationX=currentMouseLocationX; menuLocationY=currentMouseLocationY; var selObjs:Vector.; if (currentObject != null) { // The popup menu has been opened for a specific object. selObjs=diageditor.getSelectedObjects(); if (selObjs.indexOf(currentObject)  0); deleteMenuItem.caption=deleteSelectedObjectsLabel; copyMenuItem.caption=copySelectedObjectsLabel; cutMenuItem.caption=cutSelectedObjectsLabel; layoutMenuItem.caption=layoutAllLabel; layoutMenuItem.visible=true; layoutMenuItem.enabled=((diageditor.graph.numEleme nts > 0) && ((diageditor.graph.nodeLayout != null) || (diageditor.graph.linkLayout != null))); conneMenuItem.visible=false; enterConnectMenuItem.visible=false; } // Connect objects is only available if two nodes are selected selObjs=diageditor.getSelectedObjects(); if (selObjs.length == 2) { if (selObjs[0] is Node && selObjs[1] is Node) { connectMenuItem.visible=true; } else { connectMenuItem.visible=false; } } else { connectMenuItem.visible=false; } var hasSelection:Boolean=diageditor.hasSelection; deleteMenuItem.enabled=hasSelection; cutMenuItem.enabled=diageditor.canCopy; copyMenuItem.enabled=diageditor.canCopy; pasteMenuItem.enabled=diageditor.canPaste; groupSubMenuItem.enabled=FlexGlobals.topLevelAppli cation.canGroup; ungroupMenuItem.visible=diageditor.canUngroup; copyApplicationCustomItems(); } // The first visible item of the diagran context menu, // used by copyApplicationCustomItems to set/clear the separator. private var firstVisibleItem:ContextMenuItem=null; /** * Copies the custom menu items from the toplevel application's context menu * (typically the View Source and About Elixir Enterprise... items) */ private function copyApplicationCustomItems():void { var appMenu:ContextMenu=FlexGlobals.topLevelApplicatio n.contextMenu; if (appMenu) { var index:int=-1; // scan custom items in the application context menu for each (var item:ContextMenuItem in appMenu.customItems) { // has the item already been copied to the diagram context menu? index=-1; for (var i:int=0; i = 0) { if (firstVisibleItem) firstVisibleItem.separatorBefore=false; for (var j:int=index + 1; j =diageditor.getSelect edObjects(); var pasted:Renderer=null; for each (pasted in pastedObjects) { if (pasted is Node) { minx=Math.min(minx, Number(pasted.x)); miny=Math.min(miny, Number(pasted.y)); maxx=Math.max(maxx, Number(pasted.x + pasted.width)); maxy=Math.max(maxy, Number(pasted.y + pasted.height)); } } if (pasted != null) { var p:Point=pasted.parent.globalToLocal(new Point(menuLocationX, menuLocationY)); var dx:Number=p.x - minx; var dy:Number=p.y - miny; diageditor.translateSelectionOfDelta(dx, dy); } } /** * Group selected objects as children of a subgraph. */ private function applMenuItemGroupSubgraphHandler(event:ContextMenu Event):void { var subgraph:Subgraph=new SubProcess(); subgraph.collapsed=false; subgraph.width=resourceManager.getNumber("bpmedito r", "bpmeditor.subprocess.default.width"); subgraph.height=resourceManager.getNumber("bpmedit or", "bpmeditor.subprocess.default.height"); subgraph.label=resourceManager.getString("bpmedito r", "bpmeditor.subprocess.default.label"); FlexGlobals.topLevelApplication.groupObjects(subgr aph); } /** * Ungroup any selected subgraphs. */ private function applMenuItemUngroupHandler(event:ContextMenuEvent) :void { diageditor.ungroup(); } /** * Perform layout on the whole diagram or on the selected subgraph. */ private function applMenuItemLayoutHandler(event:ContextMenuEvent): void { if (currentObject == null) { FlexGlobals.topLevelApplication.layoutAll(); } else { FlexGlobals.topLevelApplication.layoutSelectedSubg raph(); } } /** * Returns the menu displayed for the diagram. */ public function getPopupMenu():ContextMenu { if (diagramPopupMenu == null) populatePopupMenu(); return diagramPopupMenu; } } } 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics