Eclipse plug-in writing (1), eclipse plug-in writing

Source: Internet
Author: User

Eclipse plug-in writing (1), eclipse plug-in writing

Because there are some repetitive code in the project development process and there is no business logic, it is very troublesome and error-prone to copy and paste, So I think of an eclipse plug-in to meet my work needs, record the information for future reference and learning. This article mainly explains how to develop the eclipse plug-in step by step, but does not explain each step in detail. For details, refer to Baidu and Google.

Reference website:

Http://www.ibm.com/developerworks/cn/java/os-ecplug/

Development Environment: window7, jdk1.6, eclipse4.4

1. Create an eclipse plug-in File> New> Project

Select Plug-in Project, create an eclipse Plug-in Project, enter the Project name TestPlugin, use the default path and default Project Settings, and select the version of the Plug-in running in eclipse.

Click Next To Go To The detailed settings page of the plug-in project,

ID: ID of the plug-in

Version: plug-in Version number

Name: plug-in Name

Vendor: plug-in author information

Execution Environment: Execution Environment of the plug-in

In option, Generate an activator, a Java class that controls the plug-in's life cycle. the following is an Activator input box. After this box is selected, a starter is generated, this java class is used to control the Declaration cycle of the plug-in. This starter is similar to the main method of java. The content in the input box after Activator represents the java class name of the starter to be generated.

Some templates are available after you go to the next step. For the convenience of choosing Hello, World instance project.

Click Finish. The project is created successfully. The directory is as follows:

Src: Project source code directory

Icons: Project image Resource Directory

META-INF/MANIFEST. MF: Project basic configuration information, version, name, starter, etc.

Build. properties: the compilation configuration information of the project, including the source code path, output path,

Plugin. xml: The operation configuration information of the plug-in, including the pop-up menu and the corresponding operation execution class after the menu is clicked,

2. Code Analysis MANIFEST. MF:

Contains the version, name, starter class, plug-ins that must be loaded, and runtime environment.

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: TestPlugin
Bundle-SymbolicName: TestPlugin;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: testplugin.Activator
Require-Bundle: org.eclipse.ui,
 org.eclipse.core.runtime
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
In Plug-in Manifest Editor, open the following:
 
 
Build. properties
The file contains the source file path, compiled output path, and compiled directory.
source.. = src/
output.. = bin/
bin.includes = plugin.xml,\
               META-INF/,\
               .,\
               icons/

In Plug-in Manifest Editor, open the following:

 

Plugin. xml

Relatively speaking, plugin. xml is the most easily changed content, including the plug-in operation set. The content is as follows:

<?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="TestPlugin.actionSet">
         <menu
               label="Sample &amp;Menu"
               id="sampleMenu">
            <separator
                  name="sampleGroup">
            </separator>
         </menu>
         <action
               label="&amp;Sample Action"
               icon="icons/sample.gif"
               class="testplugin.actions.SampleAction"
               tooltip="Hello, Eclipse world"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="testplugin.actions.SampleAction">
         </action>
      </actionSet>
   </extension>
</plugin>

Extension: represents an extension point. point = org. eclipse. ui. actionSets indicates that the extension point is used to add menus and toolbar buttons.

ActionSet: a set of tools

Menu: menu

Action: menu item. Here, the action has an attribute class = "testplugin. actions. SampleAction", which indicates that the run method of this action class will be triggered when you click this menu item.

SimpleAction:

SimpleAction is an automatically generated class that contains a simple program code to implement hello world, as follows:

package testplugin.actions;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.jface.dialogs.MessageDialog;
/**
 * Our sample action implements workbench action delegate.
 * The action proxy will be created by the workbench and
 * shown in the UI. When the user tries to use the action,
 * this delegate will be created and execution will be 
 * delegated to it.
 * @see IWorkbenchWindowActionDelegate
 */
public class SampleAction implements IWorkbenchWindowActionDelegate {
private IWorkbenchWindow window;
/**
 * The constructor.
 */
public SampleAction() {
}
/**
 * The action has been activated. The argument of the
 * method represents the 'real' action sitting
 * in the workbench UI.
 * @see IWorkbenchWindowActionDelegate#run
 */
public void run(IAction action) {
MessageDialog.openInformation(
window.getShell(),
"TestPlugin",
"Hello, Eclipse world");
}
/**
 * Selection in the workbench has been changed. We 
 * can change the state of the 'real' action here
 * if we want, but this can only happen after 
 * the delegate has been created.
 * @see IWorkbenchWindowActionDelegate#selectionChanged
 */
public void selectionChanged(IAction action, ISelection selection) {
}
/**
 * We can use this method to dispose of any system
 * resources we previously allocated.
 * @see IWorkbenchWindowActionDelegate#dispose
 */
public void dispose() {
}
/**
 * We will cache window object in order to
 * be able to provide parent shell for the message dialog.
 * @see IWorkbenchWindowActionDelegate#init
 */
public void init(IWorkbenchWindow window) {
this.window = window;
}
}
When you click the button in the toolbar, this class of run method is automatically called. A prompt box is displayed, prompting Hello, Eclipse world;
 
Activator:

This class is the startup class of the plug-in and controls the lifecycle of the plug-in. The content is as follows:

package testplugin;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {
// The plug-in ID
public static final String PLUGIN_ID = "TestPlugin"; //$NON-NLS-1$
// The shared instance
private static Activator plugin;
/**
 * The constructor
 */
public Activator() {
}
/*
 * (non-Javadoc)
 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
 */
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
}
/*
 * (non-Javadoc)
 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
 */
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}
/**
 * Returns the shared instance
 *
 * @return the shared instance
 */
public static Activator getDefault() {
return plugin;
}
/**
 * Returns an image descriptor for the image file at the given
 * plug-in relative path
 *
 * @param path the path
 * @return the image descriptor
 */
public static ImageDescriptor getImageDescriptor(String path) {
return imageDescriptorFromPlugin(PLUGIN_ID, path);
}
}
 
3. Run the command to check the effect.

First, right-click the project name and choose "Run as> Eclipse Application" to open a new eclipse window, where you can see the newly created plug-in,

Move the mouse over the plug-in icon and a prompt text will appear. Click to view the effect:

 

 

This is the simplest plug-in. Some other functions will be added later to complete the basic functions of a code generator, if any errors are found in this article, please correct them.

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.