Last active
December 30, 2015 19:19
-
-
Save mPorhel/7873875 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package my.project.sample.design; | |
import java.util.List; | |
import org.eclipse.emf.ecore.EObject; | |
import org.eclipse.sirius.business.api.componentization.DiagramComponentizationManager; | |
import org.eclipse.sirius.business.api.session.Session; | |
import org.eclipse.sirius.business.api.session.SessionManager; | |
import org.eclipse.sirius.viewpoint.DDiagram; | |
import org.eclipse.sirius.viewpoint.DDiagramElement; | |
import org.eclipse.sirius.viewpoint.description.Layer; | |
/** | |
* Sample service class. | |
*/ | |
public class LayerService { | |
/** | |
* Sample service able to change the activation of the layer with id equals to "SampleLayer" | |
* for the current diagram, parent of the elementView parameter. | |
* | |
* @param context the current context | |
* @param elementView any DDiagramElement | |
* @return the current context | |
*/ | |
public EObject changeSampleLayerActivation(EObject context, EObject elementView) { | |
if (elementView instanceof DDiagramElement) { | |
DDiagram diagram = ((DDiagramElement) elementView).getParentDiagram(); | |
Session session = SessionManager.INSTANCE.getSession(((DDiagramElement) elementView).getTarget()); | |
Layer layer = getLayer(diagram, session, "SampleLayer"); | |
if (layer != null) { | |
if (diagram.getActivatedLayers().contains(layer)) { | |
diagram.getActivatedLayers().remove(layer); | |
} else { | |
diagram.getActivatedLayers().add(layer); | |
} | |
} | |
} | |
return context; | |
} | |
/** | |
* Method to retrieve the layer with the given id in all available layers for the current diagram. | |
*/ | |
private Layer getLayer(DDiagram diagram, Session session, String layerId) { | |
List<Layer> allAvailableLayers = new DiagramComponentizationManager() | |
.getAllLayers(session.getSelectedViewpoints(false), diagram.getDescription()); | |
for (Layer layer : allAvailableLayers) { | |
if (layerId.equals(layer.getName())) { | |
return layer; | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment