Tip/Tutorial

Java Validated FileChooser

Fri, Nov 4, 2011 - 7:55am -- Isaac Sukin

I happened to be working on a Java project recently where I needed to let the user save files through the GUI. The Swing toolkit has a nice JFileChooser class that lets you show a file save dialog so that the user can choose the directory and name of the file to save. However, not all filenames are valid, and Java doesn't validate them for you by default. If you try to save a file with an invalid name, Java will throw an error, and this is often considered the only way to know if the filename is valid. That's bad practice though since you should never rely on an exception being thrown as a condition of your program running correctly; by definition, exceptions are unreliable and sometimes unpredictable. Additionally if your file saves successfully then you have to immediately delete it (because you were only saving it to test the filename) and that's messy. So I wrote a ValidatedFileChooser class that checks various criteria to make sure that filenames are valid before attempting to save the file, and alerts the user if one of the criteria fails. The class is below, and I'm releasing it to the public domain.

Counting Characters in a Textarea with JavaScript

Tue, May 17, 2011 - 1:01am -- Isaac Sukin

About a year ago, I wrote about how I was using the JavaScript onKeyPress event to count the characters in a textarea. It turns out that among newer browsers, the trick I employed only works in Firefox. Other browsers (IE, Chrome, Safari, Opera) don't register keypress events for non-character keys like Control and Backspace. KeyUp and KeyDown have their own problems, but we can get around this by updating the character counter on both the KeyDown and KeyUp events. I've built a test below so that you can try it for yourself.

How to Write a Drupal 7 Installation Profile

Mon, Jan 10, 2011 - 1:23pm -- Isaac Sukin

One of the features I'm most excited about in Drupal 7 is the greatly improved flexibility of installation profiles. However, documentation on writing an installation profile for Drupal 7 currently doesn't exist, so I thought I'd change that. Here's what I've discovered by reading through the code.

Installation profiles are like modules

Fancy jQuery Slide-Out Effects for Large Page Elements

Wed, Aug 25, 2010 - 9:04am -- Isaac Sukin

Sometimes, there are things in my blog posts that just don't fit nicely into the width of the content area. This is a problem with code snippets and images in particular; I only have a certain amount of horizontal space, but often that's not enough.

Inspired by a solution I witnessed in action at Lullabot.com (and the place Lullabot discovered it, Viget.com) I finally decided to solve this problem using some fancy jQuery. Now, all code blocks on this site fit correctly into the content area, with any excess text hidden until your mouse hovers over the code block. All images are automatically shrunk, until your mouse hovers over them, at which point they will enlarge to their original size. Pretty sweet! And it's all cross-browser-compatible.

Pages

Subscribe to RSS - Tip/Tutorial