Foreword
Previously, we used a simplest process to establish an Eclipse plugin in the newcomer developments in the Eclipse plugin. But we didn't explain any details. We will follow some details in some details.
This example uses an example to explain the most important file in the plugin: Plugin.xml writing method and its role.
Plug-in structure
Which part of an Eclipse's plug-in consists of? In fact, it is very simple, it consists of the following three parts:
Plugin.xml - Description file, plug-in description and action resources, such as the ICO icon (optional) Java code, included in a JAR file (optional) Need to note that a plug-in component is included in one In the directory, it is called by the Eclipse core at runtime.
Seeing this, most people will have their mouths: "Can there be a Java code plugin to run?" Let's use the example below to see.
create project
On the menu, we click File -> New -> Project, select Plugin Development in the New Project -> Plugin Project:
Click Next and enter com.huangdong.plugin.Hello in the project name:
Point Next, we use the default value in the plug-in project configuration dialog:
Point Next, in the Plug-in Code Builder dialog box we select "Create a blank plugin item":
Why has to be this way? Because we can do every step from the beginning, let us find the principle of code generation of the code provided by Eclipse.
We can notice that in this blank plug-in project, the default is only the basic reference to the Eclipse and the PLUGIN.XML file and the build.properties file prepared for compilation:
Hello button Plugin.xml
In this section, we only use the plugin.xml file to let this plugin displays a button to Eclipse.
Add dependent plugin
The project reference method I use in the newcomer in Eclipse plugin is not only everyone is also very discerning. On the one hand, it is troublesome, and there are too many items in the other. Eclipse is also very zero. This time tells you the stronger features provided in PDE.
First double-click the Plugin.xml file, enter the dependency, we will find that there is no dependency plugin that is dependent:
Let's click the "Add" button to find the "Org.eclipse.ui" item in the pop-up "Org.eclipse.ui" item to select "Finish":
At this time, org.eclipse.ui will join the required plugin list, we will also find that Eclipse will also add the related plugin references to the project after the PLUGIN.XML file store is also added to the project:
Again, after adding, press Ctrl S to make the project dependencies.
Adding an extension
Once the project relies on the new plugin reference, we can extend the extension points provided by this project. We must first declare what we have to expand. Here we want to expand org.eclipse.ui.ActionSets to add a button in Eclipse.
First switch to the extension view of Plugin.xml, click Add button, in the pop-up extension wizard selection dialog, we select "General Wizard" -> "mode-based extension":
Point Next, find the org.eclipse.ui.Actionsets entry in the Extended Point Selection dialog:
This will be more org.eclipse.ui.actionsets entry in all extended lists.
Expand ActionSet
In order to join a button, we must explain our ActionSet and all of its internal Action. This needs to be written in the org.eclipse.ui plugin for DTD to write XML, fortunately, the PDE helps us find the corresponding DTD and prompt us how to write which entries. First use the org.eclipse.ui.actionsets in the extended list in the right-click, select New> ActionSet:
We change the following properties in the Properties dialog:
ID: com.huangdong.plugin.Hello.actionSet Lable: Hello ActionSet
Here ID is the identifier of the specific expansion org.ec.ec.ec.ec.ec.eclipse.ui.actionSet we have to write, and Lable is the name of the display, and it is also good to write less.
Join Action for ActionSet
The specific Action is a behavior specified in a set of actionSets, which is also a minimum unit of behavior.
First use the Hello ActionSet (ActionSet) in the extended list in the right-click, select New> Action:
We change the following properties in the Properties dialog:
ID: com.huangdong.plugin.Hello.HelloAction Lable: Hello
These two attributes are the same as the action in the ActionSet.
First run
Write a Plugin.xml file, let's run this plugin to see the effect. At the same time, it also proved that a plugin does not require a plugin.xml, which will also make the plugin take effect.
Click Run on the menu -> Run to -> Runtime Workbench can put the plugin into a runtime workbench. Want to see if it is deployed and is easy to achieve, one is in the plugin list you can find this plugin:
There is also a window in the menu of the runtime table -> You can find the Hello ActionSet in other in the custom perspective:
We don't find that it will find changes because we haven't defined an action in this Action to a button yet.
Definition button
Let's go to the property page of Hello Action, enter: hdgroup in the value of the ToolbarPath. This way we will expose to the Action in the HDGroup group in Toolbar.
Run the plugin project again, find the Hello ActionSet in the custom perspective, select Press OK, you can see the Hello button in the toolbar:
Which red point is seen? This is our Plugin.xml writing correct and take effect. However, we will see this button will see a message dialog, tell us "The selected operation is currently not available":
This is because we didn't write related code in the specific action, let's join the specific Java code into in.
Generate an Action specific class
Write here I want to say a word: "Please check the best example of PDE when you write a plugin." PDE really gives us a very convenient plugin development environment!
We found the Hello Action in the extended list, locate the class property in its property list, and then find it after it has a button:
When you press this button, you will see the Java class selection dialog, we choose "Generate New Java Class" and enter the following parameters:
Source folder name: SRC / Package name: com.huangdong.plugin.hello class name: HelloAction
After writing, press the completion button, which generates the HelloAction framework code, which implements the iWorkbenchWindowActionDelegate interface. We add this action in the HelloAction's RUN method, we join a messageBox to display the prompt information:
Public void run (IAAADITION Action) {
MessageDialog.openInformation (NULL, NULL, "Hello, Eclipse World");
}
Run Hello Eclipse World
Finally, we will try this project into the runtime work.
Write in the last words
Unlike the newcoming of the Eclipse plugin, we don't use the eclipse's wizard, and then complete the XML file of the Hello Eclipse World plugin and the generation of the Java file. I want to pass this article, you will know more about the development process and operational principles of the Eclipse plugin. Most importantly, in this article, you will know more about the contents and writing methods of Plugin.xml in the plugin project.
If you have any comments and suggestions for this article, please contact me, tell me your thoughts, and can also discuss various technologies related to Java in the Java version of the technology sky BBS.
You can download it from here to the complete Eclipse Project package mentioned herein.
In addition, all code herein is not issued by the author's actual test in the following environments:
Eclipse 2.1.2 / 3.0m6 JDK 1.4.2_02 for Windows / FreeBSD 4.8 / FreeBSD 4.9 FreeBSD 4.8 / FreeBSD 4.9 / Windows 2000 / Windows XP