1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.diagram.adapter;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.diagram.profile.StyleBase;
19 import org.simantics.g2d.scenegraph.SceneGraphConstants;
20 import org.simantics.modeling.template2d.ontology.Template2dResource;
21 import org.simantics.modeling.template2d.ui.function.All;
22 import org.simantics.scenegraph.INode;
23 import org.simantics.scenegraph.g2d.G2DParentNode;
24 import org.simantics.scenegraph.g2d.G2DSceneGraph;
25 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
26 import org.simantics.scenegraph.loader.ScenegraphLoaderProcess;
27 import org.simantics.scenegraph.profile.DataNodeMap;
28 import org.simantics.scenegraph.profile.EvaluationContext;
29 import org.simantics.scenegraph.profile.common.ProfileVariables;
32 public class DrawingTemplateStyle extends StyleBase<INode> {
34 private static final String PAGE_TEMPLATE_NODE_NAME = "pageTemplate";
36 private static Resource getTemplateScenegraph(ReadGraph graph, Resource runtimeDiagram) throws DatabaseException {
37 Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
38 Resource template = All.getTemplate(graph, runtimeDiagram);
41 return graph.getPossibleObject(template, TEMPLATE2D.HasScenegraph);
45 public INode calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem, Variable activeComposite)
46 throws DatabaseException {
47 Resource sceneGraph = DrawingTemplateStyle.getTemplateScenegraph(graph, runtimeDiagram);
48 if (sceneGraph == null)
50 ScenegraphLoaderProcess loader = new ScenegraphLoaderProcess(new G2DParentNode(), "TemplateStyle.calculate");
51 return loader.load(graph, sceneGraph, runtimeDiagram);
55 public void applyStyleForItem(EvaluationContext evaluationContext, DataNodeMap map, Object item, INode node) {
59 G2DSceneGraph sg = evaluationContext.getSceneGraph();
62 G2DParentNode nav = (G2DParentNode) sg.getNode(SceneGraphConstants.NAVIGATION_NODE_NAME);
66 SingleElementNode pageTemplate = ProfileVariables.claimChild(evaluationContext.getSceneGraph(),
67 SceneGraphConstants.NAVIGATION_NODE_NAME, PAGE_TEMPLATE_NODE_NAME, SingleElementNode.class,
70 if (pageTemplate == null)
73 pageTemplate.setZIndex(1);
74 pageTemplate.setVisible(true);
76 // Switch template contents if the contents are not exactly the same.
77 INode previous = pageTemplate.getNode("structure");
78 if (previous != node) {
79 pageTemplate.detachNode("structure");
80 pageTemplate.attachNode("structure", node);
85 protected void cleanupStyleForItem(EvaluationContext evaluationContext, DataNodeMap map, Object item) {
86 G2DSceneGraph sg = evaluationContext.getSceneGraph();
89 G2DParentNode nav = (G2DParentNode) sg.getNode(SceneGraphConstants.NAVIGATION_NODE_NAME);
93 G2DParentNode pageTemplate = (G2DParentNode) nav.getNode(PAGE_TEMPLATE_NODE_NAME);
94 if (pageTemplate == null)
97 pageTemplate.detachNode("structure");
98 ProfileVariables.denyChild(evaluationContext.getSceneGraph(),
99 SceneGraphConstants.NAVIGATION_NODE_NAME, PAGE_TEMPLATE_NODE_NAME);
103 public String toString() {