Copyright ©1999 by AAA+ Software Forschungs- und Entwicklungs Ges.m.b.H. All Rights Reserved.
6 Developing Yellow Box Applications with Joy
This section is a step by step tutorial on how to to write a simple application with Joy inside InterfaceBuilder. It can be carried out with the free demo version of Joy as well as with Joy Explorer. If you want to write more complex stand-alone applications you need a Joy Developer license.
Overview You can not only explore the Yellow Box interactively with Joy, you can also write full applications. There is still no need to use the compiler, linker, or Project Builder as with Joy you can test the whole application, not only the interface, in InterfaceBuilder's test mode. Some example applications are included with the Joy distribution. They can be found in the directory %NEXT_ROOT%\Local\Library\Joy\Examples. An application with Joy: LengthConverterWe will now build a very simple Joy application which converts centimeters to inches and vice versa. We will only use InterfaceBuilder.app and Edit.app to do so - no ProjectBuilder, compiler or linker are necessary.If you have not done so yet, start InterfaceBuilder with the Joy palette installed (see the beginning of this document). Choose the New Joy Application command in InterfaceBuilder's Joy menu. This creates an untitled nib file already containing a main menu and a Joy interpreter. Resize the document window and set its title (in the inspector panel) to "LengthConverter". Drag an NSForm from the Views palette into this window. Label the two NSFormCells "inches" and "cm". Your window should now look something like this:
Now Ctrl-drag two connections from the Joy interpreter to the two NSFormCells. Make sure to connect the Cells (black frame when connecting) and not the whole Form (gray frame). In the Interpreter's connection inspector, click the Connect button each time. Name your variables inchesCell and cmCell, respectively.
Now we write the application code. Save your nib-file first, then double-click on the interpreter icon. An alert panel will appear.
Click the Unparse button. A JavaScript source file will be created automatically and opened in Edit. There are two objects known to the Interpreter (inchesCell and cmCell). You can teach new methods to both. Template functions for the action: method, which is invoked when a user interface element is activated (if the user presses Enter in an NSFormCell or clicks an NSButton for example), are already provided. Add the following line to inchesCell's action: method: [cmCell setDoubleValue: [inchesCell doubleValue] * 2.54] Add the following line to cmCell's action: method: [inchesCell setDoubleValue: [cmCell doubleValue] / 2.54] Save the source file and go back to InterfaceBuilder. Choose Test Interface in InterfaceBuilder's Document menu to test your application. You can enter numeric values into either of the two fields, which Enter is pressed the equivalent value is placed in the other . Choose Quit in the Main Menu to quit LengthConverter and go back to InterfaceBuilder. If you want to create a standalone application (that does not need InterfaceBuilder to run) you can use the Save Nib as App... command in the Joy menu (only with Joy Developer version). This application's binaries are very small (about 50 Kilobytes per platform) and run on any Yellow Box system that has a licensed version of the Joy framework installed. Where has my controller gone?If you have developed Yellow Box applications before, or are familar with object-oriented design patterns, you might wonder why we didn't create a class LengthConverterController that acts as the target for both cmCell and inchesCell and implements the necessary actions? Because the Joy interpreter is the controller! Instead of creating a new controller class for each nib file, you can use the interpreter object as a generic controller, once and for all. We teach action methods directly to the user interface objects instead of the controller because we think defining actions in InterfaceBuilder is a drag. The whole target/action mechanism was invented just to overcome the limitations of Objective-C as a compiled language. But Joy will not get in your way at all if you prefer using the traditional style of constructing Model/View/Controller interfaces in Yellow Box.
Debugging with JoyYou can interactively play with your application the same way as you did play with NSWindow and NSSavePanel in section 2 of this tutorial. Simply choose the Command Window menu item in your application's Joy menu while running in InterfaceBuilder's test interface mode. In LengthConverter (see above) you can manually set an NSFormCell by typing js> [inchesCell setDoubleValue: 2] Trigger the conversion by typing js> [inchesCell action: self] Check the result by typing js> [cmCell doubleValue]
Though you can write full-fledged applications using Joy alone, in many cases you will want to add Joy to your existing projects written in Objective-C or Java, maybe adding some new modules written in Joy or just using it as a debugging tool. The next section shows you step by step how you can link Joy to an existing project. |