[Drawkit] Filetype binding mechanism

Brad Larson larson at sonoplot.com
Sat Jul 19 08:50:35 PDT 2008


Thanks, this is a very clean solution.  I wish I'd thought of it.

I look forward to the new beta.  I'm holding up release of our drawing  
program in anticipation of it, to make sure that we're using the  
latest framework for the underpinnings.

On Jul 18, 2008, at 1:45 AM, Graham Cox wrote:

> General Info, but particularly directed at Brad.
>
> The next beta (due any time - should be in the next few days)  
> includes a general mechanism to expand the types of files that  
> DKDrawingDocument is able to read and write without the need to  
> subclass it or hack around with its code. Originally Brad was  
> looking to do this as a way to read and write SVG and other file  
> types, and his solution was to use the file type string to build a  
> method name which was then invoked. Unfortunately this approach  
> suffers from two problems - 1. the file name string must be  
> compatible with allowed method naming - where the file type is a UTI  
> that is not the case, you end up with an illegal method name that  
> contains dots. 2. The string concatenation approach doesn't permit  
> the same method to be invoked by different file types. This is a  
> much smaller problem because for one thing it's not that likely to  
> occur and for another it's easy to simply set up two methods, which  
> ultimately call the same one.
>
> Nevertheless the new mechanism solves both of these issues. It's  
> very simple too.
>
> An application that wishes to extend the types of files will  
> implement the data translation methods as a category of DKDrawing.  
> For file import, a class method that accepts NSData and returns a  
> DKDrawing instance is required. For file export, a parameterless  
> method that returns NSData is required. You can call them whatever  
> you want. Then your app needs to register these methods against the  
> file type or types the methods work with with the DKDrawingDocument  
> class. The methods are:
>
> + (void)	bindFileImportType:(NSString*) fileType toSelector:(SEL)  
> aSelector;
> + (void)	bindFileExportType:(NSString*) fileType toSelector:(SEL)  
> aSelector;
>
> The default methods in DKDrawingDocument for - 
> readFromData:ofType:error: and -dataOfType:error: now use the  
> registered information to look up the method needed and invoke it.  
> The standard drawing types are automatically pre-registered so  
> things work exactly the same as before for the default situation.
>
> You are still free to subclass DKDrawingDocument or to use some  
> entirely different NSDocument class in your app in which case you  
> are responsible, as with any Cocoa app, for handling your file I/O  
> as you need. It also has no bearing on the plist or filetype to  
> document class mapping  that Cocoa does anyway - this is purely a  
> supplementary mechanism that extends DKDrawingDocument for the  
> common need of handling more file types.
>
> cheers, Graham
> _______________________________________________
> Drawkit mailing list
> Drawkit at lists.apptree.net
> http://lists.apptree.net/listinfo.cgi/drawkit-apptree.net

______________________
Brad Larson
SonoPlot, Inc.
3030 Laura Lane, Suite 120
Middleton, WI 53562





More information about the Drawkit mailing list