]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/elements/MonitorClass.java
Make Write-interfaces as @FunctionalInterface for lambdas
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / elements / MonitorClass.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.diagram.elements;
13
14 import java.awt.BasicStroke;
15 import java.awt.Color;
16 import java.awt.Font;
17 import java.awt.FontMetrics;
18 import java.awt.Graphics2D;
19 import java.awt.Rectangle;
20 import java.awt.Shape;
21 import java.awt.geom.AffineTransform;
22 import java.awt.geom.Path2D;
23 import java.awt.geom.Point2D;
24 import java.awt.geom.Rectangle2D;
25 import java.util.EnumSet;
26 import java.util.Map;
27
28 import javax.vecmath.Vector2d;
29
30 import org.simantics.db.layer0.variable.RVI;
31 import org.simantics.g2d.diagram.IDiagram;
32 import org.simantics.g2d.element.ElementClass;
33 import org.simantics.g2d.element.ElementHints;
34 import org.simantics.g2d.element.ElementUtils;
35 import org.simantics.g2d.element.IElement;
36 import org.simantics.g2d.element.SceneGraphNodeKey;
37 import org.simantics.g2d.element.handler.ElementHandler;
38 import org.simantics.g2d.element.handler.FillColor;
39 import org.simantics.g2d.element.handler.InternalSize;
40 import org.simantics.g2d.element.handler.LifeCycle;
41 import org.simantics.g2d.element.handler.Move;
42 import org.simantics.g2d.element.handler.Outline;
43 import org.simantics.g2d.element.handler.Rotate;
44 import org.simantics.g2d.element.handler.Scale;
45 import org.simantics.g2d.element.handler.SceneGraph;
46 import org.simantics.g2d.element.handler.StaticSymbol;
47 import org.simantics.g2d.element.handler.Text;
48 import org.simantics.g2d.element.handler.TextEditor;
49 import org.simantics.g2d.element.handler.TextEditor.Modifier;
50 import org.simantics.g2d.element.handler.Transform;
51 import org.simantics.g2d.element.handler.impl.BorderColorImpl;
52 import org.simantics.g2d.element.handler.impl.FillColorImpl;
53 import org.simantics.g2d.element.handler.impl.ParentImpl;
54 import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
55 import org.simantics.g2d.element.handler.impl.StaticSymbolImpl;
56 import org.simantics.g2d.element.handler.impl.TextColorImpl;
57 import org.simantics.g2d.element.handler.impl.TextEditorImpl;
58 import org.simantics.g2d.element.handler.impl.TextFontImpl;
59 import org.simantics.g2d.element.handler.impl.TextImpl;
60 import org.simantics.g2d.elementclass.MonitorHandler;
61 import org.simantics.g2d.image.Image;
62 import org.simantics.g2d.image.ProviderUtils;
63 import org.simantics.g2d.image.impl.AbstractImage;
64 import org.simantics.g2d.utils.Alignment;
65 import org.simantics.scenegraph.Node;
66 import org.simantics.scenegraph.g2d.G2DParentNode;
67 import org.simantics.scenegraph.g2d.nodes.ShapeNode;
68 import org.simantics.scl.runtime.function.Function1;
69 import org.simantics.utils.datastructures.cache.IFactory;
70 import org.simantics.utils.datastructures.cache.IProvider;
71 import org.simantics.utils.datastructures.cache.ProvisionException;
72 import org.simantics.utils.datastructures.hints.IHintContext.Key;
73 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
74 import org.simantics.utils.strings.format.MetricsFormat;
75 import org.simantics.utils.strings.format.MetricsFormatList;
76
77 /**
78  * @author Tuukka Lehtonen
79  */
80 public class MonitorClass {
81
82     static final Font        FONT                      = Font.decode("Helvetica 12");
83
84     /**
85      * Back-end specific object describing the monitored component.
86      */
87     public static final Key  KEY_MONITOR_COMPONENT     = new KeyOf(Object.class, "MONITOR_COMPONENT");
88
89     /**
90      * Existence of this hint indicates that the monitor is showing a value that
91      * does not originate from the owner diagram.
92      */
93     public static final Key  KEY_MONITOR_IS_EXTERNAL   = new KeyOf(Boolean.class, "MONITOR_IS_EXTERNAL");
94
95     /**
96      * The valuation suffix string describing the monitored variable of the
97      * component described by {@link #KEY_MONITOR_COMPONENT}.
98      */
99     public static final Key  KEY_MONITOR_SUFFIX        = new KeyOf(String.class, "MONITOR_SUFFIX");
100
101     public static final Key  KEY_MONITOR_SUBSTITUTIONS = new KeyOf(Map.class, "MONITOR_SUBSTITUTIONS");
102     public static final Key  KEY_MONITOR_GC            = new KeyOf(Graphics2D.class, "MONITOR_GC");
103     public static final Key  KEY_MONITOR_HEIGHT        = new KeyOf(Double.class, "MONITOR_HEIGHT");
104     public static final Key  KEY_NUMBER_FORMAT         = new KeyOf(MetricsFormat.class, "NUMBER_FORMAT");
105     public static final Key  KEY_TOOLTIP_TEXT          = new KeyOf(String.class, "TOOLTIP_TEXT");
106
107     public static final Key  KEY_EXPRESSION             = new KeyOf(String.class, "EXPRESSION");
108     public static final Key  KEY_INPUT_VALIDATOR             = new KeyOf(Object.class, "INPUT_VALIDATOR");
109     public static final Key  KEY_RVI             = new KeyOf(RVI.class, "RVI");
110
111     /**
112      * If this hint is defined, the monitor will force its x-axis to match this
113      * angle. If this hint doesn't exist, the monitor will not force x-axis
114      * orientation.
115      */
116     public static final Key  KEY_DIRECTION             = new KeyOf(Double.class, "MONITOR_DIRECTION");
117
118     public static final Key  KEY_BORDER_WIDTH          = new KeyOf(Double.class, "MONITOR_BORDER");
119
120     public static final Key  KEY_SG_NODE               = new SceneGraphNodeKey(TextNode.class, "MONITOR_SG_NODE");
121     public static final Key  KEY_SG_NODE2              = new SceneGraphNodeKey(ShapeNode.class, "MONITOR_SG_NODE2");
122
123     final static BasicStroke STROKE                    = new BasicStroke(1.0f);
124
125     public final static Alignment DEFAULT_HORIZONTAL_ALIGN  = Alignment.CENTER;
126     public final static Alignment DEFAULT_VERTICAL_ALIGN    = Alignment.CENTER;
127     public final static MetricsFormat DEFAULT_NUMBER_FORMAT = MetricsFormatList.METRICS_DECIMAL;
128
129     public final static Color     DEFAULT_FILL_COLOR        = new Color(224, 224, 224);
130     public final static Color     DEFAULT_BORDER_COLOR      = Color.BLACK;
131
132     public final static double    DEFAULT_HORIZONTAL_MARGIN = 5.0;
133     public final static double    DEFAULT_VERTICAL_MARGIN   = 2.5;
134
135     static Alignment getHorizontalAlignment(IElement e) {
136         return ElementUtils.getHintOrDefault(e, ElementHints.KEY_HORIZONTAL_ALIGN, DEFAULT_HORIZONTAL_ALIGN);
137     }
138
139     static Alignment getVerticalAlignment(IElement e) {
140         return ElementUtils.getHintOrDefault(e, ElementHints.KEY_VERTICAL_ALIGN, DEFAULT_VERTICAL_ALIGN);
141     }
142
143     static MetricsFormat getNumberFormat(IElement e) {
144         return ElementUtils.getHintOrDefault(e, KEY_NUMBER_FORMAT, DEFAULT_NUMBER_FORMAT);
145     }
146
147     static void setNumberFormat(IElement e, MetricsFormat f) {
148         ElementUtils.setOrRemoveHint(e, KEY_NUMBER_FORMAT, f);
149     }
150
151     static double getHorizontalMargin(IElement e) {
152         return DEFAULT_HORIZONTAL_MARGIN;
153     }
154
155     static double getVerticalMargin(IElement e) {
156         return DEFAULT_VERTICAL_MARGIN;
157     }
158
159     static Font getFont(IElement e) {
160         return ElementUtils.getHintOrDefault(e, ElementHints.KEY_FONT, FONT);
161     }
162
163     public static class MonitorHandlerImpl implements MonitorHandler {
164         private static final long          serialVersionUID = -4258875745321808416L;
165         public static final MonitorHandler INSTANCE         = new MonitorHandlerImpl();
166     }
167
168     static class Initializer implements LifeCycle {
169         private static final long serialVersionUID = 4404942036933073584L;
170
171         IElement parentElement;
172         Map<String, String> substitutions;
173         Object component;
174         String suffix;
175         boolean hack;
176
177         Initializer(IElement parentElement, Map<String, String> substitutions, Object component, String suffix, boolean hack) {
178             this.parentElement = parentElement;
179             this.substitutions = substitutions;
180             this.component = component;
181             this.suffix = suffix;
182             this.hack = hack;
183         }
184
185         @Override
186         public void onElementActivated(IDiagram d, IElement e) {
187             if(!hack) {
188                 hack = true;
189
190                 Point2D parentPos = ElementUtils.getPos(parentElement);
191                 Point2D thisPos = ElementUtils.getPos(e);
192
193                 Move move = e.getElementClass().getSingleItem(Move.class);
194                 move.moveTo(e, thisPos.getX() - parentPos.getX(), thisPos.getY() - parentPos.getY());
195             }
196         }
197         @Override
198         public void onElementCreated(IElement e) {
199             if(parentElement != null) e.setHint(ElementHints.KEY_PARENT_ELEMENT, parentElement);
200             if(substitutions != null) e.setHint(KEY_MONITOR_SUBSTITUTIONS, substitutions);
201             if(component != null) e.setHint(KEY_MONITOR_COMPONENT, component);
202             if(suffix != null) e.setHint(KEY_MONITOR_SUFFIX, suffix);
203             e.setHint(KEY_DIRECTION, 0.0);
204             e.setHint(KEY_NUMBER_FORMAT, DEFAULT_NUMBER_FORMAT);
205             //e.setHint(KEY_HORIZONTAL_ALIGN, Alignment.LEADING);
206             //e.setHint(KEY_VERTICAL_ALIGN, Alignment.LEADING);
207         }
208
209         @Override
210         public void onElementDestroyed(IElement e) {
211         }
212
213         @Override
214         public void onElementDeactivated(IDiagram d, IElement e) {
215         }
216     };
217
218     static String finalText(IElement e) {
219         String text = e.getElementClass().getSingleItem(Text.class).getText(e);
220         if (text == null)
221             return null;
222         return substitute(text, e);
223     }
224
225     public static String editText(IElement e) {
226         return substitute("#v1", e);
227     }
228
229     private static String formValue(IElement e) {
230         // TODO: consider using substitute
231         Map<String, String> substitutions = e.getHint(KEY_MONITOR_SUBSTITUTIONS);
232         if (substitutions != null) {
233             String value = substitutions.get("#v1");
234             if (substitutions.containsKey("#u1") && substitutions.get("#u1").length() > 0) {
235                 value += " " + substitutions.get("#u1");
236             }
237             return value;
238         } else {
239                 return ElementUtils.getText(e);
240         }
241     }
242
243     static String substitute(String text, IElement e) {
244         Map<String, String> substitutions = e.getHint(KEY_MONITOR_SUBSTITUTIONS);
245         return substitute(text, substitutions);
246     }
247
248     static String substitute(String text, Map<String, String> substitutions) {
249         if (substitutions != null) {
250             // TODO: slow as hell
251             for(Map.Entry<String, String> entry : substitutions.entrySet()) {
252                 if (entry.getValue() != null) {
253                     text = text.replace(entry.getKey(), entry.getValue());
254                 } else {
255                     text = text.replace(entry.getKey(), "<null>");
256                 }
257             }
258         }
259         return text;
260     }
261
262     public static void update(IElement e) {
263         MonitorSGNode node = e.getElementClass().getSingleItem(MonitorSGNode.class);
264         node.update(e);
265     }
266
267     public static void cleanup(IElement e) {
268         MonitorSGNode node = e.getElementClass().getSingleItem(MonitorSGNode.class);
269         node.cleanup(e);
270     }
271
272     public static boolean hasModifier(IElement e) { 
273         TextEditor editor = e.getElementClass().getAtMostOneItemOfClass(TextEditor.class);
274         Modifier modifier = editor != null ? editor.getModifier(e) : null;
275         return modifier != null;
276     }
277
278     static final Rectangle2D DEFAULT_BOX = new Rectangle2D.Double(0, 0, 0, 0);
279
280     static Shape createMonitor(IElement e) {
281         Alignment hAlign = getHorizontalAlignment(e);
282         Alignment vAlign = getVerticalAlignment(e);
283         double hMargin = getHorizontalMargin(e);
284         double vMargin = getVerticalMargin(e);
285
286         String text = finalText(e);
287         if(text == null) {
288             return align(hMargin, vMargin, hAlign, vAlign, DEFAULT_BOX);
289         }
290
291         Graphics2D g = e.getHint(KEY_MONITOR_GC);
292         if(g == null) {
293             return align(hMargin, vMargin, hAlign, vAlign, DEFAULT_BOX);
294         }
295
296         Font f = getFont(e);
297         FontMetrics fm   = g.getFontMetrics(f);
298         Rectangle2D rect = fm.getStringBounds(text, g);
299
300         return align(hMargin, vMargin, hAlign, vAlign, rect);
301     }
302
303     static Shape align(double hMargin, double vMargin, Alignment hAlign, Alignment vAlign, Rectangle2D rect) {
304         //System.out.println("align: " + hMargin + ", " + vMargin + ", " + hAlign + ", " + vAlign + ": " + rect);
305         double tx = align(hMargin, hAlign, rect.getMinX(), rect.getMaxX());
306         double ty = align(vMargin, vAlign, rect.getMinY(), rect.getMaxY());
307         //System.out.println("    translate: " + tx + " "  + ty);
308         double nw = rect.getWidth() + 2*hMargin;
309         double nh = rect.getHeight() + 2*vMargin;
310         return makePath(tx + rect.getMinX(), ty + rect.getMinY(), nw, nh);
311     }
312
313     static double align(double margin, Alignment align, double min, double max) {
314         double s = max - min;
315         switch (align) {
316             case LEADING:
317                 return -min;
318             case TRAILING:
319                 return -s - 2 * margin - min;
320             case CENTER:
321                 return -0.5 * s - margin - min;
322             default:
323                 return 0;
324         }
325     }
326
327     static Path2D makePath(double x, double y, double w, double h) {
328         Path2D path = new Path2D.Double();
329         path.moveTo(x, y);
330         path.lineTo(x+w, y);
331         path.lineTo(x+w, y+h);
332         path.lineTo(x, y+h);
333         path.closePath();
334         return path;
335     }
336
337     public static final Shape BOX_SHAPE = new Rectangle(-1, -1, 2, 2);
338
339     public static class MonitorSGNode implements SceneGraph, InternalSize, Outline {
340         private static final long serialVersionUID = -106278359626957687L;
341
342         static final MonitorSGNode INSTANCE = new MonitorSGNode();
343
344         @SuppressWarnings("unchecked")
345         @Override
346         public void init(final IElement e, final G2DParentNode parent) {
347             // Create node if it doesn't exist yet
348             TextNode node = e.getHint(KEY_SG_NODE);
349             String nodeId = null;
350             if(node == null || node.getBounds() == null || node.getParent() != parent) {
351                 nodeId = ElementUtils.generateNodeId(e);
352                 node = parent.addNode(nodeId, TextNode.class);
353                 e.setHint(KEY_SG_NODE, node);
354
355                 node.setTextListener(new ITextListener() {
356
357                     @Override
358                     public void textChanged() {}
359
360                     @Override
361                     public void textEditingStarted() {}
362
363                     @Override
364                     public void textEditingCancelled() {}
365
366                     boolean isEndingEdit = false;
367
368                     @Override
369                     public void textEditingEnded() {
370                         TextNode node = e.getHint(KEY_SG_NODE);
371                         if (node == null)
372                             return;
373
374                         // Prevent recursive execution which will happen
375                         // due to the endEdit(node) invocation at the end.
376                         if (isEndingEdit)
377                             return;
378                         isEndingEdit = true;
379
380                         try {
381                             TextEditor editor = e.getElementClass().getAtMostOneItemOfClass(TextEditor.class);
382                             if (editor != null) {
383                                 Modifier modifier = editor.getModifier(e);
384                                 if (modifier != null) {
385                                     String newValue = node.getText();
386                                     String error = modifier.isValid(e, newValue);
387                                     if (error == null) {
388                                         // Only modify if the modification was not
389                                         // cancelled and the value is valid.
390                                         modifier.modify(e, newValue);
391                                     } else {
392                                         // TODO: show error somehow, possibly through status bar
393
394                                         // Make sure that the monitor content gets
395                                         // reset to its previous value.
396                                         node.setText(formValue(e));
397                                     }
398                                 }
399                             }
400                         } finally {
401                             isEndingEdit = false;
402                         }
403                     }
404                 });
405
406                 Object validator = e.getHint(KEY_INPUT_VALIDATOR);
407                 if(validator != null) {
408                     node.setValidator((Function1<String, String>)validator);
409                 }
410                 
411                 RVI rvi = e.getHint(KEY_RVI);
412                 if(rvi != null) {
413                     node.setRVI(rvi);
414                 }
415
416                 Double border_width = (Double)e.getHint(KEY_BORDER_WIDTH);
417                 if(border_width == null) border_width = 0.1;
418
419                 node.setBorderWidth(border_width);
420
421 //                Rectangle2D bounds = (Rectangle2D)e.getHint(ElementHints.KEY_BOUNDS);
422 //                if(bounds != null) node.setBounds(bounds);
423                 Font font = ElementUtils.getTextFont(e);
424                 Color color = ElementUtils.getTextColor(e);
425                 String text = ElementUtils.getText(e);
426                 node.init(text, font, color, 0, 0, 1.0);
427             }
428
429             Boolean isExternal = e.getHint(KEY_MONITOR_IS_EXTERNAL);
430             if (Boolean.TRUE.equals(isExternal)) {
431                 ShapeNode shape = e.getHint(KEY_SG_NODE2);
432                 if (shape == null || shape.getParent() != parent) {
433                     if (nodeId == null)
434                         nodeId = ElementUtils.generateNodeId(e);
435                     nodeId += "-ext";
436                     shape = parent.addNode(nodeId, ShapeNode.class);
437                     shape.setZIndex(-1);
438                     shape.setColor(Color.BLACK);
439                     shape.setFill(true);
440                     shape.setStroke(null);
441                     shape.setShape( arrow(4, 2, 0) );
442                     e.setHint(KEY_SG_NODE2, shape);
443                 }
444             } else {
445                 ShapeNode shape = e.getHint(KEY_SG_NODE2);
446                 if (shape != null)
447                     shape.remove();
448             }
449
450             update(e);
451         }
452
453         public void update(IElement e) {
454             String value = null;
455
456             final Text t = e.getElementClass().getAtMostOneItemOfClass(Text.class);
457             assert(t != null);
458
459             value = formValue(e);
460
461             TextNode node = (TextNode) e.getHint(KEY_SG_NODE);
462             if (node != null && value != null) {
463
464                 node.setText(value);
465                 Object component = e.getHint(KEY_MONITOR_COMPONENT);
466                 if (component != null && hasModifier(e)) {
467                     node.setEditable(true);
468                 } else {
469                     node.setEditable(false);
470                 }
471
472                 // FIXME: set only if changed .. (but quickfix is not to clone)
473                 Font font = ElementUtils.getTextFont(e);
474                 if (node.getFont() != font) { // Don't update if we have a same object
475                     node.setFont(font);
476                 }
477                 Color color = ElementUtils.getTextColor(e);
478                 node.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()));
479
480                 AffineTransform at = ElementUtils.getTransform(e);
481                 if(at != null)
482                     node.setTransform(at);
483
484                 Alignment halign = e.getHint(ElementHints.KEY_HORIZONTAL_ALIGN);
485                 if (halign != null)
486                     node.setHorizontalAlignment((byte) halign.ordinal());
487                 Alignment valign = e.getHint(ElementHints.KEY_VERTICAL_ALIGN);
488                 if (valign != null)
489                     node.setVerticalAlignment((byte) valign.ordinal());
490
491 //                String tt = (String) e.getHint(KEY_TOOLTIP_TEXT);
492 //                if (tt != null)
493 //                    node.setToolTipText(new String(tt));
494
495                 ShapeNode shape = e.getHint(KEY_SG_NODE2);
496                 if (shape != null) {
497                     AffineTransform at2 = new AffineTransform(at);
498                     Rectangle2D bounds = node.getBoundsInLocal();
499                     at2.translate(bounds.getMinX(), bounds.getCenterY());
500                     shape.setTransform(at2);
501                 }
502             }
503         }
504
505         @Override
506         public void cleanup(IElement e) {
507             TextNode node = (TextNode)e.removeHint(KEY_SG_NODE);
508             if (node != null)
509                 node.remove();
510         }
511
512         @Override
513         public Rectangle2D getBounds(IElement e, Rectangle2D size) {
514             TextNode node = (TextNode)e.getHint(KEY_SG_NODE);
515             if (node != null) {
516                 Rectangle2D bounds = node.getBoundsInLocal();
517                 if (bounds != null) {
518                     if (size == null)
519                         size = new Rectangle2D.Double(0, 0, 0, 0);
520                     size.setRect(bounds);
521                 }
522             }
523             return size;
524         }
525
526         @Override
527         public Shape getElementShape(IElement e) {
528             Shape shape = new Rectangle2D.Double(0, 0, 0, 0);
529
530             TextNode node = (TextNode)e.getHint(KEY_SG_NODE);
531             if(node != null && node.getBoundsInLocal() != null) {
532                 shape = node.getBoundsInLocal();
533             }
534
535             return shape;
536         }
537
538     }
539
540     public static class Transformer implements Transform, Move, Rotate, Scale, LifeCycle {
541
542         private static final long serialVersionUID = -3704887325602085677L;
543
544         public static final Transformer INSTANCE = new Transformer(null);
545
546         Double aspectRatio;
547
548         public Transformer() {
549             this(null);
550         }
551
552         public Transformer(Double aspectRatio) {
553             this.aspectRatio = aspectRatio;
554         }
555
556         @Override
557         public Double getFixedAspectRatio(IElement e) {
558             return aspectRatio;
559         }
560
561         @Override
562         public Point2D getScale(IElement e) {
563             AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);
564             return _getScale(at);
565         }
566
567         @Override
568         public void setScale(IElement e, Point2D newScale) {
569             // Doesn't work for monitors.
570             Point2D oldScale = getScale(e);
571             double sx = newScale.getX() / oldScale.getX();
572             double sy = newScale.getY() / oldScale.getY();
573             AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);
574             at = new AffineTransform(at);
575             at.scale(sx, sy);
576             e.setHint(ElementHints.KEY_TRANSFORM, at);
577         }
578
579         @Override
580         public Point2D getMaximumScale(IElement e) {
581             return null;
582         }
583
584         @Override
585         public Point2D getMinimumScale(IElement e) {
586             return null;
587         }
588
589         private static Point2D _getScale(AffineTransform at) {
590             double m00 = at.getScaleX();
591             double m11 = at.getScaleY();
592             double m10 = at.getShearY();
593             double m01 = at.getShearX();
594             // Project unit vector to canvas
595             double sx = Math.sqrt(m00 * m00 + m10 * m10);
596             double sy = Math.sqrt(m01 * m01 + m11 * m11);
597             return new Point2D.Double(sx, sy);
598         }
599
600         @Override
601         public void rotate(IElement e, double theta, Point2D origin) {
602             if (Double.isNaN(theta)) return;
603             theta = Math.toDegrees(theta);
604             Double angle = e.getHint(KEY_DIRECTION);
605             double newAngle = angle != null ? angle+theta : theta;
606             newAngle = Math.IEEEremainder(newAngle, 360.0);
607             e.setHint(KEY_DIRECTION, newAngle);
608         }
609
610         @Override
611         public double getAngle(IElement e) {
612             Double angle = e.getHint(KEY_DIRECTION);
613             return angle != null ? Math.toRadians(angle) : 0;
614         }
615         @Override
616         public Point2D getPosition(IElement e) {
617             AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);
618             Point2D p = new Point2D.Double(at.getTranslateX(), at.getTranslateY());
619             return p;
620         }
621
622         @Override
623         public void moveTo(IElement e, double x, double y) {
624             AffineTransform origAt = e.getHint(ElementHints.KEY_TRANSFORM);
625
626             AffineTransform result = new AffineTransform(origAt);
627             result.preConcatenate(AffineTransform.getTranslateInstance(x - origAt.getTranslateX(), y - origAt.getTranslateY()));
628             e.setHint(ElementHints.KEY_TRANSFORM, result);
629         }
630
631         @Override
632         public AffineTransform getTransform(IElement e) {
633             AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);
634
635             IElement parentElement = e.getHint(ElementHints.KEY_PARENT_ELEMENT);
636             if (parentElement == null)
637                 return at;
638
639             Transform parentTransform = parentElement.getElementClass().getSingleItem(Transform.class);
640             assert(parentTransform!=null);
641
642             AffineTransform result = (AffineTransform)at.clone();
643             AffineTransform parentAT = parentTransform.getTransform(parentElement);
644             result.preConcatenate(AffineTransform.getTranslateInstance(parentAT.getTranslateX(), parentAT.getTranslateY()));
645
646             return result;
647         }
648
649         @Override
650         public void setTransform(IElement e, AffineTransform at) {
651             e.setHint(ElementHints.KEY_TRANSFORM, at.clone());
652         }
653
654         @Override
655         public void onElementActivated(IDiagram d, IElement e) {
656         }
657
658         @Override
659         public void onElementCreated(IElement e) {
660             e.setHint(ElementHints.KEY_TRANSFORM, new AffineTransform());
661         }
662
663         @Override
664         public void onElementDeactivated(IDiagram d, IElement e) {
665         }
666
667         @Override
668         public void onElementDestroyed(IElement e) {
669 //            List<SceneGraph> nodeHandlers = e.getElementClass().getItemsByClass(SceneGraph.class);
670 //            for(SceneGraph n : nodeHandlers) {
671 //                System.out.println("element gone:"+e);
672 //                n.cleanup(e);
673 //            }
674         }
675     }
676
677     static double getOrientationDelta(IElement e, AffineTransform tr) {
678         Double angle = e.getHint(KEY_DIRECTION);
679         if (angle == null || Double.isNaN(angle))
680             return Double.NaN;
681         double angrad = Math.toRadians(angle);
682
683         Vector2d forcedAxis = new Vector2d(Math.cos(angrad), Math.sin(angrad));
684         Vector2d x = new Vector2d(tr.getScaleX(), tr.getShearX());
685         forcedAxis.normalize();
686         x.normalize();
687         double cosa = forcedAxis.dot(x);
688         double delta = Math.acos(cosa);
689         return delta;
690     }
691
692     static class MonitorImageFactory implements IFactory<Image> {
693         private double staticScaleX = 1, staticScaleY = 1;
694
695         public MonitorImageFactory(double staticScaleX, double staticScaleY) {
696             this.staticScaleX = staticScaleX;
697             this.staticScaleY = staticScaleY;
698         }
699
700         @Override
701         public Image get() throws ProvisionException {
702             return new Img();
703         }
704
705         public class Img extends AbstractImage {
706
707             Shape path = align(DEFAULT_HORIZONTAL_MARGIN, DEFAULT_VERTICAL_MARGIN, DEFAULT_HORIZONTAL_ALIGN, DEFAULT_VERTICAL_ALIGN,
708                     new Rectangle2D.Double(0, 0, 50*staticScaleX, 22*staticScaleY));
709
710             @Override
711             public int hashCode() {
712                 long temp = Double.doubleToLongBits(staticScaleX);
713                 int result = (int) (temp ^ (temp >>> 32));
714                 temp = Double.doubleToLongBits(staticScaleY);
715                 result = 31 * result + (int) (temp ^ (temp >>> 32));
716                 return result;
717             }
718
719             @Override
720             public boolean equals(Object obj) {
721                 if (this == obj)
722                     return true;
723                 if (obj == null)
724                     return false;
725                 if (getClass() != obj.getClass())
726                     return false;
727                 MonitorImageFactory other = (MonitorImageFactory) obj;
728                 return Double.doubleToLongBits(staticScaleX) == Double.doubleToLongBits(other.staticScaleX)
729                         && Double.doubleToLongBits(staticScaleY) != Double.doubleToLongBits(other.staticScaleY);
730             }
731
732             @Override
733             public Rectangle2D getBounds() {
734                 return path.getBounds2D();
735             }
736
737             @Override
738             public EnumSet<Feature> getFeatures() {
739                 return EnumSet.of(Feature.Vector);
740             }
741
742             @Override
743             public Shape getOutline() {
744                 return path;
745             }
746
747             @Override
748             public Node init(G2DParentNode parent) {
749                 TextNode node = parent.getOrCreateNode(""+hashCode(), TextNode.class);
750                 node.init("Drop Me", FONT, Color.BLACK, 0, 0, 0.2);
751                 //node.setSize(50, 22);
752                 node.setBorderWidth(1);
753                 node.setTransform(AffineTransform.getScaleInstance(staticScaleX, staticScaleY));
754                 node.setEditable(false);
755                 return node;
756             }
757         };
758     }
759
760     static final IProvider<Image> MONITOR_IMAGE =
761         ProviderUtils.reference(
762                 ProviderUtils.cache(
763                         ProviderUtils.rasterize(
764                                 new MonitorImageFactory(0.5, 0.5)
765                         )));
766
767     static final StaticSymbol MONITOR_SYMBOL = new StaticSymbolImpl( MONITOR_IMAGE.get() );
768
769     static final FillColor FILL_COLOR = new FillColorImpl(DEFAULT_FILL_COLOR);
770
771     static final ElementClass MONITOR_CLASS_BASE =
772         ElementClass.compile(
773                 MonitorHandlerImpl.INSTANCE,
774                 Transformer.INSTANCE,
775                 BorderColorImpl.BLACK,
776                 FILL_COLOR,
777                 MonitorSGNode.INSTANCE,
778 //                ClickableImpl.INSTANCE,
779                 TextImpl.INSTANCE,
780                 TextEditorImpl.INSTANCE,
781                 TextFontImpl.DEFAULT,
782                 TextColorImpl.BLACK,
783                 SimpleElementLayers.INSTANCE,
784                 ParentImpl.INSTANCE
785         );
786
787     // staticScale{X,Y} define the scale of the static monitor image
788     public static ElementClass create(double staticScaleX, double staticScaleY, ElementHandler... extraHandlers) {
789         // Bit of a hack to be able to define the scale
790         IProvider<Image> staticMonitorSymbolProvider = ProviderUtils.reference(
791                 ProviderUtils.cache(
792                         ProviderUtils
793                         .rasterize(
794                                 new MonitorImageFactory(staticScaleX, staticScaleY))));
795         StaticSymbol staticMonitorSymbol = new StaticSymbolImpl( staticMonitorSymbolProvider.get() );
796         return MONITOR_CLASS_BASE.newClassWith(staticMonitorSymbol).newClassWith(extraHandlers);
797     }
798
799     // staticScale{X,Y} define the scale of the static monitor image
800     public static ElementClass create(IElement parentElement, Map<String, String> substitutions, Object component, String suffix, double staticScaleX, double staticScaleY, ElementHandler... extraHandlers) {
801         return create(staticScaleX, staticScaleY, extraHandlers)
802                 .newClassWith(
803                         new Initializer(parentElement, substitutions, component,
804                                 suffix, parentElement != null ? false : true));
805     }
806
807     public static Shape arrow(double length, double width, double space) {
808         Path2D.Double path = new Path2D.Double();
809         path.moveTo(-space, 0);
810         path.lineTo(-length - space, -width);
811         path.lineTo(-length - space, +width);
812         path.closePath();
813         return path;
814     }
815
816 }