1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.diagram.synchronization.graph.layer;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
20 import java.util.concurrent.atomic.AtomicInteger;
22 import org.simantics.databoard.Bindings;
23 import org.simantics.db.AsyncReadGraph;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.WriteGraph;
27 import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;
28 import org.simantics.db.common.request.ResourceRead;
29 import org.simantics.db.exception.CancelTransactionException;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.exception.ServiceException;
32 import org.simantics.db.layer0.util.RemoverUtil;
33 import org.simantics.db.procedure.AsyncProcedure;
34 import org.simantics.db.procedure.Listener;
35 import org.simantics.diagram.stubs.DiagramResource;
36 import org.simantics.diagram.synchronization.IModificationQueue;
37 import org.simantics.diagram.synchronization.ModificationAdapter;
38 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
39 import org.simantics.g2d.diagram.DiagramHints;
40 import org.simantics.g2d.diagram.IDiagram;
41 import org.simantics.g2d.element.ElementHints;
42 import org.simantics.g2d.element.IElement;
43 import org.simantics.g2d.layers.IEditableLayer;
44 import org.simantics.g2d.layers.IEditableLayer.ILayerListener;
45 import org.simantics.g2d.layers.IEditableLayer.LayerChangeEvent;
46 import org.simantics.g2d.layers.ILayer;
47 import org.simantics.g2d.layers.ILayers;
48 import org.simantics.g2d.layers.ILayersEditor;
49 import org.simantics.g2d.layers.ILayersEditor.ILayersEditorListener;
50 import org.simantics.g2d.layers.SimpleLayers;
51 import org.simantics.layer0.Layer0;
54 * @author Tuukka Lehtonen
56 public class GraphLayerManager {
58 class LayerListener implements ILayersEditorListener, ILayerListener {
60 public void layerAdded(final ILayer layer) {
61 modificationQueue.offer(new CreateLayer(layer, getDiagramResource()), null);
62 modificationQueue.flush();
66 public void layerRemoved(final ILayer layer) {
67 modificationQueue.offer(new RemoveLayer(layer, getDiagramResource()), null);
68 modificationQueue.flush();
72 public void layerActivated(ILayer layer) {
73 postActivation(layer, true);
77 public void layerDeactivated(ILayer layer) {
78 postActivation(layer, false);
82 public void ignoreFocusChanged(boolean value) {
83 // Ignore, not written to graph.
87 public void ignoreVisibilityChanged(boolean value) {
88 // Ignore, not written to graph.
91 void postActivation(ILayer layer, boolean activated) {
92 modificationQueue.offer(new LayerActivation(layer, activated), null);
93 modificationQueue.flush();
97 public void layerChanged(LayerChangeEvent event) {
98 LayerChange change = null;
99 if (IEditableLayer.PROP_NAME.equals(event.getProperty())) {
100 String oldName = (String) event.getOldValue();
101 String newName = (String) event.getNewValue();
102 synchronized (layers) {
103 GraphLayer gl = layers.remove(oldName);
106 layers.put(newName, gl.withName(newName));
107 change = new LayerChange(event, gl);
110 if (change != null) {
111 modificationQueue.offer(change, null);
112 modificationQueue.flush();
117 public static final boolean DEBUG_LAYERS = false;
119 IModificationQueue modificationQueue;
125 * All the layers currently loaded from the diagram.
127 ConcurrentMap<String, GraphLayer> layers = new ConcurrentHashMap<String, GraphLayer>();
129 SimpleLayers layerEditor;
132 * The listener for ILayersEditor and all IEditableLayer.
134 LayerListener layerListener = new LayerListener();
136 boolean disposed = false;
138 public GraphLayerManager(ReadGraph graph, IModificationQueue modificationQueue, Resource diagram) {
139 this.modificationQueue = modificationQueue;
140 this.diagram = diagram;
141 this.l0 = Layer0.getInstance(graph);
142 this.dia = DiagramResource.getInstance(graph);
145 public void dispose() {
146 for (ILayer layer : layerEditor.getLayers()) {
147 if (layer instanceof IEditableLayer) {
148 ((IEditableLayer) layer).removeLayerListener(layerListener);
155 Resource getDiagramResource() {
159 // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
161 // ------------------------------------------------------------------------
163 class CreateLayer extends ModificationAdapter {
166 final Resource diagram;
168 public CreateLayer(ILayer layer, Resource diagram) {
170 assert layer != null;
172 this.diagram = diagram;
174 if (layer instanceof IEditableLayer) {
175 ((IEditableLayer) layer).addLayerListener(layerListener);
180 public void perform(WriteGraph g) throws Exception {
181 String newName = layer.getName();
182 for (Resource layer : g.getObjects(diagram, dia.HasLayer)) {
183 String name = g.getRelatedValue(layer, l0.HasName);
184 if (newName.equals(name)) {
189 IGraphLayerUtil util = g.adapt(DiagramResource.getInstance(g).Layer, IGraphLayerUtil.class);
190 GraphLayer layer = util.createLayer(g, newName, false);
191 g.claim(diagram, dia.HasLayer, layer.getLayer());
192 layers.put(newName, layer);
196 class RemoveLayer extends ModificationAdapter {
199 final Resource diagram;
201 public RemoveLayer(ILayer layer, Resource diagram) {
203 assert layer != null;
205 this.diagram = diagram;
207 if (layer instanceof IEditableLayer) {
208 ((IEditableLayer) layer).removeLayerListener(layerListener);
213 public void perform(WriteGraph g) throws Exception {
214 String removedName = layer.getName();
215 for (Resource l : g.getObjects(diagram, dia.HasLayer)) {
216 String name = g.getRelatedValue(l, l0.HasName);
217 if (removedName.equals(name)) {
218 g.denyStatement(diagram, dia.HasLayer, l);
226 void deleteLayer(WriteGraph g, Resource layer) throws DatabaseException {
227 RemoverUtil.remove(g, layer);
231 class LayerActivation extends ModificationAdapter {
234 final boolean activated;
236 public LayerActivation(ILayer layer, boolean activated) {
238 assert layer != null;
240 this.activated = activated;
244 public void perform(WriteGraph g) throws Exception {
245 GraphLayer gl = layers.get(layer.getName());
247 throw new CancelTransactionException("Diagram has no matching layer description: " + layer.getName());
249 g.claimLiteral(gl.getLayer(), dia.IsActive, Boolean.valueOf(activated));
253 class LayerChange extends ModificationAdapter {
254 final LayerChangeEvent event;
258 public LayerChange(LayerChangeEvent event, GraphLayer gl) {
260 assert event != null;
267 public void perform(WriteGraph g) throws Exception {
268 // Resource name = g.getSingleObject(gl.getLayer(), b.HasName);
269 // g.claimValue(name, event.getSource().getName());
270 g.claimLiteral(gl.getLayer(), l0.HasName, event.getSource().getName(), Bindings.STRING);
274 void deleteTag(WriteGraph g, Resource tag) throws DatabaseException {
278 Collection<GraphLayer> getActiveLayers(ReadGraph g) throws DatabaseException {
279 Collection<GraphLayer> result = new ArrayList<GraphLayer>();
280 for (GraphLayer gl : layers.values()) {
281 Boolean active = g.getPossibleRelatedValue(gl.getLayer(), dia.IsActive);
282 if (Boolean.TRUE.equals(active)) {
289 void tagElementWithActiveLayers(WriteGraph g, Resource element) throws DatabaseException {
290 Collection<GraphLayer> activeLayers = getActiveLayers(g);
291 for (GraphLayer activeLayer : activeLayers) {
292 DiagramGraphUtil.tag(g, element, activeLayer.getVisible(), true);
293 DiagramGraphUtil.tag(g, element, activeLayer.getFocusable(), true);
297 public ILayersEditor loadLayers(IDiagram diagram, ReadGraph g, Resource diagramResource) throws DatabaseException {
299 layerEditor = new SimpleLayers();
300 layerEditor.addLayerEditorListener(layerListener);
302 String[] fixed = diagram.getHint(DiagramHints.KEY_FIXED_LAYERS);
304 g.syncRequest(new ResourceRead<LayersSpec>(diagramResource) {
307 public LayersSpec perform(ReadGraph g) throws DatabaseException {
308 Collection<GraphLayer> gls = new ArrayList<>();
309 for (Resource layer : g.getObjects(resource, dia.HasLayer)) {
310 IGraphLayerUtil layerUtil = g.adapt(g.getSingleObject(layer, Layer0.getInstance(g).InstanceOf), IGraphLayerUtil.class);
311 GraphLayer gl = layerUtil.loadLayer(g, layer);
314 return new LayersSpec(gls);
317 }, new Listener<LayersSpec>() {
320 public void execute(LayersSpec layersSpec) {
321 ConcurrentMap<String, GraphLayer> newLayers = new ConcurrentHashMap<String, GraphLayer>();
322 Set<ILayer> visibleLayers = new HashSet<>();
323 Set<ILayer> allLayers = new HashSet<>();
326 for (GraphLayer gl : layersSpec.getGraphLayers()) {
327 for (String name : fixed) {
328 if (name.equals(gl.getName())) {
329 ILayer l = gl.getILayer();
330 newLayers.put(gl.getName(), gl);
332 visibleLayers.add(l);
340 System.out.println("Loading layers");
342 for (GraphLayer gl : layersSpec.getGraphLayers()) {
343 ILayer l = gl.getILayer();
345 newLayers.put(gl.getName(), gl);
348 System.out.println(" Loaded " + (gl.isActive() ? "active" : "inactive") + " layer '" + gl.getName() + "'");
350 if (l instanceof IEditableLayer)
351 ((IEditableLayer) l).addLayerListener(layerListener);
355 visibleLayers.add(l);
359 // Show all and focus all by default if there are no layers
362 if (newLayers.isEmpty()) {
363 layerEditor.setIgnoreVisibilitySettings(true);
364 layerEditor.setIgnoreFocusSettings(true);
368 System.out.println("Loaded " + newLayers.size() + " layers");
370 layerEditor.update(allLayers, visibleLayers);
376 public void exception(Throwable t) {
381 public boolean isDisposed() {
391 public void loadLayersForElement(ReadGraph graph, ILayersEditor layersEditor, IElement e, Resource element)
392 throws DatabaseException {
394 System.out.println("Loading layers for element " + element + " - " + e);
396 Set<ILayer> visible = null;
397 Set<ILayer> focusable = null;
399 for (ILayer l : layersEditor.getLayers()) {
400 GraphLayer gl = layers.get(l.getName());
402 if (graph.hasStatement(element, gl.getVisible(), element)) {
404 visible = new HashSet<ILayer>(4);
407 System.out.println(" Visible on layer '" + gl.getName() + "'");
409 if (graph.hasStatement(element, gl.getFocusable(), element)) {
410 if (focusable == null)
411 focusable = new HashSet<ILayer>(4);
414 System.out.println(" Focusable on layer '" + gl.getName() + "'");
420 visible = new HashSet<ILayer>(1);
421 if (focusable == null)
422 focusable = new HashSet<ILayer>(1);
424 e.setHint(ElementHints.KEY_VISIBLE_LAYERS, visible);
425 e.setHint(ElementHints.KEY_FOCUS_LAYERS, focusable);
430 * @param layersEditor
433 * @param procedure a procedure whose exception method may be called 0 to
434 * many times depending on how many errors occur during layer loading
435 * @throws DatabaseException
437 public void loadLayersForElement(AsyncReadGraph graph, ILayers layers2, final IElement e,
438 Resource element, final AsyncProcedure<IElement> callback) {
440 System.out.println("Loading layers for element " + element + " - " + e);
442 final Set<ILayer> visible = new HashSet<ILayer>(2);
443 final Set<ILayer> focusable = new HashSet<ILayer>(2);
445 // NOTE: must not set layer hints into element until the layer sets have
446 // been properly loaded.
448 Set<ILayer> allLayers = layers2.getLayers();
449 if (allLayers.isEmpty()) {
450 e.setHint(ElementHints.KEY_VISIBLE_LAYERS, visible);
451 e.setHint(ElementHints.KEY_FOCUS_LAYERS, focusable);
452 callback.execute(graph, e);
456 final AtomicInteger ready = new AtomicInteger(allLayers.size() * 2);
458 for (final ILayer l : allLayers) {
459 final GraphLayer gl = layers.get(l.getName());
461 graph.forHasStatement(element, gl.getVisible(), element, new AsyncProcedureAdapter<Boolean>() {
463 public void execute(AsyncReadGraph graph, Boolean result) {
465 synchronized (visible) {
470 System.out.println(" Visible on layer '" + gl.getName() + "'");
471 if (ready.decrementAndGet() == 0) {
472 e.setHint(ElementHints.KEY_VISIBLE_LAYERS, visible);
473 e.setHint(ElementHints.KEY_FOCUS_LAYERS, focusable);
474 callback.execute(graph, e);
478 public void exception(AsyncReadGraph graph, Throwable t) {
479 callback.exception(graph, t);
482 graph.forHasStatement(element, gl.getFocusable(), element, new AsyncProcedureAdapter<Boolean>() {
484 public void execute(AsyncReadGraph graph, Boolean result) {
486 synchronized (focusable) {
491 System.out.println(" Focusable on layer '" + gl.getName() + "'");
492 if (ready.decrementAndGet() == 0) {
493 e.setHint(ElementHints.KEY_VISIBLE_LAYERS, visible);
494 e.setHint(ElementHints.KEY_FOCUS_LAYERS, focusable);
495 callback.execute(graph, e);
499 public void exception(AsyncReadGraph graph, Throwable t) {
500 callback.exception(graph, t);
506 if (ready.addAndGet(-2) == 0) {
507 e.setHint(ElementHints.KEY_VISIBLE_LAYERS, visible);
508 e.setHint(ElementHints.KEY_FOCUS_LAYERS, focusable);
509 callback.execute(graph, e);
516 public void putElementOnVisibleLayers(IDiagram diagram, WriteGraph g, Resource element) throws DatabaseException {
517 // Make the new element visible and focusable on all currently
519 ILayers diagramLayers = diagram.getHint(DiagramHints.KEY_LAYERS);
520 if (diagramLayers != null) {
521 Set<ILayer> visibleLayers = diagramLayers.getVisibleLayers();
524 System.out.println("Marking element visible and focusable in the graph on visible layers: "
527 for (ILayer layer : visibleLayers) {
528 GraphLayer gl = layers.get(layer.getName());
530 gl.forEachTag(tag -> DiagramGraphUtil.tag(g, element, tag, true));
536 public void removeFromAllLayers(WriteGraph graph, Resource element) throws ServiceException {
537 // Remove element from all layers.
538 graph.deny(element, dia.IsVisible);
539 graph.deny(element, dia.IsFocusable);
542 public GraphLayer getGraphLayer(String name) {
543 return layers.get(name);