Saturday, February 14, 2009

WindowManager

package
{
import flash.events.Event;

import mx.core.UIComponent;
import mx.core.UIComponentGlobals;
import mx.core.mx_internal;
import mx.managers.FocusManager;
import mx.managers.IFocusManagerContainer;
import mx.managers.ILayoutManagerClient;

use namespace mx_internal;

public class WindowManagerImpl
{
private var _windowContainer:UIComponent;
private var _popups:Array = [];

public function WindowManagerImpl()
{
}

public function set windowContainer(value:UIComponent):void
{
_windowContainer = value;
}

public function addWindow(window:UIComponent):void
{
window.isPopUp = true;
_windowContainer.addChild(window);


var fm:FocusManager = new FocusManager(window as IFocusManagerContainer, true);
_windowContainer.systemManager.numModalWindows++;

if (window is ILayoutManagerClient )
{
UIComponentGlobals.layoutManager.validateClient(ILayoutManagerClient (window), true);
}

window.addEventListener(Event.REMOVED, handleRemove);
window.setFocus();
_popups.push(window);
}

private function handleRemove(event:Event):void
{
var window:UIComponent = event.target as UIComponent;
_windowContainer.systemManager.numModalWindows--;
_windowContainer.systemManager.removeFocusManager(IFocusManagerContainer(window));
_popups.pop();
if (_popups.length > 0)
{
var lastWindow:UIComponent = _popups[_popups.length - 1] as UIComponent;
lastWindow.focusManager.activate();
}
_windowContainer.setFocus();
window.removeEventListener(Event.REMOVED, handleRemove);
window.isPopUp = false;
}

public function removeWindow(window:UIComponent):void
{
_windowContainer.removeChild(window);
}

}
}

Monday, March 3, 2008

How to set a custom ToolTipManager

I needed to set a custom implementation of the ToolTipManager (more struts for our life!!!). I was looking for solution and found it. Here it is.

To register your own class you should invoke Singleton.registerClass(interfaceName, clazz) method with appropriate parameters. If class with interfaceName have been registered yet you cannot override it . It means you cannot register class in creationComplete, initialize or preinitialize handlers. You should create custom Preloader class and register class in constructor and set one to the application class via MXML code:


<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" preloader="CustomPreloader">



Here is the example of custom preloader class:


public class CustomPreloader extends DownloadProgressBar
{
public function CustomPreloader()
{
super();
Singleton.registerClass("mx.managers::IToolTipManager2",
CustomToolTipManager);
}

}



Maybe anyone has another solution.

Friday, February 22, 2008

SingleListener

Добаляет Listener к объекту который срабатывает только 1 раз.


public class SingleListener {

public static function wrap(target: IEventDispatcher, eventName: String, callback: Function): void
{

var listener: Function = function (event: Event): void
{
try {

callback.call(event.currentTarget, event);
} finally {
target.removeEventListener(eventName, listener);
}

}
target.addEventListener(eventName, listener);
}

}





Пример


[Embed(source="/res/image.png")]
[Bandable]

private var _imageSource: Class;

private var _image: Image = new Image();

override protected function createChildren(): void
{

super.createChildren();
addChild(_image);
_image.source = _imageSource;
workWithImageSize(_image.width, _image.height)

}

private function workWithImageSize(width: Number, height: Number): void
{
trace(width + "x" + height);

}



Output:
0x0

Но на самом деле картинка "/res/image.png" имеет размеры 640x480. Т.е. реальные данные о размере картинки мы получим после первого изменения размеров объекта _image.

override protected function createChildren(): void
{
super.createChildren();
addChild(_image);
_image.source = _imageSource;
//use this strut

SingleListener.wrap(_image, ResizeEvent.RESIZE,
function (event: Event): void
{

workWithImageSize(_image.width, _image.height);
});
}

Output:
640x480

Если после каждого изменения source требуется обработать размеры картинки, можно поступить так.

override protected function createChildren(): void
{
super.createChildren();
addChild(_image);
_image.addEventListener("sourceChanged", doOnSourceChanged );
_image.source = _imageSource;

}

private fucntion doOnSourceChanged(event: Event): void
{
//use this strut here
SingleListener.wrap(_image, ResizeEvent.RESIZE,
function (event: Event): void
{

workWithImageSize(_image.width, _image.height);
});
}

Wednesday, February 20, 2008

Custom List

List component doesn't allow to disable COPY action if Drag'n'Drop enabled. The following is a custom component that permit to disable COPY action:

package org.selectionLists
{
import mx.controls.List;
import flash.events.Event;
import mx.events.DragEvent;
import mx.managers.DragManager;

public class CustomList extends List
{

private var _dragCopyEnabled:Boolean = false;

public function get dragCopyEnabled():Boolean {
return _dragCopyEnabled;
}

public function set dragCopyEnabled(value:Boolean):void {
_dragCopyEnabled = value;
}

override protected function dragDropHandler(event:DragEvent):void {
if (!_dragCopyEnabled) {

event.action = DragManager.MOVE;
DragManager.showFeedback(DragManager.MOVE);
}

super.dragDropHandler(event);
}

override protected function dragEnterHandler(event:DragEvent):void {

if (!_dragCopyEnabled) {
event.ctrlKey = false;
}

super.dragEnterHandler(event);
}

override protected function dragOverHandler(event:DragEvent):void {

if (!_dragCopyEnabled) {
event.ctrlKey = false;
}

super.dragOverHandler(event);
}

}
}