/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.swt.widgets.figures;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;

import org.csstudio.swt.widgets.introspection.DefaultWidgetIntrospector;
import org.csstudio.swt.widgets.introspection.Introspectable;
import org.eclipse.draw2d.Border;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureListener;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.FreeformViewport;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ScalableFreeformLayeredPane;
import org.eclipse.draw2d.ScrollPane;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.gef.editparts.ZoomManager;
import org.eclipse.swt.widgets.Display;

/**The figure of linking container, which can host children widgets from another OPI file.
 * @author Xihui Chen
 *
 */
public class LinkingContainerFigure extends Figure implements Introspectable {
	
	private ScalableFreeformLayeredPane pane;
	
	
	private ZoomManager zoomManager;
	
	private boolean zoomToFitAll;
	
	@SuppressWarnings("deprecation")
	public LinkingContainerFigure() {
		ScrollPane scrollPane = new ScrollPane();
		pane = new ScalableFreeformLayeredPane();
		pane.setLayoutManager(new FreeformLayout());
		setLayoutManager(new StackLayout());
		add(scrollPane);
		FreeformViewport viewPort = new FreeformViewport();
		scrollPane.setViewport(viewPort);
		scrollPane.setContents(pane);	
		
		zoomManager = new ZoomManager(pane, viewPort);
		
		addFigureListener(new FigureListener(){
			public void figureMoved(IFigure source) {
				Display.getDefault().asyncExec(new Runnable(){
					public void run() {
						updateZoom();
					}
				});
				
			}
		});
		
		
		updateZoom();
	}
	
	public IFigure getContentPane(){
		return pane;
	}
	
	public boolean isZoomToFitAll() {
		return zoomToFitAll;
	}
	
	@Override
	public void setBorder(Border border) {
		super.setBorder(border);
		Display.getDefault().asyncExec(new Runnable(){
			public void run() {
				updateZoom();
			}
		});
	}

	public void setZoomToFitAll(boolean zoomToFitAll) {
		this.zoomToFitAll = zoomToFitAll;
		Display.getDefault().asyncExec(new Runnable(){
			public void run() {
				updateZoom();
			}
		});
	}
	
	/**
	 * Refreshes the zoom.
	 */
	public void updateZoom() {
		zoomManager.setZoom(1.0);

		if (zoomToFitAll) {
			zoomManager.setZoomAsText(ZoomManager.FIT_ALL);
		}
	}
	
	public ZoomManager getZoomManager() {
		return zoomManager;
	}

	public BeanInfo getBeanInfo() throws IntrospectionException {
		return new DefaultWidgetIntrospector().getBeanInfo(this.getClass());
	}
	

	
	
	
}

@@ -1901,24 +1901,253 @@
 e, viewPort)
+%7B%0A%09%09%09@Override%0A%09%09%09protected double getFitPageZoomLevel() %7B%0A%09%09%09%09double fitPageZoomLevel = super.getFitPageZoomLevel();%0A%09%09%09%09if(fitPageZoomLevel%3C=0)%7B%0A%09%09%09%09%09fitPageZoomLevel = 0.1;%09%09%09%09%09%0A%09%09%09%09%7D%0A%09%09%09%09return fitPageZoomLevel;%0A%09%09%09%09%0A%09%09%09%7D%0A%09%09%7D
 ;%0A%09%09%0A%09%09addFi
@@ -2940,36 +2940,10 @@
 () %7B
-%0A
 %09%09
-zoomManager.setZoom(1.0);
 %0A%0A%09%09
@@ -3016,16 +3016,49 @@
 LL);%0A%09%09%7D
+else%0A%09%09%09zoomManager.setZoom(1.0);
 %0A%09%7D%0A%09%0A%09p

Fix a linking container zoom bug