]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/TextElementClassFactory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / TextElementClassFactory.java
index ce76b2392aab2d52fa93f1fabed58d3c9d806744..40f0f6688cf7f24d20ae8d6eefff578c9adc212e 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.diagram.adapter;\r
-\r
-import java.awt.geom.AffineTransform;\r
-import java.awt.geom.Rectangle2D;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.diagram.G2DUtils;\r
-import org.simantics.diagram.elements.ElementPropertySetter;\r
-import org.simantics.diagram.elements.MonitorClass;\r
-import org.simantics.diagram.elements.ResizeRectangularSceneGraph;\r
-import org.simantics.diagram.elements.TextElementHandler;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.diagram.stubs.G2DResource;\r
-import org.simantics.diagram.synchronization.CompositeHintSynchronizer;\r
-import org.simantics.diagram.synchronization.IHintSynchronizer;\r
-import org.simantics.diagram.synchronization.SynchronizationHints;\r
-import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;\r
-import org.simantics.diagram.synchronization.graph.TextSynchronizer;\r
-import org.simantics.diagram.synchronization.graph.TransformSynchronizer;\r
-import org.simantics.g2d.canvas.ICanvasContext;\r
-import org.simantics.g2d.diagram.IDiagram;\r
-import org.simantics.g2d.element.ElementClass;\r
-import org.simantics.g2d.element.ElementHints;\r
-import org.simantics.g2d.element.ElementUtils;\r
-import org.simantics.g2d.element.IElement;\r
-import org.simantics.g2d.element.handler.Text;\r
-import org.simantics.g2d.element.handler.impl.DefaultTransform;\r
-import org.simantics.g2d.element.handler.impl.SimpleElementLayers;\r
-import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
-import org.simantics.g2d.element.handler.impl.StaticSymbolImpl;\r
-import org.simantics.g2d.element.handler.impl.TextColorImpl;\r
-import org.simantics.g2d.element.handler.impl.TextFontImpl;\r
-import org.simantics.g2d.image.DefaultImages;\r
-import org.simantics.g2d.image.Image;\r
-import org.simantics.g2d.svg.SVGImage;\r
-import org.simantics.g2d.utils.Alignment;\r
-\r
-\r
-/**\r
- * Element(Class)Factory for TextElements\r
- * \r
- * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
- *\r
- */\r
-public class TextElementClassFactory extends SyncElementFactory {\r
-\r
-    public static final ElementFactory INSTANCE = new TextElementClassFactory();\r
-\r
-    public static final IHintSynchronizer TEXT_SYNCHRONIZER = new CompositeHintSynchronizer(TextSynchronizer.INSTANCE, TransformSynchronizer.INSTANCE);\r
-\r
-    public static final ElementPropertySetter RESIZE_PROPERTY_SETTER =\r
-            new ElementPropertySetter(ResizeRectangularSceneGraph.KEY_SG_NODE);\r
-    \r
-    @Override\r
-    public ElementClass create(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType) throws DatabaseException {\r
-        String id = "TextElement: " + NameUtils.getSafeName(graph, elementType);\r
-        G2DResource g2d = G2DResource.getInstance(graph);\r
-        String svgDoc = graph.getPossibleRelatedValue(elementType, g2d.HasSVGDocument);\r
-        Image image = null;\r
-        if (svgDoc != null)\r
-            image = new SVGImage(id+".svg", svgDoc);\r
-        else\r
-            image = DefaultImages.ERROR_DECORATOR.get();\r
-        return ElementClass.compile(\r
-                Text.INSTANCE,\r
-                TextColorImpl.BLACK,\r
-                TextFontImpl.DEFAULT,\r
-                new StaticObjectAdapter(elementType),\r
-                DefaultTransform.INSTANCE,\r
-                SimpleElementLayers.INSTANCE,\r
-                TextElementHandler.INSTANCE,\r
-                ResizeRectangularSceneGraph.INSTANCE,\r
-                RESIZE_PROPERTY_SETTER,                \r
-                new StaticSymbolImpl(image)\r
-        )\r
-        .setId(id)\r
-        ;\r
-    }\r
-\r
-    @Override\r
-    public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource element, IElement e) throws DatabaseException {\r
-        G2DResource G2D = G2DResource.getInstance(graph);\r
-        DiagramResource DIA = DiagramResource.getInstance(graph);\r
-\r
-        String text = (String) graph.getPossibleRelatedValue(element, DIA.HasText);\r
-        if (text == null)\r
-               text = ElementUtils.getText(e);\r
-        if (text == null)\r
-            text = "[empty]";\r
-        ElementUtils.setText(e, text);\r
-\r
-        if (graph.isInstanceOf(element, DIA.FontProvider)) {\r
-            Resource fontResource = graph.getPossibleObject(element, G2D.HasFont);\r
-            if (fontResource != null)\r
-                ElementUtils.setTextFont(e, G2DUtils.getFont(graph, fontResource));\r
-        }\r
-        if (graph.isInstanceOf(element, DIA.ColorProvider)) {\r
-            Resource colorResource = graph.getPossibleObject(element, G2D.HasColor);\r
-            if (colorResource != null)\r
-                ElementUtils.setTextColor(e, G2DUtils.getColor(graph, colorResource));\r
-        }\r
-\r
-        // Load alignments\r
-        Resource hAlign = graph.getPossibleObject(element, G2D.HasHorizontalAlignment);\r
-        Resource vAlign = graph.getPossibleObject(element, G2D.HasVerticalAlignment);\r
-        if (hAlign != null)\r
-            e.setHint(ElementHints.KEY_HORIZONTAL_ALIGN, DiagramGraphUtil.toAlignment(hAlign, G2D, Alignment.LEADING));\r
-        if (vAlign != null)\r
-            e.setHint(ElementHints.KEY_VERTICAL_ALIGN, DiagramGraphUtil.toVerticalAlignment(vAlign, G2D, Alignment.BASELINE));\r
-\r
-        // Load border\r
-        final Float borderWidth = graph.getPossibleRelatedValue(element, G2D.HasStrokeWidth);\r
-        if (borderWidth != null)\r
-            e.setHint(MonitorClass.KEY_BORDER_WIDTH, borderWidth.doubleValue());\r
-\r
-        // Load transform\r
-        AffineTransform at = DiagramGraphUtil.getAffineTransform(graph, element);\r
-        ElementUtils.setTransform(e, at);\r
-\r
-        // This synchronizes only text and transformation (not font and color)\r
-        e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, TEXT_SYNCHRONIZER);\r
-        \r
-        double bounds[] = DiagramGraphUtil.getPossibleRelatedDoubleArray(graph, element, G2D.HasBounds);\r
-        if (bounds != null) {\r
-            e.setHint(ElementHints.KEY_BOUNDS, new Rectangle2D.Double(bounds[0], bounds[1], bounds[2], bounds[3]));\r
-        }\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.diagram.adapter;
+
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.diagram.G2DUtils;
+import org.simantics.diagram.elements.ElementPropertySetter;
+import org.simantics.diagram.elements.MonitorClass;
+import org.simantics.diagram.elements.ResizeRectangularSceneGraph;
+import org.simantics.diagram.elements.TextElementHandler;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.stubs.G2DResource;
+import org.simantics.diagram.synchronization.CompositeHintSynchronizer;
+import org.simantics.diagram.synchronization.IHintSynchronizer;
+import org.simantics.diagram.synchronization.SynchronizationHints;
+import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
+import org.simantics.diagram.synchronization.graph.TextSynchronizer;
+import org.simantics.diagram.synchronization.graph.TransformSynchronizer;
+import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.g2d.element.ElementClass;
+import org.simantics.g2d.element.ElementHints;
+import org.simantics.g2d.element.ElementUtils;
+import org.simantics.g2d.element.IElement;
+import org.simantics.g2d.element.handler.Text;
+import org.simantics.g2d.element.handler.impl.DefaultTransform;
+import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
+import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
+import org.simantics.g2d.element.handler.impl.StaticSymbolImpl;
+import org.simantics.g2d.element.handler.impl.TextColorImpl;
+import org.simantics.g2d.element.handler.impl.TextFontImpl;
+import org.simantics.g2d.image.DefaultImages;
+import org.simantics.g2d.image.Image;
+import org.simantics.g2d.svg.SVGImage;
+import org.simantics.g2d.utils.Alignment;
+
+
+/**
+ * Element(Class)Factory for TextElements
+ * 
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
+ *
+ */
+public class TextElementClassFactory extends SyncElementFactory {
+
+    public static final ElementFactory INSTANCE = new TextElementClassFactory();
+
+    public static final IHintSynchronizer TEXT_SYNCHRONIZER = new CompositeHintSynchronizer(TextSynchronizer.INSTANCE, TransformSynchronizer.INSTANCE);
+
+    public static final ElementPropertySetter RESIZE_PROPERTY_SETTER =
+            new ElementPropertySetter(ResizeRectangularSceneGraph.KEY_SG_NODE);
+    
+    @Override
+    public ElementClass create(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType) throws DatabaseException {
+        String id = "TextElement: " + NameUtils.getSafeName(graph, elementType);
+        G2DResource g2d = G2DResource.getInstance(graph);
+        String svgDoc = graph.getPossibleRelatedValue(elementType, g2d.HasSVGDocument);
+        Image image = null;
+        if (svgDoc != null)
+            image = new SVGImage(id+".svg", svgDoc);
+        else
+            image = DefaultImages.ERROR_DECORATOR.get();
+        return ElementClass.compile(
+                Text.INSTANCE,
+                TextColorImpl.BLACK,
+                TextFontImpl.DEFAULT,
+                new StaticObjectAdapter(elementType),
+                DefaultTransform.INSTANCE,
+                SimpleElementLayers.INSTANCE,
+                TextElementHandler.INSTANCE,
+                ResizeRectangularSceneGraph.INSTANCE,
+                RESIZE_PROPERTY_SETTER,                
+                new StaticSymbolImpl(image)
+        )
+        .setId(id)
+        ;
+    }
+
+    @Override
+    public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource element, IElement e) throws DatabaseException {
+        G2DResource G2D = G2DResource.getInstance(graph);
+        DiagramResource DIA = DiagramResource.getInstance(graph);
+
+        String text = (String) graph.getPossibleRelatedValue(element, DIA.HasText);
+        if (text == null)
+               text = ElementUtils.getText(e);
+        if (text == null)
+            text = "[empty]";
+        ElementUtils.setText(e, text);
+
+        if (graph.isInstanceOf(element, DIA.FontProvider)) {
+            Resource fontResource = graph.getPossibleObject(element, G2D.HasFont);
+            if (fontResource != null)
+                ElementUtils.setTextFont(e, G2DUtils.getFont(graph, fontResource));
+        }
+        if (graph.isInstanceOf(element, DIA.ColorProvider)) {
+            Resource colorResource = graph.getPossibleObject(element, G2D.HasColor);
+            if (colorResource != null)
+                ElementUtils.setTextColor(e, G2DUtils.getColor(graph, colorResource));
+        }
+
+        // Load alignments
+        Resource hAlign = graph.getPossibleObject(element, G2D.HasHorizontalAlignment);
+        Resource vAlign = graph.getPossibleObject(element, G2D.HasVerticalAlignment);
+        if (hAlign != null)
+            e.setHint(ElementHints.KEY_HORIZONTAL_ALIGN, DiagramGraphUtil.toAlignment(hAlign, G2D, Alignment.LEADING));
+        if (vAlign != null)
+            e.setHint(ElementHints.KEY_VERTICAL_ALIGN, DiagramGraphUtil.toVerticalAlignment(vAlign, G2D, Alignment.BASELINE));
+
+        // Load border
+        final Float borderWidth = graph.getPossibleRelatedValue(element, G2D.HasStrokeWidth);
+        if (borderWidth != null)
+            e.setHint(MonitorClass.KEY_BORDER_WIDTH, borderWidth.doubleValue());
+
+        // Load transform
+        AffineTransform at = DiagramGraphUtil.getAffineTransform(graph, element);
+        ElementUtils.setTransform(e, at);
+
+        // This synchronizes only text and transformation (not font and color)
+        e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, TEXT_SYNCHRONIZER);
+        
+        double bounds[] = DiagramGraphUtil.getPossibleRelatedDoubleArray(graph, element, G2D.HasBounds);
+        if (bounds != null) {
+            e.setHint(ElementHints.KEY_BOUNDS, new Rectangle2D.Double(bounds[0], bounds[1], bounds[2], bounds[3]));
+        }
+    }
+
+}