站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题NetBeans Google Toolbar Module Tutorial

NetBeans Google Toolbar Module Tutorial

摘要: NetBeans Google Toolbar Module Tutorial This tutorial demonstrates how to create a module that adds a Google Search toolbar to the IDE. You use a wizard to create an action that extends CallableSystemAction. The wizard lets you specify that the action will be registered as a toolbar button. You then create a Swing panel, containing a JTextField and a JLabel, and you override the action's getToolbarPresenter() method to return the panel. When the user presses Enter in the JTextField, URLDisplayer is used to send a google search string to the IDE's default browser. The default browser opens and displays the results of the user's search string.

This tutorial demonstrates how to create a module that adds a Google Search toolbar to the IDE. You use a wizard to create an action that extends CallableSystemAction. The wizard lets you specify that the action will be registered as a toolbar button. You then create a Swing panel, containing a JTextField and a JLabel, and you override the action's getToolbarPresenter() method to return the panel. When the user presses Enter in the JTextField, URLDisplayer is used to send a google search string to the IDE's default browser. The default browser opens and displays the results of the user's search string.

The following topics are covered below:

Once the software is installed, this tutorial can be completed in 20 minutes.

For more information on working with modules, see the NetBeans Development Project home on the NetBeans website. If you have questions, visit the NetBeans Developer FAQ or use the feedback link at the top of this page.


Installing the Software

Before you begin, you need to install the following software on your computer:


Installing the Sample

Take the following steps to install the sample:

  1. Unzip the attached file.

  2. In the IDE, choose File > Open Project and browse to the folder that contains the unzipped file. Open the module project. It should look as follows:

    All source files.

  3. Right-click the project node and choose Install/Reload in Target Platform. The target platform opens and the module is installed.

  4. Verify that the module is correctly installed by using it as described in the Using the Module section.

Now that you know what the end result looks like, you will create the module from scratch and learn about each part while creating it.


Setting up the Module Project

Before you start writing the module, you have to make sure you that your project is set up correctly. NetBeans IDE 5.x provides a wizard that sets up all the basic files needed for a module.

Creating the Module Project

  1. Choose File > New Project (Ctrl-Shift-N). Under Categories, select NetBeans Plug-in Modules. Under projects, select Module Project and click Next.
  2. In the Name and Location panel, type GoogleToolbar in Project Name. Change the Project Location to any directory on your computer, such as c:\mymodules. Leave the Standalone Module radiobutton and the Set as Main Project checkbox selected. Click Next.

  3. In the Basic Module Configuration panel, replace yourorghere in Code Name Base with myorg, so that the whole code name base is org.myorg.googletoolbar. Leave the location of the localizing bundle and XML layer, so that they will be stored in a package with the name org/myorg/googletoolbar. Click Finish.

The IDE creates the Show XML Structure project. The project contains all of your sources and project metadata, such as the project's Ant build script. The project opens in the IDE. You can view its logical structure in the Projects window (Ctrl-1) and its file structure in the Files window (Ctrl-2). For example, the Projects window should now look as follows:

Initial Projects window.

For basic information on each of the files above, see the Introduction to NetBeans Module Development.


Coding the Module


Creating the Action

  1. Right-click the project node and choose New > File/Folder. Under Categories, select NetBeans Module Development. Under Projects, select Action. Click Next.
  2. In the Action Type panel keep the default setting, which will let the IDE create an action that subclasses CallableSystemAction, as shown below:

    Step 1 of New Action wizard.

    Click Next.

  3. In the GUI Registration panel, select the 'Edit' category in the Category drop-down list. The Category drop-down list controls where an action is shown in the Keyboard Shortcuts editor in the IDE. Next, unselect Global Menu Item and select Global Toolbar Button. In the Toolbar drop-down list, select Edit and in the Position drop-down list, select Find...-HERE, as shown below:

    Step 2 of New Action wizard.

    Click Next.

  4. In the Name and Location panel, type GoogleAction as the Class Name and type Google Action as the Display Name. Browse to an icon that has a dimension of 16x16 pixels. In fact, you will not use the icon—instead, you will display the JPanel Form that you create in the next section. However, the Action wizard requires you to specify an icon. Therefore, the icon could be of any dimension, since you will not be using it. Click Finish. GoogleAction.java is added to the package.

Creating the JPanel Form

  1. Right-click the project node and choose New > File/Folder. Under Categories, select Java GUI Forms. Under Projects, select JPanel Form. Click Next.
  2. In the Name and Location panel, type GooglePanel as the Class Name and select the package from the drop-down list. Click Finish. GooglePanel.java is added to the package and is opened in the Design view in the Source Editor.

  3. Select the JPanel and put the cursor at the bottom right-hand corner, as shown below:

    Big Jpanel.

  4. Drag the cursor to resize the JPanel, so that its width and length resemble that of a toolbar, as shown below:

    Small jpanel.

  5. Drag a JTextField item and a JLabel item into the panel, and resize the JPanel and the other two items so that they fit snugly together, as shown below:

    Small jpanel.

    Then, delete the default text in the JTextField. Next, click the JLabel and change its text to Google:

  6. Select the JTextField and, in the Property Inspector, click Events and then click in the keyTyped event, so that the jTextField1KeyTyped method is created, as shown below:

    Small jpanel.

  7. In the Source view, fill out the jTextFieldKeyTyped method, as follows:

        private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {                                    
            int i = evt.getKeyChar();
            if (i==10){//The ENTER KEY
                // we display the google url.
                try{
    URLDisplayer.getDefault().showURL
    (new URL("http://www.google.com/search?hl=en&q="+jTextField1.getText()+"&btnG=Google+Search"));//NOI18N
                } catch (Exception eee){
                    return;//nothing much to do
                }
            }
        }

    Right-click in the Source Editor and choose Reformat Code (Ctrl-Shift-F). Notice that a line of code is underlined in red. This is because its package has not been imported yet. You will resolve this in the next step.

  8. Right-click in the Source Editor, choose Fix Imports (Alt-Shift-F), select java.net.url, and click OK. The red underline is still not resolved. Right-click the project, choose Properties, click Libraries, click Add, and start typing URLDisplayer and watch the returned modules narrow until only Core and UI Utilities API are left, as shown below

    Getting the dependencies.

    Select UI Utilities API and click OK. Then click OK to confirm your selection and to exit the Project Properties dialog box. Right-click in the Source Editor again, and choose Fix Imports (Alt-Shift-F). Now the red underline disappears and the package is imported.

  9. Finally, since this is the component that will display the toolbar, you need to override the getToolbarPresenter() method in the action class:

        public java.awt.Component getToolbarPresenter() {
            return retValue;
        }

    At the top of the action class declare the following:

        GooglePanel retValue = new GooglePanel();

Building and Installing the Module

The IDE uses an Ant build script to build and install your module. The build script is created for you when you create the module project.

Installing the Module

  • In the Projects window, right-click the GoogleToolbar project and choose Install/Reload in Target Platform.

    The module is built and installed in the target platform. The target platform opens so that you can try out your new module. The default target platform is the installation used by the current instance of the development IDE.

Using the Module

  1. When it is successfully installed, the module adds a new button in the Edit toolbar. But the toolbar button does not display an icon. Instead, it displays the JLabel and JTextField that you added to the JPanel:

    Toolbar.

  2. Type a search string in the text field:

    Toolbar.

  3. Press Enter. The IDE's default browser starts up. The Google URL and your search string are sent to the browser and a search is performed. When the search returns, you can see the returned results in the browser.

Creating a Shareable Module Binary

  1. In the Projects window, right-click the GoogleToolbar project and choose Create NBM.

    The NBM file is created and you can view it in the Files window (Ctrl-2):

    Shareable NBM.

  2. Make it available to others via, for example, e-mail.


Next Steps

For more information about creating and developing module, see the following resources:


↑返回目录
前一篇: NetBeans 4.1 End-2-End Demo
后一篇: NetBeans IDE 4.1/5.0 Profiler Tutorial