nicolae.mariuta
nicolae.mariuta

Reputation: 67

Turn Java Swing application into Eclipse Plugin

I already have a Java application with Swing GUI which reads a bunch of XML files and makes some graphs based on the information found in those XML files.

Now I was asked to turn that application into an Eclipse Plugin so the application can be launched from inside the Eclipse IDE. On top of that I have to make my application to sometimes open an XML file which contains the data that the user clicks.

Now, after a fast walk through a tutorial about how to make an Eclipse Plugin, it does not seem that I will be able to use Swing components inside a Plugin Project. I have seen that there are other tools and frameworks for making GUI for a plugin.

I need a suggestion for how can I turn my Swing application into an Eclipse plugin, the easiest possible. Even with some frameworks for Swing I had a hard time to make a treelayout graph. I imagine that should be even harder to implement into Eclipse plugin if Swing components do not work over there.

Here it is how my application looks like right now, based on Swing components: enter image description here

Upvotes: 3

Views: 1730

Answers (2)

Joe
Joe

Reputation: 4514

You can use Swing components inside a Eclipse plugin.

For demonstaration I took the Swing components from https://code.google.com/p/treelayout/ and put them into an Eclipse view:

enter image description here

The important file looks like this:

package createaview.views;


import org.abego.treelayout.demo.swing.SwingDemo;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;


public class TreeView extends ViewPart {


  public static final String ID = "createaview.views.SampleView";

  private TableViewer viewer;

  class ViewContentProvider implements IStructuredContentProvider {
    public void inputChanged(Viewer v, Object oldInput, Object newInput) {}

    public void dispose() {}

    public Object[] getElements(Object parent) {
      return new String[] {"One", "Two", "Three"};
    }
  }
  class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
    public String getColumnText(Object obj, int index) {
      return getText(obj);
    }

    public Image getColumnImage(Object obj, int index) {
      return getImage(obj);
    }

    public Image getImage(Object obj) {
      return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
    }
  }
  class NameSorter extends ViewerSorter {
  }

  public TreeView() {}

  public void createPartControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
    java.awt.Frame frame = SWT_AWT.new_Frame(composite);
    frame.add(SwingDemo.getPanel());
  }

  public void setFocus() {
    viewer.getControl().setFocus();
  }
}

and if you pass me an email address I'll bundle up the demo project I did and send them to you (actually, it should probably be that if this is looking like the right answer, I'll put the projects in a zip file somewhere around here for the community to have a look at)

Upvotes: 2

Alex K.
Alex K.

Reputation: 3304

If you don't want to rewrite the whole application, you may want to check possibilities of using SWT_AWT bridge, which allows to integrate Swing applications into SWT world. It is pretty easy, but you may want to check some articles as well.

I used it to integrate some Swing-based print preview functionality into existing Eclipse-RCP application. Worked well, though it still has its own underwater rocks.

Upvotes: 3

Related Questions