Wednesday, December 23, 2009

Overriding the Java toString method using For loop

This solution uses a combination of reflection, constants and regular expressions to build a meaningful toString output:



private String TO_STRING_FIELDS = "email isPKI isTrustedRoot isITU_TX509 caName"

@Override public String toString() {
try {
StringBuilder result = new StringBuilder();
//String newLine = System.getProperty("line.separator");
String[] fieldList = TO_STRING_FIELDS.split( "[\\s,]+");
Field curField = null;
for (String f : fieldList) {
curField = this.getClass().getDeclaredField(f);
result.append(" " + f);
result.append(": ");
//requires access to private field:
result.append( curField.get(this) );
}
} catch (Exception e) {
System.out.println("Error in toString for class: " + this.getClass().getName() + "\n" + e.getMessage());
}

return null;
}

Refurbishing a Morgan 30

Hull Preparation and Painting

After all the work I did restoring my Flying Scot this summer, I can imagine what a labor (of love) this Morgan 30 refurb was. There are a lot of great tips and experience to be gleaned from his notes.

Tuesday, December 22, 2009

Flex Relative paths for embedded images in buttons with @Embed

Flash, Flex and ActionScript: Relative paths for embedded images

Setting the @Embed source with an absolute path (based on flex_src root) :
<mx:Button icon="@Embed(source='/assets/blue_delete.png')" />

Wednesday, December 2, 2009

as3corelib Contains Crypto Utils for FLEX MD5 Hash

I needed to store MD5 hashed passwords in the database and was delighted to find the as3corelib already contains the necessary libraries.  While there are many more features, I only needed the MD5 Hash function so I only copied in the com.adobe.crypto.MD5 and com.adobe.utils.* files.  With the .as files added to my Flex project, hashing the password was a simple as calling:

    MD5.hash( passwordComp.getPassword() );

 

I verified that it returns the same hash as the MySQL MD5 function

UPDATE user_table SET password = MD5(‘change_me’) WHERE username = ‘sandman’

Monday, October 26, 2009

Dynamically position a Flex popup relative to a button mouse click

I was having trouble with positioning a wide popup window that was in a component nested several containers deep.  The PopUpManager.centerPopUp method would center on the container and push the window off the right edge of the screen.  To correct this, I moved the popup over 100 pixels using:

popup.x = popup.x – 100;

That worked, but the problem would come back if the window grew wider.  Bush league.  To make it a bit more reliable, I changed to use the mouseEvent localX/Y properties.  Note you have to convert the “local” coordinates to “global”:

 

      private function showPopup(event:MouseEvent):void

        {

            pop = EnotaryRegReturnPopup(PopUpManager.createPopUp(this,EnotaryRegReturnPopup,true));

            pop.title = "Please select return reasons.";

            pop.showCloseButton =true;

            pop.enotaryRegReturnList = this.enotaryRegReturnList;

            pop.buildUnselectedReturns();

           

                  // position the window next to the "add" button

            var pt:Point = new Point(event.localX, event.localY);

            pt = event.target.localToGlobal(pt);

            pop.x = pt.x - pop.width;

            pop.y = pt.y;

           

            pop.addEventListener("close",removeMe);

            pop["cancelButton"].addEventListener("click", removeMe);

            pop["okButton"].addEventListener("click",processReturns);

        }

 

 

Monday, October 5, 2009

Delete rows from DataTable using DataRow

I was surprised by the delete functionality in DataTable.  It was my understanding that when you call DataRow.Delete it marks the row for deletion, but waits for a DataTable.AcceptChanges call before actually removing them, but when I tried to delete inside a For Each dr as DataRow in dt.Rows I was getting an error “Collection was modified”.  Thankfully I ran across a thread that suggested walking backwards through the collection to avoid those types of errors.  I ended up with this to solve my problem:

 

        Do While i >= 0

            If dt.Rows(i)("KEEP_ME"))) = False Then dt.Rows(i).Delete()

            i -= 1

        Loop

        dt.AcceptChanges()

 

 

Monday, September 28, 2009

DOS BAT File Copy Rename with String Manipulation

I needed to rename a directory of files to truncate the file names to the first 5 characters (plus the file extension).  I found one example that showed how two write a FOR loop that broke out the filename and extension and passed it to a second BAT file:

for /f %%a IN (’dir /b *.txt’) do call runner.bat %%~na %%~xa

And using a second example that covered DOS string manipulation, I put together this batch file that accepts two parameters and does a substring to get the first 5 characters of the first parameter:

set file=%1
set file=%file:~0,5%
ren %1%2 %file%%2