Pages

Monday, 16 May 2011

The components and ViewStack TabNavigator

The components and ViewStack TabNavigator in flex is quite simple but some of my flex developer friends are getting some problem while creating this component this code is usefule if you want to use it to interface a page or screen. the best way to use a component of this type is tabnavigator component.

Here is an example of using TabNavigator which incorporates

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" viewSourceURL="srcview/index.html">
    <mx:TabNavigator width="400" height="240">
        <mx:Form label="Nom" icon="@Embed(source='./application_form.png')">
            <mx:FormItem label="Prénom">
                <mx:TextInput id="first"/>
            </mx:FormItem>
            <mx:FormItem label="Middle Name">
                <mx:TextInput id="middle"/>
            </mx:FormItem>
            <mx:FormItem label="Last Name">
                <mx:TextInput id="last"/>
            </mx:FormItem>
        </mx:Form>
        <mx:Form label="Commentaires" width="100%" height="100%">
            <mx:FormItem label="Commentaires">
                <mx:TextArea width="200" height="100" id="comments"/>
            </mx:FormItem>
        </mx:Form>
    </mx:TabNavigator>
</mx:Application>
 

Read more...

Tuesday, 10 May 2011

Spark ButtonBar control in Flex 4 for Setting a fixed button width


Given coding is about creating custom horizontal layout and setting the columnwidth and variablecolumnwidth properties in flex 4 to put fixed button width on speark buttonbar control

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/12/10/setting-a-fixed-button-width-on-a-spark-buttonbar-control-in-flex-4/ -->
<s:Application name="Spark_ButtonBar_layout_columnWidth_test"
               xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx">

    <s:ButtonBar id="bBar" horizontalCenter="0" verticalCenter="0">
        <s:layout>
            <s:HorizontalLayout gap="-1" columnWidth="80" variableColumnWidth="false" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList source="[Red,Halo Orange,Yellow,Halo Green,Halo Blue,A,The quick brown fox]" />
        </s:dataProvider>
    </s:ButtonBar>

</s:Application>

Read more...

Wednesday, 4 May 2011

Help to Securing the Client With Chimp in Flex Enterprise Secrity

Chimp is ideal for filtering user interface components on the client side

This post will guid in how you implement your server, the key is that you have truly secured your services and data on the server side, as that should be the heart of your security implementation.

I will guid you how to review or authenticating users in Flex through the ChannelSet, and then i will discuss filtering what users are able to see based on their roles or grants.

User must be aythenticated before clip can be used to protect user interface componets. Channelset for accessing blazeds services are used in flex To authenticate user. In the Flex authentication code below a userService remote object is created. The username and password parameters come from a login form and are passed to the authenticate function. On a successful login, the loginResultEvent handler function is called, and post login logic can be invoked.


<mx:AMFChannel id="myamf" uri="http://localhost:8080/spring/messagebroker/amf"/>
<mx:ChannelSet id="channelSet" channels="{[myamf]}"/>
<mx:RemoteObject id="userService" destination="userService" showBusyCursor="true"
channelSet="{channelSet}"/>
<mx:Script>
    <![CDATA[
            protected function authenticate(username:String, password:String) : void {
                     var token:AsyncToken = userService.channelSet.login(username, password);
                     token.addResponder(new ItemResponder(loginResultEvent, loginFaultEvent));
            }

            private function loginResultEvent(event:ResultEvent, token:AsyncToken):void {
                 //DO post login stuff
            }
   ]]>
</mx:Script>

Adding Chimp to your project

Begin with Chimp by adding the SWC file to your Flex application. First, download the chimp.swc Google Code. In a standard Flex Builder project, follow these steps to add the file to your project library: file from

  1. Right click the project name in the Project Explorer view
  2. Select Properties
  3. Select Flex Build Path
  4. Select Library path
  5. Click Add SWC
  6. Browse to chimp.swc (see Figure 1)
  7. Click "Ok"


Next, you need to update your project to keep the metadata used by Chimp. In a standard Flex Builder project, follow these steps to tell the Flex compiler to keep the metadata:
  1. Right click the project name in the Project Explorer view
  2. Select Properties
  3. Select Flex Compiler
  4. Add the following to the Additional Compiler Arguments (See Figure 2):
    -keep-as3-metadata+=Protected
  5. Click OK

Loading Chimp in a Flex Application

The simplest way to load Chimp before the user interface components is to use the Flex preinitialize event of the application.

private function preintializeHandler(event:Event):void { 
Chimp.load(null); 
}

Loading users and permissions
Chimp is now added and loaded into the Flex application. Next, the permissions that will be used to determine what the user can see need to be loaded from Spring.

public class UserIdentity {
     private String username;
     private List roles;
     public User getUser() {
           return user;
     }
     public void setUser(User user) {
           this.user = user;
     }
      public List getRoles() {
           return roles;
      }
      public void setRoles(List roles) {
           this.roles = roles;
      }
}


The UserIdentity object is assembled from the Spring SecurityContextHolder:


    public UserIdentity getUserIdentity() {
    UserIdentity ui = new UserIdentity();

    //set user name
   ui.setUsername(userDao.getUser(SecurityContextHolder.getContext().getAuthentication().getName()));

    //set string permissions
    List&lt;String&gt; perms = new ArrayList&lt;String&gt;();
    GrantedAuthority[] gas = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
    for(int i=0; i &lt; gas.length; i++) {
        perms.add(gas[i].getAuthority());
    }

    ui.setRoles(perms);
    //return user identity object
    return ui;
}

After doing this Flex application will call the getUserIdentity() method after successfully authenticating the user:


public var userService:RemoteObject;

private function loginResultEvent(event:ResultEvent, token:AsyncToken):void {
    var userIdentityToken:AsyncToken = userService.getUserIdentity();
    userIdentityToken.addResponder(new ItemResponder(userIdentityEvent, faultEvent));
}

private function userIdentityEvent(event:ResultEvent, token:AsyncToken):void {
    //set user permissions on Chimp
    Chimp.updatePerms(event.result.roles);
}

After the user is successfully authenticated, the user identity information is loaded and the permissions are updated on the instance of the Chimp library. This information is used with the metadata to restrict what the user can see in the Flex user interface.

Adding metadata

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="/2006/mxml">
    <mx:Metadata>
       [Protected(permissions="admin",notInPermissionAction="removeChild",componentId="box2")]
       [Protected(permissions="admin",notInPermissionAction="removeFromLayout",componentId="p2")]
       [Protected(permissions="admin",inPermissionAction="visible",componentId="adminButton")]
       [Protected(permissions="user",inPermissionAction="enable",componentId="updateButton")]
     </mx:Metadata>
    <mx:Button id="adminButton" label="Admin Button" visible="false"  />
    <mx:Button id="updateButton" label="Update Button" enabled="false" />
    <mx:HBox>
        <mx:Panel id="p1" title="Panel 1" backgroundColor="#FF0000"/>
        <mx:Panel id="p2" title="Panel 2" backgroundColor="#00FF00"/>
        <mx:Panel id="p3" title="Panel 3" backgroundColor="#0000FF"/>
    </mx:HBox>
    <mx:TabNavigator id="tabNav" width="300" height="300">
        <mx:VBox id="box1" width="100%" height="100%" label="Tab One">
            <mx:Text text="first tab" />
        </mx:VBox>
        <mx:VBox id="box2" width="100%" height="100%" label="Tab Two">
            <mx:Text text="second tab" />
        </mx:VBox>      
        <mx:VBox id="box3" width="100%" height="100%" label="Tab Three">
            <mx:Text text="third tab" />
        </mx:VBox>      
    </mx:TabNavigator>   
</mx:VBox>

The line below removes the second box in the tab navigator if the "admin" permission is not found. A removeChild is used because a removeFromLayout does not work with the TabNavigator component. The removeFromLayout option is preferred when it works, because it is less aggressive and has more predictable behavior if the "admin" permissions are provided at a later time.

[Protected(permissions="admin",notInPermissionAction="removeChild",componentId="box2")]

With the following line, if the "admin" permission is not found, the second panel in the HBox is removed from the layout so that it is no longer visible:

[Protected(permissions="admin",notInPermissionAction="removeFromLayout",componentId="p2")]

Below, the adminButton is made visible if the user has the "admin" permission:

[Protected(permissions="admin",inPermissionAction="visible",componentId="adminButton")]

And Finally, the updateButton is enabled if the user has the "user" permission

[Protected(permissions="user",inPermissionAction="enable",componentId="updateButton")]

The protected annotation is fairly simple to use. It has the following properties:
  • permissions: The permissions property expects a comma delimited string of the string permissions to use for the protected operation.
  • componentId: This componentId is the string name of the component that is to be protected. This can be omitted or set to ‘this' for the current component. To protect a child, set the componentId to the ‘id' string of the component.
  • notInPermissionAction: The notInPermissionAction is invoked if the user does not have any of the permissions (use only this or inPermissionAction – not both).
  • inPermissionAction: The inPermissionAction is invoked if the user has any of the permissions (use only this or notInPermissionAction – not both). 
The notInPermissionAction and inPermissionAction properties invoke actions on the target component. They can take the following values:
  • removeChild : Removes the component completely, by calling ‘comp.parent.removeChild().'
  • removeFromLayout : Use the includeInLayout property to remove components.
  • invisible: Sets the compl.visible property to false.
  • visible: Sets the comp.visible property to true.
  • disable: Sets the comp.enable property to false.
  • enable: Sets the comp.enable property to true
As you can see, the Chimp library is straightforward to use inside of a Flex application. Once installed and loaded into the application, it provides a simple and elegant way to manage permissions of the view components.
Source

Read more...

Tuesday, 3 May 2011

Using the ProgressBar with a file download in Flex

when you are working with some time consuming procedure while flex application development you dont have information or idea about the progress. It is true that flex provides nice moving clock to let you know that you are doing something.

You might have idea about adobe provided the ProgressBar with flex to experiment the same ProgressBar with file download  from the server Try the given code.


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" viewSourceURL="srcview/index.html">

<mx:Script>
    <![CDATA[
       
        [Bindable]
        private var fileRef:FileReference = null;
       
        private function downloadFile():void{
           
            var urlRequest:URLRequest = new URLRequest("http://www.flex-blog.com/samples/sample3/wallpaper.jpg");
            fileRef = new FileReference();
            fileRef.download(urlRequest, "wallpaper.jpg");
            fileRef.addEventListener(ProgressEvent.PROGRESS, setDownloadProgress);
            fileRef.addEventListener(Event.COMPLETE, completeHandler);

           
        }
       
        private function setDownloadProgress(event:ProgressEvent):void{
           
            pbProgress.setProgress(event.bytesLoaded, event.bytesTotal);
            pbProgress.label = "Downloading [" + Math.round(event.bytesLoaded / event.bytesTotal * 100).toString() + "%]";
           
        }
       
        private function completeHandler(event:Event):void{
           
            fileRef = null;
           
        }       
       
    ]]>
</mx:Script>

<mx:VBox top="10" left="10">

    <mx:Text buttonMode="true" click="downloadFile()" text="Download Wallpaper" textDecoration="underline" fontSize="12"/>

    <mx:ProgressBar visible="{fileRef != null}" mode="manual" label="" id="pbProgress" labelPlacement="center"/>

</mx:VBox>
</mx:Application>


Read more...