Archive for the ‘Quick Tips’ Category

Mac OS X: A Keyboard Shortcut for Maximize Window

Wednesday, July 30th, 2008

This is probably one of the most useful ‘custom’ tweaks I’ve added to my mac since I got it – I use it constantly.

Add a Keyboard Shortcut to Maximize the Current Window using Quicksilver

I use Command-Shift-M.

Otherwise Blow Up

Wednesday, July 30th, 2008

Our applications frequently need to convert XML documents between systems. While straight XSL does a great job at mapping System A’s enumerations to System B’s, I’ve found it’s very handy to include an <xsl:otherwise> to make sure mismatches are mapped to an obvious error value instead of just being quietly lost in translation.

Let’s say my homegrown candy inventory system uses numeric enumerations to indicate different types of candy, but our vendor-made candy production system uses text codes. The following XSL snippet will translate MyCandyType to VendorCandyType:

      <xsl:element name="VendorCandyType">
<xsl:choose>
<xsl:when test="MyCandyType = 100">MILKCHOC</xsl:when>
<xsl:when test="MyCandyType = 200">DARKCHOC</xsl:when>
</xsl:choose>
</xsl:element>

This works great as long as MyCandyType is 100 or 200, but if I pass in 300, VendorCandyType will be blank. Is 300 a valid enumeration? Maybe it indicates white chocolate (yuck), and I need to add that
enumeration to my XSL, or maybe my source data is just wrong. The point is, I’m trying to map between values, and throwing the source value away makes it very difficult to diagnose the problem. It’s even worse if that field is optional, because the target system will accept the input and give no indication the data was lost.

By including an <xsl:otherwise> clause that maps the unknown value 300 to the string UNKNOWN VALUE(300),
I can rest assured that schema validation in the target system will reject this value. However, I’ll still be able to see what the source value was and determine that it was the XSL that didn’t know how to deal with it. Take a look:

      <xsl:element name="VendorCandyType">
<xsl:choose>
<xsl:when test="MyCandyType = 100">MILKCHOC</xsl:when>
<xsl:when test="MyCandyType = 200">DARKCHOC</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('UNKNOWN VALUE (', MyCandyType, ')')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>

My vendor system is expecting MILKCHOC or DARKCHOC. It’s not expecting UNKNOWN VALUE(300).
It will reject the input, immediately and clearly showing me the nature of the mismatch between the two systems. It might hurt, but blowing yourself up makes your bugs much easier to catch, and thus to fix.

Defining Dynamic XML Literals in ActionScript 3

Friday, July 25th, 2008

Recently, I was working on my first Flex project where I was consuming XML via a service. Whenever I wanted to make data changes with the service, I was required to send in an XML packet. Since AS3 has native support for XML literals, I wanted to create my small XML packet inline. For example:

var changeSet:XML =
ACTIVEQUOTE
;

Continue reading»

Charles, SSL, and you

Tuesday, July 22nd, 2008

Is Charles messing with your SSL traffic? You can fix it by telling Firefox (or, shudder, IE) that the Charles SSL proxy is a valid certificate provider. Here are the instructions:

Use grep? Try ack!

Thursday, July 17th, 2008

For you command line addicts like myself.

I find myself using grep constantly to search within files. Most often in the format ‘grep -r “phrase” * | grep -v .svn’ To search subdirectories, and then filter out any subversion files. Which sucks.

I recently stumbled upon ‘ack’, which is a similar search function, but with a whole lot more features, and way faster by default.

With it, I only need to type ‘ack “phrase” *’ to get the same result. Neat!

Some reasons to use ack:

  • Ignores subversion directories by default.
  • Color highlighting of matches.
  • Full perl regular expression search capable
  • type specific filtering with –php, –perl, etc
  • Regular expression based file matching

It’s neat. Check it out: http://petdance.com/ack/

Extending Column Background Color–without Images!

Thursday, July 10th, 2008

Lets go straight to the HTML:

My title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

My title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

My title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

and its corresponding style:

* { margin: 0; padding: 0; }
body { text-align: center; /*Hello IE*/ }
p { padding-bottom: 1em; line-height: 1.4; }
#canvas { width: 49em; margin: 0 auto; text-align: left; background-color: #F0F0F0; }
#right { float: right; width: 25%; }
#right ul { margin-left: 2em; }
#left { margin-right: 25%;background-color: #FFF; }

Explanation:
Use the id “canvas” to assign the right column color. Then give id “left” a background color of white.
Click here to view an example.

Enabling A Drop Shadow On A Flex Container

Thursday, June 26th, 2008

To set a drop shadow on say a VBox or HBox component you simply set the boolean property dropShadowEnabled to “true” — but this is not enough to make the drop shadow appear. An example:

  1. 

To have the drop shadow appear (with its default shadow color) you need to specify a value for borderStyle since the default is none. Setting the borderStyle property doesn’t necessarily mean you have to display a border, but it is needed for the drop shadow to appear. So if the border is not desired, simply set the style property borderThickness to 0. An example:

   1. 

The above sample will display a drop shadow for the HBox component without a visible border.

In praise of System.Object

Wednesday, June 25th, 2008

In this Quick Tip, I show you how to make a method that deals with System.Object parameters more type-safe by converting it to use generics. Last year I had written a wrapper class to talk to the ASP.NET caching system for me. It returned cached objects as System.Object for maximum flexibility. The client would cast to the desired type, but it always bugged me, because it wasn’t very type-safe. Consider the following chunk of code:

Cache.Set("WhenDidIDoThis", DateTime.Now);
...
int numberOfTicks = (int) Cache.Get("WhenDidIDoThis");

It will compile just fine, but it will blow up when I run it, because silly me, WhenDidIDoThis contains a DateTime, not an integer. It’s far safer to rewrite my code to use generics:

Cache.Set<datetime>("WhenDidIDoThis", DateTime.Now);
...
int numberOfTicks = Cache.Get<int>("WhenDidIDoThis");

Hmm. Actually, that same logic error still compiles just fine, but hey, at least my code now signals its intent more clearly. The bigger problem is now I’m getting a compiler error elsewhere. Let’s see why.

Continue reading»

Using ColdFusion and LCDS in AIR-powered Flex Applications

Thursday, June 12th, 2008

Previously, I’ve discussed how to use ColdFusion Components (CFCs) in AIR-powered Flex applications but I never got a chance to follow up on how to use CF8′s integrated LiveCycle Data Services ES in AIR.

I’ve figured out that all I need to do is specify a ChannelSet component that is reused in both the Consumer and Producer. Since I was developing locally, I set up my AMFChannel to use my local server. You can see in the following example:



cf-polling-amf




I know there are other channels, such as RTMP, but I have not used them yet. Since I was learning how to use CF/LCDS in AIR, I used it in an AIR version of a simple Chat application. I was quite impressed how easy this was to do. By simply setting up the ChannelSet, I had a working LCDS powered AIR application.

Layers of Java Logging

Wednesday, June 11th, 2008


The Java Logging API is great for tracking the internal workings of your application, both during development and after it goes to production. The API is based on a highly flexible configuration model that gives you complete control over what gets logged and where it gets logged.

I recently started using the API after some time away, and it took a bit to get reacquainted with some of its concepts. In my first few attempts, I ended up with either all logging statements output or none, when what I really wanted was output from only a few specific classes.

I think my problem stemmed from confusion between the various ways to filter output. In the end, I found it easiest to think of the API as two separate layers – a filtering layer that answers the question “What should be output?” and a handler layer that answers the question “How should it be output?”

Below is an example configuration divided into two sections. Although they both end up in a single logging.properties file, I’ve broken them up to demonstrate the concept of separate layers. In the first, I define a default filter level for the entire application, as well as custom filter levels for select packages and classes.

Filter Layer

# Set the global filtering level to INFO. By default all statements below the
# INFO level will not be logged.
.level = INFO
# Set the filtering level for a few packages to ALL. This overrides the
# global level, so all statements in these packages will be output.
# This is good for limiting output to a section of code you're currently
# working with.
com.arc90.package1.level = ALL
com.arc90.package2.level = ALL
# Set the filtering level for a few individual classes. Again, this overrides
# less specific settings. In this case, all statements of level FINE and
# higher in ClassA will be output, and all statements of level FINER and
# higher in ClassB will be ouput.
com.arc90.package3.ClassA.level = FINE
com.arc90.package3.ClassB.level = FINER

Next, I’ll define the handlers that will be used. In addition, I’ll specify filters for the handlers themselves. This performs high level filtering on the final output from the filtering layer. Here, I say that I want only INFO level and higher statements sent to the console, and all statements sent to the log file. The important thing to remember is that these levels apply to the output from the filter layer only – if statements were already filtered out in that layer, they will never make it this far.

Handler Layer

# Specify the handlers to be used. Multiple handlers can be specified
# in a comma separated list.
handlers = java.util.logging.ConsoleHandler,java.util.logging.FileHandler
# Specify the formatters that will be used for each handler
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
# Specify the log file location for the FileHandler. In this case the log will
# be written to my home directory, in a file named example.log.
java.util.logging.FileHandler.pattern = "%h/example.log"
# Finally, specify the filtering level for each handler
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.FileHandler.level = ALL

Thinking of the Java Logging API this way has helped to clear things up for me, and I hope it will for others as well. If you haven’t used the API, you can try it out for yourself by dropping the above text into a logging.properties file (make sure there aren’t spaces at the end of the lines or it won’t work), which must be palced into your JVM’s lib directory or a path specified as a -Djava.util.logging.config.file VM argument. Finally, just add some logging statements to your Java code and you should start seeing output to the console and the log file.