Mastering Eclipse V3.4, Part 3: JDT text editor tips and tricks

This "Mastering Eclipse" series of articles teaches complete newcomers to Eclipse the ins and outs of the Eclipse IDE. By the end of the series, you'll be on par with advanced users. In this article, take a tour of Eclipse's Java™ editor and learn about some of its advanced features.

Prashant Deva, Founder, Placid Systems

Prashant DevaPrashant Deva is the founder of Placid Systems and the author of the ANTLR Studio plug-in for Eclipse. He also provides consulting related to ANTLR and Eclipse plug-in development. He has written several articles related to ANTLR and Eclipse plug-ins, and he frequently contributes ideas and bug reports to Eclipse development teams. He is currently busy creating the next great developer tool.


developerWorks Contributing author
        level

13 January 2009

Also available in Japanese Vietnamese

Develop skills on this topic

This content is part of a progressive knowledge path for advancing your skills. See Open source development with Eclipse: Master the basics

This part of the "Mastering Eclipse" series takes a detailed look at Eclipse's Java editor. The editor is where developers spend a majority of their time, so understanding its advanced features can significantly improve your productivity.

The Organize Imports command

The Organize Imports command adds missing imports and organizes existing import declarations in your Java files. You can run this command in the current editor by using Ctrl+Shift+O. To apply the Organize Imports command to your whole project, right-click the project in the Project Explorer and select Source > Organize Imports.

Suppose you used a class somewhere in your Java file, but you've forgotten to import it; Organize Imports can automatically import it for you. If the command is unsure about the class's location, a window opens with a list of options for you to choose from. For example, if you use the List class in your code, when you run the Organize Imports command, it may pop up a window asking you to choose between java.util.List and javax.swing.List because the command can't decide on its own.

Organize Imports also breaks the commonly used .* style import declarations into individual imports. For example, suppose you have an import like import java.util.* in your file, and you use only the List class from that package. The Organize Imports command replaces the original import statement with import java.util.List.

Pick out part of string

Often, you need to concatenate a variable between two strings. Most of the time, these strings are part of a single statement, and it's easy to make the mistake of leaving out a space on either side of the strings. For example, you may end up with output like You have5seconds left, instead of You have 5 seconds left. Eclipse helps prevent you from falling into this trap:

  1. Type your string as you would normally within your code:
    static String getMsg(int time)
    {
        return "You have 5 seconds left";
    }
  2. Select the portion of the string you want to replace with a variable — in this example, the numeral 5.
  3. Press Ctrl+1 and select Pick out selected part of String.
    Figure 1. Select Pick out selected part of String
    Select Pick out selected part of String
    The result is shown in Figure 2.
    Figure 2. Selected part of string is picked
    Figure 2. Selected part of string is picked
  4. Replace the string in the middle with your variable.
    Figure 3. Replace the string with your variable
    Figure 3. Replace the string with your variable

Now you can rest assured that you made no mistakes about the spaces.

Automatically create locals

You often need to call a method and assign its value to a new local variable. Eclipse makes this so easy that you'll never again assign locals the old way:

  1. Type the call to the method:
    public void foo()
    {
        getMsg(3);
    }
  2. Without moving your insertion marker, press Ctrl+1 and select Assign statement to a new local variable (see Figure 4). You can select Assign statement to a new field if you want to use a field instead of a local variable.
    Figure 4. Select Assign statement to a new local variable
    Figure 4. Select Assign statement to a new local variable
    A new local variable of the same type as the method's return value is created for you. It's given an appropriate name, but you can change it if you'd like (see Figure 5). Press Enter to accept the name.
    Figure 5. You can change the variable's name
    Figure 5. You can change the variable's name

Quick Outline

Outline view is useful because it lets you easily jump to methods in your Java file. But this view takes up a lot of your precious on-screen real state.

Quick Outline view provides all the functionality of Outline view without needing the view to occupy your screen. To activate Quick Outline view from inside your editor, press Ctrl+O. A pop-up window appears that shows an outline of your file.

Figure 6. Pressing Ctrl+O brings up Quick Outline view
Figure 6. Pressing Ctrl-O brings up Quick Outline view

You can navigate to any method by using your arrow keys. To jump to a method even more quickly, begin typing its name. The list begins to filter, showing you only methods that begin with the characters you're typing.

Figure 7. Start typing and Outline view begins filtering
Figure 7. Start typing and Outline view begins filtering

Quick Open Type

The Open Type window does for Package Explorer what Quick Outline does for Outline view.

Press Ctrl+Shift+T from your editor. A window pops up, in which you can type the name of any class in your workspace (see Figure 8). Click OK, and that class opens instantly in the editor. No need to hunt through trees in Package Explorer to open the class you want.

Figure 8. The Open Type window lets you instantly jump to any class in your workspace
Figure 8. The Open Type window lets you instantly jump to any class in your workspace

You can use wildcards when you type the class name to filter the list below the text box. For example, type *Exception to show all Exception classes.

Breadcrumb bar

The breadcrumb bar was introduced in Eclipse V3.4. It sits at the top of your editor window, just below the tabs, and provides the functionality of both the Package Explorer and Outline views (see Figure 9). If it isn't enabled, you can enable it by pressing Alt+Shift+B.

Figure 9. The breadcrumb bar
Figure 9. The breadcrumb bar

The breadcrumb bar points out the path of the current editor relative to the current workspace. Clicking one of the black arrows shows you the contents of the element, thus allowing you to browse your entire workspace from the bar.

Figure 10. Breadcrumb bar lets you browse your workspace from the bar
Breadcrumb bar lets you browse your workspace from the bar

Quick Javadoc

Are you trying to figure out what a method does? Eclipse can easily show you the Javadoc for any method. Just hover your cursor over the method and a Javadoc window appears.

Figure 11. Hovering your cursor over any Java element shows related Javadoc -- in this case, the indexOf method
Figure 11. Hovering your cursor over any Java element shows related Javadoc -- in this case, the indexOf method

But this functionality doesn't end there. Move your cursor inside the pop-up window, and the window becomes scrollable (see Figure 12). The buttons at the bottom let you open the Javadoc in an external window and go to the declaration of the element whose Javadoc you're viewing.

Figure 12. Moving your cursor inside the pop-up turns it into a scrollable window
Figure 12: Moving your cursor inside the pop-up turns it into a scrollable window

Auto format

You can use Eclipse's auto-format functionality to automatically format documents. Right-click a document in the Package Explorer and select Source > Format. You can even format your entire project by right-clicking the project instead of an individual file in Package Explorer.

To configure how Eclipse formats your code:

  1. Choose Window > Preferences. then browse to Java > Code Style > Formatter.
    Figure 13. Editing how Eclipse formats your documents
    Figure 13. Editing how Eclipse formats your documents
  2. Create a new profile for formatting by clicking New and entering a name in the window that opens.
  3. After you create the profile, click Edit to begin editing formatting preferences for that profile.
  4. The Profile window that opens contains detailed settings for configuring the formatting of your source code (see Figure 14). You can configure everything from how much indentation is required for each element in the source code to how many blank lines appear between import groups.
    Figure 14. Eclipse lets you configure your formatting settings in detail
    Figure 14. Eclipse lets you configure your formatting settings in detail
  5. Click OK to close the Profile window. Click OK again to close the Preferences window.

Note that the new formatting settings will apply the next time you format your source code.

Save Actions

Save actions are triggered when you save a document. They let you do things like format your code and organize imports automatically when you save your document. To configure save actions:

  1. Choose Window > Preferences > Java > Editor > Save Actions to open the Preferences window.
    Figure 15. Configuring Save Actions in the Preferences window
    Figure 15. Configuring Save Actions in the Preferences window
  2. Select the Perform the selected actions on save checkbox.
  3. Select all the actions you want to be performed on save. Click Configure to see more actions.
    Figure 16. Clicking Configure displays more actions
    Figure 16. Clicking Configure displays more actions
    For example, you can select the Convert for loops to enhanced check box on the Code Style tab to convert all your for loops to Java 5-style enhanced loops every time you save your document.

Save Actions can save you a lot of time. For example, you can type your code any way you want, without worrying about the formatting, and it will still be formatted perfectly automatically each time you save your document.

Code folding

Code folding lets you literally fold pieces of code so the editor doesn't get too cluttered. To fold a method, click the - icon on the left ruler.

Figure 17. Clicking the - sign folds the code
Figure 17. Clicking the - sign folds the code

When you fold code, the - icon turns into a +, which you can click again to unfold the method. If you hover your cursor over the + sign when the method is folded, a pop-up window displays the text inside the method.

Figure 18. Hovering your cursor over the + icon shows a preview of the code that has been folded
Figure 18. Hovering your cursor over the + icon shows a preview of the code that has been folded

Generate hashCode() and equals()

Eclipse lets you automatically generate the hashCode() and equals() methods for your classes so you don't have to do it yourself. This way, you can avoid making errors while writing these methods. To generate the methods:

  1. Choose Source > Generate hashCode() and equals().
  2. The window that opens asks you which fields you want to include when the hashCode() and equals() methods are called on your class.
    Figure 19. Select the fields on which to generate hashCode() and equals() methods
    Figure 19. Select the fields on which to generate hashCode() and equals() methods
  3. Select the fields you want and click OK. The equals() and hashcode() methods are generated for you.

Overview ruler

The overview ruler is embedded to the right of the editor window. It lets you instantly see any errors, warnings, or other markers that require your attention in the editor. If there are any errors in your editor, then the upper-right corner of the bar displays a red box.

Figure 20. The red box in the overview ruler indicates one or more errors in the editor
Figure 20. The red box in the overview ruler indicates one or more errors in the editor

A yellow box, on the other hand, means there are warnings in the editor.

Figure 21. Overview ruler indicating the presence of warnings in the editor
Figure 21. Overview ruler indicating the presence of warnings in the editor

Using the overview ruler, you can jump instantly to any error/warning in your editor. The ruler displays little yellow/red markings wherever an error/warning appears in your editor (see Figure 22). If you click the marking, the editor instantly jumps to the appropriate position in your code.

Figure 22. The overview ruler showing two warnings and one TODO comment
Figure 22. The overview ruler showing two warnings and one TODO comment

The overview ruler can also indicate other points of interest in your editor. For example, if you have a TODO comment, or if you perform a search, the related lines are marked in the overview ruler; clicking the markers takes you to the corresponding line.

As the name suggests, the overview ruler gives you an overview of the lines of interest in your editor. You'll find it an invaluable companion as you write Java code.


Conclusion

This article has examined some of the advanced features of the Java editor in Eclipse. You've seen how to generate common pieces of code automatically. You also learned how to close Eclipse views and still use their functionality, thanks to shortcuts offered by Eclipse editor. The article also discussed other features of the Java editor that can significantly improve your productivity.

Resources

Learn

Get products and technologies

Discuss

  • The Eclipse Platform newsgroups should be your first stop to discuss questions regarding Eclipse. (Selecting this will launch your default Usenet news reader application and open eclipse.platform.)
  • The Eclipse newsgroups has many resources for people interested in using and extending Eclipse.
  • Participate in developerWorks blogs and get involved in the developerWorks community.

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Open source on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source
ArticleID=362337
ArticleTitle=Mastering Eclipse V3.4, Part 3: JDT text editor tips and tricks
publish-date=01132009