Page 1 of 1

Provide a mechanism for storing resources

Posted: Fri Aug 07, 2009 10:37 am
by ConvertFromOldNGs
by Craig Shearer >> Fri, 17 Mar 2000 0:57:16 GMT

JADE should provide a mechanism for storing resources such as images, sound files etc. at development time.

I often find that I want to have some images provided with the application which are loaded into controls at runtime. The best way to do this is to have an invisible Picture control on the form, and load it with my image. Such a facility does not exist for other types of binary data though, such as .wav files.

What would be ideal is to have some sort of resource repository that could be extracted with a schema. This would solve a similar type of problem with custom controls in JADE. Often a custom control has to have some images loaded into it at runtime. In JADE you can't do this, and you'd need to define a form that goes with the control just to store the control's images.

Comments?
--
Craig Shearer
Email craig.shearer@bigfoot.com

Re: Provide a mechanism for storing resources

Posted: Fri Aug 07, 2009 10:37 am
by ConvertFromOldNGs
by Torrie Moore >> Fri, 17 Mar 2000 1:06:32 GMT

I agree. Currently we are storing these pictures/ wave files etc as references / collections on objects. This requires a script to load them into the database at startup. Having the ability to store them similar to a resouce file would be great.

Torrie
torrie@concept-eng.co.nz

Re: Provide a mechanism for storing resources

Posted: Fri Aug 07, 2009 10:37 am
by ConvertFromOldNGs
by Craig Shearer >> Mon, 20 Mar 2000 21:48:24 GMT

Having thought about how to work around this, I've come up with a Control subclass that accomplishes this.

The idea is that if you build a control, you can store whatever you want on instances of the control painted on a form. Therefore, I've designed a control to do just this. The control is a subclass of BaseControl.

Basically, the idea is that you'd create a form called (say) ResourceForm. Then you'd paint BinaryContainer controls on the form - one for each resource you wanted to store in your schema. In the Painter, you can then use the control's property sheet to load a binary from a file and store it in the binary property on the control. When the form is saved, the binary's value will be saved in the control.

Then, when you want to access your resources at runtime, you use the following example method, on either app or global:

getResource(resourceName: String): Binary;
vars
f : ResourceForm;
ctrl : Control;begin

create f transient;
ctrl := f.getPropertyValue(resourceName).Control;
if ctrl <> null and ctrl.isKindOf(BinaryContainer) then
return ctrl.BinaryContainer.binary;
else
return null;
endif;
epilog
delete f;
end;

as in

vars
data : Binary;begin

data := app.getResource("binData");
// use the data retrieved from the schema's resources..

The above code creates an instance of the form, then attempts to find a BinaryContainer control based upon the name passed to it. If successful, it retrieves the value of the binary property off the control, and returns it. The form is then deleted.

How to make it work:

I have attached a .cls extract file with this post. To make it work in your schema, do the following:

1. Find the BaseControl class.
2. Add a new subclass - BinaryContainer (this is very important - if you just load the .cls file, it won't work!)
3. Use the schema load to locate and load the BinaryContainer.cls file (supplied)

Now, use the class change dialog to set up the control's design time Properties and Events as follows:

Properties: binary, description, name
Events: none

Now, go into the Painter and create your resource form. Paint a BinaryContainer control on your form. The control will be invisible at runtime, but has been designed to show the name of the control in the Painter. Go to the properties sheet and you'll see, on the specific tab, that is has a binary property. This shows the current value - whether it is null or contains binary data, and if you drop it down, you can select change to bring up a file open dialog to load a new file.

You can save your form and the binary data will be saved in the control with it. You can verify this by going to the class browser and looking at the instances of the BinaryContainer control - you should see your binary data stored on the control.

Then, you can implement a method on either your Application or Global subclass (or whereever else!) to get at the resource at runtime.

Enjoy!
Craig.

Re: Provide a mechanism for storing resources

Posted: Fri Aug 07, 2009 10:37 am
by ConvertFromOldNGs
by Carl Ranson >> Mon, 20 Mar 2000 22:19:02 GMT

Good, pragmatic solution to the problem, Craig.

Does the control as shown in painter give you any feedback on what is stored there?
It would be nice if it showed the filename, binary size and some indication of data type when you edit it in painter.

CR

Re: Provide a mechanism for storing resources

Posted: Fri Aug 07, 2009 10:38 am
by ConvertFromOldNGs
by Craig Shearer >> Mon, 20 Mar 2000 22:41:36 GMT

Well, it could easily be expanded to have those features... I just provided it as a demo of what's possible though. I think the idea of having the filename is good - at least it allows you to see what file it was originally loaded from, though you could put all this sort of stuff in the description property on the control.

Craig.

Re: Provide a mechanism for storing resources

Posted: Fri Aug 07, 2009 10:38 am
by ConvertFromOldNGs
by Mike Hoorens >> Tue, 25 Apr 2000 21:21:16 GMT

I'm all for that idea.

I wrote a simple resource manager a while ago which accomplishes something like this. The idea was that code would reference resources
by global constants through a call to the resource manager. The
resource manager knew where the file physically lived, and could load
it in if it wasn't already in an object (for when you do a clean
schema load).

But the idea was that once you've loaded the image once, the resource manager stores it in a suitable object, and allows you to reference it from there in the future.

This was fine for small images like you mention, but you probably
don't want the resource manager forcing things out of your cache when
you try to load large .WAV files and what have you.

Cheers, Mike.
Mike Hoorens