In this tutorial, you will use the org.eclipse.ui.popupMenus extension point to create a pop-up menu action that will appear in the context menu for any .jar file, for any connection to any system type. The action will be labeled "Show contents" and will simply run the jar -tvf JDK command when selected, displaying the results in the command console. You could expand this example to copy the file to a local temporary folder, extract the list of file names within the jar, and display those names in an Eclipse table view.
<!-- ======================================= -->
<!-- Remote Object Popup Menu Actions -->
<!-- ======================================= -->
<extension point="org.eclipse.ui.popupMenus">
<objectContribution
objectClass="org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile"
nameFilter="*.jar"
id="actions.jar">
<action
label="Show contents"
tooltip="List contents of this file"
class="samples.ui.actions.ShowJarContents"
menubarPath="additions"
enablesFor="1"
id="actions.jar.show">
</action>
</objectContribution>
</extension>
Save and close the file.
private List _selectedFiles;
/**
* Constructor for ShowJarContents.
*/
public ShowJarContents() {
_selectedFiles = new ArrayList();
}
protected Shell getShell() {
return SystemBasePlugin.getActiveWorkbenchShell();
}
protected IRemoteFile getFirstSelectedRemoteFile() {
if (_selectedFiles.size() > 0) {
return (IRemoteFile)_selectedFiles.get(0);
}
return null;
}
protected ISubSystem getSubSystem() {
return getFirstSelectedRemoteFile().getParentRemoteFileSubSystem();
}
public void runCommand(String command) {
IRemoteCmdSubSystem cmdss = getRemoteCmdSubSystem();
if (cmdss != null && cmdss.isConnected()) {
// Run the command in a visible shell
RemoteCommandHelpers.runUniversalCommand(getShell(), command, ".", cmdss); //$NON-NLS-1$
} else {
MessageDialog.openError(getShell(), "No command subsystem", "Found no command subsystem");
}
}
/**
* Gets the Command subsystem associated with the current host
*/
public IRemoteCmdSubSystem getRemoteCmdSubSystem() {
IHost myHost = getSubSystem().getHost();
IRemoteCmdSubSystem[] subsys = RemoteCommandHelpers.getCmdSubSystems(myHost);
for (int i = 0; i < subsys.length; i++) {
if (subsys[i].getSubSystemConfiguration().supportsCommands()) {
return subsys[i];
}
}
return null;
}
public void run(IAction action) {
IRemoteFile selectedFile = getFirstSelectedRemoteFile();
String cmdToRun = "jar -tvf " + selectedFile.getAbsolutePath(); //$NON-NLS-1$
try {
runCommand(cmdToRun);
} catch (Exception e) {
String excType = e.getClass().getName();
MessageDialog.openError(getShell(), excType, excType + ": " + e.getLocalizedMessage()); //$NON-NLS-1$
e.printStackTrace();
}
}
public void selectionChanged(org.eclipse.jface.action.IAction action, org.eclipse.jface.viewers.ISelection selection) {
_selectedFiles.clear();
// store the selected jars to be used when running
Iterator theSet = ((IStructuredSelection) selection).iterator();
while (theSet.hasNext()) {
Object obj = theSet.next();
if (obj instanceof IRemoteFile) {
_selectedFiles.add(obj);
}
}
}
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}
Now, you can try your new action. Use Run->Run As->Eclipse Application. Drill down in the RSE to a Jar file in a local or remote connection and right-click to see and run your new action. Notice how it does not appear for files that do not end with the ".jar" extension. This is because of the "nameFilter" attribute in our extension point .xml file.