Showing posts with label Custom Components. Show all posts
Showing posts with label Custom Components. Show all posts

Tuesday, December 20, 2011

SkinnablePopUpContainer Example

The SkinnablePopUpContainer class is a SkinnableContainer that functions as a pop-up. One typical use for a SkinnablePopUpContainer container is to open a simple window in an application, such as an alert window, to indicate that the user must perform some action.
You do not create a SkinnablePopUpContainer container as part of the normal layout of its parent container. Instead, it appears as a pop-up window on top of its parent. Therefore, you do not create it directly in the MXML code of your application.
Instead, you create is as an MXML component, often in a separate MXML file. To show the component create an instance of the MXML component, and then call the open() method. You can also set the size and position of the component when you open it.
To close the component, call the close() method. If the pop-up needs to return data to a handler, you can add an event listener for the PopUp.CLOSE event, and specify the returned data in the close() method.
The SkinnablePopUpContainer is initially in its closed skin state. When it opens, it adds itself as a pop-up to the PopUpManager, and transition to the normal skin state. To define open and close animations, use a custom skin with transitions between the closed and normal skin states.



Here is the code for Above Example:



<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  xmlns:s="library://ns.adobe.com/flex/spark"
  xmlns:mx="library://ns.adobe.com/flex/mx" >
<fx:Style source="CustomPopup.css"/>

<fx:Script>
<![CDATA[
import mx.controls.Alert;
protected function login_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
if(txtUserName.text=='Rocky' && txtPassword.text=='Hacker')
{
Alert.show("Successfully login to Adobe Flex 4.5 ....","Login ");
login.close();
}
else
{
Alert.show("Please enter the correct User Name and Password","Error");
}


}

protected function logout_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
Alert.show("Successfully Logout to Adobe Flex 4.5 ....","Log Out ");
login.close();
}

]]>
</fx:Script>

<fx:Declarations>
<s:SkinnablePopUpContainer id="login">
<s:Panel title="Launching Adobe Flex 4.5 ...">
<s:Form x="-14" y="5" width="450" height="200" color="#3F9122" fontFamily="Verdana" fontSize="13">
<s:FormItem label="User Name">
<s:TextInput id="txtUserName" />
</s:FormItem>
<s:FormItem label="Password" >
<s:TextInput id="txtPassword" displayAsPassword="true"/>
</s:FormItem>
<s:HGroup>
<s:Button label="Log In" click="login_clickHandler(event)"/>
<s:Button label="Log Out" click="logout_clickHandler(event)"/>
</s:HGroup>
<s:Label text="Enter User name Rocky and Password Hacker to login"/>
</s:Form>

</s:Panel>
</s:SkinnablePopUpContainer>
</fx:Declarations>
<s:Button x="53" y="37" label="Click here to Sign to Adobe Flex 4.5"
 click="login.open(this,false)"/>
<s:Label x="53" y="11" text="Welcome to Adobe Flex 4.5 Components "/>

</s:Application>


Css file:



/* CSS file */
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";




global
{
font-family: Georgia;
font-size: 14;
color: #3363A4;
chrome-color: #A2B8A2;
selection-color: #424234;
}




Monday, December 19, 2011

Custom Alert Message in Flex 4.5

The Alert control is a pop-up dialog box that can contain a message, a title, buttons (any combination of OK, Cancel, Yes, and No) and an icon. The Alert control is modal, which means it will retain focus until the user closes it.
Import the mx.controls.Alert class into your application, and then call the static show() method in ActionScript to display an Alert control. You cannot create an Alert control in MXML.
The Alert control closes when you select a button in the control, or press the Escape key.




Here is the Code for the Alert Message:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
  xmlns:s="library://ns.adobe.com/flex/spark" 
  xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Style source="PopUpWindow.css"/>

<fx:Script>
<![CDATA[
import mx.controls.Alert;
protected function button1_clickHandler(event:MouseEvent):void
{
Alert.yesLabel="Flex is Awesome";
Alert.noLabel="I am not Agree !";
Alert.buttonWidth=120;
Alert.show("Flex 4.5 is Awesome ....","Flex Message",(Alert.YES|Alert.NO));

}
]]>
</fx:Script>

<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button x="216" y="182" label="Show Alert Message" click="button1_clickHandler(event)"/>
</s:Application>

Css:

/* CSS file */
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";


global
{
font-family: Georgia;
font-size: 14;
color: #762C2C;
}



Saturday, November 12, 2011

Date Sorting on Datagrid Column

For Sorting the Date timestamp column in datagrid you need to add the following changes in datagrid to the MXML file which contain timestamp value for EST timezone conversion .
The following code  is an example:-

Sorting on Date Column on Datagrid use the following code



<mx:DataGridColumn headerText="Timestamp" dataField="errorDateTime" width="200" sortCompareFunction="dateSortCompare"/>

Sorting is done by calling sortComapareFunction in Datagrid Column. Please follow method below for date sorting.

public static function dateSortCompare(date1:Object, date2:Object):int
{
var dateA:Date=DateFormatter.parseDateString(date1.errorDateTime);
var dateB:Date=DateFormatter.parseDateString(date2.errorDateTime);
//trace("date1.errorDateTime="+date1.errorDateTime+", date2.errorDateTime="+date2.errorDateTime+", "+ObjectUtil.dateCompare(dateA, dateB));
return ObjectUtil.dateCompare(dateA, dateB);
}

Text Image Renderer Component

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" horizontalScrollPolicy="off" textAlign="center">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.collections.*;
import flash.events.Event;
import mx.controls.DataGrid;
import com.ApplicationConstants;
]]>
</mx:Script>
<mx:HBox width="100%" verticalAlign="middle" horizontalAlign="center">
<mx:TextInput id="txtUniCircuitId" width="80%" automationName="txtUniCircuitIdAN"/>
<mx:Image horizontalAlign="center" source="@Embed(source='image source')" buttonMode="true" useHandCursor="true" width="20%"/>
</mx:HBox>
</mx:Canvas>

Alert with Label Link Component

<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="center" title="Order Successfully Created" textAlign="center" width="460" styleName="alert" height="110" verticalGap="15" color="black" shadowDirection="center" shadowDistance="10" >
  <mx:Script>
        <![CDATA[
    import mx.events.CloseEvent;
    import mx.managers.PopUpManager;
   
   
        public var  closeHandler:Function = null;
       
       
        private function clickHandler(evt:MouseEvent):void
    {
        var closeEvent:CloseEvent = new CloseEvent(CloseEvent.CLOSE);
            closeEvent.detail = 001;
            closeHandler(closeEvent);
        PopUpManager.removePopUp(this);

    }
        private function removeHandler(evt:MouseEvent):void
    {
       
        mx.managers.PopUpManager.removePopUp(this);

    }
          ]]>
  </mx:Script>
  <mx:Style>
    .alert{
color : #0f3177;
title-style-name : "alertTitle";
header-height:25;
drop-shadow-enabled: true;
drop-shadow-color :#d1ddf7;
background-color: #ffffff;
corner-radius :6;
header-colors : #90a4d1, #5970a0;
footer-colors : #9db6d9, #ffffff;
border-color : #5970a0;
}
  </mx:Style>
  <mx:HBox id="genId1281449230637_2" automationName="genId1281449230637_2AN">
    <mx:FormItem label="Order Id:" id="genId1281449230637_3">
      <!-- <mx:Label id="orderLabel" color="blue" selectable="true" textDecoration="underline" styleName="alert" click="clickHandler(event)" useHandCursor="true" mouseChildren="false" buttonMode="true" /> -->
       <mx:LinkButton id="orderLabel"    color="blue"   styleName="alert" useHandCursor="true" mouseChildren="false" click="clickHandler(event)"
             textDecoration="underline" fontWeight="normal"  />
    </mx:FormItem>
  </mx:HBox>
  <mx:Button id="btnOk" label="OK" click="removeHandler(event)" styleName="alert" fillColors="0xFFFFFF, 0xAAAAAA"/>
</mx:TitleWindow>

Export to Excel and CSV Component

package com.main.components
{import flash.errors.*;
import flash.events.*;
import flash.external.*;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.net.navigateToURL;
import flash.system.Capabilities;

import mx.collections.ArrayCollection;
import mx.controls.DataGrid;



public class ExportToExcelorCSV
{
public static const EXPORT_ALL:String = "exportAll";
public static const EXPORT_VISIBLE:String = "exportVisible";
public static const EXPORT_SELECTED:String = "exportSelected"; 
private static var titleDate:String = '';
private static var titleNTE:String = '';
public static const TYPE_CSV:String = "typeCsv";
public static const TYPE_TSV:String = "typeTsv";
public static var data:String = "";

private static function convertDGToHTMLTable(dg:DataGrid ):String { 
//Set default values 
var font:String = dg.getStyle('fontFamily'); 
var size:String = dg.getStyle('fontSize'); 
var str:String = ''; 
var colors:String = ''; 
var style:String = 'style="font-family:'+font+';font-size:'+size+'pt;"'; 
var hcolor:Array; 

//Retrieve the headercolor 
if(dg.getStyle("headerColor") != undefined) { 
hcolor = [dg.getStyle("headerColor")]; 
} else { 
hcolor = dg.getStyle("headerColors"); 


//Set the htmltabel based upon knowlegde from the datagrid 
str+= '<table><thead><tr width="'+dg.width+'" style="background-color:#1E90FF">'; 

//Set the tableheader data (retrieves information from the datagrid header 
for(var i:int = 0;i<dg.columns.length;i++) { 
colors = dg.getStyle("themeColor"); 

if(dg.columns[i].headerText != undefined) { 
str+="<th "+style+">"+dg.columns[i].headerText+"</th>"; 
} else { 
str+= "<th "+style+">"+dg.columns[i].dataField+"</th>"; 


str += "</tr></thead><tbody>"; 
colors = dg.getStyle("alternatingRowColors"); 

//Loop through the records in the dataprovider and 
//insert the column information into the table 
if(dg.dataProvider != null ){

for(var j:int =0;j<dg.dataProvider.length;j++) 

str+="<tr width=\""+Math.ceil(dg.width)+"\">"; 

for(var k:int=0; k < dg.columns.length; k++)


//Do we still have a valid item? 
if(dg.dataProvider.getItemAt(j) != undefined && dg.dataProvider.getItemAt(j) != null)


//Check to see if the user specified a labelfunction which we must 
//use instead of the dataField 
if(dg.columns[k].labelFunction != undefined) 

str += "<td "+style+">"+dg.columns[k].labelFunction(dg.dataProvider.getItemAt(j),dg.columns[k].dataField)+"</td>"; 

} else

str += "<td "+style+">"+dg.dataProvider.getItemAt(j)[dg.columns[k].dataField]+"</td>"; 


}
str += "</tr>"; 

}
str+="</tbody></table>"; 

return str; 


public static function getTitleAndDate(title:String , date:String):void
{
titleDate = '<p align="center"><font size = "4"><b>'+ title + '</b></font></p><p align= "right">' + date + '</p>'
}
public static function getTitle(title:String ):void
{
titleNTE = '<p align="center"><font size = "4"><b>'+ title + '</b></font></p>'


public static function loadDataGridInExcel( dataGrid:DataGrid, dataGridNTE:DataGrid = null ):void 
{
//Pass the htmltable in a variable so that it can be delivered
//to the backend script
if(dataGridNTE == null)
{
var variables:URLVariables = new URLVariables();
variables.htmlTable =titleDate + convertDGToHTMLTable(dataGrid);
var url:String = "export.jsp";
var u:URLRequest = new URLRequest( url );
u.data = variables; //Pass the variables
u.method = URLRequestMethod.POST; 
navigateToURL( u, "_self" );
}
else
{
var variable:URLVariables = new URLVariables();
variable.htmlTable =titleDate + convertDGToHTMLTable(dataGrid) + titleNTE + convertDGToHTMLTable(dataGridNTE);
var urlQ:String = "export.jsp";
var ur:URLRequest = new URLRequest( urlQ );
ur.data = variable; //Pass the variables
ur.method = URLRequestMethod.POST;
navigateToURL( ur, "_self" );

}

// Function For Converting into CSV String 
public static function copyData( dataGrid:DataGrid, fileType:String, exportType:String ):String
{
var str:String = "";
var value:String = "";
var skipColumns:Array = [];

for (var i:int = 0;i<dataGrid.columns.length;i++) 
{
if (dataGrid.columns[i].headerText != undefined) 
{
value = dataGrid.columns[i].headerText + "\n"; 

else 
{
value = dataGrid.columns[i].dataField;
}

// we won't include columns which don't have titles
if (value.length == 0)
{
skipColumns.push( i );
continue;
}
else
{
if (fileType == TYPE_CSV)
{
str += '"' + value + '"';
}
else
{
str += value;
}
}

if (i < dataGrid.columns.length-1)
{
str += fileType == TYPE_CSV ? "," : "\t";
}
}
str += lineEnding; 
var data:Array;

if (exportType == EXPORT_ALL)
{
data = ArrayCollection( dataGrid.dataProvider ).source;
}
else
{
data = dataGrid.selectedItems;
}

for each (var item:Object in data)

for(var k:int=0; k < dataGrid.columns.length; k++) 
{
// check if we're skipping this column
if (skipColumns.indexOf( k ) >= 0)
{
continue;
}
//Check to see if the profile specified a labelfunction which we must
//use instead of the dataField
if (dataGrid.columns[k].labelFunction != undefined) 
{
value = dataGrid.columns[k].labelFunction(item, dataGrid.columns[k]); 

else 
{
var dataField:String = dataGrid.columns[k].dataField;
value = item[ dataField ];
}
if (value)
{
var pattern:RegExp = /["]/g;
value = value.replace( pattern, "" );

if (fileType == TYPE_CSV)
{
value = '"' + value + '"';
}
}
else
{
if (fileType == TYPE_CSV)
{
value = '""';
}
}
str += value;
if (k < dataGrid.columns.length - 1)
{
str += fileType == TYPE_CSV ? "," : "\t";
}
}
str += lineEnding;
}
setData(str);

return str; 
}

public static function setData( text:String ):void{

data = text;
}
public static function getData( ):String{

return data;
}
public static function getItemsFromText( text:String ):Array
{
var rows:Array = text.split( lineEnding );
if (rows.length > 0 && !rows[rows.length - 1])
{
rows.pop();
}
var itemsFromText:Array = [];
for each (var row:String in rows)
{
var fields:Array = row.split("\t");
var item:Object = {};

for (var i:int = 0; i < fields.length; i++)
{
item["col" + (i+1)] = fields[i];
}
itemsFromText.push(item);
}
return itemsFromText;
}

private static function get lineEnding():String
{
if (Capabilities.os == "Linux")
{
return "\n";
}
else if (Capabilities.os.indexOf( "Mac" ) >= 0)
{
return "\r";
}
else
{
return "\r\n";
}
}

public static function loadDataGridCSVExcel( dataGrid:DataGrid ):void 
{
var variables:URLVariables = new URLVariables(); 
variables.htmlTable = copyData( dataGrid, TYPE_CSV, EXPORT_ALL );
var url:String = "export.jsp";
var u:URLRequest = new URLRequest( url );
u.data = variables; 
u.method = URLRequestMethod.POST; 
navigateToURL( u, "_self" ); 




}




This component is used for export the data into Excel spreadsheet or CSV file.

  • Simple script to convert a Datagrid to a HTML table and then 
     pass it on to an external excel exporter 
     
     Has Method Which Coverts to CSV string 
     Can Pass Title and need to Change the Code a little If anything else Needed