Filtering File Selectors

Why is it that almost every language/framework offers totally different, and yet similar, solutions for filtering in/out files from selection menus?


.NET

OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"

In the example there are two types of selectors: text files (.txt); all files (*.*). Labels (Text files) are separated from their filter values (*.txt) by the pipe character and each filter type is separated by… a pipe too! It looks weird, but considering it’s based off old-school Windows API I can understand and sort of respect their desire to not change. MFC sucks and using weird DSLs instead of creating more objects can have it’s benefits.


Flash

var filters:Array = new Array();
filters.push(new FileFilter('Text Files', '*.txt'));
filters.push(new FileFilter('Rich Text', '*.rtf'));

var file:flash.net.FileReference = new FileReference();
file.browse(filters);

FileFilter is constructed with the rules like so: FileFilter(‘Text Files’, ‘*.txt’). You can either specify multiple FileFilters or append multiple rulsets to the same label like so: FileFilter(‘Text files’, ‘*.txt;*.txt2′). Truly a cleaner interface than .NET’s, but many could argue it’s Object over kill (AWT/SWING is probably worse).


Gtk+

GtkFileFilter *file_filter = gtk_file_filter_new();
gtk_file_filter_set_name(file_filter, "Log Files (*.bab)");
gtk_file_filter_add_pattern(file_filter, "*.bab");

Even though Gtk+ is in C, it has a nice interface (which many languages like Flash could be accused of robbing). The name is different than the pattern, and there aren’t ugly crazy separators like in .NET/MFC.


Swing

ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg");
filter.addExtension("gif");
filter.setDescription("JPG & GIF Images");

JFileChooser dialog = new JFileChooser();
dialog.setFileFilter(filter);

This isn’t too bad until you realize that ExampleFileFilter is a custom class which extends abstract FileFilter. Yes there are other ways, but boy is this annoying!


Python (Gtk+)

        dialog = gtk.FileChooserDialog(title="Select the World"
                            , action=gtk.FILE_CHOOSER_ACTION_OPEN
                            , buttons=(gtk.STOCK_CANCEL
                                                    , gtk.RESPONSE_CANCEL
                                                    , gtk.STOCK_OPEN
                                                    , gtk.RESPONSE_OK))
        filter = gtk.FileFilter()
        filter.set_name("All files")
        filter.add_pattern("*")
        dialog.add_filter(filter)

Just like Gtk+ but with the Python-esk feel.


Conclusion

I must be stating the obvious: Framework developers hate programmers.

IE7, IE8 Beta2… meet <object/>

Why is it that IE7 and IE8 Beta 2 still can’t quit get the <object/> tag correct? When using Javascript to “dynamically” add object tags to the interface my experience, bad luck, and bouts of pain teach me that using the DOM API just doesn’t cut it. Well, to be fair the following functions don’t cut it:

  • document.createElement
  • document.createAttribute
  • [DOMElement].setAttribute(key, value)
  • [DOMElement].setAttributeNode([DOMAttribute])

For any and all other “dynamic” HTML modifications the above functions work great! But if I use them to embed Flash or Silverlight movies it just tanks. Instead you have to construct ugly strings that represent the HTML you want and use [DOMElement].innerHTML. When you do this, suddenly the .swf/.xap loads and all is well.

Why oh why?

And for anyone who cares, following file does a decent job of HTML generation within Javascript… except for IE and the <object/> tag…. and input of type radio (WTF!).

http://cowarthill.com/blog/wp-content/uploads/2008/11/html_obj.js

Silverlight vs. Flash

I’ve been a long time hater of Flash as a web-service consumer because of its inability to use any HTTP method other than GET or POST. People revert to stupid URL argument tricks like ?_method=DELETE and actually use a GET or POST, neither of which work if your service layer is actually … truly … RESTful.

Silverlight has the same damn issue. Apparently none of the blog posts I read a WHILE back (2+ yrs) mentioned the fact that Flash, and now Silverlight, use the lowest common denominator for their web request classes…. NPAPI (Gecko/Mozilla Plugin SDK). I’m sure there are good reasons for using the embedded ones, security, re-usability, performance too – but it’s rather annoying!

Why the NPAPI developers don’t augment the API, I have no idea. But at least Silverlight allows for a HTML event to trigger the OpenFileDialog.ShowDialog() without having to do fun invisible overlays.

Silverlight:
http://wilcob.com/Wilco/Silverlight/http-requests-in-silverlight.aspx

Flash:

http://www.adobe.com/devnet/flashplayer/articles/fplayer10_security_changes_02.html#head34

http://livedocs.adobe.com/flex/3/langref/flash/net/FileReference.html
http://theflashblog.com/?p=423