blob: c2982cdceca46b59d7d45e138d594157adb98cea [file] [log] [blame]
/*********************************************************************
* Copyright (c) 2005, 2019 SAP SE
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* Contributors:
* SAP SE - initial API, implementation and documentation
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
package org.eclipse.graphiti.features.impl;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.IReason;
import org.eclipse.graphiti.features.IRemoveFeature;
import org.eclipse.graphiti.features.context.IRemoveContext;
import org.eclipse.graphiti.features.context.IUpdateContext;
import org.eclipse.graphiti.features.context.impl.RemoveContext;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.mm.pictograms.PictogramLink;
import org.eclipse.graphiti.services.Graphiti;
/**
* The Class UpdateNoBoFeature. An update feature, which marks a pictogram
* element without a linked business object as "update needed". On "update" such
* a pictogram element is deleted.
*/
public class UpdateNoBoFeature extends AbstractUpdateFeature {
/**
* Creates a new {@link UpdateNoBoFeature}.
*
* @param fp
* the feature provider
*/
public UpdateNoBoFeature(IFeatureProvider fp) {
super(fp);
}
public boolean canUpdate(IUpdateContext context) {
return context.getPictogramElement() != null;
}
public IReason updateNeeded(IUpdateContext context) {
IReason ret = Reason.createFalseReason();
PictogramElement pe = context.getPictogramElement();
if (pe != null) {
PictogramLink linkForPictogramElement = Graphiti.getLinkService().getLinkForPictogramElement(pe);
if (linkForPictogramElement != null && linkForPictogramElement.getBusinessObjects().isEmpty()) {
ret = new Reason(true, "No business object linked to graphical representation"); //$NON-NLS-1$
}
}
return ret;
}
public boolean update(IUpdateContext context) {
boolean ret = false;
PictogramElement pe = context.getPictogramElement();
if (pe != null) {
PictogramLink linkForPictogramElement = Graphiti.getLinkService().getLinkForPictogramElement(pe);
if (linkForPictogramElement != null) {
List<EObject> businessObject = linkForPictogramElement.getBusinessObjects();
if (businessObject != null && businessObject.isEmpty()) {
ret = removeIfPossible(pe);
ret = true;
}
}
}
return ret;
}
private boolean removeIfPossible(PictogramElement pe) {
IRemoveContext context = new RemoveContext(pe);
final IRemoveFeature removeFeature = getFeatureProvider().getRemoveFeature(context);
if (removeFeature != null && removeFeature.canRemove(context)) {
removeFeature.remove(context);
return true;
}
return false;
}
}