/******************************************************************************* | |
* <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; | |
import org.eclipse.graphiti.features.IDirectEditingFeature; | |
import org.eclipse.graphiti.features.IDirectEditingInfo; | |
import org.eclipse.graphiti.features.IFeatureProvider; | |
import org.eclipse.graphiti.features.context.ICustomContext; | |
import org.eclipse.graphiti.features.context.impl.DirectEditingContext; | |
import org.eclipse.graphiti.features.custom.AbstractCustomFeature; | |
import org.eclipse.graphiti.mm.algorithms.AbstractText; | |
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm; | |
import org.eclipse.graphiti.mm.pictograms.ContainerShape; | |
import org.eclipse.graphiti.mm.pictograms.PictogramElement; | |
import org.eclipse.graphiti.mm.pictograms.Shape; | |
/** | |
* This class is responsible for enabling direct editing behavior in case of | |
* double click on the shape. | |
* | |
* @author Benjamin Schmeling | |
* | |
*/ | |
public class DirectEditingDoubleclickFeature extends AbstractCustomFeature { | |
/** | |
* Main constructor. | |
* | |
* @param featureProvider The feature provider. | |
*/ | |
public DirectEditingDoubleclickFeature(IFeatureProvider featureProvider) { | |
super(featureProvider); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.eclipse.graphiti.features.custom.AbstractCustomFeature#canExecute | |
* (org.eclipse.graphiti.features.context.ICustomContext) | |
*/ | |
@Override | |
public boolean canExecute(ICustomContext context) { | |
if (context.getPictogramElements().length == 1 | |
&& context.getPictogramElements()[0] instanceof Shape) { | |
PictogramElement element = context.getPictogramElements()[0]; | |
DirectEditingContext ctx = new DirectEditingContext(element, | |
element.getGraphicsAlgorithm()); | |
IDirectEditingFeature directEditingFeature = getFeatureProvider() | |
.getDirectEditingFeature(ctx); | |
return directEditingFeature != null; | |
} else | |
return false; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.eclipse.graphiti.features.custom.ICustomFeature#execute(org.eclipse | |
* .graphiti.features.context.ICustomContext) | |
*/ | |
@Override | |
public void execute(ICustomContext context) { | |
if (context.getPictogramElements().length > 0 | |
&& context.getPictogramElements()[0] instanceof ContainerShape) { | |
ContainerShape element = (ContainerShape) context | |
.getPictogramElements()[0]; | |
AbstractText text = findTextInContainer(element); | |
editText(element, text); | |
} else if (context.getPictogramElements().length > 0 | |
&& context.getPictogramElements()[0].getGraphicsAlgorithm() instanceof AbstractText) { | |
PictogramElement element = context.getPictogramElements()[0]; | |
editText(element, (AbstractText) element.getGraphicsAlgorithm()); | |
} | |
} | |
/** | |
* Enables editing of the text. | |
* | |
* @param element The pictogram element which was double-clicked. | |
* @param text The text to be edited by the user | |
*/ | |
private void editText(PictogramElement element, AbstractText text) { | |
IDirectEditingInfo directEditingInfo = getFeatureProvider() | |
.getDirectEditingInfo(); | |
directEditingInfo.setMainPictogramElement(element); | |
directEditingInfo.setGraphicsAlgorithm(text); | |
if (text != null) | |
directEditingInfo.setPictogramElement(text.getPictogramElement() != null ? text.getPictogramElement() : element); | |
directEditingInfo.setActive(true); | |
getDiagramBehavior().refresh(); | |
} | |
/** | |
* Finds the text in a container shape and returns it. | |
* | |
* @param container The container shape to find the text in. | |
* @return The found abstract text, else null. | |
*/ | |
private AbstractText findTextInContainer(ContainerShape container) { | |
for (Shape shape : container.getChildren()) { | |
if (shape instanceof ContainerShape) { | |
for (Shape nested : ((ContainerShape) shape).getChildren()) { | |
if (nested.getGraphicsAlgorithm() instanceof AbstractText) | |
return findTextInGraphicsAlgorithm(nested); | |
} | |
} | |
if (shape.getGraphicsAlgorithm() instanceof AbstractText) | |
return findTextInGraphicsAlgorithm(shape); | |
} | |
return findTextInGraphicsAlgorithm(container); | |
} | |
/** | |
* Searches in the graphics algorithm and the graphics algorithm children of | |
* th shape for text algorithms. | |
* | |
* @param shape | |
* The shape to find texts in. | |
* @return The first text that has been found. | |
*/ | |
private AbstractText findTextInGraphicsAlgorithm(Shape shape){ | |
if (shape.getGraphicsAlgorithm() instanceof AbstractText) | |
return (AbstractText) shape.getGraphicsAlgorithm(); | |
else if(shape.getGraphicsAlgorithm() != null){ | |
for(GraphicsAlgorithm algoChild : shape.getGraphicsAlgorithm().getGraphicsAlgorithmChildren()){ | |
if (algoChild instanceof AbstractText) | |
return (AbstractText) algoChild; | |
} | |
return null; | |
} | |
else | |
return null; | |
} | |
} |