Eclipse plug-in development-adding simple GUI Elements

Source: Internet
Author: User

The method for compiling the Eclipse plug-in is very simple. You only need to write an application and "add" it to Eclipse. However, like writing music, you must first learn a lot of relevant knowledge before you can create a book.

This article will discuss a few simple GUI elements:

· Toolbar button

· Menu items

· Dialog box

To use these elements, we will slightly modify the existing plug-ins and compile a usable tool class.

Extension Points)

You cannot add widgets to any part of the Eclipse user interface at will. You can only add widgets at special, specified, and recorded locations. These locations are called extension points ). There are hundreds of available extensions in the basic Eclipse installation. Plug-ins can also release new extensions. Let's take a look at the extension points in the list. Open the Invokatron project (see skynet 《Eclipse plug-in development Quick Start) To Go To The Extensions' property page. The "all extensions" tree lists different widgets of the plug-in and classifies them based on the extension points they appear. For example, the Invokatron editor is located in the editor (org. eclipse. ui. editor) Extension point. Figure 1 shows this property page.

Figure 1: Extended page

Click ADD. The list shown in Figure 2 is displayed.

Figure 2: Add extension wizard, extension list

Scroll up or down to view many available extension points. You may have noticed two types of extension points: those with an extension template (marked with a plus sign) and those without an extension template. Most of the commonly used extension points are provided with templates to assist in development extension. The extended point Action Set selected in Figure 2 carries a template called "Hello World" operation Set. When you select a template, a brief description is displayed. The "Next" page will ask about the parameters used by the template.

Close the wizard and return to the extended property page. Select "Invokatron Editor ". On this property page, you may have noticed the information we entered for the Invokatron editor in the Wizard. A normal extension requires a unique identifier (ID field), a Name (Name field) for display, and a Point field to which it belongs ). More parameters are required for the extension created in the template (for example, the Invokatron editor. More information is required if there is no template extension point, but this information can only be entered in the text editor.

Add Toolbar

Now we know what the extension points are. Let's add an extension. We first add a toolbar button. This button will call the newly created Invokatron wizard.

There are three steps to add a toolbar:

1. Declare a new extension.

2. Use a specific tag to expand the declaration.

3. Write the operation delegate class

1. Declare a new extension

We already know how to implement this step. Return to the plugin. xml editor under the extension point. Click "add ". The toolbar button is under the org. eclipse. ui. actionSets extension point. Do not use the template. Click "finish ". Enter the following content:

· Id: NewInvokatronAction

· Name: New Invokatron Document Action

· Point: (use the default value org. eclipse. ui. actionSets)

Return to the plugin. xml property page. Eclipse adds a new code snippet to this file.

2. Use a specific tag to expand the declaration.

This new extension has almost no content. We add some tags below ). Do you know which tags can be used? You can right-click the elements in the "all extensions" tree and select the "add" menu to get a list. You can also view the Eclipse documentation.

Now we can see that we can add an <actionSet> tag inside the <extension> tag. It can contain zero or more <menu> tags, followed by zero or more <action> tags, and can be selected to use <description> tags. But the most important mark is <action>. It can describe toolbar buttons and menu items at the same time.> BR> below is the XML code snippet of the toolbar button we will add. The new Code is in bold. We will analyze this code later.

<Extension id = "NewInvokatronAction"

Name = "New Invokatron Document Action"

Point = "org. eclipse. ui. actionSets">

<ActionSet id = "invokatron. actionSet"

Label = "Invokatron Actions"

Visible = "true">

<Action id = "invokatron. wizard. RunWizardAction"

ToolbarPath = "org. eclipse. ui. workbench. file/new. ext"

Icon = "icons/InvokatronIcon16.gif"

Tooltip = "Starts the New Invokatron Document Wizard ."

Class = "invokatron. wizard. RunWizardAction">

</Action>

</ActionSet>

</Extension>

All these operations can be done in a graphical manner in the plugin. xml editor, but we can view XML to clarify the complete text content of the field. The <actionSet> tag here only contains one action ). The operation is represented by an item in the menu or an object of a button in the toolbar. There are too many operation attributes. You can refer to them in the online document. Some of the most interesting attributes are:

· Id: Unique Identifier of an operation. It can be used to reference operations at runtime.

· ToolbarPath: place the toolbar button.

· Icon: the icon displayed on the left of the toolbar button or menu item. It is a 16 × 16 GIF file associated with the Development Directory. Store the image in the Invokatronicons folder. This folder has been included in the binary build path, so this icon will be placed in the document directory of the plug-in.

· Tooltip: The text that appears when you move your cursor over the toolbar button.

· Class: complete qualified class names for these operations.
Tool bar path

The toolbar path specifies the position of the add toolbar button. Since anyone can create a toolbar and sometimes a button can contain sub-options, we use a list of hierarchical identifiers to access this location. The following lists frequently used tool bar lists and their paths:

· File: org. eclipse. ui. workbench. file has some common grouping signs (you can add more buttons ):

O "new" Area: new. ext

O "save": save. ext

O "print" Area: print. ext

O "build": build. ext

· Navigation: org. eclipse. ui. workbench. navigate

· Loading: org. eclipse. debug. ui. launchActionSet

· Editor: org. eclipse. ui. edit. text. actionSet. presentation

· Search: org. eclipse. search. searchActionSet

· Java element creation: org. eclipse. jdt. ui. JavaElementCreationActionSet

· Group: Team

· CVS: CVS

If the toolbar ID you provide does not contain a flag ID, your button will be added to a new toolbar next to this toolbar. The new toolbar can be added to the Eclipse GUI. Sometimes you will see the plug-in using the toolbar path "Normal. This is the old name conversion. In Eclipse 3, a new toolbar named "Normal" is created. If you create a new toolbar ID, your Toolbar will be added to the "file" toolbar.

Note the "new" group icon of the "file" toolbar. This is where we add our own buttons. Because the flag ID is new. ext, the complete path is:

Org. eclipse. ui. workbench. file/new. ext

3. Write the operation delegate class

The last step is to write a small amount of Java for implementation operations. This class is called Operation delegate.

Package invokatron. wizard;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.