/******************************************************************************* | |
* <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.features.add; | |
import org.eclipse.graphiti.features.IFeatureProvider; | |
import org.eclipse.graphiti.features.context.IAddContext; | |
import org.eclipse.graphiti.features.impl.AbstractAddShapeFeature; | |
import org.eclipse.graphiti.mm.algorithms.Image; | |
import org.eclipse.graphiti.mm.pictograms.ContainerShape; | |
import org.eclipse.graphiti.mm.pictograms.PictogramElement; | |
import org.eclipse.graphiti.services.Graphiti; | |
import org.eclipse.graphiti.services.IGaService; | |
import org.eclipse.graphiti.services.IPeService; | |
import org.eclipse.graphiti.ui.services.GraphitiUi; | |
import org.eclipse.swt.graphics.Rectangle; | |
/** | |
* This feature is responsible to add an image to the diagram. | |
* | |
* @author Benjamin Schmeling | |
* | |
*/ | |
public class ImageAddFeature extends AbstractAddShapeFeature { | |
/** | |
* The id of the image. | |
*/ | |
protected String imageId; | |
/** | |
* Constructor. | |
* | |
* @param featureProvider | |
* The feature provider. | |
* @param imageId | |
* The id of the image (registered at the image provider). | |
*/ | |
public ImageAddFeature(IFeatureProvider featureProvider, String imageId) { | |
super(featureProvider); | |
this.imageId = imageId; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.eclipse.graphiti.func.IAdd#canAdd(org.eclipse.graphiti.features.context | |
* .IAddContext) | |
*/ | |
@Override | |
public boolean canAdd(IAddContext context) { | |
return true; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.eclipse.graphiti.func.IAdd#add(org.eclipse.graphiti.features.context | |
* .IAddContext) | |
*/ | |
@Override | |
public PictogramElement add(IAddContext context) { | |
IGaService gaService = Graphiti.getGaService(); | |
IPeService peService = Graphiti.getPeService(); | |
ContainerShape parent = context.getTargetContainer(); | |
ContainerShape newShape; | |
newShape = peService.createContainerShape(parent, true); | |
Rectangle imageBounds = GraphitiUi.getImageService() | |
.getImageForId(getFeatureProvider().getDiagramTypeProvider().getProviderId(), imageId).getBounds(); | |
Image image = gaService.createImage(newShape, imageId); | |
image.setId(imageId); | |
image.setStretchH(true); | |
image.setStretchV(true); | |
gaService.setLocationAndSize(image, context.getX(), context.getY(), | |
imageBounds.width, imageBounds.height); | |
link(newShape, context.getNewObject()); | |
layoutPictogramElement(newShape); | |
return newShape; | |
} | |
} |