<commit_before>/*
 * Perfclispe
 * 
 * 
 * Copyright (c) 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.perfclipse.ui.editors;


import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.EventObject;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.draw2d.ConnectionLayer;
import org.eclipse.draw2d.ShortestPathConnectionRouter;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.dnd.TemplateTransferDragSourceListener;
import org.eclipse.gef.dnd.TemplateTransferDropTargetListener;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.parts.GraphicalEditorWithPalette;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.FileEditorInput;
import org.perfclipse.model.ScenarioModel;
import org.perfclipse.scenario.ScenarioException;
import org.perfclipse.scenario.ScenarioManager;
import org.perfclipse.ui.Activator;
import org.perfclipse.ui.gef.parts.PerfCakeEditPartFactory;
import org.perfclipse.ui.gef.parts.ScenarioEditPart;
import org.slf4j.LoggerFactory;

public class ScenarioDesignEditor extends GraphicalEditorWithPalette {
	
	final static org.slf4j.Logger log = LoggerFactory.getLogger(ScenarioDesignEditor.class);

	private ScenarioModel model;
	private ScenarioMultiPageEditor parent;
	
	private final IPropertyChangeListener preferencesChangeListener = new IPropertyChangeListener() {
		public void propertyChange(PropertyChangeEvent event) {
			getGraphicalViewer().setContents(model);
		}
	};


	public ScenarioDesignEditor(ScenarioMultiPageEditor parent) {
		super();
		setEditDomain(new DefaultEditDomain(this));
		this.parent = parent;
		Activator.getDefault().getPreferenceStore().addPropertyChangeListener(preferencesChangeListener);
	}
	
	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		super.selectionChanged(part, selection);

		if (getSite().getWorkbenchWindow().getActivePage().getActiveEditor() == parent &&
				parent.isNestedEditorActive(this)){
			updateActions(getSelectionActions());
		}
	}

	/*
	 * Notify editor that command stack change to update dirty flag
	 */
	@Override
	public void commandStackChanged(EventObject event) {
		firePropertyChange(IEditorPart.PROP_DIRTY);
		super.commandStackChanged(event);
	}

	@Override
	protected PaletteRoot getPaletteRoot() {
		return ScenarioPalleteFactory.createPalette();
	}	
	
	
	@Override
	protected void setInput(IEditorInput input){
		super.setInput(input);
		model = ((ScenarioDesignEditorInput) input).getModel();
	}

	
	@Override
	protected void configureGraphicalViewer() {
	      super.configureGraphicalViewer();
	      GraphicalViewer viewer = getGraphicalViewer();
	      viewer.setEditPartFactory(new PerfCakeEditPartFactory());
	      viewer.setRootEditPart(new ScalableFreeformRootEditPart());
	      
	      // These two lines add support for drag and drop between palette and editor
	      getGraphicalViewer().addDropTargetListener(
	    		  new TemplateTransferDropTargetListener(getGraphicalViewer()));
	      getEditDomain().getPaletteViewer().addDragSourceListener(
	    		  new TemplateTransferDragSourceListener(getEditDomain().getPaletteViewer()));
	      
	      
	   }

	   /**
	    * Initialize the graphic viewer and the connection layer so that connections
	    * are routed around existing figures.
	    * 
	    * @see org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette#initializeGraphicalViewer()
	    */
	@Override
	protected void initializeGraphicalViewer() {

		GraphicalViewer viewer = getGraphicalViewer();
		viewer.setContents(model);

		//TODO error handling for empty model - IndexOutOfBoundsException is thrown now
		ScalableFreeformRootEditPart rootEditPart =
				(ScalableFreeformRootEditPart) viewer.getRootEditPart();
		ScenarioEditPart scenarioPart =
				(ScenarioEditPart) rootEditPart.getChildren().get(0);
		ConnectionLayer connectionLayer =
				(ConnectionLayer) rootEditPart.getLayer(LayerConstants.CONNECTION_LAYER);
		connectionLayer.setConnectionRouter(new ShortestPathConnectionRouter(
				scenarioPart.getFigure()));
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		ScenarioManager manager = new ScenarioManager();

		IFile file = ((FileEditorInput) getEditorInput()).getFile();

		PipedOutputStream out = new PipedOutputStream();
		PipedInputStream in = null;
		try {
			in = new PipedInputStream(out);
			manager.createXML(model.getScenario(), out);
			out.close();
			try {
				file.setContents(in, false, true, monitor);
				getEditDomain().getCommandStack().markSaveLocation();
			} catch (CoreException e) {
				MessageDialog.openError(getSite().getShell(), "Write error", "Cannot write data into file.");
				log.error("Write error", "Cannot write data into file", e);
			} finally {
				try {
					if (in != null){
						in.close();
					}
				} catch (IOException e) {
					log.error("Cannot close piped input stream for scenario xml", e);
				}
			}
		} catch (ScenarioException e) {
			MessageDialog.openError(getSite().getShell(), "Error", "Cannot create xml representation of the model.");
			log.error("Cannot create xml representation of the model.", e);
		} catch (IOException e) {
			MessageDialog.openError(getSite().getShell(), "IO Exception", "Cannot transfer data to file.");
			log.error("Cannot handle piped stream connections.", e);
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				log.error("Cannot close piped output stream for scenario xml output", e);
			}
		}

		<commit_msg>Fix freeze when saving in design editor<commit_after>/*
 * Perfclispe
 * 
 * 
 * Copyright (c) 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.perfclipse.ui.editors;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.EventObject;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.draw2d.ConnectionLayer;
import org.eclipse.draw2d.ShortestPathConnectionRouter;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.dnd.TemplateTransferDragSourceListener;
import org.eclipse.gef.dnd.TemplateTransferDropTargetListener;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.parts.GraphicalEditorWithPalette;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.FileEditorInput;
import org.perfclipse.model.ScenarioModel;
import org.perfclipse.scenario.ScenarioException;
import org.perfclipse.scenario.ScenarioManager;
import org.perfclipse.ui.Activator;
import org.perfclipse.ui.gef.parts.PerfCakeEditPartFactory;
import org.perfclipse.ui.gef.parts.ScenarioEditPart;
import org.slf4j.LoggerFactory;

public class ScenarioDesignEditor extends GraphicalEditorWithPalette {
	
	final static org.slf4j.Logger log = LoggerFactory.getLogger(ScenarioDesignEditor.class);

	private ScenarioModel model;
	private ScenarioMultiPageEditor parent;
	
	private final IPropertyChangeListener preferencesChangeListener = new IPropertyChangeListener() {
		public void propertyChange(PropertyChangeEvent event) {
			getGraphicalViewer().setContents(model);
		}
	};


	public ScenarioDesignEditor(ScenarioMultiPageEditor parent) {
		super();
		setEditDomain(new DefaultEditDomain(this));
		this.parent = parent;
		Activator.getDefault().getPreferenceStore().addPropertyChangeListener(preferencesChangeListener);
	}
	
	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		super.selectionChanged(part, selection);

		if (getSite().getWorkbenchWindow().getActivePage().getActiveEditor() == parent &&
				parent.isNestedEditorActive(this)){
			updateActions(getSelectionActions());
		}
	}

	/*
	 * Notify editor that command stack change to update dirty flag
	 */
	@Override
	public void commandStackChanged(EventObject event) {
		firePropertyChange(IEditorPart.PROP_DIRTY);
		super.commandStackChanged(event);
	}

	@Override
	protected PaletteRoot getPaletteRoot() {
		return ScenarioPalleteFactory.createPalette();
	}	
	
	
	@Override
	protected void setInput(IEditorInput input){
		super.setInput(input);
		model = ((ScenarioDesignEditorInput) input).getModel();
	}

	
	@Override
	protected void configureGraphicalViewer() {
	      super.configureGraphicalViewer();
	      GraphicalViewer viewer = getGraphicalViewer();
	      viewer.setEditPartFactory(new PerfCakeEditPartFactory());
	      viewer.setRootEditPart(new ScalableFreeformRootEditPart());
	      
	      // These two lines add support for drag and drop between palette and editor
	      getGraphicalViewer().addDropTargetListener(
	    		  new TemplateTransferDropTargetListener(getGraphicalViewer()));
	      getEditDomain().getPaletteViewer().addDragSourceListener(
	    		  new TemplateTransferDragSourceListener(getEditDomain().getPaletteViewer()));
	      
	      
	   }

	   /**
	    * Initialize the graphic viewer and the connection layer so that connections
	    * are routed around existing figures.
	    * 
	    * @see org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette#initializeGraphicalViewer()
	    */
	@Override
	protected void initializeGraphicalViewer() {

		GraphicalViewer viewer = getGraphicalViewer();
		viewer.setContents(model);

		//TODO error handling for empty model - IndexOutOfBoundsException is thrown now
		ScalableFreeformRootEditPart rootEditPart =
				(ScalableFreeformRootEditPart) viewer.getRootEditPart();
		ScenarioEditPart scenarioPart =
				(ScenarioEditPart) rootEditPart.getChildren().get(0);
		ConnectionLayer connectionLayer =
				(ConnectionLayer) rootEditPart.getLayer(LayerConstants.CONNECTION_LAYER);
		connectionLayer.setConnectionRouter(new ShortestPathConnectionRouter(
				scenarioPart.getFigure()));
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		ScenarioManager manager = new ScenarioManager();

		IFile file = ((FileEditorInput) getEditorInput()).getFile();

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			manager.createXML(model.getScenario(), out);
			ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
			out.close();

			try {
				file.setContents(in, false, true, monitor);
				getEditDomain().getCommandStack().markSaveLocation();
			} catch (CoreException e) {
				MessageDialog.openError(getSite().getShell(), "Write error", "Cannot write data into file.");
				log.error("Write error", "Cannot write data into file", e);
			} finally {
				try {
					if (in != null){
						in.close();
					}
				} catch (IOException e) {
					log.error("Cannot close piped input stream for scenario xml", e);
				}
			}
		} catch (ScenarioException e) {
			MessageDialog.openError(getSite().getShell(), "Error", "Cannot create xml representation of the model.");
			log.error("Cannot create xml representation of the model.", e);
		} catch (IOException e) {
			MessageDialog.openError(getSite().getShell(), "IO Exception", "Cannot transfer data to file.");
			log.error("Cannot handle piped stream connections.", e);
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				log.error("Cannot close piped output stream for scenario xml output", e);
			}
		}

		
{'fraction_non_alphanumeric': 0.09751853533060978, 'fraction_numerical': 0.0019670146769556664, 'mean_word_length': 4.875111111111111, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, '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': '535214', 'n_tokens_mistral': 4071, 'n_tokens_neox': 3701, 'n_words': 886}