Editing and Refactoring Code in NetBeans
- Opening the Source Editor
- Managing Automatic Insertion of Closing Characters
- Displaying Line Numbers
- Generating Code Snippets without Leaving the Keyboard
- Using Code Completion
- Inserting Snippets from Code Templates
- Using Editor Hints to Generate Missing Code
- Matching Other Words in a File
- Generating Methods to Implement and Override
- Generating JavaBeans Component Code
- Creating and Using Macros
- Creating and Customizing File Templates
- Handling Imports
- Displaying Javadoc Documentation While Editing
- Formatting Code
- Text Selection Shortcuts
- Navigating within the Current Java File
- Navigating from the Source Editor
- Searching and Replacing
- Deleting Code Safely
- Changing a Method's Signature
- Encapsulating a Field
- Moving a Class to a Different Package
- Moving Class Members to Other Classes
- Creating a Method from Existing Statements
- Creating an Interface from Existing Methods
- Extracting a Superclass to Consolidate Common Methods
- Changing References to Use a Supertype
- Unnesting Classes
- Tracking Notes to Yourself in Your Code
- Comparing Differences Between Two Files
- Splitting the Source Editor
- Maximizing Space for the Source Editor
- Changing Source Editor Keyboard Shortcuts
- Opening the Source Editor
- Managing Automatic Insertion of Closing Characters
- Displaying Line Numbers
- Inserting Snippets from Code Templates
- Using Editor Hints to Generate Missing Code
- Matching Other Words in a File
- Generating Methods to Implement and Override
- Creating and Using Macros
- Creating and Customizing File Templates
- Handling Imports
- Displaying Javadoc Documentation While Editing
- Formatting Code
- Text Selection Shortcuts
- Navigating within the Current Java File
- Navigating from the Source Editor
- Searching and Replacing
- Refactoring Commands
- Deleting Code Safely
- Extracting a Superclass to Consolidate Common Methods
- Changing References to Use a Supertype
- Unnesting Classes
- Tracking Notes to Yourself in Your Code
- Comparing Differences Between Two Files
- Splitting the Source Editor
- Maximizing Space for the Source Editor
- Changing Source Editor Keyboard Shortcuts
NETBEANS IDE PROVIDES A WIDE VARIETY OF TOOLS to support Java application development, but it is the Source Editor where you will spend most of your time. Given that fact, a lot of attention has been put into features and subtle touches to make coding faster and more pleasurable.
Code completion and other code generation features help you identify code elements to use and then generate code for you. Refactoring features enable you to easily make complex changes to the structure of your code and have those changes propagated throughout your project. Keyboard shortcuts for these code generation features and for file navigation ensure that your hands rarely have to leave the keyboard.
Architecturally, the Source Editor is a collection of different types of editors, each of which contains features specific to certain kinds of files. For example, when you open a Java file, there is a syntax highlighting scheme specifically for Java files, along with code completion, refactoring, and other features specific to Java files. Likewise, when you open JSP, HTML, XML, .properties, deployment descriptor, and other types of files, you get a set of features specific to those files.
Perhaps most importantly, the Source Editor is tightly integrated with other parts of the IDE, which greatly streamlines your workflow. For example, you can specify breakpoints directly in the Source Editor and trace code as it executes. When compilation errors are reported in the Output window, you can jump to the source of those errors by double-clicking the error or pressing F12.
In this chapter, we will demonstrate the ways you can use the IDE's editing features to simplify and speed common coding tasks.
Opening the Source Editor
Before starting to work in the Source Editor, you will typically want to have an IDE project set up. You can then open an existing file or create a new file from a template. See Chapter 3 for basic information on creating projects and files and for a description of the various file templates.
If you would simply like to create a file without setting up a project, you can use the Favorites window. The Favorites window enables you to make arbitrary folders and files on your system accessible through the IDE. The Favorites window is not designed for full-scale project development, but it can be useful if you just want to open and edit a few files quickly.
To use the Source Editor without creating a project:
- Choose Window | Favorites to open the Favorites window.
- Add the folder where you want the file to live (or where it already lives) by right-clicking in the Favorites window, choosing Add to Favorites, and choosing the folder from the file chooser.
-
In the Favorites window, navigate to the file that you want to edit and doubleclick it to open it in the Source Editor.
If you want to create a new file, right-click a folder node, choose New | Empty File, and enter a filename (including extension).