]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/impl/DummyDiagramMutator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / diagram / impl / DummyDiagramMutator.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.diagram.impl;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import org.simantics.g2d.diagram.DiagramMutator;
18 import org.simantics.g2d.diagram.IDiagram;
19 import org.simantics.g2d.diagram.handler.ElementFactory;
20 import org.simantics.g2d.element.ElementClass;
21 import org.simantics.g2d.element.ElementClassProviders;
22 import org.simantics.g2d.element.ElementClasses;
23 import org.simantics.g2d.element.ElementHints;
24 import org.simantics.g2d.element.IElement;
25 import org.simantics.g2d.element.IElementClassProvider;
26 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
27 import org.simantics.g2d.element.impl.Element;
28 import org.simantics.g2d.elementclass.FlagClass;
29 import org.simantics.g2d.elementclass.connection.ConnectionClass;
30 import org.simantics.utils.datastructures.cache.ProvisionException;
31
32 /**
33  * Diagrams manipulation requires a DiagramMutator. This one works without any
34  * backend system.
35  * 
36  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
37  * @author Tuukka Lehtonen
38  */
39 public class DummyDiagramMutator implements DiagramMutator {
40
41     public static final IElementClassProvider DEFAULT_PROVIDER = ElementClassProviders.mappedProvider(
42             ElementClasses.CONNECTION, ConnectionClass.CLASS.newClassWith(new StaticObjectAdapter(new Object())),
43             ElementClasses.FLAG, FlagClass.FLAGCLASS.newClassWith(new StaticObjectAdapter(new Object()))
44     );
45
46     IElementClassProvider classProvider;
47
48     private final Map<IElement, Object> map = new HashMap<IElement, Object>();
49
50     private final IDiagram diagram;
51
52     public DummyDiagramMutator(IDiagram diagram) {
53         this(diagram, DEFAULT_PROVIDER);
54     }
55
56     public DummyDiagramMutator(IDiagram diagram, IElementClassProvider classProvider) {
57         this.diagram = diagram;
58         this.classProvider = classProvider;
59
60         ensureClassProvision(ElementClasses.CONNECTION);
61         ensureClassProvision(ElementClasses.FLAG);
62     }
63
64     private void ensureClassProvision(Object key) {
65         ElementClass ec = classProvider.get(key);
66         if (ec == null)
67             throw new ProvisionException();
68     }
69
70     @Override
71     public void synchronizeHintsToBackend(IElement element) {
72         // No backend, nothing to synchronize
73     }
74
75     @Override
76     public void synchronizeElementOrder() {
77         // No backend, nothing to synchronize
78     }
79
80     @Override
81     public void register(IElement element, Object object) {
82         // No point in registering anything.
83         map.put(element, object);
84     }
85
86     private IElement spawn(ElementClass clazz) {
87         ElementFactory ef = diagram.getDiagramClass().getAtMostOneItemOfClass(ElementFactory.class);
88         if (ef != null)
89             return ef.spawnNew(clazz);
90         return Element.spawnNew(clazz);
91     }
92
93     @Override
94     public IElement newElement(ElementClass clazz) {
95         IElement element = spawn(clazz);
96         element.setHint(ElementHints.KEY_OBJECT, new Object());
97         return element;
98     }
99
100     @Override
101     public void modifyTransform(IElement element) {
102     }
103
104     @Override
105     public void commit() {
106         map.clear();
107     }
108
109     @Override
110     public void clear() {
111         map.clear();
112     }
113
114     @SuppressWarnings("unchecked")
115     @Override
116     public <T> T backendObject(IElement element) {
117         return (T) map.get(element);
118     }
119
120 }