Change vs Click in the Flex 3 DataGrid

April 18th, 2008

(related to a tip I found, but for Flex 3 and my own memory…)

In Flex3 you can be notified of changes to selections in your datagrid from using listeners to either the ‘click’ or ‘change’ events, the difference being that if you only listen for click events you won’t be notified when the user navigates or selects with the keyboard (obvious, but confused me for a bit). This seems a shame given Flex’s pretty good handling of keyboard navigation, so unless you only want mouse click events use a ‘change’ handler as standard.

Using a Click Handler:

<mx:DataGrid id="DG1" click="clickHandler(event)"/>
<mx:Script>
  public function clickHandler(event:MouseEvent):void
  {
      someControl.text = event.currentTarget.selectedItem.someDataField;
  } 
</mx:Script>

Using a Change Handler:

<mx:DataGrid id="DG2" change="changeHandler(event)"/>
<mx:Script>
  public function changeHandler(event:Event):void
  {
      someControl.text = event.target.selectedItem.someDataField;
  } 
</mx:Script>

The joys of regex

December 11th, 2007

So a couple of days ago someone (forgotten who though, sorry!) put up an interesting post about using Regex (Regular Expressions) in Eclipse to save you loads of repetative typing. As a follow up I thought I’d show a real life example of using regex that just saved me a whole heap of laborious grunt work.

The problem

A client asked me to update the images on a site I’m building for them. The image details are all in a simple xml file with some details and a link to the image file, e.g…

<images>
<image file="img1.jpg"><client>Foobar</client><by>Someone</by></image>
<image file="img2.jpg"><client>Barfoo</client><by>Somebody Else</by></image>
...
</images>

The client however supplied me with a text doucument that looked something like…

1. Foobar/Someone
2. Barfoo/Somebody Else
....

…and a bunch of jpeg files that had been named to match the document, so they were actually called ‘1. Foobar_Someone.jpg’ etc and needed to be renamed for safe use on the web (I never like having mixed case and spaces in web filenames).

Now as there were around 80 of these files it could have been a long and boring ‘rename & save’ job, then a whole bunch of cutting and pasting, so instead I used Eclipse and Perl’s regex powers.

Eclipse solution

The first thing I did was load the text file in Eclipse, then hit CTRL + F for the find & replace dialogue and checked the ‘Regular Expressions’ box. In the ‘find’ box I put

^(\d*)\. (.*)/(.*)$

This is a fairly simple pattern match using the braces to capture matching ‘groups’ that we can use later. Taking it from the begining…

  • the ^ character matches the start of a line, the (\d*) matches the first numbers
  • the \. matches a litteral dot (the slash is an escape character as the dot normally means match anything)
  • It may be hard to see here, but there’s then a space which we ignore
  • the (.*)/(.*) matches the two groups of words around the slash
  • and the $ matches the end of the line

Then in the replace box I put

<image file="img$1.jpg"><client>$2</client><by>$3</by></image>

The dollar+number means use the contents of capture group n, so you can see I’m simply ‘pasting’ the captured bits in the correct places.

Then just hit ‘replace all’ and job done!
(Hint: You can also use CTRL+SPACE in the find & replace input boxes to remind you of the regex syntax)

Perl solution

Of course I still needed to rename all the files, so next I used the ‘rename’ command. I’m working on Linux, but I believe ‘rename’ comes as part of Perl, so it should be somewhere on your system and work the same no matter what platform.

The syntax for the rename command is…

rename perlexpr [ files ]

…and basically runs the regular expression ‘perlexpr’ on the filename of all files matching [files]. The expression I used here is…

rename -v 's/(d*)\..*/img$1.jpg/' *.jpg

The regular expression is the messy looking bit inside the inverted commas, and it’s matching all the .jpg’s in the folder. Again taking the regex from the top…

  • The “s” means substitute. The syntax is s/old/new/ — substitute the old with the new
  • The (d*) captures the intial number in the filename
  • The \. matches a litteral dot
  • The .* matches anything else after it
  • We then discard all the other crap apart from the captured number, and use it in the substituted filename.

(Hint: more info : http://tips.webdesign10.com/how-to-bulk-rename-files-in-linux-in-the-terminal)

Conclusion

Well it’s taken me a hell of a lot longer to write this post than it did to rename all those files, I wouldn’t like to guess how long it would have taken by hand but I’m sure it was much easier this way.

Try bit of regex yourself, you just might like it! :)

Will it blend…?

December 17th, 2006

So, intrigued by some of the stuff I saw @ FOTB I’ve been playing a little with the latest beta of Microsoft’s Expression Blend and trying to get my head around what exactly it does and how.

There doesn’t seem to be much in the way of info or tutorials around at the moment (other than the few that come with it and a couple of handy ones @ ExpressionBlend.com), so I’ve been pretty much making it up as I go along. The result of my first little expriments is this simple analog clock app…

blend_clock.jpg

As I don’t really know if I’m doing this the right way, and to perhaps help others just starting, I’ve made a video walkthough of the creation of the clock and put the source code up for you to have a look at, if anyone knows a ‘better’ way of making this please comment (sorry about the bored cockney voiceover, it’s scary hearing myself speak! :) )

(Edit - just noticed the right click pop-ups don’t show up in the video, sorry about that, must be a Camtasia issue. I’ll try to fix it if I do another video)

UPDATE - 31/01/07 -
Due to popular demand (well, errr, Roger anyway :) ) the video is now available as a downloadable WMV also. Enjoy.

expressionblend.com for expression blend tutorials, xaml demo, wpf info

AOL Search - The New BigBrother?

August 14th, 2006

Guess you all heard the fuss last week when AOL ‘accidentaly’ released 2 months worth of saved search data from 658,000 users (info here)

While it’s obviously a massive and worrying breach of privacy, it’s also bloody intresting reading, obviously some people seem to think a search engine is a shrink and tell it everything! Who needs to watch a bunch of self obsessed wierdos act up for the camera on BB when all the freakiness of someones ‘real life’ is here in black and white?

Take for instance this womans search data…
http://czern.homeip.net/aolsearch/search2.php?text=711391

This is gripping reading… in the 2 months in question she gets more and more pissed off with her husband, has a few lesbian fantasies, sneakes off and has an affair with a married prison officer she met online (and searches for him & his prison by name…so much for anonymous data!), he gets found out & leaves his wife, he then stalks her, she has a nasty sounding facial growth removed…. while all while searching for good biblical references and quotes like ‘god can heal affairs’!
Wow, my life is soooo boring!

AOL Search Log search…
http://czern.homeip.net/aolsearch/

(EDIT: I may be completely wrong about all of this of course, hey, I’m making it up from a bunch of search strings after all… but that’s what’s so fun, no?)

Safari Bookshelf

June 27th, 2006

If you’re anything like me you’ve got loads of huge old books on long dead or outdated technology gathering dust on your bookshelves, a few of which you’ve probably not even read more than half way.

You’ve probably got a couple more books you plan on buying soon? And there’s also a pretty good chance that a large proportion of all of these books have some strange animal on the front and are published by the O’Reilly network. Well if so, you have to try out the O’Reilly Safari Bookshelf

Safari Bookshelf is like an online library featuring a large chunk of the O’Reilly catalogue (both past & current). Every month you can place a set number of books into your bookshelf (10 on the standard subscription), and you can swap these each month. At only $20 a month it’s pretty good value (one ‘real’ book normaly costing about 3 times this!), and at the moment they have a free one month trial too.

I only joined today but have already added “Head First Design Patterns”, “Information Architecture for the World Wide Web” & “Mind Performance Hacks” to my bookshelf, all titles I’ve looked at but never got round to buying. Now all I have to do is find the time to read them!