package org.csstudio.opibuilder.widgets.figures;


import org.csstudio.opibuilder.model.AbstractContainerModel;
import org.csstudio.platform.ui.util.CustomMediaFactory;
import org.csstudio.platform.ui.util.UIBundlingThread;
import org.eclipse.draw2d.Graphics;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.internal.browser.BrowserViewer;

/**Figure for a web browser widget.
 * @author Xihui Chen
 *
 */
@SuppressWarnings("restriction")
public class WebBrowserFigure extends AbstractSWTWidgetFigure {

	
	private final static Color WHITE_COLOR = 
		CustomMediaFactory.getInstance().getColor(CustomMediaFactory.COLOR_WHITE);
	
	private BrowserViewer browserViewer;
	private Browser browser;
	
	
	public WebBrowserFigure(Composite composite, AbstractContainerModel parentModel, boolean runmode) {
		super(composite, parentModel);
		this.runmode = runmode;
		if(runmode){
			browserViewer = new BrowserViewer(composite, BrowserViewer.BUTTON_BAR | BrowserViewer.LOCATION_BAR);
			browser = browserViewer.getBrowser();
//			toolbarArmedBrowser = new Composite(composite, SWT.None);
//			toolbarArmedBrowser.setLayout(new GridLayout(3, false));
//			ToolBar toolbar = new ToolBar(toolbarArmedBrowser, SWT.NONE);
//			ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
//			itemBack.setImage(CustomMediaFactory.getInstance().getImageFromPlugin(
//					Activator.PLUGIN_ID, "icons/nav_backward.gif"));
//			ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
//			itemForward.setImage(CustomMediaFactory.getInstance().getImageFromPlugin(
//					Activator.PLUGIN_ID, "icons/nav_forward.gif"));
//			ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
//			itemStop.setImage(CustomMediaFactory.getInstance().getImageFromPlugin(
//					Activator.PLUGIN_ID, "icons/nav_stop.gif"));
//			ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
//			itemRefresh.setImage(CustomMediaFactory.getInstance().getImageFromPlugin(
//					Activator.PLUGIN_ID, "icons/nav_refresh.gif"));
//		
//			
//			GridData data = new GridData();
//			toolbar.setLayoutData(data);
//			
//			final Text location = new Text(toolbarArmedBrowser, SWT.BORDER);
//			data = new GridData();
//			data.horizontalAlignment = GridData.FILL;
//			data.grabExcessHorizontalSpace = true;
//			location.setLayoutData(data);
//			
//			Button goButton = new Button(toolbarArmedBrowser, SWT.PUSH);
//			goButton.setImage(CustomMediaFactory.getInstance().getImageFromPlugin(
//					Activator.PLUGIN_ID, "icons/nav_go.gif"));
//
//			browser = new Browser(toolbarArmedBrowser, SWT.NONE);
//			browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		}
	}

	public void setUrl(String url){
		if(runmode && url.trim().length() > 0)
			browserViewer.setURL(url);
	}

	@Override
	protected void paintClientArea(Graphics graphics) {			
		//draw this so that it can be seen in the outline view
		if(!runmode){
			graphics.setBackgroundColor(WHITE_COLOR);
			graphics.fillRectangle(getClientArea());
		}
		super.paintClientArea(graphics);	
	}
	
	@Override
	public Composite getSWTWidget() {
		return browserViewer;
	}	
	
	@Override
	public void dispose() {
		if(runmode){
			super.dispose();	
			//the browser must be disposed in the queue of UI thread, 
			//so that multiple browsers can be properly disposed.
			UIBundlingThread.getInstance().addRunnable(new Runnable() {			
				public void run() {				
					browserViewer.dispose();
					browserViewer = null;
					browser = null;
				}
			});
		}
		
	}

	public Browser getBrowser() {
		return browser;
	}
	
}

@@ -996,16 +996,22 @@
 rViewer(
+%0A%09%09%09%09%09
 composit
@@ -1071,1524 +1071,165 @@
 BAR)
-;%0A%09%09%09browser = browserViewer.getBrowser();%0A//%09%09%09toolbarArmedBrowser = new Composite(composite, SWT.None);%0A//%09%09%09toolbarArmedBrowser.setLayout(new GridLayout(3, false));%0A//%09%09%09ToolBar toolbar = new ToolBar(toolbarArmedBrowser, SWT.NONE);%0A//%09%09%09ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);%0A//%09%09%09itemBack.setImage(CustomMediaFactory.getInstance().getImageFromPlugin(%0A//%09%09%09%09%09Activator.PLUGIN_ID, %22icons/nav_backward.gif%22));%0A//%09%09%09ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);%0A//%09%09%09itemForward.setImage(CustomMediaFactory.getInstance().getImageFromPlugin(%0A//%09%09%09%09%09Activator.PLUGIN_ID, %22icons/nav_forward.gif%22));%0A//%09%09%09ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);%0A//%09%09%09itemStop.setImage(CustomMediaFactory.getInstance().getImageFromPlugin(%0A//%09%09%09%09%09Activator.PLUGIN_ID, %22icons/nav_stop.gif%22));%0A//%09%09%09ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);%0A//%09%09%09itemRefresh.setImage(CustomMediaFactory.getInstance().getImageFromPlugin(%0A//%09%09%09%09%09Activator.PLUGIN_ID, %22icons/nav_refresh.gif%22));%0A//%09%09%0A//%09%09%09%0A//%09%09%09GridData data = new GridData();%0A//%09%09%09toolbar.setLayoutData(data);%0A//%09%09%09%0A//%09%09%09final Text location = new Text(toolbarArmedBrowser, SWT.BORDER);%0A//%09%09%09data = new GridData();%0A//%09%09%09data.horizontalAlignment = GridData.FILL;%0A//%09%09%09data.grabExcessHorizontalSpace = true;%0A//%09%09%09location.setLayoutData(data
+%7B%0A%09%09%09%09@Override%0A%09%09%09%09public void setBounds(int x, int y, int width,%0A%09%09%09%09%09%09int height) %7B%0A%09%09%09%09%09super.setBounds(x, y, width, height);%0A%09%09%09%09%09layout(
 );%0A
-//
 %09%09%09
-%0A//%09%09%09Button goButton = new Button(toolbarArmedBrowser, SWT.PUSH);%0A//%09%09%09goButton.setImage(CustomMediaFactory.getInstance().getImageFromPlugin(%0A//%09%09%09%09%09Activator.PLUGIN_ID, %22icons/nav_go.gif%22));%0A//%0A//
+%09%7D%0A%09%09%09%7D;%0A
 %09%09%09b
@@ -1241,123 +1241,33 @@
 r = 
-new B
+b
 rowser
-(toolbarArmedBrowser, SWT.NONE);%0A//%09%09%09browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)
+Viewer.getBrowser(
 );%0A%09

fix a bug on web browser widget that the browser is not located corretly