27 October 2013

Deploying swc library in repository and adding maven artifacts for that

We can either deploy the swc file on a web server or a local Maven repository.

To deploy xyz.swc on a webserver, the command is (in the folder containing xyz.swc):
mvn deploy:deploy-file -Dfile=xyz.swc -Durl=file:///path/to/repository -DgroupId=com.abc -DartifactId=xyz -Dversion=1.01 -Dpackaging=swc

This deploys the xyz.swc file to /path/to/repository (using the groupId, artifactId, version).
Users then can refer to this Web URL (by defining a repository in their Maven project) and use xyz as a dependency in their Maven projects.

To deploy the swc in a local Maven repository, we can execute (in the folder containing xyz.swc):
mvn install:install-file -Dfile=xyz.swc -DgroupId=com.abc -DartifactId=xyz -Dversion=1.01 -Dpackaging=swc -DgeneratePom=true -DcreateChecksum=true

Users then can add the dependency in the pom.xml file under <project> tag as:
    <dependencies>
        <dependency>
            <groupId>com.abc</groupId>
            <artifactId>xyz</artifactId>
            <version>1.01</version>
            <type>swc</type>
        </dependency>
    </dependencies>

12 October 2013

Preserving open nodes in AdvancedDataGrid with HierarchicalCollectionView

My dataProvider is an ArrayCollection, which is populated using XML.

var initXML:XML = <items>
        <item id="News" label="News" value_1="unchecked" value_2="unchecked" value_3="checked"/>
        <item id="BBC" label="The Web" value_1="unchecked" value_2="unchecked" value_3="checked">
            <item id="BBC1" label="BBC Homepage" value_1="unchecked" value_2="unchecked" value_3="checked">
                <item id="YouTube" label="YouTube" value_1="unchecked" value_2="unchecked" value_3="checked"/>
                <item id="GoogleMaps" label="Google Maps" value_1="unchecked" value_2="unchecked" value_3="checked"/>
                <item id="MSN" label="MSN" value_1="unchecked" value_2="unchecked" value_3="checked"/>
            </item>
            <item id="Wikipedia" label="Wikipedia" value_1="unchecked" value_2="unchecked" value_3="checked"/>
            <item id="Dilbert" label="Jasper Report" value_1="unchecked" value_2="unchecked" value_3="checked"/>
            <item id="PhD" label="Test Page" value_1="unchecked" value_2="unchecked" value_3="unchecked"/>
        </item>
        <item id="Blogs" label="Jasper Reports" value_1="unchecked" value_2="unchecked" value_3="checked">
            <item id="Blog1" label="Report Invoker" value_1="unchecked" value_2="unchecked" value_3="checked"/>
            <item id="Blog2" label="Dashboard" value_1="unchecked" value_2="unchecked" value_3="checked"/>
            <item id="Blog3" label="Morning Report" value_1="unchecked" value_2="unchecked" value_3="checked"/>
        </item>
    </items>;


public function loadDataResultHandler(newXML:XML):void
{
    dataProvider.removeAll(); // dataProvider is an ArrayCollection
    for each (var s:XML in newXML.item)
        dataProvider.addItem(s);
    dataProvider.refresh();
    advancedDG.validateNow();

    var openNodes:Object;
    if (advancedDG.dataProvider != null)
        openNodes = IHierarchicalCollectionView(advancedDG.dataProvider).openNodes;
    IHierarchicalCollectionView(advancedDG.dataProvider).openNodes = {}; // prevents memory leak

    for each (var openNode:XML in openNodes)
        for each (var item:XML in newXML.descendants("item"))
            if (item.@id[0] == (openNode.@id)[0])
            {
                advancedDG.expandItem(item, true);
                break;
            }
}

01 October 2013

Spark TextInput for Currency values

In my project, I wanted to have an spark TextInput to show currency values.
While formatting it should show unformatted number.
The way I did it is by creating a new CurrencyTextInput class extending spark TextInput.
Code for the component is (CurrencyTextInput.as):

package com.abcd.component
{
    import flash.events.Event;
    import flash.events.FocusEvent;

    import spark.components.TextInput;
    import spark.formatters.CurrencyFormatter;

    public class CurrencyTextInput extends TextInput
    {
        public function CurrencyTextInput()
        {
            super();
            formatter = new CurrencyFormatter();
            formatter.currencySymbol = "£";
            formatter.fractionalDigits = 2;
            formatter.useGrouping = true;
            formatter.useCurrencySymbol = true;
            formatter.negativeCurrencyFormat = 1;
            formatter.trailingZeros = true;
        }

        public var formatter:CurrencyFormatter = null;

        public var formatterRegExp:RegExp = /[,£ ]/g;

        [Inspectable(category = "General", defaultValue = "")]
        override public function get text():String
        {
            return super.text.replace(formatterRegExp, "");
        }

        [Bindable("change")]
        [Bindable("textChanged")]
        [CollapseWhiteSpace] // Compiler will strip leading and trailing whitespace from text string.
        override public function set text(value:String):void
        {
            super.text = formatter.format(value);

            // Trigger bindings to textChanged.
            dispatchEvent(new Event("textChanged"));
        }

        /**
         * Flush out [,] and currency symbol characters before getting focus
         * @param    event
         */
        override protected function focusInHandler(event:FocusEvent):void
        {
            super.focusInHandler(event);
            if (this.focusEnabled)
                super.text = text.replace(formatterRegExp, "");
        }

        /**
         * Format data before lost focus event happen
         * @param    event
         */
        override protected function focusOutHandler(event:FocusEvent):void
        {
            super.focusOutHandler(event);
            super.text = formatter.format(text);
        }
    }
}


The way to use this component is:

<fx:declarations>
    <s:currencyformatter

        currencysymbol="{resourceManager.getString(ApplicationConstants.RESOURCE_BUNDLE_NAME, 'currency.symbol')}"
        fractionaldigits="2"
        id="currencyFormatter"
        negativecurrencyformat="1"
        trailingzeros="true"
        usecurrencysymbol="true"
        usegrouping="true"/>
</fx:declarations>

<fx:Script>

    <![CDATA[
    
    [Bindable]        public var currencyFormatterRegExp:RegExp = new RegExp('[,' + resourceManager.getString(ApplicationConstants.RESOURCE_BUNDLE_NAME, 'currency.symbol') + ' ]', 'g');
    ]]>

</fx:Script>

<component:CurrencyTextInput
    width="217"
    fontWeight="bold"
    textAlign="right"
    text="@{billingValue}"
    formatterRegExp="{currencyFormatterRegExp}"
    formatter="{currencyFormatter}" restrict="0-9"/>

13 February 2013

Eclipse crashing and not starting

Problem: After restarting Eclipse, it crashes immediately and asks me to check C:\Users\username\Adobe Flash Builder 4.6\.metadata\.log
In log it shows following error:
!ENTRY org.eclipse.osgi 4 0 2013-02-13 11:53:46.760
!MESSAGE Application error
!STACK 1
org.eclipse.swt.SWTError: Cannot initialize Drop
    at org.eclipse.swt.dnd.DND.error(DND.java:266)
    at org.eclipse.swt.dnd.DND.error(DND.java:227)
    at org.eclipse.swt.dnd.DropTarget.(DropTarget.java:142)
    at org.eclipse.ui.internal.EditorSashContainer.addDropSupport(EditorSashContainer.java:542)
    at org.eclipse.ui.internal.EditorSashContainer.createControl(EditorSashContainer.java:534)
    at org.eclipse.ui.internal.EditorAreaHelper.(EditorAreaHelper.java:41)
    at org.eclipse.ui.internal.WorkbenchPage.init(WorkbenchPage.java:2507)
    at org.eclipse.ui.internal.WorkbenchPage.(WorkbenchPage.java:653)
    at org.eclipse.ui.internal.tweaklets.Workbench3xImplementation.createWorkbenchPage(Workbench3xImplementation.java:47)
    at org.eclipse.ui.internal.WorkbenchWindow$16.runWithException(WorkbenchWindow.java:2222)
    at org.eclipse.ui.internal.StartupThreading$StartupRunnable.run(StartupThreading.java:31)
    at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
    at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
    at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4140)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3757)
    at org.eclipse.ui.application.WorkbenchAdvisor.openWindows(WorkbenchAdvisor.java:803)
    at org.eclipse.ui.internal.Workbench$33.runWithException(Workbench.java:1595)
    at org.eclipse.ui.internal.StartupThreading$StartupRunnable.run(StartupThreading.java:31)
    at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
    at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
    at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4140)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3757)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
    at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
    at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at com.adobe.flexbuilder.standalone.FlashBuilderApplication.start(FlashBuilderApplication.java:108)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1410)

!ENTRY org.eclipse.core.jobs 2 2 2013-02-13 11:53:47.166
!MESSAGE Job found still running after platform shutdown.  Jobs should be canceled by the plugin that scheduled them during shutdown: org.eclipse.ui.internal.ide.IDEWorkbenchActivityHelper$4



Solution: Delete C:\Users\username\Adobe Flash Builder 4.6\.metadata\.plugins\org.eclipse.core.resources\.root\.indexes\properties.index file. It was corrupted and throwing EOFException.

12 December 2012

Flex ByteArray to Java ByteArray

While trying to import XLS files from Flex front-end, we needed to pass the file contents to Java middle tier, where Apache POI library was used to parse the contents.
The Java method expects a Java ByteArray containing the file contents, but when we pass a Flex ByteArray to it, it throws error.
To overcome this instead of passing Flex ByteArray, we should pass an Array of bytes, as depicted in following code snippet:


public static function byteArrayToArrayOfBytes(ba:ByteArray):Array
{
    var arrayOfBytes:Array = [];
    ba.position = 0;
    while(ba.bytesAvailable > 0)
        arrayOfBytes.push(ba.readByte());
    ba.position = 0;
    return arrayOfBytes;
}

public function onFileSelectionComplete(fileReference:FileReference)
{
    var payload:Object = new Object();
    payload.byteArray = byteArrayToArrayOfBytes(fileReference.data);

    ExternalInterface.call("parseXLS", payload);
}