Important: The Java API for Cocoa is deprecated in Mac OS X version 10.4 and later. You should use the Objective-C API instead. For a tutorial on using Cocoa with Objective-C, see Cocoa Application Tutorial.
This chapter describes other integrated components of Cocoa. Do you recall how little code was required to build Currency Converter into a working application? You may be surprised how many classes and features come prepackaged with Cocoa to minimize the time you spend coding.
The simplest Cocoa application, even one without a line of code added to it, includes a wealth of features that you get “for free.” You do not have to program these features yourself. You can see this when you test an interface in Interface Builder.
Application and Window Behavior
Controls and Text
Menu Commands
Document Management
File Management
Communicating With Other Applications
Custom Drawing and Animation
Internationalization
Editing Support
Printing
Help
Plug-in Architecture
In Interface Builder’s test mode, Currency Converter behaves almost like any other application. Click elsewhere on the screen, and Currency Converter is deactivated, becoming totally or partially obscured by the windows of other applications.
If you closed the application, run it again. After the Currency Converter window is open, move it around by its title bar. Here are some other tests you can perform:
Click the Edit menu. Its items appear and disappear when you release the mouse button, as with any application menu.
Click the minimize button. Click the window’s icon in the Dock to get the application back.
Click the close button. The Currency Converter window disappears. Quit the application and launch it again.
The buttons and text fields in the Currency Converter window come with many built-in behaviors. Notice that the Convert button pulses as is the default for Aqua buttons that respond to Return key presses. Click the Convert button. Notice how the button is highlighted momentarily.
If you had buttons of a different style they would also respond in characteristic ways to mouse clicks.
Now click in one of the text fields. See how the cursor blinks in place. Type some text and select it. Use the commands in the Edit menu to copy the selected text and paste it in the other text field.
Do you recall the nextKeyView
connections you made between the text fields in the Currency Converter window? Insert the cursor in a text field, press the Tab key, and watch the cursor jump from field to field.
Interface Builder gives every new application a default main menu that includes the Application, File, Edit, Window, and Help menus. Some of these menus, such as Edit, contain ready-made sets of commands. For example, with the Services submenu (whose items are added by other applications at runtime) you can communicate with other Cocoa applications; and with the Window menu you can manage your application’s windows.
Currency Converter needs only a few commands: the Quit and Hide commands and the Edit menu’s Copy, Cut, and Paste commands. You can delete the unwanted commands if you wish. However, you can also add new ones and get “free” behavior. An application designed in Interface Builder can acquire extra functionality with the simple addition of a menu or menu command, without the need for compilation. For example:
The Font submenu adds behavior for applying fonts to text in text view objects, like the one in the text view object in the Text palette. Your application gets the Font window and a font manager “for free.” See Font Panel for more information.
The Text submenu allows you to align text anywhere text is editable, and to display a ruler in the NSText object for tabbing, indentation, and alignment.
Thanks to the PDF graphics core of Mac OS X, many objects that display text or images can print their contents as PDF data.
Many applications create and manage repeatable, semiautonomous objects called documents. Documents contain discrete sets of information and support the entry and maintenance of that information. A word-processing document is a typical example. The application coordinates with the user and communicates with its documents to create, open, save, close, and otherwise manage them.
See Document-Based Applications Overview for more information.
An application can use the Open dialog, which is created and managed by the Application Kit framework, to help the user locate files in the file system and open them. It can also use the Save dialog to save information in files. Cocoa also provides classes for managing files in the file system (creating, comparing, copying, moving, and so forth) and for managing user defaults.
Cocoa gives an application several ways to exchange information with other applications:
Pasteboards. Pasteboards are a global facility for sharing information among applications. Applications can use the pasteboards to hold data that the user has cut or copied and may paste into another application.
Services. Any application can access the services provided by another application, based on the type of selected data (such as text). An application can also provide services to other applications such as encryption, language translation, or record fetching.
Drag and drop. If your application implements the proper protocol, users can drag objects to and from the interfaces of other applications.
Cocoa lets you create custom views that draw their own content and respond to user actions. To assist you in this, Cocoa provides classes and functions for drawing, for example, the NSBezierPath class.
Cocoa provides interfaces and tool support for internationalizing the strings, images, sounds, and nib files that are part of an application. Internationalizing your application allows you to localize it to multiple languages and locales without significant overhead.
You can get several utility windows (and associated functionality) when you add certain menus to your application’s menu bar in Interface Builder. These “add-ons” include the Font window (and font management), the color picker (and color management), the text ruler, and the tabbing and indentation capabilities the Text menu brings with it.
Formatter classes enable your application to format numbers, dates, and other types of field values. Support for validating the contents of fields is also available.
With a simple Interface Builder procedure, Cocoa automates simple printing of views that contain text or graphics. When a user starts a print action, an appropriate dialog helps to configure the print process. The output is WYSIWYG.
Several Application Kit classes give you greater control over the printing of documents and forms, including features such as pagination and page orientation.
You can very easily create context-sensitive help for your application using the Interface Builder inspector. When the pointer hovers over an object for which you’ve specified help content, a small window appears with the information you provided.
You can design your application so that users can incorporate new modules later on. For example, a drawing program could have a tools palette with a pencil, a brush, an eraser, and so on. You could create a new tool and have users install it. When the application is next started, this tool appears in the palette.
© 2002, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-10-03)