|
Two Ways of Extending JStyle Functionality As we saw earlier, JStyle supports two scripting languages. One is JMScript, a proprietary scripting language built on top of Java and the other is VBScript, a standard component of Windows platform. The support for scripting allows you to customize and extend the functionality of JStyle to suit your specific requirements. Two kinds of extensions are possible through the scripting environment:
When a comment rule is added to JStyle, the analyzer will automatically invoke these rules at appropriate points in the same manner that it applies built-in rules. On the other hand, there is no automatic invocation mode for the second type of scripts. These will be explicitly invoked via a menu item, or executed through the context sensitive menu. We call the second type of scripts independent scripts or script executables. In this chapter, we will look at scripts written in JMScript. It is possible to achieve the same functionality by writing VBScript executables as well. The information given here is not complete and is only intended to give an idea about what independent scripts are and how to use them. For detailed information, please refer to the online help. What Information is Available To Scripts? Executable scripts have access to the objects in the application environment, the active project and more importantly the parsed information of your project. From the script you can:
Information to do all of the above is exposed via several objects, whose properties and methods can be used to achieve the desired behaviour. Writing a Script Executable A JMScript executable file has a .jms extension and can be created using any text editor, including the one that is part of JStyle. It must have a main() method, which is the entry point for the script. Let us write a simple script. From the File menu, select New. A tabbed dialog box appears with several options. Select the icon labeled JMScript Executable and click OK. This creates a bare-bones script file and opens it in the editor. // JMScript Executable file Application objects allow access to the application environment such as the document, the output areas, views, and windows. Project objects allow us to access parsed information pertaining to the current project. Let us modify the above script to print basic information about a project. See the code below.
main(s) { Since we do not need application objects in this example, the call to getApplication() has been deleted from the generated skeletal script. After entering the code shown above, save the script file as sample.jms. Executing the Script There are two ways to execute a script. One is by right clicking the mouse in the script window and selecting Run Script in the context sensitive menu. If you run the above script, you will see that information about the various files in the project and classes contained in them are displayed in the script tab. The println function redirects output to the script window.
Fig. 8.1 Another way to execute a script is by invoking it through a menu. For this, it is first necessary to bind a script to a menu item. There are two ways to do this. One approach is to open the script file in the editor window (if it is not already open) and right click the mouse in the window. This pops up the context sensitive menu. Select Associate with Menu. This displays a dialog box as shown in the following figure. Enter the menu name as you would like it to appear. Enter Dump Project Details for our example. We can ignore the other options for now. When you click OK, the dialog disappears. You will notice a new menu item labeled Dump Project Details under Tools main menu. Hereafter any time you select this menu item, JStyle will automatically invoke our script. In this case, the script file need not be open in the editor window. Here is the other approach to bind a script to a menu item: From the main menu, select Settings->Scripts. This brings up a tabbed dialog. Select Executable Scripts tab and click Add button. In the top field, enter the complete path name of the script file and in the field just below it, specify the menu item name as in the previous case. When you click OK, the menu item will appear under Tools and will be correctly associated with our script. There are several examples of executable scripts distributed with JStyle. You may find it useful to experiment with them. For additional information on scripting and runtime objects available for use, please refer to the online help.
|