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.modeling.ui.diagram;
14 import java.awt.geom.AffineTransform;
15 import java.awt.geom.Rectangle2D;
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;
64 public class SliderClassFactory extends SyncElementFactory {
66 private static final IHintSynchronizer HINT_SYNCHRONIZER = new CompositeHintSynchronizer(MonitorSynchronizer.INSTANCE, TransformSynchronizer.INSTANCE);
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));
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));
79 public void load(ReadGraph g, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e) throws DatabaseException {
81 if(!g.hasStatement(element)) return;
83 Layer0 l0 = Layer0.getInstance(g);
84 final G2DResource g2d = G2DResource.getInstance(g);
85 final DiagramResource dr = DiagramResource.getInstance(g);
87 ElementClass ec = e.getElementClass();
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);
95 AffineTransform at = DiagramGraphUtil.getAffineTransform(g, element);
96 ElementUtils.setTransform(e, at);
99 MetricsFormat tmpf = null;
101 tmpf = G2DUtils.getMetricsFormat(g, g.getSingleObject(element, dr.HasFormat));
102 } catch(NoSingleResultException ex) {
103 tmpf = MetricsFormatList.METRICS_GENERIC;
105 final MetricsFormat format = tmpf;
106 e.setHint(SliderClass.KEY_NUMBER_FORMAT, format);
108 double bounds[] = DiagramGraphUtil.getPossibleRelatedDoubleArray(g, element, g2d.HasBounds);
110 e.setHint(ElementHints.KEY_BOUNDS, new Rectangle2D.Double(bounds[0], bounds[1], bounds[2], bounds[3]));
113 String label = g.getPossibleRelatedValue(element, l0.HasLabel);
114 ElementUtils.setText(e, label);
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);
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));
129 final Resource parentDiagramElement = g.getPossibleObject(sliderComponent, componentToElement);
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() {
136 public void load(ReadGraph g, IDiagram diagram, IElement element) {
137 loadRelationships(g, diagram, element, parentDiagramElement);
144 if (sliderSuffix != null) {
145 e.setHint(SliderClass.KEY_TOOLTIP_TEXT, sliderSuffix);
148 final Session session = g.getSession();
150 String sessionId = diagram.getHint(DiagramModelHints.KEY_SESSION_ID);
153 g.syncRequest(new PossibleSessionValuation(sliderComponent, sessionId, sliderSuffix)) :
154 g.syncRequest(new PossibleActiveValuation(sliderComponent, sliderSuffix));
156 if(valuation == null) {
160 session.asyncRequest(new ValueImplied<Object>(valuation), new Listener<Object>() {
163 public void execute(final Object result) {
164 final IElement el = ElementUtils.getByData(diagram, element);
166 ThreadUtils.asyncExec(canvas.getThreadAccess(), new Runnable() {
169 el.setHint(SliderClass.KEY_SLIDER_VALUE, result);
170 SliderClass.update(el);
177 public void exception(Throwable t) {
181 public boolean isDisposed() {
182 return canvas.isDisposed();
187 final StringModifier modifier = g.adapt(valuation, StringModifier.class);
188 TextEditor ed = ec.getAtMostOneItemOfClass(TextEditor.class);
189 ed.setModifier(e, new TextEditor.Modifier() {
192 public String getValue(IElement element) {
193 return MonitorClass.editText(element);
197 public String isValid(IElement element, String text) {
198 // TODO: implement validation properly
203 public void modify(final IElement element, final String text) {
204 session.asyncRequest(new WriteRequest() {
206 public void perform(WriteGraph graph) throws DatabaseException {
207 modifier.modify(graph, text);
214 } catch (ResourceNotFoundException e1) {
215 e1.printStackTrace();
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);
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);
237 public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType,
238 AsyncProcedure<ElementClass> procedure) {
239 procedure.execute(graph, createSliderClass(elementType));