|
Studio Editor has multiple code editing features that simplify code typing, navigation and provide convenient code transformations. The Editor is specialized to meet Java programming requirements and has advanced editing features like code completion, source code browsing and object browsing.
This chapter discusses the important coding aid features provided in Studio:
Code Completion QuiCode facility multiples productivity while working with .java files
Source Code Browsing Allows easy navigation and location of objects in source code
Method Browser Allows faster access to methods and fields
Object Browser Allows browsing the hierarchy of classes and its superclasses, implemented interfaces, fields, methods (both overridden and extended) and object members
Java Coding Aids Java coding aids like Implement Methods, Override Methods and Field Get/Set that provide the list of methods to be implemented, non-overridden methods inherited by a specific class, and also the unimplemented getter and setter methods for all the fields in a class
Use QuiCode to accelerate your programming and increase productivity. QuiCode provides a Suite of productivity enhancement tools that helps the developer while writing .java files. It brings up a context-sensitive popup window in the Display Panel and helps in completing code. Both new and experienced developers can use this feature and code quickly.
QuiCode is enabled by default. To switch off QuiCode feature in Studio, click on Code > Configure and uncheck the option QuiCode. Close the window using the Close button.
Switching off QuiCode from Studio removes the following features, which it supports:
QuiCode provides intelligent code completion based on an existing index constructed by parsing Java sources and reflecting Java classes. This feature keeps track of changes in Java sources made through Studio's editor. It also helps catch the first syntactical error encountered in the file by underlining the first parse error encountered with a wavy red line.
Setting up QuiCode involves the following two steps:
QuiCode uses the indexer to index sources and classes. The Indexer enables QuiCode to provides code completion, and other coding aids by parsing Java sources and reflecting Java classes. The source paths and classpaths are set by default when the Desk is brought up. Though the Index gets updated automatically as changes are made in the file, it can be re-generated and updated specifically. The scope of the index can be per project (per Desk), or across projects (across multiple Desks).
The Indexing process is memory-intensive, and it is recommended that Studio be restarted after an intensive indexing operation for better performance. The QuiCode index database needs free disk space, which is dependent on the number of files being indexed.
To index sources go to Code > QuiCode > Indexer from the main menu, and follow the given process:
Select the Index Scope Select the index scope as either Desk or Global, before setting the source files and classes to be indexed. Selecting Desk makes the indexed sources and classes available only for the active Desk. Selecting Global sets the scope of the indexed sources and classes as Studio, and can be used across all Desks. The Desk modules are added to the index by default, when a Desk is created.
JDK sources, utilities used across projects should be ideally indexed Globally. Global indices are static and have to be updated manually. Desk Indices get updated automatically when changes are made through the editor. However, in certain cases the files may be modified and QuiCode may not be able to notice these changes. In such cases, indices have to be explicitly updated.
The global index is stored in <install_dir>/quicode and the Desk index is stored in desk_root/quicode, where desk_root is the location of where the Desk is stored.
Indexing Source Roots The source roots to be indexed are added here. The Source Roots of the active Desk are added by default. To add any other Source Root, click on +. To remove the Source Roots that have been already added, click -, and re-generate the index. Source JARs can also be provided to the Indexer. Source JARs are un-jarred and then parsed. The index is not updated for these sources. The Update operation parses files from a source JAR, and does not look for changed classes. It reflects all the classes in the classpath.
To order the Source Roots, select the source Root and click on the up and down arrow. Source Roots have to be ordered in cases when multiple file names are found in multiple source roots. The file in the first source Root is accepted and the next instances ignored, in such cases.
Indexing Classpaths The classes required for indexing are added here. To add a class path, click on +. To order the class paths, select the class path and click on the up and down arrow.
The class paths have to be ordered in cases where the same class name is found in more than one class path. The class in the first class path is then accepted and the next instances ignored.
Though parsing Java classes from classpaths is faster than parsing Java source files from source roots, the information about the name of the parameter is lost in parsing Java classes.
Include Packages Enter the packages to be included in this field. Leave this field blank to index all Java classes/sources. Specifying a package in this field allows only that package and all its sub-packages to be indexed. Use the systems path separator (: on Unix and ; on Windows) to separate the package names.
Exclude Packages Enter the packages to be explicitly excluded from the index in this field. Leaving this field blank does not exclude any packages. Use the systems path separator (: on Unix and ; on Windows) to separate the package names. This option might be used in the cases where packages holding test cases or for packages that hold code which is not used.
Ensure that the entire class hierarchy is provided for indexing. Providing incomplete information may not allow QuiCode to suggest the entire possible set.
Creating the Indexed Sources and Classes Clicking on Index creates a new index for a project, or re-creates the index. Choosing Index deletes the existing index and creates a new one. This operation can be also used to re-create a corrupt index.
Updating the Indexed Sources and Classes Clicking on Update updates the index with modified or new Java files. To know more about this, read the section on Updating Indexed Sources.
Removing the indexed sources and classes from the Indexer panel, and clicking on Index re-generates the index explicitly, and deletes the previous index.. Using the Update button only updates those sources and classes, which are present in the Indexer panel, and does not delete the previous indexed sources and classes from the index.
Close the Indexer panel after indexing the Sources or the Classes. A progress bar shows the indexing of the Java sources followed by the Java classes. Once the indexing is complete, the QuiCode feature is ready to be used.
The Desk Indices created using QuiCode Indexer get updated automatically when changes are made to source code using the Editor. They are automatically updated each time the Save option is used in Studio Editor, or new files/modules added to the Desk. Global indices are static and have to be updated manually.
The Update option available in the Indexer panel is used to update the specific Indexed source or class. It uses the class path and source path settings of the Indexer. It does not look for changed classes and reflects all the classes in the classpath. The Update option also parses all Java files if a source.jar is specified in the field, Source Roots.
Click on Code > QuiCode > Indexer available in the main menu, add or remove the required source files and classes, and click on the Update button. This brings up to date the existing classes or sources in the index, with the latest classes or sources. Constant updating of the index will waste a little disk space. Using Update on the Indexer panel releases the wasted space by packing the indexed, after the sources and classes have been updated. Auto updating of indices does not release the wasted space on the disk. The Include Packages and Exclude Packages information can also be updated in the index.
Example
Remove a package that was previously entered in the Exclude Package field and click on Update. This now includes the package in the Indexed sources.
To set the QuiCode settings provided in Studio, click on Code > QuiCode > Preferences from the main menu. This brings up the QuiCode Preferences dialog.
Auto Popup This option is used to switch off the QuiCode from auto triggering its options. Check this field to automatically pop up the QuiCode window in the Display Panel after the user has typed the activation character and paused for the time specified in the delay slide bar.
Delay The Delay slider bar sets the duration of the pause before the code is parsed. The Delay value is set in milli seconds.
Popup Color Use this section to customize the color preferences while using QuiCode. Four fields are provided where the colors can be set:
Preview The preview of colors selected in the previous section are displayed here.
Key Stroke There are two key strokes available for using QuiCode in Studio. The key strokes can be customized in this field. The two types of key strokes that can be customized are Code Popup and Parameter Popup. Place the cursor in the field and enter the new key stroke combination in the keyboard. This modifies the existing key stroke to the new key stroke.
Click OK after making the required changes. To restore the default factory settings, click on the Restore button.
QuiCode can be used to do the following in the Editor:
Code Completion This feature completes code while the developer is typing and is a very useful feature for any developer. Type the name of a class instance (such as AccountBeanClass) followed by a dot, and press the Ctrl + Spacebar keys. This displays a list of all properties and methods that are part of this instance. This is very useful as the name of that method or property need not be remembered, and just selecting the right one from the alphabetically sorted list is enough.
Package Completion This feature lists the packages available in the index. Typing the keyword package, the first package name followed by a dot, and then pressing Ctrl + Spacebar keys displays the list of packages in the class path. For example package com.displays the all the possible options.
Parameters Lists This feature in the QuiCode displays the parameters that the method or constructor needs. Type the name of the method or constructor and the opening parenthesis and press Ctrl + Shift + Spacebar. This displays the list of parameter types and the parameter names that can be added.
Indexing Sources displays the list of parameter types along with the names. Indexing Classes displays just the list of parameter types.
New Completions Typing the keyword new displays the list of possible classes and interfaces that the statement can define.
Implements Completions Typing the keyword implements in a Java statement displays the list of interfaces that a class can implement in the .java file.
Extends Completions Typing the keyword extends in a Java statement displays the list of classes that a class can extend in the .java file.
Import Lists Typing a declaration of a class name that is not in the import statements and pressing Ctrl + Spacebar displays a list of possible import statements. Select the import statement and press the tab key on the keyboard. This automatically adds the import statement in the most appropriate place in the file.
This feature also includes, Auto import one of the highest productivity enhancements provided by QuiCode. QuiCode identifies a class that has not been imported, and suggest the possible imports for that class. Auto-Import only works on indexed classes and Java files.
Examples
1. In all the following examples assume that JTree is not imported.
Typing the following code
JTree tree = new JTree();
tree.
automatically pops up the quicode panel with all the possible options.
2. Typing Jtree invokes all the available options on the screen
3. Choosing the import statement using Tab or Enter adds the import statement to the imports list at the top of the file, in the correct position according to an alphabetical sorting of the imports. Typing import javax pops up all the available options on the screen.
Source code browsing allows easy navigation and location of source code for objects in developed code. It also helps in easy development and maintenance of code without the user having to keep track of declarations and references. It helps search through the object hierarchies, classes, functions, variables, types, and constants in the application's code.
Source code browsing can be invoked on Fields, Classes, Constructors, Methods, etc. and then helps navigate to the appropriate file. In the most trivial case, the method being browsed for exists in the same Java file. In the more complex cases, the right implementation may be buried under a long class hierarchy, hidden by abstractions and overridden methods.
To invoke source code browsing, using the keyboard shortcut - Ctrl + /. If there are multiple possible destinations, or if source code browsing cannot resolve the context uniquely, a window pops-up with the appropriate options, which can be used to navigate to the appropriate file. The navigation can be retraced back to the file the navigation started from using the keyboard shortcut Ctrl + Shift + /.
The following examples help understand source code browsing:
Invoking source code browsing with the cursor anywhere inside Foo() takes the user to the line in Foo.java file where the class is declared.
Invoking source code browsing with the cursor anywhere inside Foo behaves in a similar manner as case 1.
{
foo.fooMethod();
}
Invoking source code browsing anywhere in the variable foo. takes the user to the word Foo in case 2 to indicate the declaration of the variable foo.
Invoking source code browsing anywhere in the method fooMethod() takes the user to the file, Foo.java and highlights the line where fooMethod is defined.
Use the "Method Browser" feature provided in Studio for faster access to methods and fields. It displays all the fields, methods and constructors of a current Java file opened in the editor. It shows the state of last saved file. Before starting to use the method browser, configure Quicode from Tools > Config in the main menu.
Using Code > Method Browser from the main menu or pressing the keys, Ctrl + Shift + \ pops up a list of all the fields, methods and constructors, grouped by fields, associated classes, constructors and methods respectively and sorted alphabetically in the group. Method Browser also enables context sensitive invocation of methods. Select from the list, using the up and down arrow keys on the keyboard. Pressing the Enter key on finding the required item, takes the cursor to that line where that item has been declared and highlights it.
To search for fields or methods, the user can also start typing the textfield when the list pops up. This displays the text in a new pop up, and the Tab key can be used for searching the list incrementally. Incremental search ensures that as the user types, the item which starts with the text written is highlighted.
If the last saved file has an error (parse or compilation), a message in red is displayed in the pop up list. The message describes the type of error and where it occurred; and also lists all the fields and methods that were declared before the line, where the parse error occurred.
Example
If there is a parse error at line number 15, the message reads "parse error at line no 15".
Pressing Enter on the message, takes the cursor to the line where the error has occurred.
The Object Browser in Studio is a streamlined and logical view of the sources present in the Desk. It helps speed development time by enabling developers to filter through and then browse the hierarchy of classes, and its superclasses, sub classes extending a class, implemented interfaces, fields, methods (both overridden and extended) and object members.
To start working with the Object Browser, create either a Java Module or EJB Module in the Desk, and point the module to all the required source files, and the JDK sources. After pointing the module to the source files, open the required Java file, and click on Code > Object Browser from the main menu or press Ctrl + \ keys to bring up the Object Browser.
The Object Browser that comes up can be used to navigate to the correct source files. Double clicking on any member in the Object Browser opens the required source file in the Display Panel and highlights the context.
The toggle buttons provided on top are used to apply different filters to customize the view. The list given below provides the functionalities of each of the buttons in the panel, in the way they are positioned:
Alphabetically Sort Use the button,
to sort the items alphabetically within the member type. For example, fields within fields, methods inside methods. Not sorting the types, make them appear under the class that they exist.
Class Hierarchy Clicking on the button,
brings up a panel at the bottom of the dialog, and displays the hierarchy of the classes.
Declared Methods and Fields Clicking on the button,
displays only those methods and fields that have been declared in the current class. Not selecting this option displays all the methods and fields extended by the class from its parents. Enabling the next button, grouped by extended methods displays only the declared methods, even if the declared button is disabled.
Extended Methods Clicking on the button,
groups the methods of the parent extended by the class under a tree node with an icon having E at the left and followed by a class icon. If the declared button is not selected at the same time as the extended button, the extended methods are displayed under the extended class node, and not under the current node.
Implemented Methods Clicking on the button,
groups the implemented methods of the interface or class under a tree node having the name of the respective interface or class. The interface or class icon has the letter I at the left.
Overridden Methods Clicking on the button,
groups the method overridden by this class from its parent class under a tree node with the name of respective class. The class icon has the letter O at the left.
Fields The button,
represents a general filter button. Clicking on this button displays the fields.
Methods The button,
represents a general filter button, and clicking on it displays the methods.
Nested Classes and Interfaces Clicking on the button,
displays the inner or nested classes and interfaces, along with the methods and fields declared in them.
Public Members Clicking on the button,
displays the public members.
Private Members Clicking on the button,
displays the private members.
Protected Members Clicking on the button,
displays the protected members.
To use the Object Browser for another file, close the first Object Browser instance and re-open for the new file.
Note: The Object Browser allows the user two kinds of search facilities, Incremental Search and Deep Search. For detailed information on these, read the relevant sections in the chapter, Organizing Source Files.
The Index of the Desk (created in an older version) must be recreated in SP3 because the old index does not have all the information required to complete tasks in Object Browser.
Click on Code > Implement... from the main menu or press Ctrl + Alt + I on the keyboard to choose the accessible abstract methods from the base classes/interfaces to be implemented, and create the stubs for these methods inside the currently edited class.
The methods displayed in the list are:
Note: To implement a method, which has been already implemented by a class in the class heirarchy, use the Override option (Code>>Override...) from the main menu instead of Implement.
The option, "Implement Methods" functions only in context of a class, and the caret must be positioned inside the class at the time of invocation. When the context is not understood then an error message, "the class under the caret could not be recognized" pops up.
The "Implement Methods" feature is designed to be used for completing the contract of a class and to create concrete implementations. It can also be used for anonymous inner classes; and therefore used effectively for generating stubs of listener classes.
Indentation of methods generated for anonymous inner classes may be incorrect if the option, "Use Spaces for Tab Character" is not selected in File Preferences dialog.
The Implements dialog contains two fields displaying the information:
Select the methods to be implemented, and click on OK (keyboard shortcut Ctrl + Enter). Methods once generated do not appear in the list when the action is invoked again.
If the method has a valid return type and is not abstract, a return statement delegating the method to super is generated. If the method is abstract, then a concrete implementation is provided by applying the following code generation rules.
It returns null when the return type of the implemented method is not a primitive type (subclass of Object). If the return type is a primitive then for
The generated method body has a valid return statement, and gets compiled. The user may however need to change the code as per requirements.
When a class is not fully parseable, code generation may fail. In cases where code generation may fail, a dialog pops up with a message stating that the class is not fully parseable and results may not be as expected. Code Generation process is then continued only if the user wants to.
The preferences set in the Editor Preferences dialog box, are used to determine whether to generate tabs or spaces. To do this, click on File > Preferences from the main menu and select the Editor tab.
The code generator uses the first method/constructor in the class to determine whether to generate braces in the same line or in a new line. The default is set as a new line in case it could not resolve from the first method.
Click on Code > Override... from the main menu or press Ctrl + Alt + O on the keyboard to choose the accessible methods from the base classes to be overridden, and create the stubs for these methods inside the currently edited class.
The Override Methods panel that comes up, displays a list of
The Overrides Dialog shows a list of methods against the class name where they are visible. Select the methods to be overridden and press OK(Ctrl+Enter). This generates the appropriate method body in the class. Methods once generated do not appear in the list when the action is invoked again.
If the method has a valid return type and is not abstract, a return statement delegating the method to super is generated. If the method is abstract, then a concrete implementation is provided by applying the following code generation rules.
It returns null when the return type of the implemented method is not a primitive type (subclass of Object). If the return type is a primitive then for
This feature functions only in context of a class, and the caret must be positioned inside the class at the time of invocation. When the context is not understood then an error message, "the class under the caret could not be recognized" pops up.
When a class is not fully parseable, code generation may fail. In some cases, when the file is not parseable, the list of methods may not get filtered accurately, and methods already implemented may be displayed in the list.
In cases where code generation may fail, a dialog pops up with a message stating that the class is not fully parseable and results may not be as expected. Code Generation process is then continued only if the user wants to.
The preferences set in the Editor Preferences dialog box, are used to determine whether to generate tabs or spaces. To do this, click on File > Preferences from the main menu and select the Editor tab.
The code generator uses the first method/constructor in the class to determine whether to generate braces in the same line or in a new line. The default is set as a new line in case it could not resolve from the first method.
Click on Code > Go To Previous Method on the main menu or press Alt + Up on the keyboard to move the caret position from the current method to the previous method, irrespective of class boundaries.
Similarly, click on Code > Go To Next Method on the main menu or press Alt + Down to move the caret position from the current method to the next method, irrespective of class boundaries.
To navigate to the previous/next method depending on class boudaries, press
This featue may not work when the file is not paresable, because of parser limitations. In such cases, the system beeps and an appropriate message appears in the status bar.
Clicking on Code > Field Get/Set... from the main menu or pressing Ctrl + Alt + F on the keyboard to choose the unimplemented getter and setter methods for all the fields in the class and create the stubs for these methods inside the currently edited class.
The Field Get/Set dialog consists a list of getter and setter methods and the corresponding field names.
Select the methods to be generated and press OK (Ctrl+Enter). Methods once generated do not appear in the list when the action is invoked again.
The generated method follows the JavaBean rules. For a set method, the method body sets the value passed as a method argument to the field. For a get method, the generated method body returns the fields value.
This feature functions only in context of a class, and the caret must be positioned inside the class at the time of invocation. When the context is not understood then an error message, "the class under the caret could not be recognized" pops up.
When a class is not fully parseable, code generation may fail. In some cases, when the file is not parseable, the list of methods may not get filtered accurately, and methods already implemented may be displayed in the list.
In cases where code generation may fail, a dialog pops up with a message stating that the class is not fully parseable and results may not be as expected. Code Generation process is then continued only if the user wants to.
The preferences set in the Editor Preferences dialog box, are used to determine whether to generate tabs or spaces. To do this, click on File > Preferences from the main menu and select the Editor tab.
The code generator uses the first method/constructor in the class to determine whether to generate braces in the same line or in a new line. The default is set as a new line in case it could not resolve from the first method.
| Pramati Technologies © Copyright |
|