Creating a Remote Resource pop-up Menu Action

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.

Tip: If you prefer your Java code to use lined-up braces, select the first two options in the Code Formatter preferences page for Java, via Windows->Preferences. This will affect code generated by wizards. The source code shown assumes this option has been set, but this is not required.

Step-by-Step: Creating an RSE Remote Resource Pop-up Menu Action

  1. If you have not already, first create or prepare a plugin project.
  2. Open the plugin.xml file for editing by right-clicking on it and selecting Open With->Text Editor. Before the ending </plugin> statement, add the following lines:
    
       <!-- ======================================= -->
       <!-- 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.
  3. Create the Java package: right-click on the src source folder and select New->Package to open the New Java Package wizard. Enter "samples.ui.actions" for the name of the package and press Finish.
  4. Create the Java class: right-click on the new "samples.ui.actions" package folder and select New->Class to open the New Java Class wizard. Enter "ShowJarContents" for the Name and "org.eclipse.rse.files.ui.actions.SystemAbstractRemoteFilePopupMenuExtensionAction" for the Superclass. Select the Constructors from superclass check box, as shown here. Press Finish to create the ShowJarContents class.
  5. Edit the generated ShowJarContents.java file as follows:
    1. Add the following three statements to the body of the run() method:
    2. 
      		IRemoteFile selectedFile = getFirstSelectedRemoteFile();
      		String cmdToRun = "jar -tvf " + selectedFile.getAbsolutePath();
      		runCommand(cmdToRun);  
        
    3. Add the following two methods to find the subsystem and run the command:
    4. 
      	private void runCommand(String command) {
      		IRemoteCmdSubSystem cmdss = getRemoteCmdSubSystem();
      		if (cmdss != null && cmdss.isConnected()) {
      			RemoteCommandHelpers.runUniversalCommand(getShell(), command, ".", cmdss);
      		} else {
      			MessageDialog.openError(getShell(), "No command subsystem", "Found no command subsystem");
      		}
      	}
      	
      	/**
      	 * Gets the Command subsystem associated with the current host
      	 */
      	private 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;
      	}
        
    5. User the "Source -> Organize Imports" context menu item to add the appropriate import statements.
    The final result after editing is shown here.

Thats it! Now, you can try your new action. Use Run->Run As->Run-time Workbench. 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.