1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

[194215] formatted code in popup example, updated tutorial from example, tested

This commit is contained in:
David Dykstal 2007-06-27 01:52:12 +00:00
parent 85464385c7
commit e8659be28e
5 changed files with 229 additions and 143 deletions

View file

@ -10,34 +10,36 @@
<body bgcolor="#ffffff">
<h1>ShowJarContents Class After Creation</h1>
<p>
<pre><samp>
package samples.ui.actions;
import org.eclipse.rse.files.ui.actions.SystemAbstractRemoteFilePopupMenuExtensionAction;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
public class <b>ShowJarContents</b>
extends SystemAbstractRemoteFilePopupMenuExtensionAction
{
public class ShowJarContents implements IObjectActionDelegate {
/**
* Constructor for ShowJarContents.
*/
public <b>ShowJarContents</b>()
{
super();
public ShowJarContents() {
// TODO Auto-generated constructor stub
}
/**
* @see org.eclipse.rse.ui.actions.SystemAbstractPopupMenuExtensionAction#run()
*/
public void <b>run</b>()
{
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
// TODO Auto-generated method stub
}
public void run(IAction action) {
// TODO Auto-generated method stub
}
public void selectionChanged(IAction action, ISelection selection) {
// TODO Auto-generated method stub
}
}
</samp></pre>
</p>
</body>
</html>

View file

@ -4,49 +4,79 @@
<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, 2006. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." >
<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>ShowJarContents Class After Editing</title>
</head>
<body bgcolor="#ffffff">
<h1>ShowJarContents Class After Editing</h1>
<p>
<pre><samp>
package samples.ui.actions;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.rse.core.model.IHost;
import org.eclipse.rse.files.ui.actions.SystemAbstractRemoteFilePopupMenuExtensionAction;
import org.eclipse.rse.core.subsystems.ISubSystem;
import org.eclipse.rse.shells.ui.RemoteCommandHelpers;
import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile;
import org.eclipse.rse.subsystems.shells.core.subsystems.IRemoteCmdSubSystem;
import org.eclipse.rse.ui.SystemBasePlugin;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
public class ShowJarContents2 extends SystemAbstractRemoteFilePopupMenuExtensionAction {
/**
* An action that runs a command to display the contents of a Jar file.
* The plugin.xml file restricts this action so it only appears for .jar files.
*/
public class ShowJarContents implements IObjectActionDelegate {
private List _selectedFiles;
public ShowJarContents2() {
super();
/**
* Constructor for ShowJarContents.
*/
public ShowJarContents() {
_selectedFiles = new ArrayList();
}
public void run() {
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();
}
/* (non-Javadoc)
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
*/
public void run(IAction action) {
IRemoteFile selectedFile = getFirstSelectedRemoteFile();
String cmdToRun = "jar -tvf " + selectedFile.getAbsolutePath();
runCommand(cmdToRun);
}
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");
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();
}
}
/**
* Gets the Command subsystem associated with the current host
*/
private IRemoteCmdSubSystem getRemoteCmdSubSystem() {
public IRemoteCmdSubSystem getRemoteCmdSubSystem() {
//get the Command subsystem associated with the current host
IHost myHost = getSubSystem().getHost();
IRemoteCmdSubSystem[] subsys = RemoteCommandHelpers.getCmdSubSystems(myHost);
for (int i = 0; i < subsys.length; i++) {
@ -57,8 +87,31 @@ public class ShowJarContents2 extends SystemAbstractRemoteFilePopupMenuExtension
return null;
}
public void runCommand(String command) throws Exception {
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");
}
}
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) {
}
}
</samp></pre>
</p>
</body>
</html>

View file

@ -22,12 +22,6 @@ to a local temporary folder, extract the list of file names within the jar, and
display those names in an Eclipse table view.
</p>
<p><b>Tip:</b> If you prefer your Java code to use lined-up braces, select the
first two options in the <b><A href="preferences_JavaFormatting.gif">Code
Formatter</A></b> preferences page for <b>Java</b>, via <b>Windows-&gt;Preferences</b>.
This will affect code generated by wizards. The source code shown assumes this option has been set, but this is not required.
<h2>Step-by-Step: Creating an RSE Remote Resource Pop-up Menu Action</h2>
<ol>
@ -42,7 +36,7 @@ This will affect code generated by wizards. The source code shown assumes this o
&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;´
nameFilter=&quot;*.jar&quot;
id=&quot;actions.jar&quot;&gt;
&lt;action
label=&quot;Show contents&quot;
@ -58,30 +52,54 @@ This will affect code generated by wizards. The source code shown assumes this o
Save and close the file.
</li>
<li>
Create the Java package: right-click on the <B>src</B> source folder and select <B>New-&gt;Package</B> to open the <B>New
Java Package</B> wizard. Enter <B>&quot;samples.ui.actions&quot;</B> for the name of the package and press <B>Finish</B>.</li>
<li>
Create the Java class: right-click on the new <B>&quot;samples.ui.actions&quot;</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>
and <b>&quot;org.eclipse.rse.files.ui.actions.SystemAbstractRemoteFilePopupMenuExtensionAction&quot;</b>
for the <b>Superclass</b>. Select the <b>Constructors from superclass</b> check box, as shown
<A href="popup_newClass.gif">here</A>.
Press <b>Finish</b> to create the <samp><a href="ShowJarContents1.html">ShowJarContents</a></samp> class.
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>Edit the generated <samp>ShowJarContents.java</samp> file as follows:
<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 the following three statements to the body of the <samp>run()</samp> method:</li>
<pre><code>
IRemoteFile selectedFile = getFirstSelectedRemoteFile();
String cmdToRun = "jar -tvf " + selectedFile.getAbsolutePath();
runCommand(cmdToRun);
</code></pre>
<li type="i">Add the following two methods to find the subsystem and run the command:</li>
<pre><code>
private void runCommand(String command) {
<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 && cmdss.isConnected()) {
RemoteCommandHelpers.runUniversalCommand(getShell(), command, ".", cmdss);
// 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");
}
@ -90,7 +108,7 @@ Press <b>Finish</b> to create the <samp><a href="ShowJarContents1.html">ShowJarC
/**
* Gets the Command subsystem associated with the current host
*/
private IRemoteCmdSubSystem getRemoteCmdSubSystem() {
public IRemoteCmdSubSystem getRemoteCmdSubSystem() {
IHost myHost = getSubSystem().getHost();
IRemoteCmdSubSystem[] subsys = RemoteCommandHelpers.getCmdSubSystems(myHost);
for (int i = 0; i < subsys.length; i++) {
@ -100,16 +118,46 @@ Press <b>Finish</b> to create the <samp><a href="ShowJarContents1.html">ShowJarC
}
return null;
}
</code></pre>
<li type="i">User the "Source -> Organize Imports" context menu item to add the appropriate import statements.</li>
</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>Thats it! Now, you can try your new action. Use <b>Run-&gt;Run As-&gt;Run-time Workbench</b>. Drill
<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
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>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View file

@ -12,6 +12,7 @@
*
* Contributors:
* Martin Oberhuber (Wind River) - Adapted original tutorial code to Open RSE.
* David Dykstal (IBM) - formatting for tutorial
********************************************************************************/
package samples.ui.actions;
@ -47,49 +48,42 @@ import org.eclipse.ui.IWorkbenchPart;
* An action that runs a command to display the contents of a Jar file.
* The plugin.xml file restricts this action so it only appears for .jar files.
*/
public class ShowJarContents implements IObjectActionDelegate
{
public class ShowJarContents implements IObjectActionDelegate {
private List _selectedFiles;
/**
* Constructor for ShowJarContents.
*/
public ShowJarContents()
{
public ShowJarContents() {
_selectedFiles = new ArrayList();
}
protected Shell getShell()
{
protected Shell getShell() {
return SystemBasePlugin.getActiveWorkbenchShell();
}
protected IRemoteFile getFirstSelectedRemoteFile()
{
if (_selectedFiles.size() > 0)
{
return (IRemoteFile)_selectedFiles.get(0);
protected IRemoteFile getFirstSelectedRemoteFile() {
if (_selectedFiles.size() > 0) {
return (IRemoteFile) _selectedFiles.get(0);
}
return null;
}
protected ISubSystem getSubSystem()
{
protected ISubSystem getSubSystem() {
return getFirstSelectedRemoteFile().getParentRemoteFileSubSystem();
}
/* (non-Javadoc)
* @see org.eclipse.rse.ui.actions.SystemAbstractPopupMenuExtensionAction#run()
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
*/
public void run(IAction action) {
IRemoteFile selectedFile = getFirstSelectedRemoteFile();
String cmdToRun = "jar -tvf " + selectedFile.getAbsolutePath(); //$NON-NLS-1$
try {
runCommand(cmdToRun);
} catch(Exception e) {
} catch (Exception e) {
String excType = e.getClass().getName();
MessageDialog.openError(getShell(), excType, excType+": "+e.getLocalizedMessage()); //$NON-NLS-1$
MessageDialog.openError(getShell(), excType, excType + ": " + e.getLocalizedMessage()); //$NON-NLS-1$
e.printStackTrace();
}
}
@ -98,7 +92,7 @@ public class ShowJarContents implements IObjectActionDelegate
//get the Command subsystem associated with the current host
IHost myHost = getSubSystem().getHost();
IRemoteCmdSubSystem[] subsys = RemoteCommandHelpers.getCmdSubSystems(myHost);
for (int i=0; i<subsys.length; i++) {
for (int i = 0; i < subsys.length; i++) {
if (subsys[i].getSubSystemConfiguration().supportsCommands()) {
return subsys[i];
}
@ -106,12 +100,9 @@ public class ShowJarContents implements IObjectActionDelegate
return null;
}
public void runCommand(String command) throws Exception
{
public void runCommand(String command) throws Exception {
IRemoteCmdSubSystem cmdss = getRemoteCmdSubSystem();
if (cmdss!=null && cmdss.isConnected()) {
if (cmdss != null && cmdss.isConnected()) {
//Option A: run the command invisibly through SubSystem API
//runCommandInvisibly(cmdss, command);
//Option B: run the command invisibly through Service API
@ -126,71 +117,63 @@ public class ShowJarContents implements IObjectActionDelegate
public static class StdOutOutputListener implements IHostShellOutputListener {
public void shellOutputChanged(IHostShellChangeEvent event) {
IHostOutput[] lines = event.getLines();
for(int i=0; i<lines.length; i++) {
for (int i = 0; i < lines.length; i++) {
System.out.println(lines[i]);
}
}
}
/** New version of running commands through IShellService / IHostShell */
public void runCommandInvisiblyService(IRemoteCmdSubSystem cmdss, String command) throws Exception
{
public void runCommandInvisiblyService(IRemoteCmdSubSystem cmdss, String command) throws Exception {
if (cmdss instanceof IShellServiceSubSystem) {
IShellService shellService = ((IShellServiceSubSystem)cmdss).getShellService();
String [] environment = new String[1];
environment[0] = "AAA=BBB"; //$NON-NLS-1$
String initialWorkingDirectory = "."; //$NON-NLS-1$
IShellService shellService = ((IShellServiceSubSystem) cmdss).getShellService();
String[] environment = new String[1];
environment[0] = "AAA=BBB"; //$NON-NLS-1$
String initialWorkingDirectory = "."; //$NON-NLS-1$
IHostShell hostShell = shellService.launchShell(initialWorkingDirectory, environment, new NullProgressMonitor());
IHostShell hostShell = shellService.launchShell(initialWorkingDirectory, environment, new NullProgressMonitor());
hostShell.addOutputListener(new StdOutOutputListener());
//hostShell.writeToShell("pwd"); //$NON-NLS-1$
//hostShell.writeToShell("echo ${AAA}"); //$NON-NLS-1$
//hostShell.writeToShell("env"); //$NON-NLS-1$
hostShell.writeToShell(command);
hostShell.writeToShell("exit"); //$NON-NLS-1$
//hostShell.writeToShell("pwd"); //$NON-NLS-1$
//hostShell.writeToShell("echo ${AAA}"); //$NON-NLS-1$
//hostShell.writeToShell("env"); //$NON-NLS-1$
hostShell.writeToShell(command);
hostShell.writeToShell("exit"); //$NON-NLS-1$
}
}
/** Old version of running commands through the command subsystem */
public void runCommandInvisibly(IRemoteCmdSubSystem cmdss, String command) throws Exception
{
public void runCommandInvisibly(IRemoteCmdSubSystem cmdss, String command) throws Exception {
command = command + cmdss.getParentRemoteCmdSubSystemConfiguration().getCommandSeparator() + "exit"; //$NON-NLS-1$
Object[] result = cmdss.runCommand(command, null, false, new NullProgressMonitor());
if (result.length>0 && result[0] instanceof IRemoteCommandShell) {
IRemoteCommandShell cs = (IRemoteCommandShell)result[0];
if (result.length > 0 && result[0] instanceof IRemoteCommandShell) {
IRemoteCommandShell cs = (IRemoteCommandShell) result[0];
while (cs.isActive()) {
Thread.sleep(1000);
}
Object[] output = cs.listOutput();
for (int i=0; i<output.length; i++) {
for (int i = 0; i < output.length; i++) {
if (output[i] instanceof IRemoteOutput) {
System.out.println(((IRemoteOutput)output[i]).getText());
System.out.println(((IRemoteOutput) output[i]).getText());
} else if (output[i] instanceof IRemoteError) {
System.err.println(((IRemoteError)output[i]).getText());
System.err.println(((IRemoteError) output[i]).getText());
}
}
cmdss.removeShell(cs);
}
}
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 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) {
// TODO Auto-generated method stub
}
}