Get started with plug-ins for Eclipse plugin development

Source: Internet
Author: User


Recently, due to special needs, began to learn plug-in development.

The previous contact with Java is some time, but has not known that there is a plug-in development of such a technical path, this thinking that the development tools are ready for you, directly use not on the line. But there are always some it factories, in order to save costs, develop their own development tools, but also save time, always can not do everything themselves. After all, it takes a long time to develop an eclipse. As a result, plug-in development appears on the historical stage.


  First of all to understand the plug-in development, you have to start from Swt/jface. SWT is an open-source interface development framework that used to use some panel when learning Java, like this. JFace is also an SWT-based UI that is missing from the API. Eclipse is developed using this, which provides the power of Eclipse extensibility, so that users can arbitrarily insert their own desired plugins to develop their own IDE.


Let's get a simple plugin right here!





First meet eclipse, this people should be very familiar with!



1 The red part is our toolbar



2 The blue part is the view



3 The yellow part is the editor



Usually we use the editor for code manipulation, or for business operations. In the view, some resources are viewed and so on. Red introduces some common features that help us with our operations.



Let's start with a simple toolbar control and learn about Eclipse's plug-in development process!


1 Create a new plug-in project




2 Create your own plug-in name, this name is best a special point, once again fused to eclipse, there will be no conflict.





  First, write your own plug-in name.



The source folder is the code path for the plug-in.



The output folder is the destination path for the plug-in outputs.



The following is the version of Eclipse that developed the plugin.


3 Next, make specific details of the plugin settings





ID is the identity of the plugin



Version is the release of the plugin



Name is the plug-in Names



Provider is a developer's message






The following activator, the activation class for plugins, is used to manage the life cycle of the plug-in.






The last is to choose whether to develop RCP, rich client applications, for the moment, do not select the line.


4 Create a plug-in tool using the plugin template





Choose Hello World. This is a toolbar button.


5 Next, make the button information set.





The default is to generate the name of the class, the path (package name), and a message prompt from the toolbar button.


6 Click Finish to complete the creation of the plugin.





That way, we're done creating a plugin, so let's see what eclipse has generated for us.





1 The jar packages needed to import the plugin

2 imported the libraries that the plug-in relies on

3 Source files

4 Plugin button Picture

5 configuration information for the plugin

   MANIFEST. MF plug-in bundle information
 
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: my Plug-in unit 
Bundle-SymbolicName: com.test.myplugin; singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.test.myplugin.Activator
Bundle-Vendor: xingoo
Require-Bundle: org.eclipse.ui,
 org.eclipse.core.runtime
Bundle-ActivationPolicy: lazy


Name is the plugin name we set previously

Symblicname is the package name of our plugin

Version is the release of the plugin

Activator is the activation class for plugins

Vendor is a plugin developer's information

Bundle is a library since the plugin


  This information corresponds to the information on the overview page of the plugin.



  


detailed setup documentation for Plugin.xml plug-ins, including extension point information for plug-ins, and plugin's own information
 
 
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

   <extension
         point="org.eclipse.ui.actionSets">
      <actionSet
            label="Sample Action Set"
            visible="true"
            id="com.test.myplugin.actionSet">
         <menu
               label="Sample &amp;Menu"
               id="sampleMenu">
            <separator
                  name="sampleGroup">
            </separator>
         </menu>
         <action
               label="&amp;Sample Action"
               icon="icons/sample.gif"
               class="com.test.myplugin.actions.SampleAction"
               tooltip="Hello, Eclipse world"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="com.test.myplugin.actions.SampleAction">
         </action>
      </actionSet>
   </extension>

</plugin>


The body of the <plugin> elements list

<extension> plug-in functionality extension, which includes the identity of point extension points, ID extension instance identification, name of the user provided by names, etc.

List of elements built build.properties
 
source.. = src/
output.. = bin/
bin.includes = plugin.xml,               META-INF/,               .,               icons/

It includes the source file directory of the plug-in, generates the file directory, and introduces some configuration information.

plug-in class provided Activator.java
1 package com.test.myplugin;
Two
3 import org.eclipse.jface.resource.ImageDescriptor;
4 import org.eclipse.ui.plugin.AbstractUIPlugin;
5 import org.osgi.framework.BundleContext;
Six
7 / * *
8  * The activator class controls the plug-in life cycle
9 * /
10 public class Activator extends AbstractUIPlugin {
Eleven
12     // The plug-in ID
13     public static final String PLUGIN_ID = "com.test.myplugin";
Fourteen
15     // The shared instance
16     private static Activator plugin;
Seventeen
18 / * *
19      * The constructor
20 * /
21     public Activator() {
22}
Twenty-three
24 / *
25      * (non-Javadoc)
26      * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
27 * /
28     public void start(BundleContext context) throws Exception {
29         super.start(context);
30         plugin = this;
31}
Thirty-two
33 / *
34      * (non-Javadoc)
35      * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
36 * /
37     public void stop(BundleContext context) throws Exception {
38         plugin = null;
39         super.stop(context);
40}
Forty-one
42 / * *
43      * Returns the shared instance
44 *
45      * @return the shared instance
46 * /
47     public static Activator getDefault() {
48         return plugin;
49}
Fifty
51 / * *
52      * Returns an image descriptor for the image file at the given
53      * plug-in relative path
54 *
55      * @param path the path
56      * @return the image descriptor
57 * /
58     public static ImageDescriptor getImageDescriptor(String path) {
59         return imageDescriptorFromPlugin(PLUGIN_ID, path);
60}
61} 

Start () and stop () are used for plug-in start and stop invocation functions, respectively.











Finally, let's run the plugin!









Start Mode 1 directly in the overview interface click;



Start Mode 2 You can also click the Run or Debug button to run the mode select Eclipse Application.



  



Clicking Start will re-open an eclipse for us, which is the new eclipse with the plugin we created. The start effect is as follows:









Such a simple plug-in has been developed! Let's really set sail!!!


Related Article

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.