]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/SliderClassFactory.java
Improved Copy Visible Data usability in time series chart editor
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / SliderClassFactory.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.modeling.ui.diagram;
13
14 import java.awt.geom.AffineTransform;
15 import java.awt.geom.Rectangle2D;
16
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.AsyncReadGraph;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.primitiverequest.ValueImplied;
24 import org.simantics.db.common.request.WriteRequest;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.exception.NoSingleResultException;
27 import org.simantics.db.exception.ResourceNotFoundException;
28 import org.simantics.db.layer0.adapter.StringModifier;
29 import org.simantics.db.layer0.request.PossibleActiveValuation;
30 import org.simantics.db.layer0.request.PossibleSessionValuation;
31 import org.simantics.db.procedure.AsyncProcedure;
32 import org.simantics.db.procedure.Listener;
33 import org.simantics.diagram.G2DUtils;
34 import org.simantics.diagram.adapter.SyncElementFactory;
35 import org.simantics.diagram.elements.MonitorClass;
36 import org.simantics.diagram.stubs.DiagramResource;
37 import org.simantics.diagram.stubs.G2DResource;
38 import org.simantics.diagram.synchronization.CompositeHintSynchronizer;
39 import org.simantics.diagram.synchronization.IHintSynchronizer;
40 import org.simantics.diagram.synchronization.SynchronizationHints;
41 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
42 import org.simantics.diagram.synchronization.graph.ElementLoader;
43 import org.simantics.diagram.synchronization.graph.MonitorSynchronizer;
44 import org.simantics.diagram.synchronization.graph.TransformSynchronizer;
45 import org.simantics.diagram.ui.DiagramModelHints;
46 import org.simantics.g2d.canvas.ICanvasContext;
47 import org.simantics.g2d.diagram.IDiagram;
48 import org.simantics.g2d.diagram.handler.DataElementMap;
49 import org.simantics.g2d.diagram.handler.Relationship;
50 import org.simantics.g2d.diagram.handler.RelationshipHandler;
51 import org.simantics.g2d.element.ElementClass;
52 import org.simantics.g2d.element.ElementHints;
53 import org.simantics.g2d.element.ElementUtils;
54 import org.simantics.g2d.element.IElement;
55 import org.simantics.g2d.element.handler.TextEditor;
56 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
57 import org.simantics.layer0.Layer0;
58 import org.simantics.modeling.ModelingResources;
59 import org.simantics.modeling.ui.diagram.SliderClass.Range;
60 import org.simantics.utils.strings.format.MetricsFormat;
61 import org.simantics.utils.strings.format.MetricsFormatList;
62 import org.simantics.utils.threads.ThreadUtils;
63
64 public class SliderClassFactory extends SyncElementFactory {
65
66     private static final IHintSynchronizer HINT_SYNCHRONIZER = new CompositeHintSynchronizer(MonitorSynchronizer.INSTANCE, TransformSynchronizer.INSTANCE);
67
68     public static ElementClass createSliderClass(Resource elementClass) {
69         // set "default scale" to no scaling, 1.0, 1.0
70         return SliderClass.create(null, null, null, null, null, 1.0, 1.0, new StaticObjectAdapter(elementClass));
71     }
72
73     // staticScale{X,Y} define the scale of the static monitor image
74     public static ElementClass createSliderClass(Resource elementClass, IElement parentElement, Range<Double> range, Double value, Object component, String suffix, double staticScaleX, double staticScaleY) {
75         return SliderClass.create(parentElement, range, value, component, suffix, staticScaleX, staticScaleY, new StaticObjectAdapter(elementClass));
76     }
77
78     @Override
79     public void load(ReadGraph g, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e) throws DatabaseException {
80
81         if(!g.hasStatement(element)) return;
82
83         Layer0 l0 = Layer0.getInstance(g);
84         final G2DResource g2d = G2DResource.getInstance(g);
85         final DiagramResource dr = DiagramResource.getInstance(g);
86
87         ElementClass ec = e.getElementClass();
88
89         // Must be done here to allow for the relationship manager to work properly.
90         e.setHint(ElementHints.KEY_OBJECT, element);
91         e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, HINT_SYNCHRONIZER);
92         //System.out.println("    ElementClass:" + ec);
93         //System.out.println("    Element:" + e);
94
95         AffineTransform at = DiagramGraphUtil.getAffineTransform(g, element);
96         ElementUtils.setTransform(e, at);
97
98         // Load alignments
99         MetricsFormat tmpf = null;
100         try {
101             tmpf = G2DUtils.getMetricsFormat(g, g.getSingleObject(element, dr.HasFormat));
102         } catch(NoSingleResultException ex) {
103             tmpf = MetricsFormatList.METRICS_GENERIC;
104         }
105         final MetricsFormat format = tmpf;
106         e.setHint(SliderClass.KEY_NUMBER_FORMAT, format);
107
108         double bounds[] = DiagramGraphUtil.getPossibleRelatedDoubleArray(g, element, g2d.HasBounds);
109         if (bounds!=null) {
110             e.setHint(ElementHints.KEY_BOUNDS, new Rectangle2D.Double(bounds[0], bounds[1], bounds[2], bounds[3]));
111         }
112
113         String label = g.getPossibleRelatedValue(element, l0.HasLabel);
114         ElementUtils.setText(e, label);
115
116         try {
117             final Resource componentToElement = ModelingResources.getInstance(g).ComponentToElement;
118             final Resource sliderComponent = g.getPossibleObject(element, dr.HasSliderComponent);
119             final String sliderSuffix = g.getPossibleRelatedValue(element, dr.HasSliderSuffix, Bindings.STRING);
120             // FIXME: use range instead
121             final Double min = g.getPossibleRelatedValue(element, dr.HasSliderMinValue, Bindings.DOUBLE);
122             final Double max = g.getPossibleRelatedValue(element, dr.HasSliderMaxValue, Bindings.DOUBLE);
123
124             e.setHint(SliderClass.KEY_SLIDER_COMPONENT, sliderComponent);
125             e.setHint(SliderClass.KEY_SLIDER_SUFFIX, sliderSuffix);
126             e.setHint(SliderClass.KEY_SLIDER_RANGE, new SliderClass.Range<Double>(min, max));
127             e.setHint(SliderClass.KEY_SLIDER_VALUE, new Double(0));
128
129             final Resource parentDiagramElement = g.getPossibleObject(sliderComponent, componentToElement);
130
131             // Load parent relationship now, or it unsuccessful, after all elements have been loaded.
132             if (parentDiagramElement != null) {
133                 if (!loadRelationships(g, diagram, e, parentDiagramElement)) {
134                     e.setHint(DiagramModelHints.KEY_ELEMENT_LOADER, new ElementLoader() {
135                         @Override
136                         public void load(ReadGraph g, IDiagram diagram, IElement element) {
137                             loadRelationships(g, diagram, element, parentDiagramElement);
138                         }
139                     });
140                 }
141             }
142
143             // create tooltip
144             if (sliderSuffix != null) {
145                 e.setHint(SliderClass.KEY_TOOLTIP_TEXT, sliderSuffix);
146             }
147
148             final Session session = g.getSession();
149
150             String sessionId = diagram.getHint(DiagramModelHints.KEY_SESSION_ID);
151             Resource valuation =
152                 sessionId != null ?
153                         g.syncRequest(new PossibleSessionValuation(sliderComponent, sessionId, sliderSuffix)) :
154                             g.syncRequest(new PossibleActiveValuation(sliderComponent, sliderSuffix));
155
156                         if(valuation == null) {
157                             return;
158                         }
159
160                         session.asyncRequest(new ValueImplied<Object>(valuation), new Listener<Object>() {
161
162                             @Override
163                             public void execute(final Object result) {
164                                 final IElement el = ElementUtils.getByData(diagram, element);
165                                 if(el != null) {
166                                     ThreadUtils.asyncExec(canvas.getThreadAccess(), new Runnable() {
167                                         @Override
168                                         public void run() {
169                                             el.setHint(SliderClass.KEY_SLIDER_VALUE, result);
170                                             SliderClass.update(el);
171                                         }
172                                     });
173                                 }
174                             }
175
176                             @Override
177                             public void exception(Throwable t) {
178                             }
179
180                             @Override
181                             public boolean isDisposed() {
182                                 return canvas.isDisposed();
183                             }
184
185                         });
186
187                         final StringModifier modifier = g.adapt(valuation, StringModifier.class);
188                         TextEditor ed = ec.getAtMostOneItemOfClass(TextEditor.class);
189                         ed.setModifier(e, new TextEditor.Modifier() {
190
191                             @Override
192                             public String getValue(IElement element) {
193                                 return MonitorClass.editText(element);
194                             }
195
196                             @Override
197                             public String isValid(IElement element, String text) {
198                                 // TODO: implement validation properly
199                                 return null;
200                             }
201
202                             @Override
203                             public void modify(final IElement element, final String text) {
204                                 session.asyncRequest(new WriteRequest() {
205                                     @Override
206                                     public void perform(WriteGraph graph) throws DatabaseException {
207                                         modifier.modify(graph, text);
208                                     }
209                                 });
210                             }
211
212                         });
213
214         } catch (ResourceNotFoundException e1) {
215             e1.printStackTrace();
216         }
217     }
218
219     boolean loadRelationships(ReadGraph g, IDiagram diagram, IElement element, Resource parentElementResource) {
220         // If no relationship handler is available, stop loading.
221         RelationshipHandler rh = diagram.getDiagramClass().getAtMostOneItemOfClass(RelationshipHandler.class);
222         if (rh == null)
223             return true;
224
225         DataElementMap map = diagram.getDiagramClass().getSingleItem(DataElementMap.class);
226         IElement parentElement = map.getElement(diagram, parentElementResource);
227         if (parentElement != null) {
228             element.setHint(ElementHints.KEY_PARENT_ELEMENT, parentElement);
229             rh.claim(diagram, element, Relationship.CHILD_OF, parentElement);
230             return true;
231         }
232
233         return false;
234     }
235
236     @Override
237     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType,
238             AsyncProcedure<ElementClass> procedure) {
239         procedure.execute(graph, createSliderClass(elementType));
240     }
241
242 }