< Previous PageNext Page > Hide TOC

Legacy Documentclose button

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.

Expanding on the Basics

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.

In this section:

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


Application and Window Behavior

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:

  1. Click the Edit menu. Its items appear and disappear when you release the mouse button, as with any application menu.

  2. Click the minimize button. Click the window’s icon in the Dock to get the application back.

  3. Click the close button. The Currency Converter window disappears. Quit the application and launch it again.

Controls and Text

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.

Menu Commands

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:

Document Management

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.

File Management

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.

Communicating With Other Applications

Cocoa gives an application several ways to exchange information with other applications:

Custom Drawing and Animation

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.

Internationalization

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.

Editing Support

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.

Printing

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.

Help

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.

Plug-in Architecture

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.



< Previous PageNext Page > Hide TOC


© 2002, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-10-03)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.