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