mdimanager.as
来自「flex的一些小例子」· AS 代码 · 共 1,077 行 · 第 1/3 页
AS
1,077 行
/*Copyright (c) 2007 FlexLib Contributors. See: http://code.google.com/p/flexlib/wiki/ProjectContributorsPermission is hereby granted, free of charge, to any person obtaining a copy ofthis software and associated documentation files (the "Software"), to deal inthe Software without restriction, including without limitation the rights touse, copy, modify, merge, publish, distribute, sublicense, and/or sell copiesof the Software, and to permit persons to whom the Software is furnished to doso, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.*/package flexlib.mdi.managers{ import flash.display.DisplayObject; import flash.events.ContextMenuEvent; import flash.events.Event; import flash.events.EventDispatcher; import flash.geom.Point; import flash.ui.ContextMenu; import flash.ui.ContextMenuItem; import flash.utils.Dictionary; import flexlib.mdi.containers.MDIWindow; import flexlib.mdi.effects.IMDIEffectsDescriptor; import flexlib.mdi.effects.MDIEffectsDescriptorBase; import flexlib.mdi.effects.effectClasses.MDIGroupEffectItem; import flexlib.mdi.events.MDIManagerEvent; import flexlib.mdi.events.MDIWindowEvent; import mx.collections.ArrayCollection; import mx.core.Application; import mx.core.EventPriority; import mx.core.IFlexDisplayObject; import mx.core.UIComponent; import mx.effects.CompositeEffect; import mx.effects.Effect; import mx.effects.effectClasses.CompositeEffectInstance; import mx.events.EffectEvent; import mx.events.ResizeEvent; import mx.managers.PopUpManager; import mx.utils.ArrayUtil; //-------------------------------------- // Events //-------------------------------------- /** * Dispatched when a window is added to the manager. * * @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_ADD */ [Event(name="windowAdd", type="flexlib.mdi.events.MDIManagerEvent")] /** * Dispatched when the minimize button is clicked. * * @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_MINIMIZE */ [Event(name="windowMinimize", type="flexlib.mdi.events.MDIManagerEvent")] /** * If the window is minimized, this event is dispatched when the titleBar is clicked. * If the window is maxmimized, this event is dispatched upon clicking the restore button * or double clicking the titleBar. * * @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_RESTORE */ [Event(name="windowRestore", type="flexlib.mdi.events.MDIManagerEvent")] /** * Dispatched when the maximize button is clicked or when the window is in a * normal state (not minimized or maximized) and the titleBar is double clicked. * * @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_MAXIMIZE */ [Event(name="windowMaximize", type="flexlib.mdi.events.MDIManagerEvent")] /** * Dispatched when the minimize button is clicked. * * @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_CLOSE */ [Event(name="windowClose", type="flexlib.mdi.events.MDIManagerEvent")] /** * Dispatched when the window gains focus and is given topmost z-index of MDIManager's children. * * @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_FOCUS_START */ [Event(name="windowFocusStart", type="flexlib.mdi.events.MDIManagerEvent")] /** * Dispatched when the window loses focus and no longer has topmost z-index of MDIManager's children. * * @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_FOCUS_END */ [Event(name="windowFocusEnd", type="flexlib.mdi.events.MDIManagerEvent")] /** * Dispatched when the window begins being dragged. * * @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_DRAG_START */ [Event(name="windowDragStart", type="flexlib.mdi.events.MDIManagerEvent")] /** * Dispatched while the window is being dragged. * * @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_DRAG */ [Event(name="windowDrag", type="flexlib.mdi.events.MDIManagerEvent")] /** * Dispatched when the window stops being dragged. * * @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_DRAG_END */ [Event(name="windowDragEnd", type="flexlib.mdi.events.MDIManagerEvent")] /** * Dispatched when a resize handle is pressed. * * @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_RESIZE_START */ [Event(name="windowResizeStart", type="flexlib.mdi.events.MDIManagerEvent")] /** * Dispatched while the mouse is down on a resize handle. * * @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_RESIZE */ [Event(name="windowResize", type="flexlib.mdi.events.MDIManagerEvent")] /** * Dispatched when the mouse is released from a resize handle. * * @eventType flexlib.mdi.events.MDIManagerEvent.WINDOW_RESIZE_END */ [Event(name="windowResizeEnd", type="flexlib.mdi.events.MDIManagerEvent")] /** * Dispatched when the windows are cascaded. * * @eventType flexlib.mdi.events.MDIManagerEvent.CASCADE */ [Event(name="cascade", type="flexlib.mdi.events.MDIManagerEvent")] /** * Dispatched when the windows are tiled. * * @eventType flexlib.mdi.events.MDIManagerEvent.TILE */ [Event(name="tile", type="flexlib.mdi.events.MDIManagerEvent")] /** * Class responsible for applying effects and default behaviors to MDIWindow instances such as * tiling, cascading, minimizing, maximizing, etc. */ public class MDIManager extends EventDispatcher { private static var globalMDIManager:MDIManager; public static function get global():MDIManager { if(MDIManager.globalMDIManager == null) { globalMDIManager = new MDIManager(Application.application as UIComponent); globalMDIManager.isGlobal = true; } return MDIManager.globalMDIManager; } private var isGlobal:Boolean = false; private var windowToManagerEventMap:Dictionary; private var tiledWindows:ArrayCollection; public var tileMinimize:Boolean = true; public var tileMinimizeWidth:int = 200; public var showMinimizedTiles:Boolean = false; public var tilePadding:Number = 8; public var minTilePadding:Number = 5; public var enforceBoundaries:Boolean = true; public var effects:IMDIEffectsDescriptor = new MDIEffectsDescriptorBase(); public static const CONTEXT_MENU_LABEL_TILE:String = "Tile"; public static const CONTEXT_MENU_LABEL_TILE_FILL:String = "Tile + Fill"; public static const CONTEXT_MENU_LABEL_CASCADE:String = "Cascade"; public static const CONTEXT_MENU_LABEL_SHOW_ALL:String = "Show All Windows"; /** * Contstructor() */ public function MDIManager(container:UIComponent, effects:IMDIEffectsDescriptor = null):void { this.container = container; if(effects != null) { this.effects = effects; } if(tileMinimize) { tiledWindows = new ArrayCollection(); } this.container.addEventListener(ResizeEvent.RESIZE, containerResizeHandler); // map of window events to corresponding manager events windowToManagerEventMap = new Dictionary(); windowToManagerEventMap[MDIWindowEvent.MINIMIZE] = MDIManagerEvent.WINDOW_MINIMIZE; windowToManagerEventMap[MDIWindowEvent.RESTORE] = MDIManagerEvent.WINDOW_RESTORE; windowToManagerEventMap[MDIWindowEvent.MAXIMIZE] = MDIManagerEvent.WINDOW_MAXIMIZE; windowToManagerEventMap[MDIWindowEvent.CLOSE] = MDIManagerEvent.WINDOW_CLOSE; windowToManagerEventMap[MDIWindowEvent.FOCUS_START] = MDIManagerEvent.WINDOW_FOCUS_START; windowToManagerEventMap[MDIWindowEvent.FOCUS_END] = MDIManagerEvent.WINDOW_FOCUS_END; windowToManagerEventMap[MDIWindowEvent.DRAG_START] = MDIManagerEvent.WINDOW_DRAG_START; windowToManagerEventMap[MDIWindowEvent.DRAG] = MDIManagerEvent.WINDOW_DRAG; windowToManagerEventMap[MDIWindowEvent.DRAG_END] = MDIManagerEvent.WINDOW_DRAG_END; windowToManagerEventMap[MDIWindowEvent.RESIZE_START] = MDIManagerEvent.WINDOW_RESIZE_START; windowToManagerEventMap[MDIWindowEvent.RESIZE] = MDIManagerEvent.WINDOW_RESIZE; windowToManagerEventMap[MDIWindowEvent.RESIZE_END] = MDIManagerEvent.WINDOW_RESIZE_END; // these handlers execute default behaviors, these events are dispatched by this class addEventListener(MDIManagerEvent.WINDOW_ADD, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.WINDOW_MINIMIZE, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.WINDOW_RESTORE, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.WINDOW_MAXIMIZE, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.WINDOW_CLOSE, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.WINDOW_FOCUS_START, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.WINDOW_FOCUS_END, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.WINDOW_DRAG_START, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.WINDOW_DRAG, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.WINDOW_DRAG_END, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.WINDOW_RESIZE_START, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.WINDOW_RESIZE, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.WINDOW_RESIZE_END, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.CASCADE, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); addEventListener(MDIManagerEvent.TILE, executeDefaultBehavior, false, EventPriority.DEFAULT_HANDLER); } private var _container:UIComponent; public function get container():UIComponent { return _container; } public function set container(value:UIComponent):void { this._container = value; } /** * @private * the managed window stack */ [Bindable] public var windowList:Array = new Array(); public function add(window:MDIWindow):void { if(windowList.indexOf(window) < 0) { window.windowManager = this; this.addListeners(window); this.windowList.push(window); this.addContextMenu(window); if(this.isGlobal) { PopUpManager.addPopUp(window,Application.application as DisplayObject); this.position(window); } else { // to accomodate mxml impl if(window.parent == null) { this.container.addChild(window); this.position(window); } } dispatchEvent(new MDIManagerEvent(MDIManagerEvent.WINDOW_ADD, window, this)); bringToFront(window); } } /** * Positions a window on the screen * * <p>This is primarly used as the default space on the screen to position the window.</p> * * @param window:MDIWindow Window to position */ public function position(window:MDIWindow):void { window.x = this.windowList.length * 30; window.y = this.windowList.length * 30; if((window.x + window.width) > container.width) window.x = 40; if((window.y + window.height) > container.height) window.y = 40; } public function addContextMenu(window:MDIWindow,contextMenu:ContextMenu=null):void { // add default context menu if(contextMenu == null) { var defaultContextMenu:ContextMenu = new ContextMenu(); defaultContextMenu.hideBuiltInItems(); var arrangeItem:ContextMenuItem = new ContextMenuItem(CONTEXT_MENU_LABEL_TILE); arrangeItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler); defaultContextMenu.customItems.push(arrangeItem); var arrangeFillItem:ContextMenuItem = new ContextMenuItem(CONTEXT_MENU_LABEL_TILE_FILL); arrangeFillItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler); defaultContextMenu.customItems.push(arrangeFillItem); var cascadeItem:ContextMenuItem = new ContextMenuItem(CONTEXT_MENU_LABEL_CASCADE); cascadeItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler); defaultContextMenu.customItems.push(cascadeItem); var showAllItem:ContextMenuItem = new ContextMenuItem(CONTEXT_MENU_LABEL_SHOW_ALL); showAllItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler); defaultContextMenu.customItems.push(showAllItem); this.container.contextMenu = defaultContextMenu; } else { // add passed in context menu window.contextMenu = contextMenu; } } private function menuItemSelectHandler(event:ContextMenuEvent):void { var win:MDIWindow = event.contextMenuOwner as MDIWindow; switch(event.target.caption) { case(CONTEXT_MENU_LABEL_TILE):
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?