When 2 – 1 = 2 when doing math operations with dates in ActionScript

September 4th, 2011

Let’s say we have the following use-case: we have to create a calendar with Month view. The Month view can start on any day, it can be 1st of March, it can be 15th of March, it can be even 31th of March.

So, assume that our current date range starts with 31th of March. And now, what if we want to scroll the date back one month? Hah,easy, you would jump immediately with straightforward solution, just subtract 1 month from current Date.

Writing a few lines of code, doing some traces.

var startDate : Date = new Date (2011, 2, 31);
var newDate : Date = new Date (startDate.time); //displays Thu Mar 31 2011
 
trace ("startDate=", startDate.toDateString()); 
newDate.month--;		
 
trace ("newDate=", newDate.toDateString()); //displays Thu Mar 3 2011

..and wait a minute, how it is possible that 2 – 1 = 2 for new month value?

Well, the issue is that Flash Player behind the scene is fixing invalid resulting date for February month. We could not have February date with 31 days, so somebody has to do something about it, right?

To sum up we should remember about this feature of Flash Player when dealing with invalid dates and the correct operation for subtracting 1 month from existing date can be the following:

var startDate : Date = new Date (2011, 2, 31);
var initNewDate : Date = new Date(startDate.time)
var newDate : Date;
 
initNewDate.month--;				
newDate = handleShorterMonths(startDate, initNewDate, 1);
trace ("newDate=", newDate.toDateString()); //displays Mon Feb 28 2011
 
private function handleShorterMonths(startDate : Date, dateToCheck :Date, monthIndex : int) : Date {
   var correctDate : Date = dateToCheck;
   if (dateToCheck.date < startDate.date) {
        correctDate.date = startDate.date - dateToCheck.date;	
	correctDate.month = monthIndex;
   }
   return correctDate; 
}

Interactive sample with view source enabled that illustrates the issue:

Get Adobe Flash player

Alternatively, if you do not like to do Date Math operations by yourself, you can always rely on As3Commons DateUtils.as class.

This utility class has a very well written API to do various Math operations with Dates – you add days, months, years, etc. to existing date with an ease.

v.0.15 update for InputAssist component – Flex 4.5 SDK Ready!

May 11th, 2011

Bugs fixed
———
[!] Fixed issue when open popup windows was not automatically closed when MouseEvent.MOUSE_WHEEL occurred somewhere outside of the component;
[!] Fixed issue “Error when hit Enter in InputAssist with no dataProvider”
https://github.com/JabbyPanda/InputAssist/issues/2.

And the last, but not the least – InputAssist is now ready to be used in projects compiled against Flex 4.5 SDK – I just did some code trick due to the public API change in MatrixUtil.getConcatenatedMatrix method in Flex 4.5.

Get Adobe Flash player


As always, the most recent source code of the component and compiled SWC library file is available at github.com/JabbyPanda/InputAssist

Preview of MaskedTextInput control for Flex 4 SDK

April 13th, 2011

   Long time, no see!

   My hands are scratching today to show you a preview of MaskedTextInput control for Flex 4 SDK that I’ve been working on.

   Currently only date mask in format “DD/MM/YY” is supported, but more to come. :)

   The last, but not the least announcement for today – I plan to create MaskedTextInput control that will be independent of Flex SDK too for all Flash platform developers to benefit from!

Get Adobe Flash player

December v.0.14 update for InputAssist component

December 30th, 2010

Looks like I got into the fashion of monthly updates for InputAssist UI component.

Please welcome the last update for InputAssist component in 2010 year!

Bugs fixed
———
[!] Fixed issue when adding/removing dynamically items to dataProvider did not reflect in visual updating of the component;

[!] selectedItem cannot be set to the value that is not present in dataProvider;

[!] Setting “prompt dynamically does not reset displayedText;

[!] Updating the position of popUp with data items every time when available data list shrinks/expands;

Demo that illustrates correct behaviour:

Get Adobe Flash player

internal API changes
————
[*] private property _collection is now typed as ListCollectionView;

[*] Replaced internal HighlightItemListEvent.ITEM_CLICK event with ItemClickEvent.ITEM_CLICK event;

As always, the most recent source code of the component and compiled SWC library file is available at github.com/JabbyPanda/InputAssist

I wish you all Happy New 2011 year for the Flash Platform!

DateField4 component for Flex 4 SDK

December 2nd, 2010

   As we all know, a current Flex 4 SDK does not include a new Spark DateField control. And an older ‘halo’ DateField component is left abandoned somewhere in 2008 year. This leaves the users of Flex SDK with simply no choice available out of the box to support the entry of formatted date data entry in standard international date formats.

   Thus, today I am releasing my open-sourced component DateField4 that fixes a number of bugs and issues related to international dates and inline date editing.

   I’ve named my component DateField4, to emphasize the fact that this component is only meant to be used with Flex 4 SDK and beyond.

   Below is the demo that showcases the difference between DateField4 and ‘halo’ DateField.

   For example, you may notice, that ‘halo’ DateField does not support neither French nor Italian international date formatting even to display the date, oouch.

Get Adobe Flash player


   This demo was compiled using newest build of Flex 4.5 SDK to showcase a new Spark Form (good news – we are no longer limited to display validation errors in single tooltip).

   The complete list of new features and bug-list fixes:

[*] Always enabled much improved custom validator for editable DateField that fully supports i18n dates;

[*] Limiting the data entry to “allowedFormatChars” value + numeric chars for editable DateField;

[*] Added “autoShowDropDown” property that controls when dropDown DateChooser menu is displayed;

[*] Pressing ESCAPE key resets the date selection to the previous value before DateField control had obtained a focus;

[*] Improved keyboard navigation for dropDown DateChooser menu when DateField is editable: RIGHT, LEFT, UP, DOWN, PAGE_UP, PAGE_DOWN key strokes are supported;

[!] Fixed SDK-23069 [Localization]: DateFormatter.parseDateString does not support non-latin characters in month and days names;

[!] Fixed SDK-23075 “[Localization]: DateField should support “MMM” and “MMMM” for formatString”;

[!] Fixed SDK-26715 “DateFormatter “parseDateString” method cannot parse dateString value formatted with non default en_US format”;

[!] Default width of DateField is wider by 4px to correctly display selected Date when moving cursor caret to the end of the text.

Public Git URL for this project is: https://github.com/JabbyPanda/DateField4

InputAssist is updated to 0.13 release

November 15th, 2010

   The 0.13 version does not contain a lot of new exciting features, the main focus of this version was to fix a couple of not so crucial bugs and to make sure that we will not have possible memory leaks when adding/removing InputAssist component to the DisplayList.

The only “new” feature is that InputAssist now supports dataProvider typed as ArrayList.

    Here is the full changelist for this release:

[!] Fixed issue with resetting visible “prompt” value when changing dataProvider
[!] Fixed issue by keyboard navigation by the help of Keyboard.UP and Keyboard.DOWN buttons
within option list if option list was closed and reopened.
[!] Items that had “-” or “+” symbols in its labels were not properly filtered
[!] Creating complete copy of passed ArrayCollection into dataProvider to avoid filtering of the original data source
[!] Fixed RTE when using InputAssist control within Adobe AIR app in “onFlexWindowActivate” event listener
[!] Started to use weak references when adding event listeners to avoid possible memory leaks
[!] “dataProvider” property now accepts data list typed as ArrayList.

As always, the most recent source code of the component and compiled SWC library file is available at GitHub

InputAssist control is updated to 0.12 release

October 15th, 2010

As always the latest source code and SWC file can be downloaded from GitHub:http://github.com/JabbyPanda/InputAssist

Short description of new features:

“processing” property
   Quite often the long data list is being loaded into InputAssist control asynchronously from remote destinations. Now InputAssist is able to display “hourGlass” icon during the data loading process by controlling “processing” property.

“errorMessage” property
    If InputAssist’s dataProvider is currently empty, then there is nothing to be chosen from. Displaying “errorMessage” text and disabling the control gives the user the right UI feedback about the component state.

“prompt” property
   If no value is not selected yet via InputAssist control, the control can display text specified by the “prompt” value to explain what action is expected to be taken from the user side.

   Alternative usage of “prompt” property that I can think of – is to display the suppossedly selected current data object label which is not present in the InputAssist’s dataProvider list == e.g. this selected data object is not valid and should be altered by the further user action.

And yes, sorry about long time delay to retrieve city list in the demo below, it is not a component fault, but long service time response from api.business.gov

Get Adobe Flash player

Changelog for 0.12 release:
[+] Added property “prompt”
[+] Added property “errorMessage”
[+] Added property “processing”

[!] Fixed issue with dispatching InputAssistEvent.CHANGE event for nullable selectedItem
[!] Fixed issue with auto-completion by hitting ENTER key

Flex 4 AutoComplete is now known as InputAssist, announcing 0.1 release

September 1st, 2010

The source code will be permanently available at GitHub (yes, new kid in the block instead of Google Code or SourceForge).

http://github.com/JabbyPanda/InputAssist/

Demo:

Get Adobe Flash player

Changelog for 0.1 release:

[+] Added style “highlightBackgroundColor” to InputAssist
[+] Added Ant build script
[+] Packaged as SWC library

[*] Changed classes namespace to match industry standard
[*] Changed the algorithm for the highlighting of matched text segments, thankfully to
Maxim Kachurovsky “Slov v sapogah” for an idea

[!] Fixed the appearance of popup list when “forceAuto” option is on and
system focus is moved back and forth between 2 different instances of FocusManager class.

Should my Flex 4 AutoComplete to be renamed as InputAssist?

August 25th, 2010

I am currently in a process of updating the code for the component previously known as “Flex 4 AutoComplete component with text highlighting”.

This process get me a chance to think a little bit about component branding.

What I’ve recently had learned that AutoComplete is a very common name for the component with such a functionality.

I know, at least, 4 different Flash platform contenders, all of them use “AutoComplete” word in its name:

  1. com.adobe.flex.extras.controls.AutoComplete control written for Flex 2;
  2. Flextras commercial component AutoCompleteComboBox;
  3. Hillel Coren’s AutoComplete control;
  4. And the last, but not least, the Flex 4 AutoComplete component written by Tenger Ivan.

While “AutoComplete” is a very strong brand and it is well known to the end users, I’d like to bring into Flex components naming world more creativity.

What if I will rename AutoComplete control to InputAssist? You see, my component not only “auto-completes”, it is also providing an helping hand to faster select the value in need from the list of options.

So, in general, what do you think of InputAssist name?

FTE cannot display Thai characters under Windows XP English using default set of device fonts

May 5th, 2010

   Are you unlucky user of Windows XP English with clean install?

   If yes, then probably you cannot see Thai characters in Spark text components (e.g Spark Label) in the following demo:

Get Adobe Flash player

Code listing: http://gist.github.com/390993

    New Flex Spark Label to display text uses the new Flash Text Engine (FTE) introduced in Flash Player 10. And that’s true, FTE brought many very useful new features (text subscript, bidirectional text to name a few) that were previously impossible to achieve with older flash.text.TextField object, but I’ve learned recently that FTE can fail to display Thai characters under Windows XP clean install.

   Eric Y Muller explains the technical details at Adobe forums:, why FTE cannot use neither Tahoma nor Microsoft Sans Serif standard Windows XP fonts as device fallback to display Thai characters.

   The only workaround for this issue for the frustrated user would be to insert original CD with Windows XP install and install Thai language support using “Regional and Language Options” control panel. But this would impossible to achieve for the anonymous employee that works for trans-global corporation without admin access account %-)

   As for the developer of the Flash application we can always embed font into Flash app that contains Thai characters, but this will impact the overall bytesize of the resulting SWF file.

ps
   In 2010, Windows XP remains highly relevant OS. Various stat counters show that 52% - 64% computer users use Windows XP. Thus it is very important for any Flash application to play nice under this OS without an hassle.