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