Monday 19 September 2011

Restrict Popup Window Move


public function restrictPopUpMove(event:MoveEvent):void
{
var window:UIComponent = this ;
var mainWindowArg:DisplayObjectContainer = Application.application as DisplayObjectContainer;

if ((window.x + window.width) > mainWindowArg.width)
window.x = mainWindowArg.width - window.width;

if((window.y + window.height) > mainWindowArg.height)
window.y = mainWindowArg.height - window.height;

if(window.x < 0)
window.x = 0;

if(window.y < 0)
window.y = 0;

window.move(window.x, window.y);
}

Wednesday 14 September 2011

Popup Window with Spark (Solved Dragging Isuue)


package
{
import flash.events.Event;
import mx.core.FlexGlobals;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.logging.ILogger;
import mx.managers.PopUpManager;
import spark.components.TitleWindow;
import spark.events.TitleWindowBoundsEvent;
/**
* This TitleWindow is designed so that the window cannot be dragged outside
* the boundaries of the application. This component is made to be opened with <code>PopUpManager</code>
* so that when it is closed, it will be removed from <code>PopUpManager</code>.
* @author rrubalcava
*/
public class PopUpWindow extends TitleWindow
{
public function PopUpWindow()
{
super();
this.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete_handler, false, 0, true);
this.addEventListener(CloseEvent.CLOSE, onCloseClicked_handler, false, 0, true);
this.addEventListener(TitleWindowBoundsEvent.WINDOW_MOVE, onTitleWindowMove_handler, false, 0, true);
}
/**
* Right position of window when it opens.
* @default
*/
public var popupRight:Number;
/**
* Left position of window when it opens.
* @default
*/
public var popupTop:Number;
/**
* X postion of window when it opens.
* @default
*/
public var popupX:Number;
/**
* Y postion of window when it opens.
* @default
*/
public var popupY:Number;
/**
* Will remove all elements and this window from <code>PopUpManager</code>.
* @param e
*/
protected function onCloseClicked_handler(e:CloseEvent):void
{
this.removeEventListener(CloseEvent.CLOSE, onCloseClicked_handler);
this.removeAllElements();
PopUpManager.removePopUp(this);
}
/**
* Will assign given position assignments when window is created.
* @param e
*/
protected function onCreationComplete_handler(e:FlexEvent):void
{
this.removeEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete_handler);
if (popupX > 0)
this.x = popupX;
else if (popupRight > 0)
this.right = popupRight;
if (popupY > 0)
this.y = popupY;
else if (popupTop > 0)
this.top = popupTop;
}
/**
* Repositions window so that it cannot be dragged outside of <code>Application</code> area.
* @param event
*/
protected function onTitleWindowMove_handler(event:Event):void
{
var w:Number = FlexGlobals.topLevelApplication["width"];
var h:Number = FlexGlobals.topLevelApplication["height"];
if (this.x < 0)
this.x = 5;
if (this.y < 0)
this.y = 5;
if (this.x > w - 25)
this.x = w - this.width;
if (this.y > h - 25)
this.y = h - this.height;
}
}
}

Auto Intelligent Combobox



package
{
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.TimerEvent;
import flash.ui.Keyboard;
import flash.utils.Timer;

import mx.controls.ComboBox;
import mx.utils.ArrayUtil;


public class ComboBoxItemRenderer extends ComboBox
{
//--------------------------------------------------------------------------
//
//  Constructor
//
//--------------------------------------------------------------------------

public function ComboBoxItemRenderer()
{
super();

_searchString = '';
_clearTimer = new Timer( 1000, 1 );
_clearTimer.addEventListener( TimerEvent.TIMER_COMPLETE, clearTimerCompleteHandler );
}

//--------------------------------------------------------------------------
//
//  Variables
//
//--------------------------------------------------------------------------

/**
* The string that is added to as the user types to use as the search string
*/
private var _searchString:String;

/**
* The timer that we use to clear the search string after a period of inactivity
*/
private var _clearTimer:Timer;

/**
* Keys we don't want to be bothered about handling
*/
private var _keysSuperHandles:Array = [ Keyboard.DOWN, Keyboard.UP, Keyboard.ESCAPE, Keyboard.ENTER, Keyboard.PAGE_UP, Keyboard.PAGE_DOWN ];

/**
* Used to keep track of whether we're inside the keyDownHandler or not, this
* allows us to stop the close() method from executing (default behaviour on
* selecting an item) when we're selecting an item as type.
*/
protected var _selectingItemAsTyping:Boolean = false;

//--------------------------------------------------------------------------
//
//  Properties
//
//--------------------------------------------------------------------------

/**
* The time, in milliseconds, of keyboard inactivity before the search string
* is reset
*
* @tiptext Time, in milliseconds, of keyboard inactivity before the search string is reset
* @default 1000
*/
public var inactivityResetTimeout:int = 1000;

//--------------------------------------------------------------------------
//
//  Methods
//
//--------------------------------------------------------------------------

/**
* Override the close function to take account of us changing
* the selected item (see keyDownHandler & _foxyInKeyDown)
*
* @see mx.controls.ComboBox::close()
*/
override public function close(trigger:Event = null):void
{
if( !_selectingItemAsTyping )
{
super.close();
}
}

//--------------------------------------------------------------------------
//
//  Overridden event handlers
//
//--------------------------------------------------------------------------

override protected function keyDownHandler(event:KeyboardEvent):void
{
var tmpCode:int = event.keyCode;
/*
* Note: In an ideal world the best place to do this work would be within a sub class
* of the List, but for some reason if the user doesn't open the drop down and
* starts typing (e.g. tabs to the ComboBox and starts typing) then the ComboBox
* keeps getting a new List from the factory. This stops us from keeping any sort
* of state within our List sub class, and having the feature only when the user
* has opened the drop down AND typed while it's open is not really very nice.
*
* So rather than going into overkill, by sub classing a few things and overriding
* far much more than I would like to, this compromise seemed the best option.
*/
if( ArrayUtil.getItemIndex( tmpCode, _keysSuperHandles ) != -1 )
{
// it's one of the keys we don't want to handle, let the parent handle it
super.keyDownHandler( event );
}
else if(( tmpCode >= 33 && tmpCode <= 126 ) || tmpCode == Keyboard.SPACE )
{
// Internally the ComboBox uses a private variable called
// bInKeyDown to make sure not to close the dropdown (if open)
// in this situation (where we have changed the selected item
// ourselves), we have to do something similar with a variable
// we have access to (see the overridden close() method for more details)
_selectingItemAsTyping = true;

// it's one of the keys we want to handle
_searchString += String.fromCharCode( tmpCode );

// backup the selected index incase the findString doesn't find any matches
var prevSelectedIndex:int = dropdown.selectedIndex;

// reset the selectedIndex stops from cycling through values as your typing, e.g.
// if there were the values united kingdom and united states then while typing united
// the selected item would cycle between the two (which isn't very attractive)
dropdown.selectedIndex = -1;

var matchedString:Boolean = dropdown.findString( _searchString );

if( !matchedString )
{
// if we didn't find a match we put the selection back to where it was
// as it was us that removed the selection (when we set it to -1 above)
dropdown.selectedIndex = prevSelectedIndex;
}

// kick off the clear timer
_clearTimer.reset();
_clearTimer.start();

_selectingItemAsTyping = false;
}
else
{
/*
it's another key that we don't care about (but not one we've explicitally
said we don't care about), so let the parent handle it
*/
super.keyDownHandler( event );
}
}

//--------------------------------------------------------------------------
//
//  Event handlers
//
//--------------------------------------------------------------------------

private function clearTimerCompleteHandler( event:TimerEvent ):void
{
this._searchString = '';
}

}
}

How to keep dragged TitleWindow within Flex Application Boundary

package
{
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;

import mx.containers.Canvas;
import mx.containers.TitleWindow;
import mx.events.CloseEvent;
import mx.managers.PopUpManager;

public class AdvancedTitleWindow extends TitleWindow
{
private var titleBarOverlay:TitleWindow;

public function AdvancedTitleWindow()
{
super();
}

override protected function createChildren():void
{
super.createChildren();

if(!this.titleBarOverlay)
{
this.titleBarOverlay = new TitleWindow();
this.titleBarOverlay.width = this.width;
this.titleBarOverlay.height = this.titleBar.height;

this.titleBarOverlay.showCloseButton = true;
this.titleBarOverlay.addEventListener(CloseEvent.CLOSE, onClose);

rawChildren.addChild(this.titleBarOverlay);
}

addDragEventListeners();
}

override protected function updateDisplayList(unscaledWidthArg:Number, unscaledHeightArg:Number):void
{
super.updateDisplayList(unscaledWidthArg, unscaledHeightArg);

this.titleBarOverlay.width = this.width;
this.titleBarOverlay.height = this.titleBar.height;
}

private function addDragEventListeners():void
{
this.titleBarOverlay.addEventListener(MouseEvent.MOUSE_DOWN, onTitleBarPress, false, 0, true);
this.titleBarOverlay.addEventListener(MouseEvent.MOUSE_UP, onTitleBarRelease, false, 0, true);
}

private function onTitleBarPress(mouseEventArg:MouseEvent):void
{
// Here you can set the boundary using owner, parent, parentApplication, etc.
this.startDrag(false, new Rectangle(0, 0, parent.width - this.width, parent.height - this.height));
}

private function onTitleBarRelease(mouseEventArg:MouseEvent):void
{
this.stopDrag();
}

private function onClose(closeEventArg:CloseEvent):void
{
PopUpManager.removePopUp(this);
}
}
}