I have an eclipse application from which i trigger an Swing widget.On click of a button on the SWT composite a window pops out which contains the Swing Table.

Now i want a button on this Swing page to show up an eclipse workbench window.

Any idea on how I could get across this problem?

Thanks!
Adity

Recommended Answers

All 25 Replies

PlatformUI.getWorkbench.showPerspective(perspectiveID, window)

It has been many months since I worked on Eclipse RCP .. but I think that is the correct way to do it. If you look at the methods available to you in the PlatformUI class you can trace those method calls yourself.
http://publib.boulder.ibm.com/infocenter/rsdhelp/v7r0m0/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/ui/PlatformUI.html

Adity, as I said before, it has been awhile for me. My understanding of a Perspective in Eclipse is that it is a collection of views, whereas a view is basically one window container. Given this information, the following might also be helpful to you:

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(String viewID)

Hi,
Yes I can do this when I am calling it through an action button that SWT uses..but when i am using an AWT action button it gives an error.. SWTException error

lst = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
try
{
workbench.showPerspective("stab.diagram.perspectives.StabInputPerspective",window );
}
catch (WorkbenchException ex)
{
ex.printStackTrace();
}
}
};

The above code throws an error :

Exception in thread "AWT-EventQueue-0" org.eclipse.swt.SWTException: Invalid thread access

Thanks!


PlatformUI.getWorkbench.showPerspective(perspectiveID, window)

It has been many months since I worked on Eclipse RCP .. but I think that is the correct way to do it. If you look at the methods available to you in the PlatformUI class you can trace those method calls yourself.
http://publib.boulder.ibm.com/infocenter/rsdhelp/v7r0m0/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/ui/PlatformUI.html

You get that error because you can't update SWT widgets from anywhere except the SWT Thread. In order to fix this, see these methods:

asyncExec
syncExec

Go to this link, search for "The syncExec() or asyncExec() Methods Do Not Create Threads" and then read everything from there down. They have a code example as well.

Oh!
Thanks!Let me look through it ..Try somethings they suggest and get back ..!

Thanks for the help.I didn't know these forums were this effective,.. :)

Adity

Ok,
I just thought I would be a little more specific about the problem i am tackling here.

So I need to display a file stored(diagram file) in my local hard disk on the eclipse editor utility through a click of a JButton in JFrame.

So it does require some kind of triggering from an AWT widget to SWT.

Adity

ViewPart yourView = (ViewPart)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("viewID");

You might have read through the API and gotten confused because the findView() method is defined as returning an IViewPart. IViewPart is an interface, not a class. ViewPart is a class that implements the IViewPart interface. ViewPart defines the view that you see on your screen, so casting to a ViewPart, like I did above, will be safe. Once you do what I did above to get a handle on your Eclipse RCP view, you can then modify the Composite (which is an SWT widget). I don't remember how to get the Composite with an "out of the box" method, but you can simply define your own getter method in the class. After you modify the Composite to put your diagram on the screen, you're going to want to do the following:

// .. add stuff to the Composite up here
yourComposite.layout(true);

Calling the layout() method causes the Composite to re-display all of its contents. So basically you're telling it you made changes. As for calling SWT from AWT, as far as I know it makes no difference where you call these methods from. As long as you are in an Eclipse RCP built application, you can call all the methods that I have shown you. And as long as you can call all of the methods that I have shown you, you are good to go. Do you get errors when you try to call PlatformUI.getWorkbench()?

PlatformUI.getWorkbench() works and i tried doing just the following to see if it works and it gives a null pointer exception.
What do I initialize it to ?

ViewPart yourView = (ViewPart)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("viewID");

You have to give it the correct viewID where I put "viewID". It is the number associated with your view. If you give it an invalid ID (such as my example) it won't be able to find your view, and thus, will return null, ultimately leading to a NullPointerException. You already defined the View ID when you filled out the form in RCP for your view. But anyway, you can also list all of your view ID numbers like this:

IViewReference[] refs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
for (IViewReference ref: refs) System.out.println(ref.getID());

As for not knowing your view ID, remember this screen from when you originally created your view? It contains your view ID field. (Note: this image is taken from Vogella's Eclipse RCP tutorials)

ya,

I am giving the viewId correctly as in its a string that I had defined while creating my view. Exactly taken from the place like its given in the image.

Hmm, that is interesting. I don't even have Eclipse RCP on my computer right now but I'll download it and make a project real quick and get back to you. Like I said, all of this has been from memory but it has been a while. So give me ten minutes and I'll reply back. .

Ok Wait.
I am not working with eclipse RCP.

I am using eclipse Galileo.I had developed the entire eclipse project in this and now am integrating a java application to this eclipse application.

That's fine. Eclipse RCP says Galileo on it when it runs anyway. It is just a question of having more functionality than "normal Eclipse". As long as you can call PlatformUI.getWorkbench() without problems, you should have the required libraries.

edit:

Another thing I just remembered: if you haven't properly set up the view (so that it actually shows up when your application launches) then calling findView() is not going to work and that might be why it is returning null. You should try running that other piece of code I gave you (the one that prints out all of the view IDs) and show me what it says. My bet is that your view ID is not one of the ones there...

Sorry, I was a lot more rusty with commands than I thought I was. Nevertheless the code I gave you before works. I used this class and it works fine for me. (Excuse the goofiness/bad programming practice with making the Composite a static member... )

package testpluginproj;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

public class ViewPart1 extends ViewPart {
static Composite parent = null;
	public ViewPart1() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stu
		Button ok = new Button (parent, SWT.PUSH);
		this.parent = parent;
		ok.setText ("Push to show the view!");
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				ViewPart yourView = (ViewPart)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("TestPluginProj.myview");  
				Label label = new Label(ViewPart1.parent,SWT.NONE);
				label.setText(yourView.getPartName());
				ViewPart1.parent.layout(true);
			}
		});
		
		parent.layout(true);

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}

Anyway, the above works for me and modifies the View so that it says "Kyle's View" which is the name of it.

Ok I checked and it does give out that view ID .So this is what I think could be a problem. The class which i am using to call the view does not extend ViewParts and hence does not have the CreatePartControl method. Hence the initialization doesn't take place.

because when I tried the piece of code that shows me all the view ids even that was throwing a null pointer exception.

PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(),"StoryEditor.diagram.viewer");  //this probably initializes it or soemthing

ya. This is o.k...!
But how do i call it from a class that does not extend viewParts.. And when i don't wanna write this inside createPartControl()

If you want to show something on an Eclipse Workbench Window like you said a bunch of posts ago, then that something will have to be shown in a View, which in turn, will have to extend ViewPart.

Oh, and if you use the showView method by just replacing that code I showed you earlier by changing findView to showView, then it will make the view appear. Isn't that what you wanted to do this whole time?

Even if I create a separate class like the one you have written and call the createPartControl(parent) method from outside the function what should the argument "parent" be?

I find it hard to believe that none of my suggestions thus far have helped, but additionally, you can check out this article on Swing/SWT Interactions. However, if as you said you want to "show the Swing table in the Eclipse window" you're still going to need to use the suggestions I gave you about how to find the view and how to show the view.

No it has been helpful.I am doing soemthing wrong soemwhere.

So,suppose i have this class viewPart1 like the one you have written.

How do i call it from a swing class?

bt = new JButton();
    	bt.setText("Story plot");
    	bt.setToolTipText("View Story Plot");
    	bt.setRequestFocusEnabled(false);
    	lst = new ActionListener()
    	{
			public void actionPerformed(ActionEvent e) 
			{
                         Composite parent =null;
                         ViewPart1 view = new ViewPart1();
                         view.createPartControl(parent);
                        }
       }

The above code gives me an error saying illegal argument cannot be null;

Even if I create a separate class like the one you have written and call the createPartControl(parent) method from outside the function what should the argument "parent" be?

You should never call createPartControl. Eclipse handles calling that method for you. If you want to make modifications to the View, like I have already suggested, make a getter method in your ViewPart class.

public Composite getComposite(){
return parent; //where parent is what I have in my prev. ex.
}

Then you can add your Swing components to the Composite as is detailed in the article I just linked you to. You can then call the layout(true) method as I told you earlier, causing your changes to be visible. If you give me a few minutes, I will give you a full example if this doesn't make sense.

Ok, here is a quick example for you. As you can see, I created a new SwingComponent (which adds a Swing JButton into the SWT Composite) from within the ViewPart class. But you can call new SwingComponent from anywhere and it will work the same. Again, keep in mind the things I've told you earlier in the thread though: if the View isn't currently showing, you will have to call showView in order to see anything.

package testpluginproj;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

public class ViewPart1 extends ViewPart {
static Composite parent = null;
	public ViewPart1() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stu
		Button ok = new Button (parent, SWT.PUSH);
		this.parent = parent;
		ok.setText ("Push to show the view!");
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				new SwingComponent();
			}
		});
		
		parent.layout(true);

	}

	
	public static Composite getComposite(){
		return parent;
	}
	
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
package testpluginproj;

import java.awt.Frame;

import javax.swing.JButton;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;

public class SwingComponent {

	public SwingComponent(){
		Composite parent = ViewPart1.getComposite();
		Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
		Frame frame = SWT_AWT.new_Frame(composite);
		frame.add(new JButton("STUFF"));
		frame.validate();
		parent.layout(true);
	}

}

Thanks!a lot..This should work!

I will try it and let you know soon!

Thanks :)

Thank you !
Had to make modifications.Used syncExec() also..and finally it works!

Thanks a lot!

No problem, glad you got it working! Mark the thread as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.