1 /*******************************************************************************
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.modeling.ui.diagram;
13 import java.awt.BasicStroke;
14 import java.awt.Color;
16 import java.awt.FontMetrics;
17 import java.awt.Graphics2D;
18 import java.awt.Rectangle;
19 import java.awt.Shape;
20 import java.awt.geom.AffineTransform;
21 import java.awt.geom.Path2D;
22 import java.awt.geom.Point2D;
23 import java.awt.geom.Rectangle2D;
24 import java.util.Collection;
25 import java.util.EnumSet;
28 import javax.swing.table.TableModel;
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.Transform;
48 import org.simantics.g2d.element.handler.impl.BorderColorImpl;
49 import org.simantics.g2d.element.handler.impl.FillColorImpl;
50 import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
51 import org.simantics.g2d.element.handler.impl.StaticSymbolImpl;
52 import org.simantics.g2d.element.handler.impl.TextEditorImpl;
53 import org.simantics.g2d.element.handler.impl.TextImpl;
54 import org.simantics.g2d.elementclass.MonitorHandler;
55 import org.simantics.g2d.image.Image;
56 import org.simantics.g2d.image.ProviderUtils;
57 import org.simantics.g2d.image.impl.AbstractImage;
58 import org.simantics.g2d.utils.Alignment;
59 import org.simantics.scenegraph.Node;
60 import org.simantics.scenegraph.g2d.G2DParentNode;
61 import org.simantics.spreadsheet.Model;
62 import org.simantics.utils.datastructures.cache.IFactory;
63 import org.simantics.utils.datastructures.cache.IProvider;
64 import org.simantics.utils.datastructures.cache.ProvisionException;
65 import org.simantics.utils.datastructures.hints.IHintContext.Key;
66 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
69 * @author Tuukka Lehtonen
71 public class SheetClass {
73 static final Font FONT = Font.decode("Helvetica 10");
75 public static final Key KEY_MODEL = new KeyOf(Model.class, "MODEL");
76 public static final Key KEY_TABLE_MODEL = new KeyOf(TableModel.class, "TABLE_MODEL");
78 public static final Key KEY_MONITOR_RESOURCE_PATH = new KeyOf(Collection.class, "MONITOR_RESOURCE_PATH");
79 public static final Key KEY_MONITOR_SUBSTITUTIONS = new KeyOf(Map.class, "MONITOR_SUBSTITUTIONS");
80 public static final Key KEY_MONITOR_GC = new KeyOf(Graphics2D.class, "MONITOR_GC");
81 public static final Key KEY_MONITOR_HEIGHT = new KeyOf(Double.class, "MONITOR_HEIGHT");
82 public static final Key KEY_MONITOR_FONT_METRICS = new KeyOf(FontMetrics.class, "MONITOR_FONT_METRICS");
83 public static final Key KEY_NUMBER_FORMAT = new KeyOf(String.class, "NUMBER_FORMAT");
86 * If this hint is defined, the monitor will force its x-axis to match this
87 * angle. If this hint doesn't exist, the monitor will not force x-axis
90 public static final Key KEY_DIRECTION = new KeyOf(Double.class, "MONITOR_DIRECTION");
92 public static final Key KEY_SG_NODE = new SceneGraphNodeKey(Node.class, "MONITOR_SG_NODE");
94 final static BasicStroke STROKE = new BasicStroke(1.0f);
96 public final static Alignment DEFAULT_HORIZONTAL_ALIGN = Alignment.CENTER;
97 public final static Alignment DEFAULT_VERTICAL_ALIGN = Alignment.CENTER;
98 public final static String DEFAULT_NUMBER_FORMAT = "0.0###";
100 public final static Color DEFAULT_FILL_COLOR = new Color(224, 224, 224);
101 public final static Color DEFAULT_BORDER_COLOR = Color.BLACK;
103 public final static double DEFAULT_HORIZONTAL_MARGIN = 5.0;
104 public final static double DEFAULT_VERTICAL_MARGIN = 2.5;
106 static Alignment getHorizontalAlignment(IElement e) {
107 return ElementUtils.getHintOrDefault(e, ElementHints.KEY_HORIZONTAL_ALIGN, DEFAULT_HORIZONTAL_ALIGN);
110 static Alignment getVerticalAlignment(IElement e) {
111 return ElementUtils.getHintOrDefault(e, ElementHints.KEY_VERTICAL_ALIGN, DEFAULT_VERTICAL_ALIGN);
114 static String getNumberFormat(IElement e) {
115 return ElementUtils.getHintOrDefault(e, KEY_NUMBER_FORMAT, DEFAULT_NUMBER_FORMAT);
118 static void setNumberFormat(IElement e, String format) {
119 ElementUtils.setOrRemoveHint(e, KEY_NUMBER_FORMAT, DEFAULT_NUMBER_FORMAT);
122 static double getHorizontalMargin(IElement e) {
123 return DEFAULT_HORIZONTAL_MARGIN;
126 static double getVerticalMargin(IElement e) {
127 return DEFAULT_VERTICAL_MARGIN;
130 static Font getFont(IElement e) {
131 return ElementUtils.getHintOrDefault(e, ElementHints.KEY_FONT, FONT);
134 public static class MonitorHandlerImpl implements MonitorHandler {
135 private static final long serialVersionUID = -4258875745321808416L;
136 public static final MonitorHandler INSTANCE = new MonitorHandlerImpl();
139 static class Initializer implements LifeCycle {
140 private static final long serialVersionUID = 4404942036933073584L;
142 IElement parentElement;
143 Map<String, String> substitutions;
144 Collection<Object> path;
147 Initializer(IElement parentElement, Map<String, String> substitutions, Collection<Object> path, boolean hack) {
148 this.parentElement = parentElement;
149 this.substitutions = substitutions;
155 public void onElementActivated(IDiagram d, IElement e) {
161 Point2D parentPos = ElementUtils.getPos(parentElement);
162 Point2D thisPos = ElementUtils.getPos(e);
164 Move move = e.getElementClass().getSingleItem(Move.class);
165 move.moveTo(e, thisPos.getX() - parentPos.getX(), thisPos.getY() - parentPos.getY());
171 public void onElementCreated(IElement e) {
172 if(parentElement != null) e.setHint(ElementHints.KEY_PARENT_ELEMENT, parentElement);
173 if(substitutions != null) e.setHint(KEY_MONITOR_SUBSTITUTIONS, substitutions);
174 if(path != null) e.setHint(KEY_MONITOR_RESOURCE_PATH, path);
176 e.setHint(KEY_DIRECTION, 0.0);
177 e.setHint(KEY_NUMBER_FORMAT, DEFAULT_NUMBER_FORMAT);
178 //e.setHint(KEY_HORIZONTAL_ALIGN, Alignment.LEADING);
179 //e.setHint(KEY_VERTICAL_ALIGN, Alignment.LEADING);
182 public void onElementDeactivated(IDiagram d, IElement e) {
185 public void onElementDestroyed(IElement e) {
189 static String finalText(IElement e) {
191 Text t = e.getElementClass().getAtMostOneItemOfClass(Text.class);
193 String text = t.getText(e);
194 if(text == null) return null;
195 Map<String, String> substitutions = e.getHint(KEY_MONITOR_SUBSTITUTIONS);
196 if(substitutions != null) {
197 // TODO: slow as hell
198 for(Map.Entry<String, String> entry : substitutions.entrySet()) {
199 if (entry.getValue() != null) {
200 text = text.replace(entry.getKey(), entry.getValue());
202 text = text.replace(entry.getKey(), "<null>");
210 public static void update(IElement e) {
211 SheetSGNode node = e.getElementClass().getAtMostOneItemOfClass(SheetSGNode.class);
215 public static void cleanup(IElement e) {
216 SheetSGNode node = e.getElementClass().getAtMostOneItemOfClass(SheetSGNode.class);
220 public static String editText(IElement e) {
222 Text t = e.getElementClass().getAtMostOneItemOfClass(Text.class);
225 if(text == null) return null;
226 Map<String, String> substitutions = e.getHint(KEY_MONITOR_SUBSTITUTIONS);
227 if(substitutions != null) {
228 // TODO: slow as hell
229 for(Map.Entry<String, String> entry : substitutions.entrySet()) {
230 if (entry.getValue() != null) {
231 text = text.replace(entry.getKey(), entry.getValue());
233 text = text.replace(entry.getKey(), "<null>");
241 static final Rectangle2D DEFAULT_BOX = new Rectangle2D.Double(0, 0, 0, 0);
243 static Shape createMonitor(IElement e) {
244 Alignment hAlign = getHorizontalAlignment(e);
245 Alignment vAlign = getVerticalAlignment(e);
246 double hMargin = getHorizontalMargin(e);
247 double vMargin = getVerticalMargin(e);
249 String text = finalText(e);
251 return align(hMargin, vMargin, hAlign, vAlign, DEFAULT_BOX);
254 Graphics2D g = e.getHint(KEY_MONITOR_GC);
256 return align(hMargin, vMargin, hAlign, vAlign, DEFAULT_BOX);
260 FontMetrics fm = g.getFontMetrics(f);
261 Rectangle2D rect = fm.getStringBounds(text, g);
263 return align(hMargin, vMargin, hAlign, vAlign, rect);
266 static Shape align(double hMargin, double vMargin, Alignment hAlign, Alignment vAlign, Rectangle2D rect) {
267 //System.out.println("align: " + hMargin + ", " + vMargin + ", " + hAlign + ", " + vAlign + ": " + rect);
268 double tx = align(hMargin, hAlign, rect.getMinX(), rect.getMaxX());
269 double ty = align(vMargin, vAlign, rect.getMinY(), rect.getMaxY());
270 //System.out.println(" translate: " + tx + " " + ty);
271 double nw = rect.getWidth() + 2*hMargin;
272 double nh = rect.getHeight() + 2*vMargin;
273 return makePath(tx + rect.getMinX(), ty + rect.getMinY(), nw, nh);
276 static double align(double margin, Alignment align, double min, double max) {
277 double s = max - min;
282 return -s - 2 * margin - min;
284 return -0.5 * s - margin - min;
290 static Path2D makePath(double x, double y, double w, double h) {
291 Path2D path = new Path2D.Double();
294 path.lineTo(x+w, y+h);
300 public static final Shape BOX_SHAPE = new Rectangle(-1, -1, 2, 2);
302 public static class SheetSGNode implements SceneGraph, InternalSize, Outline {
304 private static final long serialVersionUID = -5823585015844593347L;
306 static final SheetSGNode INSTANCE = new SheetSGNode();
309 public void init(final IElement e, final G2DParentNode parent) {
310 // Create node if it doesn't exist yet
311 SheetNode node = (SheetNode)e.getHint(KEY_SG_NODE);
312 if(node == null || node.getBounds() == null || node.getParent() != parent) {
313 node = parent.addNode(ElementUtils.generateNodeId(e), SheetNode.class);
314 e.setHint(KEY_SG_NODE, node);
315 Model model = e.getHint(KEY_MODEL);
316 TableModel tableModel = e.getHint(KEY_TABLE_MODEL);
317 node.init(model, tableModel);
318 System.out.println(parent);
319 node.setSize(500, 220);
324 public void update(IElement e) {
326 // String value = null;
328 final Text t = e.getElementClass().getAtMostOneItemOfClass(Text.class);
330 String text = finalText(e);
332 // System.out.println("monitorclass text = " + text);
334 // if(text != null) {
335 // Map<String, String> substitutions = e.getHint(KEY_MONITOR_SUBSTITUTIONS);
336 // for(Map.Entry<String, String> entry : substitutions.entrySet()) {
337 // System.out.println("substitute " + entry.getKey() + " -> " + entry.getValue());
338 // text.replace(entry.getKey(), entry.getValue());
342 // if(substitutions != null) {
343 // value = substitutions.get("#v1");
344 // if(substitutions.containsKey("#u1")) {
345 // value += substitutions.get("#u1");
349 // SheetNode node = (SheetNode) e.getHint(KEY_SG_NODE);
350 // if (node != null && text != null) {
351 // node.setText(text);
352 // Collection<?> path = e.getHint(KEY_MONITOR_RESOURCE_PATH);
353 // if (path != null && !path.isEmpty()) {
354 // node.setEditable(true);
356 // node.setEditable(false);
363 public void cleanup(IElement e) {
364 SheetNode node = (SheetNode)e.removeHint(KEY_SG_NODE);
370 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
371 Rectangle2D shape = new Rectangle2D.Double(0, 0, 0, 0);
373 SheetNode node = (SheetNode)e.getHint(KEY_SG_NODE);
374 if(node != null && node.getBounds() != null) {
375 shape = node.getBounds().getBounds2D();
378 if(size != null) size.setRect(shape);
383 public Shape getElementShape(IElement e) {
384 Shape shape = new Rectangle2D.Double(0, 0, 0, 0);
386 SheetNode node = (SheetNode)e.getHint(KEY_SG_NODE);
387 if(node != null && node.getBounds() != null) {
388 shape = node.getBounds();
396 public static class Transformer implements Transform, Move, Rotate, Scale {
398 private static final long serialVersionUID = -3704887325602085677L;
400 public static final Transformer INSTANCE = new Transformer(null);
404 public Transformer() {
408 public Transformer(Double aspectRatio) {
409 this.aspectRatio = aspectRatio;
413 public Double getFixedAspectRatio(IElement e) {
418 public Point2D getScale(IElement e) {
419 AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);
420 return _getScale(at);
424 public void setScale(IElement e, Point2D newScale) {
425 // Doesn't work for monitors.
426 Point2D oldScale = getScale(e);
427 double sx = newScale.getX() / oldScale.getX();
428 double sy = newScale.getY() / oldScale.getY();
429 AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);
430 at = new AffineTransform(at);
432 e.setHint(ElementHints.KEY_TRANSFORM, at);
436 public Point2D getMaximumScale(IElement e) {
441 public Point2D getMinimumScale(IElement e) {
445 private static Point2D _getScale(AffineTransform at) {
446 double m00 = at.getScaleX();
447 double m11 = at.getScaleY();
448 double m10 = at.getShearY();
449 double m01 = at.getShearX();
450 // Project unit vector to canvas
451 double sx = Math.sqrt(m00 * m00 + m10 * m10);
452 double sy = Math.sqrt(m01 * m01 + m11 * m11);
453 return new Point2D.Double(sx, sy);
457 public void rotate(IElement e, double theta, Point2D origin) {
458 if (Double.isNaN(theta)) return;
459 theta = Math.toDegrees(theta);
460 Double angle = e.getHint(KEY_DIRECTION);
461 double newAngle = angle != null ? angle+theta : theta;
462 newAngle = Math.IEEEremainder(newAngle, 360.0);
463 e.setHint(KEY_DIRECTION, newAngle);
467 public double getAngle(IElement e) {
468 Double angle = e.getHint(KEY_DIRECTION);
469 return angle != null ? Math.toRadians(angle) : 0;
473 public Point2D getPosition(IElement e) {
474 AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);
475 Point2D p = new Point2D.Double(at.getTranslateX(), at.getTranslateY());
480 public void moveTo(IElement e, double x, double y) {
481 AffineTransform origAt = e.getHint(ElementHints.KEY_TRANSFORM);
482 double oldX = origAt.getTranslateX();
483 double oldY = origAt.getTranslateY();
484 AffineTransform move = AffineTransform.getTranslateInstance(x-oldX, y-oldY);
485 AffineTransform at2 = new AffineTransform(origAt);
486 at2.preConcatenate(move);
487 e.setHint(ElementHints.KEY_TRANSFORM, at2);
491 public AffineTransform getTransform(IElement e) {
492 AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);
494 IElement parentElement = e.getHint(ElementHints.KEY_PARENT_ELEMENT);
495 if (parentElement == null)
498 Transform parentTransform = parentElement.getElementClass().getSingleItem(Transform.class);
499 assert(parentTransform!=null);
501 AffineTransform result = (AffineTransform)at.clone();
502 result.preConcatenate(parentTransform.getTransform(parentElement));
508 public void setTransform(IElement e, AffineTransform at) {
509 e.setHint(ElementHints.KEY_TRANSFORM, at.clone());
513 // public void onElementActivated(IDiagram d, IElement e) {
517 // public void onElementCreated(IElement e) {
518 // e.setHint(ElementHints.KEY_TRANSFORM, new AffineTransform());
522 // public void onElementDeactivated(IDiagram d, IElement e) {
526 // public void onElementDestroyed(IElement e) {
527 // List<SceneGraph> nodeHandlers = e.getElementClass().getItemsByClass(SceneGraph.class);
528 // for(SceneGraph n : nodeHandlers) {
529 // System.out.println("element gone:"+e);
535 static class MonitorImageFactory implements IFactory<Image> {
538 public Image get() throws ProvisionException {
539 return new AbstractImage() {
540 Shape path = align(DEFAULT_HORIZONTAL_MARGIN, DEFAULT_VERTICAL_MARGIN, DEFAULT_HORIZONTAL_ALIGN, DEFAULT_VERTICAL_ALIGN,
541 new Rectangle2D.Double(0, 0, 50, 22));
544 public Rectangle2D getBounds() {
545 return path.getBounds2D();
549 public EnumSet<Feature> getFeatures() {
550 return EnumSet.of(Feature.Vector);
554 public Shape getOutline() {
559 public Node init(G2DParentNode parent) {
560 SheetNode node = parent.getOrCreateNode(""+hashCode(), SheetNode.class);
562 // node.setSize(50, 22);
563 node.setTransform(new AffineTransform());
570 static final IProvider<Image> MONITOR_IMAGE =
571 ProviderUtils.reference(
573 ProviderUtils.rasterize(
574 new MonitorImageFactory()
577 static final StaticSymbol MONITOR_SYMBOL = new StaticSymbolImpl( MONITOR_IMAGE.get() );
579 static final FillColor FILL_COLOR = new FillColorImpl(DEFAULT_FILL_COLOR);
581 public static final ElementClass MONITOR_CLASS =
582 ElementClass.compile(
583 MonitorHandlerImpl.INSTANCE,
584 Transformer.INSTANCE,
585 BorderColorImpl.BLACK,
587 SheetSGNode.INSTANCE,
589 TextEditorImpl.INSTANCE,
590 SimpleElementLayers.INSTANCE,
594 // staticScale{X,Y} define the scale of the static monitor image
595 public static ElementClass create(IElement parentElement, Map<String, String> substitutions, Collection<Object> path, double staticScaleX, double staticScaleY, ElementHandler... extraHandlers) {
596 // Bit of a hack to be able to define the scale
597 IProvider<Image> staticMonitorSymbolProvider = ProviderUtils.reference(
601 new MonitorImageFactory())));
602 StaticSymbol staticMonitorSymbol = new StaticSymbolImpl( staticMonitorSymbolProvider.get() );
603 return ElementClass.compile(
604 new Initializer(parentElement, substitutions, path, parentElement != null ? false : true),
605 MonitorHandlerImpl.INSTANCE,
606 Transformer.INSTANCE,
607 BorderColorImpl.BLACK,
609 SheetSGNode.INSTANCE,
611 TextEditorImpl.INSTANCE,
612 SimpleElementLayers.INSTANCE,
614 ).newClassWith(extraHandlers);