/** | |
* <copyright> | |
* | |
* Copyright (c) 2012 itemis and others. | |
* All rights reserved. This program and the accompanying materials | |
* are made available under the terms of the Eclipse Public License v2.0 | |
* which accompanies this distribution, and is available at | |
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html | |
* | |
* Contributors: | |
* itemis - Initial API and implementation | |
* | |
* </copyright> | |
*/ | |
package org.eclipse.sphinx.examples.hummingbird20.diagram.graphiti.features.update; | |
import org.eclipse.graphiti.features.IFeatureProvider; | |
import org.eclipse.graphiti.features.IReason; | |
import org.eclipse.graphiti.features.context.IUpdateContext; | |
import org.eclipse.graphiti.features.impl.AbstractUpdateFeature; | |
import org.eclipse.graphiti.features.impl.Reason; | |
import org.eclipse.graphiti.mm.algorithms.Text; | |
import org.eclipse.graphiti.mm.pictograms.ContainerShape; | |
import org.eclipse.graphiti.mm.pictograms.PictogramElement; | |
import org.eclipse.graphiti.mm.pictograms.Shape; | |
import org.eclipse.sphinx.examples.hummingbird20.typemodel.ComponentType; | |
public class UpdateComponentTypeFeature extends AbstractUpdateFeature { | |
public UpdateComponentTypeFeature(IFeatureProvider fp) { | |
super(fp); | |
} | |
@Override | |
public boolean canUpdate(IUpdateContext context) { | |
// return true, if linked business object is a ComponentType | |
Object bo = getBusinessObjectForPictogramElement(context.getPictogramElement()); | |
return bo instanceof ComponentType; | |
} | |
@Override | |
public IReason updateNeeded(IUpdateContext context) { | |
// retrieve name from pictogram model | |
String pictogramName = null; | |
PictogramElement pictogramElement = context.getPictogramElement(); | |
if (pictogramElement instanceof ContainerShape) { | |
ContainerShape cs = (ContainerShape) pictogramElement; | |
for (Shape shape : cs.getChildren()) { | |
if (shape.getGraphicsAlgorithm() instanceof Text) { | |
Text text = (Text) shape.getGraphicsAlgorithm(); | |
pictogramName = text.getValue(); | |
} | |
} | |
} | |
// retrieve name from business model | |
String businessName = null; | |
Object bo = getBusinessObjectForPictogramElement(pictogramElement); | |
if (bo instanceof ComponentType) { | |
ComponentType eComponentType = (ComponentType) bo; | |
businessName = eComponentType.getName(); | |
} | |
// update needed, if names are different | |
boolean updateNameNeeded = pictogramName == null && businessName != null || pictogramName != null && !pictogramName.equals(businessName); | |
if (updateNameNeeded) { | |
return Reason.createTrueReason("Name is out of date"); //$NON-NLS-1$ | |
} else { | |
return Reason.createFalseReason(); | |
} | |
} | |
@Override | |
public boolean update(IUpdateContext context) { | |
// retrieve name from business model | |
String businessName = null; | |
PictogramElement pictogramElement = context.getPictogramElement(); | |
Object bo = getBusinessObjectForPictogramElement(pictogramElement); | |
if (bo instanceof ComponentType) { | |
ComponentType eComponentType = (ComponentType) bo; | |
businessName = eComponentType.getName(); | |
} | |
// Set name in pictogram model | |
if (pictogramElement instanceof ContainerShape) { | |
ContainerShape cs = (ContainerShape) pictogramElement; | |
for (Shape shape : cs.getChildren()) { | |
if (shape.getGraphicsAlgorithm() instanceof Text) { | |
Text text = (Text) shape.getGraphicsAlgorithm(); | |
text.setValue(businessName); | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
} |