Skip Navigation

Extending the SwingControl Class

To wrap your Swing app in either an editor or a view, you will extend the org.eclipse.albireo.core.SwingControl class to encapsulate the JComponent in your application. This component is presented to end-users.

  1. Add a new Java class. On the New Java Class dialog, extend org.eclipse.albireo.core.SwingControl.

        After you extend SwingControl, Eclipse displays the Java class. A partial sample of the code is shown below. You can find this code in SwingMainControl.java in the com.sabre.redapp.example.swing sample plug-in.

  1. You must implement the following two abstract methods from SwingControl: createSwingComponent() and getLayoutAncestor().

        createSwingComponent() returns JComponent. JComponent is presented in your view or editor.

        getLayoutAncesto r registers the class that is notified when something changes, such as the size of the Swing element. Usually it is the parent from the constructor argument.

        SwingMainControl is the sample class that wraps the SwingPanel class and extends SwingControl. You can place SwingMainControl in SWT Composite. (The JPanel class originates from our sample Swing application.)

package com.sabre.redapp.example.swing.editor;

import javax.swing.JComponent;

import org.eclipse.albireo.core.SwingControl;
import org.eclipse.swt.widgets.Composite;

import com.sabre.redapp.example.swing.app.SwingPanel;

public class SwingMainControl extends SwingControl
{

    SwingPanel panel;

    /**
     * {@inheritDoc}
     */
    public SwingMainControl(Composite parent, int style)
    {
        super(parent, style);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected JComponent createSwingComponent()
    {
        panel = new SwingPanel();
        return panel;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Composite getLayoutAncestor()
    {
        return getParent();
    }

}

 

  1. When you extended SwingControl, place the class that you created into an editor or a view. In the previous step, the class is SwingMainControl.

        Refer to the code samples with both an editor and a view. There are no major differences when wrapping a Swing app inside either an editor or a view. You can find this code in SwingView.java and SwingEditor.java in the com.sabre.redapp.example.swing sample plug-in.

        Add your class into Composite in the same way that you would add it for any SWT component.

        Add your class into Composite in a separate thread to prevent lack of responsiveness in your application and to prevent deadlock.

To add interaction with UI or Workbench Elements, complete Modifying Swing Code to Interact with UI or Workbench Elements.

Sample Code that Embeds a Swing App in an Editor

 

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import com.sabre.edge.platform.core.editor.editors.AbstractAppEditor;
import com.sabre.edge.platform.core.ui.threading.UiThreadInvoker;
public class SwingEditor extends AbstractAppEditor {

  /**
   * Constructor
   */
  public SwingEditor() {
    super();
  }

  /**
   * Creates part controls for this editor. Swing control element should be
   * put here. Note that it should be done in a separate thread, and that it
   * might affect your layout.
   *
   * @param parent
   *            parent composite
   */
  @Override
  public void createPartControl(final Composite parent) {
    progressAnimation.init(parent);
    parent.setLayout(new FillLayout());
    UiThreadInvoker<Object> invoker = new UiThreadInvoker<Object>() {

      @Override
      protected Object invoke() {
        new SwingMainControl(parent, SWT.NONE);
        return null;
      }
    };

    invoker.asyncExec();

  }
}

 

Sample Code that Embeds a Swing App in a View

 

package com.sabre.redapp.example.swing.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import com.sabre.redapp.example.swing.editor.SwingMainControl;
import com.sabre.edge.platform.core.ui.threading.UiThreadInvoker;

public class SwingView extends ViewPart
{

    SwingMainControl control;

    /**
     * {@inheritDoc}
     */
    @Override
    public void createPartControl(Composite parent)
    {
        final Composite prnt = parent;
        parent.setLayout(new FillLayout());
        UiThreadInvoker <Object> invoker = new UiThreadInvoker <Object>()
        {

            @Override
            protected Object invoke()
            {
                control = new SwingMainControl(prnt, SWT.NONE);
                return null;
            }
        };
        invoker.asyncExec();

    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setFocus()
    {
        if (control != null)
        {
            control.setFocus();
        }
    }
}