/*********************************************************************************************
 *
 * 'SwingControl.java, in plugin ummisco.gama.java2d, is part of the source code of the GAMA modeling and simulation
 * platform. (c) 2007-2016 UMI 209 UMMISCO IRD/UPMC & Partners
 *
 * Visit https://github.com/gama-platform/gama for license information and developers contact.
 * 
 *
 **********************************************************************************************/
package ummisco.gama.java2d.swing;

import java.awt.Rectangle;
import java.awt.Shape;

import javax.swing.JComponent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

import com.vividsolutions.jts.awt.ShapeWriter;

import ummisco.gama.java2d.AWTDisplayView;
import ummisco.gama.java2d.Java2DDisplaySurface;
import ummisco.gama.java2d.SWTGraphics2D;
import ummisco.gama.ui.controls.ParameterExpandBar;
import ummisco.gama.ui.controls.ParameterExpandItem;
import ummisco.gama.ui.interfaces.IParameterEditor;
import ummisco.gama.ui.parameters.AbstractEditor;
import ummisco.gama.ui.views.GamaViewPart;

public class SwingControl extends Canvas{
	AWTDisplayView view;
	ShapeWriter sw = null;
	Shape bound = null;
	GC gc; 
	SWTGraphics2D renderer;

	public SwingControl(final Composite parent, final int style, GamaViewPart v) {
		super(parent, style);
		view = (AWTDisplayView) v;
		((Java2DDisplaySurface) (((AWTDisplayView) view).getDisplaySurface())).comp = this;

		sw = new ShapeWriter((((AWTDisplayView) view).getDisplaySurface().getIGraphics()));
		bound = sw.toShape((((AWTDisplayView) view).getDisplaySurface().getOutput().getScope().getSimulation()
				.getGeometry().getInnerGeometry()));
		SWTGraphics2D.SWT_RECT.width = (int) bound.getBounds2D().getWidth();// g.getEnvelope().getWidth();//surfaceComposite.getSize().x;
		SWTGraphics2D.SWT_RECT.height = (int) bound.getBounds2D().getHeight();// g.getEnvelope().getHeight();//surfaceComposite.getSize().y;
		gc = new GC(this);
		gc.setAdvanced(true);
		gc.setAntialias(SWT.ON);
		renderer = new SWTGraphics2D(gc);

//		addControlListener(new ControlListener() {
//
//			@Override
//			public void controlResized(final ControlEvent e) {
////				if ((((AWTDisplayView) view).getDisplaySurface()) != null) {
////					((AWTDisplayView) view).getDisplaySurface().setBounds(new Rectangle(getSize().x, getSize().y));
////					((AWTDisplayView) view).getDisplaySurface().resizeImage(getSize().x, getSize().y, true);
////				}
//				System.out.println("resize"+e);
//			}
//
//			@Override
//			public void controlMoved(ControlEvent e) {
//				// TODO Auto-generated method stub
//
//			}
//		});
//		addPaintListener(new PaintListener() {
//
//			@Override
//			public void paintControl(PaintEvent arg0) {
//				if ((((AWTDisplayView) view).getDisplaySurface()) != null) {
//					((AWTDisplayView) view).getDisplaySurface().setBounds(new Rectangle(getSize().x, getSize().y));
//					((AWTDisplayView) view).getDisplaySurface().resizeImage(getSize().x, getSize().y, true);
//				}
//				redraw();
//			}
//		});
	}

	@Override
	public void redraw() {
//		super.redraw();
		if ((((AWTDisplayView) view).getDisplaySurface()) != null) {
//			if ((((AWTDisplayView) view).getDisplaySurface().getIGraphics()) == null) {
//				((AWTDisplayView) view).getDisplaySurface().setBounds(new Rectangle(getSize().x, getSize().y));
//			}
//			IShape g =getOutput().getScope().getSimulation().getGeometry();
//			System.out.println("paint  "+s.getBounds2D());
//			getDisplaySurface().setBounds(new Rectangle(width, height));
//			getDisplaySurface().resizeImage(width, height, true);
//			SWTGraphics2D renderer=new SWTGraphics2D(arg0.gc, arg0.display);
//			renderer.SWT_RECT.width=width;
//			renderer.SWT_RECT.height=height;

			((AWTDisplayView) view).getDisplaySurface().setBounds(new Rectangle(getSize().x, getSize().y));
			((AWTDisplayView) view).getDisplaySurface().resizeImage(getSize().x, getSize().y, true);
			((Java2DDisplaySurface) (((AWTDisplayView) view).getDisplaySurface())).paintComponent(renderer);
//			renderer.dispose();
		}
	}

}

@@ -2932,18 +2932,16 @@
 //%09%09%7D);%0A
-//
 %09%09addPai
@@ -2965,37 +2965,33 @@
 intListener() %7B%0A
-//%0A//
+%0A
 %09%09%09@Override%0A//%09
@@ -2979,34 +2979,32 @@
 %7B%0A%0A%09%09%09@Override%0A
-//
 %09%09%09public void p
@@ -3030,26 +3030,24 @@
 ent arg0) %7B%0A
-//
 %09%09%09%09if ((((A
@@ -3095,26 +3095,24 @@
  != null) %7B%0A
-//
 %09%09%09%09%09((AWTDi
@@ -3200,18 +3200,16 @@
 ().y));%0A
-//
 %09%09%09%09%09((A
@@ -3294,26 +3294,22 @@
  true);%0A
-//
 %09%09%09%09%7D%0A
-//
 %09%09%09%09redr
@@ -3314,25 +3314,21 @@
 draw();%0A
-//
 %09%09%09%7D%0A
-//
 %09%09%7D);%0A%09%7D

test ip