<commit_before>/*******************************************************************************
 * 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.opibuilder.editparts;

import org.csstudio.opibuilder.OPIBuilderPlugin;
import org.csstudio.opibuilder.model.AbstractWidgetModel;
import org.csstudio.opibuilder.model.ConnectionModel;
import org.csstudio.opibuilder.model.DisplayModel;
import org.csstudio.opibuilder.properties.IWidgetPropertyChangeHandler;
import org.csstudio.opibuilder.util.OPIColor;
import org.csstudio.ui.util.CustomMediaFactory;
import org.csstudio.ui.util.SWTConstants;
import org.csstudio.ui.util.thread.UIBundlingThread;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.SnapToGeometry;
import org.eclipse.gef.SnapToGrid;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.editparts.ZoomManager;
import org.eclipse.gef.editpolicies.RootComponentEditPolicy;
import org.eclipse.gef.rulers.RulerProvider;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IActionFilter;

/**The editpart for the root display.
 * @author <NAME> (class of same name in SDS)
 * @author <NAME>
 */
public class DisplayEditpart extends AbstractContainerEditpart {

	private ControlListener zoomListener, scaleListener;
	
	private org.eclipse.swt.graphics.Point originSize, oldSize;
	
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();

		// disallows the removal of this edit part from its parent
		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new RootComponentEditPolicy());
		removeEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE);
	}
	
	@Override
	public void activate() {
		super.activate();
		initProperties();
		
		if(getExecutionMode() == ExecutionMode.RUN_MODE && 
				getWidgetModel().getDisplayScaleData().isAutoScaleWidgets()){
			originSize = new org.eclipse.swt.graphics.Point(
					getWidgetModel().getWidth(), getWidgetModel().getHeight());
			oldSize=originSize;
			scaleListener = new ControlAdapter() {
				@Override
				public void controlResized(ControlEvent e) {
					if(getViewer() == null || getViewer().getControl().isDisposed())
						return;
					org.eclipse.swt.graphics.Point size = 
							getViewer().getControl().getSize();
					if(size.equals(oldSize))
						return;
					//In RAP, each revalidate will enlarge the shell by 1000, see  
					//org.eclipse.rwt.internal.textsize.TextSizeRecalculation.enlargeShell(Shell shell)
					if(OPIBuilderPlugin.isRAP() && (size.x - oldSize.x) == 1000 && (size.y - oldSize.y) == 1000)
						return;
					double widthRatio = size.x /(double)originSize.x;
					double heightRatio = size.y/(double)originSize.y;
					oldSize = size;
					getWidgetModel().scale(widthRatio, heightRatio);
//					oldSize = size;					
					//make sure the display figure repaint everything especially for RAP.
					if(OPIBuilderPlugin.isRAP())
						UIBundlingThread.getInstance().addRunnable(new Runnable() {
							
							@Override
							public void run() {
								getFigure().revalidate();
								getFigure().repaint();
								
							}
						});
				}
			};
			UIBundlingThread.getInstance().addRunnable(new Runnable() {
				
				@Override
				public void run() {
					scaleListener.controlResized(null);					
				}
			});
			getViewer().getControl().addControlListener(scaleListener);
		}
		
		if(getExecutionMode() == ExecutionMode.RUN_MODE && getWidgetModel().isAutoZoomToFitAll()){
			originSize = new org.eclipse.swt.graphics.Point(
					getWidgetModel().getWidth(), getWidgetModel().getHeight());
			oldSize=originSize;
			zoomListener = new ControlAdapter() {
					@Override
					public void controlResized(ControlEvent e) {
						if(!isActive() || getViewer() == null || getViewer().getControl().isDisposed())
							return;
						org.eclipse.swt.graphics.Point size = 
								((FigureCanvas)getViewer().getControl()).getSize();
						if(size.equals(oldSize))
							return;
						//In RAP, each revalidate will enlarge the shell by 1000, see  
						//org.eclipse.rwt.internal.textsize.TextSizeRecalculation.enlargeShell(Shell shell)
						if(OPIBuilderPlugin.isRAP() && (size.x - oldSize.x) == 1000 && (size.y - oldSize.y) == 1000)
							return;
						if (size.x * size.y > 0)
							((ScalableFreeformRootEditPart)getRoot()).getZoomManager().setZoomAsText(
									ZoomManager.FIT_ALL);
						oldSize = size;

					}
				};
			UIBundlingThread.getInstance().addRunnable(new Runnable() {
				
				@Override
				public void run() {
					zoomListener.controlResized(null);					
				}
			});
			getViewer().getControl().addControlListener(zoomListener);
		}
		UIBundlingThread.getInstance().addRunnable(new Runnable() {			
			@Override
			public void run() {
				getViewer().getControl().forceFocus();
			}
		});
		
	}
	
	@Override
	public void deactivate() {
		final Control control = getViewer().getControl();
		if (getViewer() != null && !control.isDisposed()) {
			//This needs to be executed in UI Thread
			final ZoomManager zoomManager = ((ScalableFreeformRootEditPart) getRoot()).getZoomManager();
			control.getDisplay().asyncExec(new Runnable() {
				
				@Override
				public void run() {
					if (zoomListener != null && !control.isDisposed()) {
						// recover zoom
						zoomManager.setZoom(1.0);
						control.removeControlListener(zoomListener);
					}<commit_msg>BOY: check viewer and control in UIBundlingThread<commit_after>/*******************************************************************************
 * 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.opibuilder.editparts;

import org.csstudio.opibuilder.OPIBuilderPlugin;
import org.csstudio.opibuilder.model.AbstractWidgetModel;
import org.csstudio.opibuilder.model.ConnectionModel;
import org.csstudio.opibuilder.model.DisplayModel;
import org.csstudio.opibuilder.properties.IWidgetPropertyChangeHandler;
import org.csstudio.opibuilder.util.OPIColor;
import org.csstudio.ui.util.CustomMediaFactory;
import org.csstudio.ui.util.SWTConstants;
import org.csstudio.ui.util.thread.UIBundlingThread;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.SnapToGeometry;
import org.eclipse.gef.SnapToGrid;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.editparts.ZoomManager;
import org.eclipse.gef.editpolicies.RootComponentEditPolicy;
import org.eclipse.gef.rulers.RulerProvider;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IActionFilter;

/**The editpart for the root display.
 * @author <NAME> (class of same name in SDS)
 * @author <NAME>
 */
public class DisplayEditpart extends AbstractContainerEditpart {

	private ControlListener zoomListener, scaleListener;
	
	private org.eclipse.swt.graphics.Point originSize, oldSize;
	
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();

		// disallows the removal of this edit part from its parent
		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new RootComponentEditPolicy());
		removeEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE);
	}
	
	@Override
	public void activate() {
		super.activate();
		initProperties();
		
		if(getExecutionMode() == ExecutionMode.RUN_MODE && 
				getWidgetModel().getDisplayScaleData().isAutoScaleWidgets()){
			originSize = new org.eclipse.swt.graphics.Point(
					getWidgetModel().getWidth(), getWidgetModel().getHeight());
			oldSize=originSize;
			scaleListener = new ControlAdapter() {
				@Override
				public void controlResized(ControlEvent e) {
					if(getViewer() == null || getViewer().getControl().isDisposed())
						return;
					org.eclipse.swt.graphics.Point size = 
							getViewer().getControl().getSize();
					if(size.equals(oldSize))
						return;
					//In RAP, each revalidate will enlarge the shell by 1000, see  
					//org.eclipse.rwt.internal.textsize.TextSizeRecalculation.enlargeShell(Shell shell)
					if(OPIBuilderPlugin.isRAP() && (size.x - oldSize.x) == 1000 && (size.y - oldSize.y) == 1000)
						return;
					double widthRatio = size.x /(double)originSize.x;
					double heightRatio = size.y/(double)originSize.y;
					oldSize = size;
					getWidgetModel().scale(widthRatio, heightRatio);
//					oldSize = size;					
					//make sure the display figure repaint everything especially for RAP.
					if(OPIBuilderPlugin.isRAP())
						UIBundlingThread.getInstance().addRunnable(new Runnable() {
							
							@Override
							public void run() {
								getFigure().revalidate();
								getFigure().repaint();
								
							}
						});
				}
			};
			UIBundlingThread.getInstance().addRunnable(new Runnable() {
				
				@Override
				public void run() {
					scaleListener.controlResized(null);					
				}
			});
			getViewer().getControl().addControlListener(scaleListener);
		}
		
		if(getExecutionMode() == ExecutionMode.RUN_MODE && getWidgetModel().isAutoZoomToFitAll()){
			originSize = new org.eclipse.swt.graphics.Point(
					getWidgetModel().getWidth(), getWidgetModel().getHeight());
			oldSize=originSize;
			zoomListener = new ControlAdapter() {
					@Override
					public void controlResized(ControlEvent e) {
						if(!isActive() || getViewer() == null || getViewer().getControl().isDisposed())
							return;
						org.eclipse.swt.graphics.Point size = 
								((FigureCanvas)getViewer().getControl()).getSize();
						if(size.equals(oldSize))
							return;
						//In RAP, each revalidate will enlarge the shell by 1000, see  
						//org.eclipse.rwt.internal.textsize.TextSizeRecalculation.enlargeShell(Shell shell)
						if(OPIBuilderPlugin.isRAP() && (size.x - oldSize.x) == 1000 && (size.y - oldSize.y) == 1000)
							return;
						if (size.x * size.y > 0)
							((ScalableFreeformRootEditPart)getRoot()).getZoomManager().setZoomAsText(
									ZoomManager.FIT_ALL);
						oldSize = size;

					}
				};
			UIBundlingThread.getInstance().addRunnable(new Runnable() {
				
				@Override
				public void run() {
					zoomListener.controlResized(null);					
				}
			});
			getViewer().getControl().addControlListener(zoomListener);
		}
		UIBundlingThread.getInstance().addRunnable(new Runnable() {			
			@Override
			public void run() {
				if(getViewer()!=null && !getViewer().getControl().isDisposed())
					getViewer().getControl().forceFocus();
			}
		});
		
	}
	
	@Override
	public void deactivate() {
		final Control control = getViewer().getControl();
		if (getViewer() != null && !control.isDisposed()) {
			//This needs to be executed in UI Thread
			final ZoomManager zoomManager = ((ScalableFreeformRootEditPart) getRoot()).getZoomManager();
			control.getDisplay().asyncExec(new Runnable() {
				
				@Override
				public void run() {
					if (zoomListener != null && !control.isDisposed()) {
						// recover zoom
						zoomManager.setZoom(1.0);
						control.removeControlListener(zoomListener);
					}
{'fraction_non_alphanumeric': 0.14255302373255538, 'fraction_numerical': 0.007096112907040921, 'mean_word_length': 4.047353760445683, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '988813', 'n_tokens_mistral': 4687, 'n_tokens_neox': 3790, 'n_words': 722}