ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Java Development on Eclipse, Part 2
Pages: 1, 2

Some Essential Skills

There are some additional skills that are good to know about. For example, if you highlight an item in the JDT editor, right-click it, and select Open Declaration, the declaration of that item will open. This is great for tracking down where and how methods and fields were created. Several of those are detailed in this section and all are worth adding to your Eclipse toolbox.



Viewing Type Hierarchies

Another menu item in the JDT editor's context menu is the Open Type Hierarchy; when you select an item in the editor and select this menu item, that item's type hierarchy appears in the Java perspective's hierarchy view, as you see at left in Figure 2-24.

Figure 2-24
Figure 2-24. The hierarchy view

This view acts like an object browser. It lets you explore a type's Java complete hierarchy, and double-clicking an item in this view opens its definition in the editor. That's useful if, for example, you want to see all the members of the System.out class—just highlight System.out in your code and open its hierarchy. You can also open this view by selecting an item in the editor and selecting the Navigate→ Open Type Hierarchy item.

The hierarchy view is not dissimilar from the outline view, which you see at right in Figure 2-24. However, the outline view is designed to show an automatic hierarchy of your code rather than the hierarchy of items you specifically select. As you work in the JDT editor, the outline view is updated automatically to show the hierarchy of the current type you're working with.

Browsing Java Code

There's even another entire perspective dedicated to letting you browse through projects in a Java-oriented way: the Java Browsing perspective. To open this perspective, select Window→ Open Perspective→ Java Browsing; you can see the results in Figure 2-25.

Figure 2-25
Figure 2-25. The Java browsing perspective

This perspective presents the information you see in the standard Java perspective in a new way, and it breaks type and member information into two new views (the Members view here is much like the Outline view in the standard Java perspective). As you'd expect, the views in this perspective are all coordinated—selecting an element in the Projects views makes its packages appear in the Packages view, for example. And selecting an item in the Members view makes that item appear in the editor, and so on.

Searching Code

Eclipse also has great facilities for searching through code in the Search menu. In fact, the main menu items in the Search menu, Search→ Search, Search→ File, Search→ Help, and Search→ Java, all open the Search dialog, although each item corresponds to a different tab in that dialog. Searching is particularly powerful in Eclipse—for example, not only can you search across multiple files, you can also search using wildcards.

The File Search tab in the Search dialog lets you search across multiple files. For example, in Figure 2-26, we're searching for the word main through all .java files in the workspace. The scope of the search is set with the radio buttons in the Scope box, and the default is to search all matching files throughout the workspace (that is, all your projects that appear in the Package Explorer). You can also restrict the search to a working set (covered in a few pages) of projects. If you want to search only the current project or just a restricted number of projects, you can select that project or those projects in the Package Explorer, then open the Search dialog and select the Selected Resources radio button in the Scope box.

Figure 2-26
Figure 2-26. Performing a file search

Clicking Search makes Eclipse search for matches to the text you've entered, and you can see the results in the Search Results view, which appears at the bottom in Figure 2-27. Double-clicking a match in the Search Results view opens the match in the JDT editor, as you can see in the figure, and both underlines the match and points to it with an arrow.

Figure 2-27
Figure 2-27. Viewing search results

You can also perform Java searches with the Java tab in the Search dialog. This lets you search for Java elements by kind—types, methods, packages, constructors, and fields—as you can see in Figure 2-28. You can also limit the search so that it only matches declarations or references.

Figure 2-28
Figure 2-28. Performing a Java search

Being able to search across files and classes is one of the big advantages of using an IDE—if you've been doing Java development using a text editor and javac, you'll find there's no comparison when you start using the project management capabilities like these in Eclipse.

Customizing the Development Environment

Our last topic in this chapter is all about customizing your development environment. Eclipse is easy to customize, starting from the most basic—you can move any view, editor, or toolbar around simply by dragging it.

TIP: If you don't like to work in an environment where things can move around with mouse movements by mistake, you can lock the toolbars with the Window→ Lock the Toolbars menu item. And if a perspective gets all scrambled by inadvertent mouse movements, use Window→ Reset Perspective to restore things.

You can also customize how Eclipse will generate code for you. For example, the default code generation style doesn't place opening curly braces on their own lines:

public void printer( ) {
   System.out.println("No worries.");
}

However, your programming style might be more like this, where each curly brace does get its own line:

public void printer( ) 
{
   System.out.println("No worries.");
}

You can customize this with the Windows→ Preferences item, opening the Preferences dialog you see in Figure 2-29. Select the Java→ Code Formatter item, which lets you specify options for code generation. Here, select the "Insert a new line before an opening brace" item, as you see in the figure; the sample code below will change to match.

Figure 2-29
Figure 2-29. Customizing code generation

Here's another way you can customize how Eclipse generates code. When you create a new file, this kind of comment is inserted automatically:

/*
 * Created on Oct 17, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

As this text says, you can change this with the Window→ Preferences item, followed by the Java→ Code Generation→ Code and Comments item (the > you see in the code is the HTML escaped version of >, which is used so the resulting Javadoc can be opened in a browser; the comment actually reads, "To change the template for this generated file go to Window → Preferences → Java → Code Generation → Code and Comments"). When you open the Preferences dialog and select the Java→ Code Generation→ Code and Comments→ Code→ New Java files item, as you see in Figure 2-30, you can edit the template Eclipse uses to generate this comment.

Figure 2-30
Figure 2-30. Configuring comment templates

You can even create new code assist items in Eclipse. For example, say you want to create a shortcut for the code needed to print out the current date. To do that, select Window→ Preferences, followed by the Java→ Editor→ Templates item. We'll create a new shortcut named ptd to print the date, as you see in the name box in Figure 2-31. In this case, the template System.out.println("$2004-06-16"); will be replaced by the code to print out the date. Besides $2004-06-16, you can use other values, such as ${cursor}, to indicate where to place the cursor after the insertion has been performed (when you type ${, code assist will display all the possible values you can use in code assist expressions).

Figure 2-31
Figure 2-31. Creating a code assist shortcut

Now when you type ptd in code, followed by Ctrl+Space, code assist will enter the code needed to print out the current date, as you see in Figure 2-32. In fact, if you type p, followed by Ctrl+Space, code assist will list all its options that begin with the letter "p"—including ptd.

Figure 2-32
Figure 2-32. Customizing code assist

Another way of customizing your development environment is to create working sets. Working sets let you limit what appears in the Package Explorer. For example, to create a working set consisting only of the projects Ch02_01 and Ch02_02, click the Package Explorer's pull-down menu (that's the inverted black triangle at the top of this view) and select the Select Working Set item, opening the Select Working Set dialog. To create a new working set, click the New button and select the Java item in the Working Set Type box in the New Working Set dialog. Then click the Next button. Now you can select the projects for this working set, as you see in Figure 2-33. In this case, we'll create a working set named 1And2Only and select only the Ch02_01 and Ch02_02 projects for this working set.

Figure 2-33
Figure 2-33. Creating a working set

Clicking Finish creates the new working set and opens the Select Working Set dialog. Select the 1And2Only working set and click OK. When you do, only the Ch02_01 and Ch02_02 projects appear in the Package Explorer, as you see in Figure 2-34.

Figure 2-34
Figure 2-34. Using a working set

To restore all projects to the Package Explorer, select the Deselect Working Set item from the Package Explorer's pull-down menu.

In fact, you can customize entire perspectives as well. To do that, select the Window→ Customize menu item, opening the Customize Perspective dialog, as you see in Figure 2-35. You can use this dialog to customize menu items available for the current perspective; for example, you can specify what views the user can switch to with the Window→ Show View menu item, as you see in the figure.

Figure 2-35
Figure 2-35. Customizing a perspective

TIP: After you're done setting Eclipse preferences, you can export those preferences so others can use them as well. To do that, use the Import and Export buttons in the Window→ Preferences dialog.

And that completes our look at some of the extraordinary power that Eclipse places at your fingertips for Java development. In the next chapter, we're going to discuss testing and debugging your Java code.

Steve Holzner is the author of O'Reilly's upcoming Eclipse: A Java Developer's Guide.


Return to ONJava.com.