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.