Page 1 of 1

Embedding into a SWT application?

Posted: Mon 19. Jan 2009, 13:36
by STRESS
First off I just started using jReality but I already like it a lot. Congrats to the development team. It's the best solution out there and trust me I tried many :roll: (including Java3D)

Now to my real question:

Has anyone a good idea how or ever tried using jReality inside a SWT environment? I tried it with the AWT to SWT bridge descriped on eclipse.org (Snippet #135). the rendering works fine but all the events and popup menus are suppresed except for the left mouse button rotation control? So my guess is there is a problem between the event handling of SWT to jReality.

I don't want to dive into the source code itself as long as I can avoid it.

Posted: Tue 27. Jan 2009, 12:42
by gunn
Oops. Sorry I overlooked this posting. I've asked another team member with more experience to look at this thread.

In the meantime you might be interested to take a look at the source folder src-swt in the jReality svn repository. We have successfully adapted jReality to use SWT but I'm personally not acquainted with the current state of this adaptation.

And thanks for the good words regarding jReality.

SWT

Posted: Tue 27. Jan 2009, 18:24
by sechel
Hello,
I have used jReality in a SWT environment. There is a library called SwingIntegration, that allows for using Swing Components as childs of SWT Compount objects. You can download example code at http://www.eclipse.org/articles/article ... /index.htm.
If you want more information I could send you some source code which uses these functions.
Stefan

Posted: Thu 29. Jan 2009, 12:30
by STRESS
Thanks gunn and sechel. Unfortunately that link of the article seems to be dead :cry:

So I would be really interested in your example code

Source Code

Posted: Mon 2. Feb 2009, 12:56
by sechel
Hi STRESS,
here is the source code of the MainWindow of my SWT/Swing/jReality application. The library used here can be found at
http://www.math.tu-berlin.de/~sechel/fi ... n0.0.2.jar

Code: Select all

package de.varylab.discreteconformal.frontend;

import static org.eclipse.swt.SWT.BORDER;
import static org.eclipse.swt.SWT.FLAT;
import static org.eclipse.swt.SWT.NONE;

import javax.swing.JComponent;
import javax.swing.JPanel;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window.IExceptionHandler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Layout;

import swingintegration.example.EmbeddedSwingComposite;
import de.varylab.discreteconformal.ConformalLab;
import de.varylab.discreteconformal.frontend.action.ExportU3DAction;
import de.varylab.discreteconformal.frontend.action.OpenMeshAction;
import de.varylab.discreteconformal.frontend.action.QuitProgramAction;
import de.varylab.discreteconformal.frontend.shrinkpanels.AppearanceShrinker;
import de.varylab.discreteconformal.frontend.shrinkpanels.TextureShrinker;
import de.varylab.discreteconformal.frontend.shrinkpanels.UnwrapShrinker;
import de.varylab.discreteconformal.frontend.widget.ShrinkPanelContainer;
import de.varylab.discreteconformal.image.ImageHook;

public class MainWindow extends ApplicationWindow implements IExceptionHandler{
	
	private EmbeddedSwingComposite 
		sourceViewer = null;
	
	public MainWindow(){
		super(null);
		addStatusLine();
		addMenuBar();
		addCoolBar(SWT.FLAT | SWT.WRAP);
		setExceptionHandler(this);
	}
	
	private class ControlsLayout extends Layout {

		private Control
			controls = null,
			mainView = null;
		private int 
			controlsWidth = 180;
		
		public ControlsLayout(Control controls, Control mainView, int width) {
			this.controls = controls;
			this.mainView = mainView;
			this.controlsWidth = width;
		}
		
		@Override
		protected Point computeSize(Composite comp, int xh, int yh, boolean ch) {
			return comp.computeSize(SWT.DEFAULT, SWT.DEFAULT, ch);
		}

		@Override
		protected void layout(Composite comp, boolean arg1) {
			Point pSize = comp.getSize();
			for (Control c : comp.getChildren()) {
				if (c == controls){
					c.setBounds(0, 0, controlsWidth, pSize.y);
				}
				if (c == mainView){
					c.setBounds(controlsWidth + 1, 0, pSize.x - controlsWidth - 2, pSize.y);
				}
			}
		}
	}
	
	
	@Override
	protected Control createContents(Composite parent) {
		setStatus("Welcome");
		getShell().setText("Discrete Conformal Parametrization");
		getShell().setSize(1000,740);
		
		CTabFolder mainFolder = new CTabFolder(parent, BORDER | FLAT);


		// object content -----------------------
		Composite objectContent = new Composite(mainFolder, NONE);
		sourceViewer = new EmbeddedSwingComposite(objectContent, BORDER) {
			@Override
			protected JComponent createSwingComponent() {
				JPanel panel = new JPanel();
				panel.setLayout(new java.awt.GridLayout(1, 1));
				panel.add(ConformalLab.getUIController().getViewerAppSource().getViewingComponent());
				return panel;
			}
		};
		sourceViewer.populate();
		
		ShrinkPanelContainer objectShrinkContainer = new ShrinkPanelContainer(objectContent);
		new UnwrapShrinker(objectShrinkContainer);
		new AppearanceShrinker(objectShrinkContainer);
		new TextureShrinker(objectShrinkContainer);

		ControlsLayout layout1 = new ControlsLayout(objectShrinkContainer, sourceViewer, 180);
		objectContent.setLayout(layout1);

		CTabItem mainPage = new CTabItem(mainFolder, NONE);
		mainPage.setText("Object");
		mainPage.setImage(ImageHook.getSWTImage("Standard_24i_box.png"));
		mainPage.setControl(objectContent);
		mainFolder.setSelection(mainPage);
		
		// texture content -------------------------
		Composite textureContent = new Composite(mainFolder, NONE);
		Composite textureViewer = new Composite(objectContent, BORDER); // dummy
		
		ShrinkPanelContainer textureShrinkContainer = new ShrinkPanelContainer(textureContent);
		ControlsLayout layout2 = new ControlsLayout(textureShrinkContainer, textureViewer, 180);
		textureContent.setLayout(layout2);	
		
		CTabItem texturePage = new CTabItem(mainFolder, NONE);
		texturePage.setText("Texture");
		texturePage.setImage(ImageHook.getSWTImage("texture.png"));
		texturePage.setControl(textureContent);
		
 		return parent;
    }	
	
	@Override
	protected MenuManager createMenuManager() {
		MenuManager manager = new MenuManager();
		MenuManager fileMenu = new MenuManager("File");
		fileMenu.add(new OpenMeshAction());
		fileMenu.add(new Separator());
		fileMenu.add(new ExportU3DAction());
		fileMenu.add(new Separator());
		fileMenu.add(new QuitProgramAction(this));
		manager.add(fileMenu);
		return manager;
	}
	
//	@Override
//	protected CoolBarManager createCoolBarManager(int style) {
//		CoolBarManager cm = new CoolBarManager(style);
//		ToolBarManager fileTools = new ToolBarManager(style);
//		fileTools.add(new OpenMeshAction());
//		cm.add(fileTools);
//		return cm;
//	}
	
	@Override
	protected StatusLineManager createStatusLineManager() {
		StatusLineManager manager = new StatusLineManager();
		manager.setCancelEnabled(false);
		return manager;
	}
	

	public void handleException(Throwable e) {
		String message = e.getLocalizedMessage();
		if (message == null || message.equals(""))
			message = "No error message provided";
		MessageDialog.openError(getShell(), "Error", message);
		e.printStackTrace();
	}
	
	@Override
	protected void handleShellCloseEvent() {
		super.handleShellCloseEvent();
		ConformalLab.getUIController().getViewerAppSource().dispose();
		sourceViewer.dispose();
	}
	
}


Posted: Tue 3. Feb 2009, 11:42
by STRESS
Thanks sechel I will have a more detail look on that as soon as it is required and I find some time to do so :D