Cocoa Applications
"Two nibs are better than one."
Anonymous
Now that you know a bit about what makes Mac OS X tick, let's jump right in and create a document-based application using Cocoa. If you're reading this book, you might have created simple nondocument-based applications in Cocoa. We'll be doing this throughout later chapters as test-bed programs are created. You know how simple they are to put together quickly. The document-based application, however, is a bit more complex. But once you see one examined, you will have little trouble creating your own.
Document-based applications allow a user to work with multiple documents at the same time. Whereas a nondocument-based application usually has one window and can only manage one set of data at a time, document-based applications can have many windows open simultaneouslyeach accessing different source data. One window is always the focus of the user while the other windows are ready and waiting for input, sitting quietly in the background. When discussing sets of data, having the ability to open multiple views simultaneously gives you the ability to work in parallel on many tasks.
NOTE
When I refer to a set of data, this can mean file-based data sitting on your local hard disk or stream-based data that is transferred over a network, possibly from a database. A document-based approach can be used to edit either of these types of data.
Mail.app, Microsoft Word, Adobe Photoshop, Project Builder, and Interface Builder are all examples of applications that take a document-based approach. You can have multiple images open in Photoshop and edit between them, back and forth, as much as you like.
As of this writing, iTunes and iPhoto are both examples of applications that are not truly document based. That is, they can only operate on one item at a time. ITunes only plays one song at a time. IPhoto only allows you to edit one image at a time. When developers refer to document-based applications, they usually mean the ability to open multiple documents at once. Windows users know this as the multiple document interface (MDI).
The specific requirements of your application will determine whether it should be document-based or not. If your users want to work with more than one set of data, you probably want to use a document-based application. Many times, this will be obvious; sometimes it will not. If you're unsure, look for other applications that work similarly to yours and see how you might learn from their approaches. Also, be sure to talk to potential users of your application to see what they would expect from it.
The Result
Let's discuss the document-based application that we will be examining in this chapter. RadarWatcherX, shown in Figure 3.1, allows you to create, open, and save multiple documents that repeatedly load a Doppler radar image and scan a "watch box" for specific colors in the image. This allows you to choose your local radar image from any weather-related Web site and keep an eye on a specific area within the image for storms or heavy rain that might be approaching. If the colors you choose appear in the watch box, an alarm will sound!
Figure 3.1 A RadarWatcherX document window.
Although you might not be able to tell from the screenshot, RadarWatcherX implements many common (and some not so common) tasks that you might need when building applications in Cocoa. Some of these include
Document window management
Saving and opening XML property lists (plists)
Custom views
Scrolling views
Timers
Internet connectivity
Image manipulation
Application preferences
If you are not familiar with some of these items, don't worry. I will discuss them enough here to get you started, and some (such as property lists) will be elaborated on in future chapters. Let's dive into the project file and source code and see how this project works.
NOTE
Property lists (plists) are text files that are formatted in either XML format or an older ASCII format. They are a great solution to store preference data because they support many different data types within the text. You can store numbers, strings, even raw data such as icons and pictures. You will see property lists used throughout your Cocoa development experience.