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
When New Action is selected, the dialog shown in Figure 9 is displayed.
Figure 9. 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.
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:
- Open the Plug-in Manifest Editor by double-clicking the plugin.xml file
- Select the Extensions tab at the bottom of the editor
- On the Extensions tab, click Add
- On the New Extension dialog, select Extension Wizards
- Select Extension Templates to display the seven templates covered in this tutorial, as well as the Help Content and Preference Page templates
- Select the Popup Menu template and click Next
- On the Sample Popup Menu page, set the Name Filter to
*, the Action Label to Copy Filename, and the Action Class toCopyFilenameAction - 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.
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.



