/******************************************************************************* | |
* <copyright> | |
* | |
* Copyright (c) 2013, 2013 SAP AG. | |
* 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 | |
* | |
* Contributors: | |
* SAP AG - initial API, implementation and documentation | |
* | |
* </copyright> | |
* | |
*******************************************************************************/ | |
package org.eclipse.fmc.blockdiagram.editor.clipboard; | |
import java.awt.Toolkit; | |
import java.awt.datatransfer.Clipboard; | |
import java.util.Collection; | |
import java.util.HashSet; | |
import org.eclipse.core.runtime.Status; | |
import org.eclipse.fmc.blockdiagram.editor.Activator; | |
import org.eclipse.fmc.blockdiagram.editor.clipboard.img.GraphitiImageConverter; | |
import org.eclipse.fmc.blockdiagram.editor.clipboard.img.ImageTransferable; | |
import org.eclipse.fmc.blockdiagram.editor.model.FMCTypeChecker; | |
import org.eclipse.fmc.blockdiagram.editor.model.FMCTypeCheckerFactory; | |
import org.eclipse.fmc.blockdiagram.editor.util.FMCUtil; | |
import org.eclipse.gef.EditPart; | |
import org.eclipse.gef.GraphicalViewer; | |
import org.eclipse.graphiti.mm.pictograms.Diagram; | |
import org.eclipse.graphiti.mm.pictograms.PictogramElement; | |
import org.eclipse.graphiti.ui.editor.DiagramEditor; | |
import org.eclipse.graphiti.ui.internal.parts.ConnectionDecoratorEditPart; | |
import org.eclipse.graphiti.ui.internal.parts.ShapeEditPart; | |
import org.eclipse.graphiti.ui.services.GraphitiUi; | |
import org.eclipse.swt.graphics.Image; | |
import org.eclipse.ui.statushandlers.StatusManager; | |
/** | |
* This class is the main entry point for copying to the clipboard. | |
* | |
* @author Benjamin Schmeling | |
* | |
*/ | |
@SuppressWarnings("restriction") | |
public class CopyToClipboard { | |
private static FMCTypeChecker helper = FMCTypeCheckerFactory.getInstance(); | |
/** | |
* Private constructor. | |
*/ | |
private CopyToClipboard() { | |
} | |
/** | |
* Copies the selected pictogram elements as image in the given format to | |
* the clipboard. | |
* | |
* @param editor | |
* The diagram editor. | |
* @param imageFormat | |
* The image format as integer, e.g. | |
* org.eclipse.swt.SWT.IMAGE_PNG or org.eclipse.swt.SWT.IMAGE_BMP | |
*/ | |
public static void copyToClipboard(DiagramEditor editor, int imageFormat) { | |
PictogramElement[] els = editor.getSelectedPictogramElements(); | |
if (els.length > 0 && els[0] instanceof Diagram) { | |
els = ((Diagram) els[0]).getChildren().toArray( | |
new PictogramElement[] {}); | |
} | |
int[] minMaxCoords = FMCUtil.getMinMaxCoords(els); | |
GraphicalViewer viewer = (GraphicalViewer) editor | |
.getAdapter(GraphicalViewer.class); | |
@SuppressWarnings("unchecked") | |
Collection<EditPart> selectedEditParts = new HashSet<EditPart>( | |
viewer.getSelectedEditParts()); | |
if (selectedEditParts.isEmpty()) { | |
for (Object part : viewer.getEditPartRegistry().values()) { | |
if (part instanceof ShapeEditPart | |
&& helper.isFMCNode(((ShapeEditPart) part) | |
.getPictogramElement())) { | |
selectedEditParts.add((ShapeEditPart) part); | |
} else if (part instanceof ConnectionDecoratorEditPart) { | |
selectedEditParts.add((ShapeEditPart) part); | |
} | |
} | |
} | |
Image scaledImage = GraphitiImageConverter.convertEditParts( | |
minMaxCoords, selectedEditParts); | |
try { | |
// Replaced due to Graphiti version update (0.10.0) -- Does not work | |
// on MACOS. | |
byte[] imageBytes = GraphitiUi.getImageService().convertImageToBytes(scaledImage, imageFormat); | |
//byte[] imageBytes = GraphitiUiInternal.getUiService().createImage( | |
// scaledImage, imageFormat); | |
Clipboard clipboard = Toolkit.getDefaultToolkit() | |
.getSystemClipboard(); | |
Toolkit toolkit = Toolkit.getDefaultToolkit(); | |
java.awt.Image image = toolkit.createImage(imageBytes); | |
clipboard.setContents(new ImageTransferable(image), null); | |
} catch (Exception e) { | |
StatusManager.getManager().handle( | |
new Status(Status.ERROR, Activator.PLUGIN_ID, | |
"Error while copying to clipboard", e)); | |
} | |
} | |
} |