1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-05 08:05:24 +02:00
cdt/rse/doc/org.eclipse.rse.doc.isv/guide/tutorial/popup.html

163 lines
6.5 KiB
HTML
Executable file

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
<meta name="copyright" content="Copyright (c) IBM Corporation and others 2002, 2007. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." >
<LINK REL="STYLESHEET" HREF="../../book.css" TYPE="text/css">
<title>Creating a Remote Resource Popup Menu Action</title>
</head>
<body bgcolor="#ffffff">
<h1>Creating a Remote Resource pop-up Menu Action</h1>
<p>In this tutorial, you will use the
<samp><A href="../plugin/popup.html">org.eclipse.ui.popupMenus</a></samp> extension point to
create a pop-up menu action that will appear
in the context menu for any <samp>.jar</samp> file, for any connection to
any system type. The action will be labeled "Show contents" and will simply
run the <samp>jar -tvf</samp> 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.
</p>
<h2>Step-by-Step: Creating an RSE Remote Resource Pop-up Menu Action</h2>
<ol>
<li>If you have not already, first <a href="pdeProject.html">create or prepare a plugin project</a>.
</li>
<li>Open the <b>plugin.xml</b> file for editing by right-clicking on it and selecting
<b>Open With-&gt;Text Editor</b>. Before the ending &lt;/plugin&gt; statement, add the following lines:
<pre><code>
&lt;!-- ======================================= --&gt;
&lt;!-- Remote Object Popup Menu Actions --&gt;
&lt;!-- ======================================= --&gt;
&lt;extension point=&quot;org.eclipse.ui.popupMenus&quot;&gt;
&lt;objectContribution
objectClass=&quot;org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile&quot;
nameFilter=&quot;*.jar&quot;
id=&quot;actions.jar&quot;&gt;
&lt;action
label=&quot;Show contents&quot;
tooltip=&quot;List contents of this file&quot;
class=&quot;samples.ui.actions.ShowJarContents&quot;
menubarPath=&quot;additions&quot;
enablesFor=&quot;1&quot;
id=&quot;actions.jar.show&quot;&gt;
&lt;/action&gt;
&lt;/objectContribution&gt;
&lt;/extension&gt;
</code></pre>
Save and close the file.
</li>
<li>
Right-click on the project and use the <b>New...</b> action to create a new package in this project named <b>samples.ui.actions</b>.
</li>
<li>
Create the Java class: right-click on the new <B>samples.ui.actions</B> package folder and select <B>New-&gt;Class</B> to open the <B>New
Java Class</B> wizard.
Enter <B>&quot;ShowJarContents&quot;</B> for the <b>Name</b>, add <b>IObjectActionDelegate</b>
to the <b>Interfaces</b> that are implemented, and check the <b>constructors from superclass</b> checkbox as shown
<A href="popup_newClass.gif">here</A>.
Click <b>Finish</b> to create the <samp><a href="ShowJarContents1.html">ShowJarContents</a></samp> class.
</li>
<li>Edit the generated <samp>ShowJarContents.java</samp> file as follows.
Use the "Source -&gt; Organize Imports" context menu item to add the appropriate import statements as you edit.
<ol>
<li type="i">Add an instance variable to hold a list of remote files and initialize it in the constructor.</li>
<pre><code>
private List _selectedFiles;
/**
* Constructor for ShowJarContents.
*/
public ShowJarContents() {
_selectedFiles = new ArrayList();
}
</code></pre>
<li type="i">Add the following three utility methods</li>
<pre><code>
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();
}
</code></pre>
<li type="i">Add the following methods to find the subsystem and run the command:</li>
<pre><code>
public void runCommand(String command) {
IRemoteCmdSubSystem cmdss = getRemoteCmdSubSystem();
if (cmdss != null &amp;&amp; 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 &lt; subsys.length; i++) {
if (subsys[i].getSubSystemConfiguration().supportsCommands()) {
return subsys[i];
}
}
return null;
}
</code></pre>
<li type="i">Finally, flesh out the methods that were created as stubs</li>
<pre><code>
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) {
}
</code></pre>
</ol>
The final result after editing is shown <a href="ShowJarContents2.html">here</a>.
</li>
</ol>
<p>
Now, you can try your new action. Use <b>Run-&gt;Run As-&gt;Eclipse Application</b>.
Drill
down in the RSE to a Jar file in a local or remote connection and right-click to <a href="popup_see.gif">see</a> and <a href="popup_run.gif">run</a> 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.
</body>
</html>