Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Building Eclipse plug-ins using templates

Christopher Judd (cjudd@juddsolutions.com), Freelance Writer, Judd Solutions, LLC
Christopher Judd is the president and primary consultant for Judd Solutions, LLC, international speaker, open source evangelist, Central Ohio Java Users Group coordinator, and co-author of Enterprise Java Development on a Budget and Pro Eclipse JST. He has spent eight years developing software in the insurance, retail, government, manufacturing, service, and transportation industries. His current focus is consulting, mentoring, and training with Java, J2EE, J2ME, Web services and related technologies.

Summary:  You may know that Eclipse is a framework meant for building other tools. You may also know that you can build your own plug-ins for Eclipse. But did you know that Eclipse comes with seven plug-in templates to get you started? This tutorial gives you a start-to-finish look at building a plug-in using the Hello World template, then introduces you to the other templates.

Date:  28 Jun 2005
Level:  Intermediate PDF:  A4 and Letter (1317 KB | 74 pages)Get Adobe® Reader®

Activity:  16448 views
Comments:  

Popup Menu template

Default Popup Menu template behavior

In this section, we will use the Popup Menu template as a starting point and modify the behavior to copy a file object's absolute file name to the operating system's clipboard.

The default behavior of the Popup Menu template plug-in is to add a New Submenu > New Action menu to the plugin.xml files context menu (see Figure 8).


Figure 8. Popup Menu template menu
Popup Menu template menu

When New Action is selected, the dialog shown in Figure 9 is displayed.


Figure 9. Popup Menu message dialog
Popup Menu message dialog

The dialog shown in Figure 9 is not very interesting, so we will modify the behavior to make it useful. We will change the behavior to copy the file's absolute file name to the operating system clipboard.


Run Popup Menu template

When we ran the Hello World template, we ran it as part of creating a new Plug-in Project. However, if we already have an existing project, we can still use the templates. For this template and the rest, we will use templates to extend our existing com.ibm.tutorial.templates project by running the template wizards from the Plug-in Manifest Editor.

To run the Popup Menu template:

  1. Open the Plug-in Manifest Editor by double-clicking the plugin.xml file
  2. Select the Extensions tab at the bottom of the editor
  3. On the Extensions tab, click Add
  4. On the New Extension dialog, select Extension Wizards
  5. Select Extension Templates to display the seven templates covered in this tutorial, as well as the Help Content and Preference Page templates
  6. Select the Popup Menu template and click Next
  7. On the Sample Popup Menu page, set the Name Filter to *, the Action Label to Copy Filename, and the Action Class to CopyFilenameAction
  8. Click Finish

Review Popup Menu template results

After running the Popup Menu template, a new CopyFilenameAction.java is added to the project, and a new extension point is added to plugin.xml.

The code for the CopyFilenameAction Popup Menu:

package com.ibm.tutorial.templates.popup.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

public class CopyFilenameAction 
    implements IObjectActionDelegate {

  /**
   * Constructor for Action1.
   */
  public CopyFilenameAction() {
    super();
  }

  /**
   * @see IObjectActionDelegate#setActivePart(
   *       IAction, IWorkbenchPart)
   */
  public void setActivePart(IAction action, 
      IWorkbenchPart targetPart) {}

  /**
   * @see IActionDelegate#run(IAction)
   */
  public void run(IAction action) {
    Shell shell = new Shell();
    MessageDialog.openInformation(
        shell,
        "Templates Plug-in",
        "Copy Filename was executed.");
  }

  /**
   * @see IActionDelegate#selectionChanged(
   *         IAction, ISelection)
   */
  public void selectionChanged(IAction action, 
      ISelection selection) {}

}

The code is an action that implements the org.eclipse.ui.IObjectActionDelegate interface. Its behavior is similar to that of the default Hello World template. Next, we will modify this behavior to copy the absolute file name to the operating system clipboard.

The code for the Popup Menu extension point added to plugin.xml:

<extension point="org.eclipse.ui.popupMenus">
  <objectContribution
      objectClass="org.eclipse.core.resources.IFile"
      nameFilter="*"
      id="com.ibm.tutorial.templates.contribution1">
    <menu
        label="New Submenu"
        path="additions"
        id="com.ibm.tutorial.templates.menu1">
        <separator name="group1"/>
    </menu>
    <action
        enablesFor="1"
        label="Copy Filename"
        class=
"com.ibm.tutorial.templates.popup.actions.CopyFilenameAction"
        menubarPath=
"com.ibm.tutorial.templates.menu1/group1"
        id="com.ibm.tutorial.templates.newAction"/>
      </objectContribution>
   </extension>

With this, CopyFilenameAction is added to any object of any view, including the Package Explorer and Resource Navigator that is an instance of the org.eclipse.core.resources.IFile interface. Just like the Hello World Template, it creates a menu and group. The action itself is then associated with the menu and group using the menubarPath.


Modify Popup Menu behavior

Make the following highlighted changes to modify the behavior of the Popup Menu to copy the absolute file name:

package com.ibm.tutorial.templates.popup.actions;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import com.ibm.tutorial.util.ClipboardUtil;

/**
 * Popup menu to copy the absolute filename to the 
 * clipboard. 
 */
public class CopyFilenameAction implements 
    IObjectActionDelegate 
{

  private ISelection selection;

  /**
   * @see IObjectActionDelegate#setActivePart(
   *          IAction, IWorkbenchPart)
   */
  public void setActivePart(
      IAction action, IWorkbenchPart targetPart) {}

  /**
   * @see IActionDelegate#run(IAction)
   */
  public void run(IAction action) {
    IFile file = getFile();
    IPath osPath = file.getLocation();
    ClipboardUtil.copyTo(
        osPath.toFile().getAbsolutePath());
  }

  /**
   * @see IActionDelegate#selectionChanged(
   *          IAction, ISelection)
   */
  public void selectionChanged(
      IAction action, ISelection selection)
  {
    this.selection = selection;
  }

  private IFile getFile() {
    return (IFile)
        ((IStructuredSelection)selection)
            .getFirstElement();
  }
}

We have now added the private field of selection. This stores a collection of the currently selected items in the tree. It is set in the selectionChanged method called anytime a new item in the tree is selected.

A getFile method was added to simplify typecasting the first item in the selection to the desired IFile.

The run method used above is similar to the implementation of the Hello World template example shown earlier. The only difference is that the file reference is obtained from the getFile method in this instance. Like before, it uses getLocation to get the file name relative to the operating system, rather than the workspace. Then we get a reference to java.io.File and pass its absolute file name to the ClipboardUtil class.

4 of 13 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source
ArticleID=133436
TutorialTitle=Building Eclipse plug-ins using templates
publish-date=06282005
author1-email=cjudd@juddsolutions.com
author1-email-cc=