]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextNode.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / elements / TextNode.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextNode.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextNode.java
new file mode 100644 (file)
index 0000000..6fb0611
--- /dev/null
@@ -0,0 +1,1965 @@
+/*******************************************************************************\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.elements;\r
+\r
+import gnu.trove.list.array.TIntArrayList;\r
+\r
+import java.awt.AlphaComposite;\r
+import java.awt.BasicStroke;\r
+import java.awt.Color;\r
+import java.awt.Composite;\r
+import java.awt.Font;\r
+import java.awt.FontMetrics;\r
+import java.awt.Graphics2D;\r
+import java.awt.RenderingHints;\r
+import java.awt.Shape;\r
+import java.awt.Toolkit;\r
+import java.awt.datatransfer.Clipboard;\r
+import java.awt.datatransfer.DataFlavor;\r
+import java.awt.datatransfer.StringSelection;\r
+import java.awt.datatransfer.Transferable;\r
+import java.awt.event.KeyEvent;\r
+import java.awt.font.FontRenderContext;\r
+import java.awt.font.LineBreakMeasurer;\r
+import java.awt.font.TextAttribute;\r
+import java.awt.font.TextHitInfo;\r
+import java.awt.font.TextLayout;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Point2D;\r
+import java.awt.geom.Rectangle2D;\r
+import java.io.IOException;\r
+import java.text.AttributedCharacterIterator;\r
+import java.text.AttributedString;\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.Hashtable;\r
+\r
+import org.simantics.datatypes.literal.RGB;\r
+import org.simantics.db.layer0.variable.RVI;\r
+import org.simantics.diagram.elements.Line.BoundsProcedure;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.scenegraph.IDynamicSelectionPainterNode;\r
+import org.simantics.scenegraph.LoaderNode;\r
+import org.simantics.scenegraph.ScenegraphUtils;\r
+import org.simantics.scenegraph.g2d.G2DNode;\r
+import org.simantics.scenegraph.g2d.G2DPDFRenderingHints;\r
+import org.simantics.scenegraph.g2d.events.Event;\r
+import org.simantics.scenegraph.g2d.events.EventTypes;\r
+import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseClickEvent;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDragBegin;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;\r
+import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
+import org.simantics.scenegraph.g2d.events.command.Commands;\r
+import org.simantics.scenegraph.g2d.events.NodeEventHandler;\r
+import org.simantics.scenegraph.utils.GeometryUtils;\r
+import org.simantics.scenegraph.utils.NodeUtil;\r
+import org.simantics.scl.runtime.function.Function1;\r
+import org.simantics.scl.runtime.function.Function2;\r
+import org.simantics.ui.colors.Colors;\r
+import org.simantics.ui.dnd.LocalObjectTransferable;\r
+import org.simantics.ui.fonts.Fonts;\r
+import org.simantics.utils.threads.AWTThread;\r
+\r
+import com.lowagie.text.DocumentException;\r
+import com.lowagie.text.Element;\r
+import com.lowagie.text.Rectangle;\r
+import com.lowagie.text.pdf.FontMapper;\r
+import com.lowagie.text.pdf.PdfFormField;\r
+import com.lowagie.text.pdf.PdfWriter;\r
+import com.lowagie.text.pdf.TextField;\r
+\r
+\r
+/**\r
+ * TextNode which supports in-line editing.\r
+ * \r
+ * By default <code>TextNode</code> is in editable = false state. Use\r
+ * {@link #setEditable(boolean)} to make it editable.\r
+ * \r
+ * @author Hannu Niemist&ouml; <hannu.niemisto@vtt.fi>\r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ * @author Tuukka Lehtonen <tuukka.lehtonen@semantum.fi>\r
+ * \r
+ * TODO:\r
+ * o proper support for defining clipping bounds for the text (needed for page templates) (currently through fixedWidth)\r
+ * o fix editing xOffset to work with fixed width and multi-line text\r
+ * o \r
+ * \r
+ * @see Line\r
+ * @see TextLayout\r
+ */\r
+public class TextNode extends G2DNode implements IDynamicSelectionPainterNode, LoaderNode {\r
+\r
+    private static final long                serialVersionUID           = 654692698101485672L;\r
+\r
+    /**\r
+     * TODO: justify existence for this\r
+     */\r
+    private static final BasicStroke         RESET_STROKE               = new BasicStroke(1);\r
+\r
+    /**\r
+     * Src-over alpha composite instance with 50% opacity.\r
+     */\r
+    private static final AlphaComposite      SrcOver_50                 = AlphaComposite.SrcOver.derive(0.5f);\r
+\r
+    /**\r
+     * For (inexact) measurement of rendered text bounds.\r
+     */\r
+    protected static final FontRenderContext FRC = new FontRenderContext(new AffineTransform(), true, true);\r
+\r
+    private static final Font FONT = Font.decode("Arial 6");\r
+    private static final Color SELECTION_BACKGROUND_COLOR = new Color(0x316ac5);\r
+//    private static final double MAX_CARET_POSITION = 1.0;\r
+\r
+    /**\r
+     * The complete text visualized by this node.\r
+     */\r
+    protected String text = null;\r
+\r
+    /**\r
+     * Tells if this node is still pending for real results or not.\r
+     */\r
+    protected boolean pending = false;\r
+\r
+    /**\r
+     * The font used to render the {@link #text}.\r
+     */\r
+    protected Font font = FONT;\r
+\r
+    /**\r
+     * The color of the rendered text. Default value is {@value Color#black}.\r
+     */\r
+    protected Color color = Color.BLACK;\r
+\r
+    /**\r
+     * The background color used for filling the background of the bounding box\r
+     * of the rendered text. <code>null</code> means no fill.\r
+     * Default value is <code>null</code>.\r
+     */\r
+    protected Color backgroundColor = null;\r
+\r
+    /**\r
+     * The color used for drawing the expanded bounding box border for the\r
+     * rendered text. <code>null</code> means no border is rendered. Default\r
+     * value is <code>null</code>.\r
+     */\r
+    protected Color borderColor = null;\r
+\r
+    protected double scale = 1.0;\r
+    protected transient double scaleRecip = 1.0;\r
+\r
+    /**\r
+     * \r
+     */\r
+    protected float borderWidth = 0.f;\r
+\r
+    protected double paddingX = 2.0;\r
+    protected double paddingY = 2.0;\r
+\r
+    /**\r
+     * Horizontal text box alignment with respect to its origin. Default value is\r
+     * 0 (leading).\r
+     */\r
+    protected byte horizontalAlignment = 0;\r
+    /**\r
+     * Vertical text box alignment with respect to its origin. Default value is\r
+     * 3 (baseline).\r
+     */\r
+    protected byte verticalAlignment = 3;\r
+\r
+    protected boolean hover = false;\r
+    boolean editable = false;\r
+    boolean showSelection = true;\r
+    \r
+    \r
+    boolean wrapText = true;\r
+\r
+    protected RVI dataRVI = null;\r
+\r
+    int caret = 0;\r
+    int selectionTail = 0;\r
+    float xOffset = 0;\r
+\r
+    float fixedWidth = 0f;\r
+\r
+    private Rectangle2D targetBounds;\r
+\r
+    Function1<String, String> validator;\r
+    ITextListener textListener;\r
+    ITextContentFilter editContentFilter;\r
+\r
+    transient boolean editing = false;\r
+    transient boolean valid = true;\r
+\r
+    private transient boolean xOffsetIsDirty = true;\r
+\r
+    /**\r
+     * The renderable line structures parsed from {@link #text} by\r
+     * {@link #parseLines(String)}, laid out by\r
+     * {@link #layoutLines(Line[], FontRenderContext)} and aligned by\r
+     * {@link #alignLines(Line[], Rectangle2D, byte, byte)}\r
+     */\r
+    protected transient Line[]           lines                      = null;\r
+    protected transient FontMetrics      fontMetrics                = null;\r
+\r
+    /**\r
+     * Stores the value of {@link #text} before edit mode was last entered. Used\r
+     * for restoring the original value if editing is cancelled.\r
+     */\r
+    private transient String             textBeforeEdit             = null;\r
+    protected transient TextEditActivation editActivation;\r
+\r
+    /**\r
+     * Stores the last scaled bounds.\r
+     */\r
+    private transient Rectangle2D        lastBounds = new Rectangle2D.Double();\r
+\r
+    /**\r
+     * This must be nullified if anything that affects the result of\r
+     * {@link #getTightAlignedBoundsInLocal(Rectangle2D, FontRenderContext)}\r
+     * changes. It will cause the cached value to be recalculated on the next\r
+     * request.\r
+     */\r
+    private transient Rectangle2D        tightBoundsCache = null;\r
+\r
+    @Override\r
+    public void init() {\r
+        super.init();\r
+        // Mark this node as pending\r
+        NodeUtil.increasePending(this);\r
+    }\r
+\r
+    @Override\r
+    public void cleanup() {\r
+        removeListeners();\r
+        super.cleanup();\r
+    }\r
+\r
+    protected void addListeners() {\r
+        addEventHandler(this);\r
+    }\r
+\r
+    protected void removeListeners() {\r
+        removeEventHandler(this);\r
+    }\r
+\r
+    /**\r
+     * Enables or disables edit mode. It also sets\r
+     * the caret at the end of text all selects the\r
+     * whole text (this is the usual convention when\r
+     * beginning to edit one line texts).\r
+     * @param edit\r
+     * @return null if no change to edit state was made\r
+     */\r
+    public Boolean setEditMode(boolean edit) {\r
+        return setEditMode(edit, true);\r
+    }\r
+\r
+    /**\r
+     * Enables or disables edit mode. It also sets\r
+     * the caret at the end of text all selects the\r
+     * whole text (this is the usual convention when\r
+     * beginning to edit one line texts).\r
+     * @param edit\r
+     * @return null if no change to edit state was made\r
+     */\r
+    protected Boolean setEditMode(boolean edit, boolean notify) {\r
+        if (edit && !editable)\r
+            return null;\r
+        if (editing == edit)\r
+            return null;\r
+        this.editing = edit;\r
+        if (edit) {\r
+            caret = text != null ? text.length() : 0;\r
+            selectionTail = 0;\r
+            textBeforeEdit = text;\r
+            if (notify)\r
+                fireTextEditingStarted();\r
+            return Boolean.TRUE;\r
+        } else {\r
+            if (notify)\r
+                fireTextEditingEnded();\r
+            return Boolean.FALSE;\r
+        }\r
+    }\r
+\r
+    @SyncField({"editable"})\r
+    public void setEditable(boolean editable) {\r
+        boolean changed = this.editable != editable;\r
+        this.editable = editable;\r
+        if (editing && !editable)\r
+            setEditMode(false);\r
+        if (changed) {\r
+            if (editable)\r
+                addListeners();\r
+            else\r
+                removeListeners();\r
+        }\r
+    }\r
+\r
+    public boolean isEditable() {\r
+        return editable;\r
+    }\r
+\r
+    public boolean isEditMode() {\r
+        return editing;\r
+    }\r
+    \r
+    @SyncField({"wrapText"})\r
+    public void setWrapText(boolean wrapText) {\r
+        this.wrapText = wrapText;\r
+    }\r
+    \r
+    /**\r
+     * @return Does the text box wrap text if \r
+     * the width of the box is fixed\r
+     */\r
+    public boolean isWrapText() {\r
+        return this.wrapText;\r
+    }\r
+\r
+    @SyncField({"showSelection"})\r
+    public void setShowSelection(boolean showSelection) {\r
+        this.showSelection = showSelection;\r
+    }\r
+\r
+    public boolean showsSelection() {\r
+        return showSelection;\r
+    }\r
+\r
+    /**\r
+     * @param text\r
+     * @param font\r
+     * @param color\r
+     * @param x not supported anymore, use {@link #setTransform(AffineTransform)} instead\r
+     * @param y not supported anymore, use {@link #setTransform(AffineTransform)} instead\r
+     * @param scale\r
+     */\r
+    @SyncField({"text", "font", "color", "x", "y", "scale"})\r
+    public void init(String text, Font font, Color color, double x, double y, double scale) {\r
+        // no value => value\r
+        if(this.text == null && text != null) NodeUtil.decreasePending(this);\r
+\r
+        if (editing)\r
+            return;\r
+\r
+        this.text = new String(text != null ? text : "");\r
+        this.font = font;\r
+        this.color = color;\r
+        this.scale = scale;\r
+        this.scaleRecip = 1.0 / scale;\r
+        this.caret = 0;\r
+        this.selectionTail = 0;\r
+\r
+        resetCaches();\r
+    }\r
+\r
+    @SyncField({"paddingX", "paddingY"})\r
+    public void setPadding(double x, double y) {\r
+        this.paddingX = x;\r
+        this.paddingY = y;\r
+    }\r
+\r
+    @SyncField({"color"})\r
+    public void setColor(Color color) {\r
+        this.color = color;\r
+    }\r
+\r
+    @SyncField({"backgroundColor"})\r
+    public void setBackgroundColor(Color color) {\r
+        this.backgroundColor = color;\r
+    }\r
+\r
+    @SyncField({"borderColor"})\r
+    public void setBorderColor(Color color) {\r
+        this.borderColor = color;\r
+    }\r
+\r
+    public String getText() {\r
+        return text;\r
+    }\r
+    \r
+    public String getTextBeforeEdit() {\r
+       return textBeforeEdit;\r
+    }\r
+\r
+    @SyncField({"text","caret","selectionTail"})\r
+    public void setText(String text) {\r
+        //System.out.println("TextNode.setText('" + text + "', " + editing + ")");\r
+        if (editing)\r
+            return;\r
+\r
+        // value => no value\r
+        if(this.text != null && text == null) NodeUtil.increasePending(this);\r
+        // no value => value\r
+        if(this.text == null && text != null) NodeUtil.decreasePending(this);\r
+\r
+        this.text = text != null ? text : "";\r
+        caret = Math.min(caret, this.text.length());\r
+        selectionTail = caret;\r
+\r
+        resetCaches();\r
+    }\r
+\r
+    @SyncField({"pending"})\r
+    public void setPending(boolean pending) {\r
+        if(!this.pending && pending) NodeUtil.increasePending(this);\r
+        if(this.pending && !pending) NodeUtil.decreasePending(this);\r
+        this.pending = pending;\r
+    }\r
+\r
+    @SyncField({"fixedWidth"})\r
+    public void setFixedWidth(float fixedWidth) {\r
+        if (fixedWidth < 0f)\r
+            throw new IllegalArgumentException("negative fixed width");\r
+        this.fixedWidth = fixedWidth;\r
+        invalidateXOffset();\r
+    }\r
+    \r
+    /**\r
+     * Bounds where the text box will be drawn\r
+     * @param bounds\r
+     */\r
+    public void setTargetBounds(Rectangle2D bounds) {\r
+        this.targetBounds = bounds;\r
+    }\r
+\r
+       final public void synchronizeWidth(float width) {\r
+               if (width >= 0.0f)\r
+                       setFixedWidth(width);\r
+       }\r
+\r
+       final public void synchronizeBorderWidth(float width) {\r
+               if (width >= 0.0f)\r
+                       setBorderWidth(width);\r
+       }\r
+\r
+    public final void synchronizeWrapText(boolean wrap) {\r
+        wrapText = wrap;\r
+    }\r
+\r
+    public boolean isHovering() {\r
+        return hover;\r
+    }\r
+\r
+    @SyncField({"hover"})\r
+    public void setHover(boolean hover) {\r
+        this.hover = hover;\r
+        repaint();\r
+    }\r
+\r
+    public Font getFont() {\r
+        return font;\r
+    }\r
+\r
+    @SyncField({"font"})\r
+    public void setFont(Font font) {\r
+        this.font = font;\r
+        resetCaches();\r
+    }\r
+\r
+    public double getBorderWidth() {\r
+        return borderWidth;\r
+    }\r
+\r
+    @SyncField({"borderWidth"})\r
+    public void setBorderWidth(float width) {\r
+        this.borderWidth = width;\r
+    }\r
+\r
+    public void setBorderWidth(double width) {\r
+        setBorderWidth((float)width);\r
+    }\r
+\r
+    @SyncField({"horizontalAlignment"})\r
+    public void setHorizontalAlignment(byte horizontalAlignment) {\r
+        if (horizontalAlignment < 0 && horizontalAlignment > 2)\r
+            throw new IllegalArgumentException("Invalid horizontal alignment: " + horizontalAlignment + ", must be between 0 and 2");\r
+        this.horizontalAlignment = horizontalAlignment;\r
+        resetCaches();\r
+    }\r
+\r
+    final public void synchronizeHorizontalAlignment(byte horizontalAlignment) {\r
+        if (horizontalAlignment >= 0 && horizontalAlignment <= 2)\r
+            setHorizontalAlignment(horizontalAlignment);\r
+    }\r
+\r
+    public byte getHorizontalAlignment() {\r
+        return horizontalAlignment;\r
+    }\r
+\r
+    @SyncField({"verticalAlignment"})\r
+    public void setVerticalAlignment(byte verticalAlignment) {\r
+        if (verticalAlignment < 0 && verticalAlignment > 3)\r
+            throw new IllegalArgumentException("Invalid vertical alignment: " + verticalAlignment + ", must be between 0 and 3");\r
+        this.verticalAlignment = verticalAlignment;\r
+        resetCaches();\r
+    }\r
+\r
+    final public void synchronizeVerticalAlignment(byte verticalAlignment) {\r
+        if (verticalAlignment >= 0 && verticalAlignment <= 3)\r
+            setVerticalAlignment(verticalAlignment);\r
+    }\r
+\r
+    public byte getVerticalAlignment() {\r
+        return verticalAlignment;\r
+    }\r
+\r
+    /**\r
+     * Rendering is single-threaded so we can use a static rectangle for\r
+     * calculating the expanded bounds for the node.\r
+     */\r
+    private static transient ThreadLocal<Rectangle2D> tempBounds = new ThreadLocal<Rectangle2D>() {\r
+        @Override\r
+        protected Rectangle2D initialValue() {\r
+            return new Rectangle2D.Double();\r
+        }\r
+    };\r
+\r
+    /**\r
+     * Rendering is single-threaded so we can use a static AffineTransform to\r
+     * prevent continuous memory allocation during text rendering.\r
+     */\r
+    private static transient ThreadLocal<AffineTransform> tempAffineTransform = new ThreadLocal<AffineTransform>() {\r
+        @Override\r
+        protected AffineTransform initialValue() {\r
+            return new AffineTransform();\r
+        }\r
+    };\r
+\r
+    @Override\r
+    public void render(Graphics2D g) {\r
+        AffineTransform ot = g.getTransform();\r
+        render(g, true);\r
+        g.setTransform(ot);\r
+    }\r
+\r
+    /**\r
+     * Note: does not return transformation, stroke, color, etc. to their\r
+     * original states\r
+     * \r
+     * @param g\r
+     * @param applyTransform\r
+     */\r
+    public void render(Graphics2D g, boolean applyTransform) {\r
+        if (text == null || font == null || color == null)\r
+            return;\r
+\r
+        // Cache font metrics if necessary\r
+        if (fontMetrics == null)\r
+            fontMetrics = g.getFontMetrics(font);\r
+\r
+        Color color = this.color;\r
+        boolean isSelected = NodeUtil.isSelected(this, 1);\r
+\r
+        if (!isSelected && hover) {\r
+            color = add(color, 120, 120, 120);\r
+        }\r
+\r
+        if (applyTransform)\r
+            g.transform(transform);\r
+        // Apply separate legacy scale\r
+        if (scale != 1.0)\r
+            g.scale(scale, scale);\r
+\r
+        // Safety for not rendering when the scale of this text is too small.\r
+        // When the scale is too small it will cause internal exceptions while\r
+        // stroking fonts.\r
+        double currentScale = GeometryUtils.getScale(g.getTransform());\r
+        //System.out.println("currentScale: " + currentScale);\r
+        if (currentScale < 1e-6)\r
+            return;\r
+\r
+        g.setFont(font);\r
+        //g.translate(x, y);\r
+\r
+        // Calculate text clip rectangle.\r
+        // This updates textLayout if necessary.\r
+        Rectangle2D r = getTightAlignedBoundsInLocal(tempBounds.get(), fontMetrics.getFontRenderContext());\r
+\r
+        computeEditingXOffset();\r
+\r
+        if (fixedWidth > 0f)\r
+            r.setFrame(r.getMinX(), r.getMinY(), fixedWidth, r.getHeight());\r
+        if(targetBounds != null) {\r
+            double w = (targetBounds.getWidth() - paddingX * 2) * scaleRecip;\r
+            double h = (targetBounds.getHeight() - paddingY * 2) * scaleRecip;\r
+            double x = (targetBounds.getMinX() + paddingX) * scaleRecip;\r
+            double y = (targetBounds.getMinY() + paddingY) * scaleRecip;\r
+            r.setRect(x, y, w, h);\r
+        }\r
+\r
+        Rectangle2D textClip = r.getBounds2D();\r
+\r
+        expandBoundsUnscaled(r);\r
+\r
+        // Speed rendering optimization: don't draw text that is too small to\r
+        // read when not editing\r
+        boolean renderText = true;\r
+        if (!editing) {\r
+            Object renderingHint = g.getRenderingHint(RenderingHints.KEY_RENDERING);\r
+            if (renderingHint != RenderingHints.VALUE_RENDER_QUALITY) {\r
+                float textSizeMM = (float) currentScale * GeometryUtils.pointToMillimeter(font.getSize2D());\r
+                if (textSizeMM < 1.5f)\r
+                    renderText = false;\r
+            }\r
+        }\r
+\r
+        Shape clipSave = g.getClip();\r
+        g.setClip(textClip);\r
+\r
+        // PDF \r
+        PdfWriter writer = (PdfWriter) g.getRenderingHint(G2DPDFRenderingHints.KEY_PDF_WRITER);\r
+        boolean isPdfField = false;\r
+        String fieldName = null;\r
+        if (writer != null) {\r
+               // TODO: replace this hack with proper text field name field\r
+               fieldName = NodeUtil.getNodeName(this);\r
+               isPdfField = ( fieldName.equals("approved_by") ||\r
+                                               fieldName.equals("checked_by") ||\r
+                                               fieldName.equals("designer name") ||\r
+                                               fieldName.equals("created_by") );\r
+        }\r
+\r
+        Color backgroundColor = valid ? this.backgroundColor : Color.red;\r
+\r
+        // RENDER\r
+        if ( !isPdfField ) {\r
+\r
+            // Fill background if necessary\r
+            if (backgroundColor != null) {\r
+                g.setColor(backgroundColor);\r
+                g.fill(r);\r
+            }\r
+\r
+            if (editing) {\r
+\r
+                int selectionMin = Math.min(caret, selectionTail);\r
+                int selectionMax = Math.max(caret, selectionTail);\r
+\r
+                // Base text\r
+                g.setColor(color);\r
+                renderText(g, xOffset);\r
+\r
+                Shape clip = g.getClip();\r
+\r
+                // Selection background & text\r
+                for (Line line : lines) {\r
+                    if (line.intersectsRange(selectionMin, selectionMax)) {\r
+                        Shape selShape = line.getLogicalHighlightShape(selectionMin, selectionMax);\r
+                        line.translate(g, xOffset, 0);\r
+                        g.setClip(selShape);\r
+                        g.setColor(SELECTION_BACKGROUND_COLOR);\r
+                        g.fill(selShape);\r
+                        g.setColor(Color.WHITE);\r
+                        //line.layout.draw(g, 0, 0);\r
+                        g.drawString(line.getText(), 0, 0);\r
+                        line.translateInv(g, xOffset, 0);\r
+                    }\r
+                }\r
+\r
+                g.setClip(clip);\r
+\r
+                // Caret\r
+                \r
+                renderCaret(g);\r
+                \r
+\r
+            } else {\r
+\r
+                if (renderText) {\r
+                    g.setColor(color);\r
+                    renderText(g, 0);\r
+                }\r
+\r
+            }\r
+        } else {\r
+            // PDF \r
+            // TODO: multiline support\r
+//                             try {\r
+                           AffineTransform at = g.getTransform();\r
+                           float height = writer.getPageSize().getHeight();\r
+                           Rectangle2D rr = textClip;\r
+       //                  Point2D pt1 = new Point2D.Double(rr.getX(), rr.getY()+rr.getHeight());\r
+       //                  Point2D pt2 = new Point2D.Double(rr.getX()+rr.getWidth(), rr.getY());\r
+                           Point2D pt1 = new Point2D.Double(0, 0);\r
+                           Point2D pt2 = new Point2D.Double(47.f/*+rr.getWidth()*/, -rr.getHeight());\r
+                           pt1 = at.transform(pt1, pt1);\r
+                           pt2 = at.transform(pt2, pt2);\r
+                               Rectangle rectangle = new Rectangle(\r
+                                               (float) pt1.getX(), \r
+                                               height-(float) pt1.getY(), \r
+                                               (float) pt2.getX(), \r
+                                               height-(float) pt2.getY()); \r
+\r
+                           FontMapper mapper = (FontMapper) g.getRenderingHint(G2DPDFRenderingHints.KEY_PDF_FONTMAPPER);\r
+//                             FontMetrics fm    = g.getFontMetrics(font);\r
+\r
+                               // TODO Oikea leveys\r
+                               // TODO Uniikki nimi\r
+                               /*\r
+                               PdfFormField field = PdfFormField.createTextField(writer, false, false, 20);\r
+                               field.setFieldName(this.getId().toString());\r
+                               field.setWidget(rectangle, PdfAnnotation.HIGHLIGHT_NONE);\r
+                               field.setQuadding(PdfFormField.Q_RIGHT);\r
+                               field.setFieldFlags(PdfFormField.FF_READ_ONLY);\r
+                               field.setRotate(90);\r
+                               writer.addAnnotation(field);\r
+                               */\r
+\r
+\r
+                               // Signature Field\r
+                               /*\r
+                               if (text==null) {\r
+                                       PdfFormField field = PdfFormField.createSignature(writer);\r
+                                       field.setWidget(rectangle, PdfAnnotation.HIGHLIGHT_NONE);\r
+                                       field.setFieldName(fieldName);\r
+                                       field.setQuadding(PdfFormField.Q_LEFT);\r
+                                       field.setFlags(PdfAnnotation.FLAGS_PRINT);\r
+                                       //field.setFieldFlags(PdfFormField.FF_READ_ONLY)\r
+                                       field.setFieldFlags(PdfFormField.FF_EDIT);\r
+                                       field.setPage();\r
+                                       field.setMKBackgroundColor( backgroundColor!=null?Color.WHITE:backgroundColor );\r
+                                       PdfAppearance tp = PdfAppearance.createAppearance(writer, 72, 48);\r
+                                       tp.rectangle(rectangle);\r
+                                       tp.stroke();                            \r
+                                       field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);\r
+                                       writer.addAnnotation(field);\r
+                               } else */ \r
+                               {\r
+                                       // Text Field\r
+                                               try {\r
+                                               TextField textField = new TextField(writer, rectangle, fieldName);\r
+                                               textField.setFieldName(fieldName);\r
+                                               textField.setFont(mapper.awtToPdf(font));\r
+                                               textField.setBorderStyle(0);\r
+                                           //textField.setAlignment(Element.ALIGN_LEFT);\r
+                                           textField.setAlignment(Element.ALIGN_BOTTOM);\r
+                                           textField.setRotation(90);\r
+                                       textField.setOptions(TextField.EDIT|TextField.DO_NOT_SPELL_CHECK);\r
+                                           if ( text!=null ) {\r
+                                                       textField.setText(text);\r
+                                           }\r
+                                           if ( color!=null ) {\r
+                                               textField.setTextColor(color);\r
+                                           }\r
+                                               textField.setBackgroundColor( backgroundColor!=null?Color.WHITE:backgroundColor );\r
+                                           PdfFormField field = textField.getTextField();\r
+                                           writer.addAnnotation(field);\r
+                                               } catch (IOException e) {\r
+                                                       e.printStackTrace();\r
+                                               } catch (DocumentException e) {\r
+                                                       e.printStackTrace();\r
+                                               }\r
+                               }\r
+\r
+//                             } catch (IOException e) {\r
+//                                     // TODO Auto-generated catch block\r
+//                                     e.printStackTrace();\r
+//                             } catch (DocumentException e) {\r
+//                                     // TODO Auto-generated catch block\r
+//                                     e.printStackTrace();\r
+//                             }\r
+        }\r
+        /// PDF\r
+\r
+        g.setClip(clipSave);\r
+\r
+        if (borderWidth > 0f && borderColor != null) {\r
+            g.setColor(borderColor);\r
+            g.setStroke(new BasicStroke((float) (scale*borderWidth)));\r
+            g.draw(r);\r
+        }\r
+\r
+        //System.out.println("bw: " + borderWidth);\r
+        if (isSelected && showsSelection()) {\r
+            Composite oc = g.getComposite();\r
+            g.setComposite(SrcOver_50);\r
+            g.setColor(Color.RED);\r
+            float bw = borderWidth;\r
+            double s = currentScale;\r
+            if (bw <= 0f) {\r
+                bw = (float) (1f / s);\r
+            } else {\r
+                bw *= 5f * scale;\r
+            }\r
+            g.setStroke(new BasicStroke(bw));\r
+            g.draw(r);\r
+            //g.draw(GeometryUtils.expandRectangle(r, 1.0));\r
+\r
+            g.setComposite(oc);\r
+        }\r
+\r
+        g.scale(scaleRecip, scaleRecip);\r
+        g.setStroke(RESET_STROKE);\r
+\r
+        lastBounds = getScaledOffsetBounds(r, lastBounds, scale, 0, 0);\r
+//        g.setColor(Color.MAGENTA); // DEBUG\r
+//        g.draw(lastBounds); // DEBUG\r
+//        g.setColor(Color.ORANGE); // DEBUG\r
+//        g.draw(getBoundsInLocal()); // DEBUG\r
+\r
+        renderSelectedHover(g, isSelected, hover);\r
+    }\r
+\r
+    private void renderCaret(Graphics2D g) {\r
+       g.setColor(Color.BLACK);\r
+       for (int i = 0; i < lines.length; i++) {\r
+               Line line = lines[i];\r
+               // prevent rendering caret twice on line changes\r
+               if (line.containsOffset(caret) &&                // line contains caret\r
+                  (caret != line.endOffset ||                   //caret is not in the end of the line\r
+                   i == lines.length-1 ||                       //caret is end of the last line\r
+                   lines[i+1].startOffset != line.endOffset)) { // beginning of the next line does not start withe the same index as current line\r
+              Shape[] caretShape = line.getCaretShapes(caret);\r
+              line.translate(g, xOffset, 0);\r
+              g.draw(caretShape[0]);\r
+              if (caretShape[1] != null)\r
+                  g.draw(caretShape[1]);\r
+              line.translateInv(g, xOffset, 0);\r
+          }\r
+        }\r
+    }\r
+    private void renderText(Graphics2D g, float xOffset) {\r
+        //g.draw(tightBoundsCache); // DEBUG\r
+        for (Line line : lines) {\r
+            //line.layout.draw(g, line.alignedPosX + xOffset, line.alignedPosY);\r
+            g.drawString(line.getText(), line.alignedPosX + xOffset, line.alignedPosY);\r
+            //g.draw(line.abbox); // DEBUG\r
+        }\r
+    }\r
+\r
+    protected Rectangle2D getScaledOffsetBounds(Rectangle2D originalBounds, Rectangle2D dst, double scale, double offsetX, double offsetY) {\r
+        AffineTransform btr = tempAffineTransform.get();\r
+        btr.setToTranslation(offsetX*scale, offsetY*scale);\r
+        btr.scale(scale, scale);\r
+        if (btr.isIdentity()) {\r
+            dst.setFrame(originalBounds);\r
+        } else {\r
+            dst.setFrame(btr.createTransformedShape(originalBounds).getBounds2D());\r
+        }\r
+        return dst;\r
+    }\r
+\r
+    /**\r
+     * Invoked when TextNode is selected and a mouse is hovering on top of it. Can be overridden to add custom rendering.\r
+     * \r
+     * @param g\r
+     */\r
+    protected void renderSelectedHover(Graphics2D g, boolean isSelected, boolean isHovering) {\r
+    }\r
+\r
+    /**\r
+     * Replaces the current selection with the content or inserts\r
+     * the content at caret. After the insertion the caret\r
+     * will be at the end of inserted text and selection will\r
+     * be empty.\r
+     * @param content\r
+     */\r
+    @SyncField({"text","caret","selectionTail"})\r
+    protected void insert(String content) {\r
+        content = editContentFilter != null ? editContentFilter.filter(this, content) : content; \r
+\r
+        int selectionMin = Math.min(caret, selectionTail);\r
+        int selectionMax = Math.max(caret, selectionTail);\r
+\r
+        String begin = text.substring(0, selectionMin);\r
+        String end = text.substring(selectionMax);\r
+        text = begin + content + end;\r
+        caret = selectionMin + content.length();\r
+        selectionTail = caret;\r
+\r
+        assert (caret <= text.length());\r
+        //System.out.println(text + " " + caret );\r
+\r
+        if(validator != null) {\r
+            String error = validator.apply(text);\r
+            valid = (error == null);\r
+        }\r
+\r
+        resetCaches();\r
+    }\r
+\r
+    @ServerSide\r
+    protected void fireTextChanged() {\r
+        if(textListener != null)\r
+            textListener.textChanged();\r
+        repaint();\r
+    }\r
+\r
+    @ServerSide\r
+    protected void fireTextEditingStarted() {\r
+        if(textListener != null)\r
+            textListener.textEditingStarted();\r
+    }\r
+\r
+    @ServerSide\r
+    protected void fireTextEditingCancelled() {\r
+        valid = true;\r
+\r
+        if (deactivateEdit()) {\r
+            if (textListener != null)\r
+                textListener.textEditingCancelled();\r
+\r
+            setEditMode(false, false);\r
+\r
+            if (textBeforeEdit != null)\r
+                setText(textBeforeEdit);\r
+\r
+            repaint();\r
+        }\r
+    }\r
+\r
+    @ServerSide\r
+    public void fireTextEditingEnded() {\r
+        if (!valid) {\r
+            fireTextEditingCancelled();\r
+            valid = true;\r
+            return;\r
+        }\r
+\r
+        if (deactivateEdit()) {\r
+            if (textListener != null)\r
+                textListener.textEditingEnded();\r
+\r
+            setEditMode(false, false);\r
+            repaint();\r
+        }\r
+    }\r
+\r
+    public void setTextListener(ITextListener listener) {\r
+        this.textListener = listener;\r
+    }\r
+\r
+    public void setValidator(Function1<String, String> validator) {\r
+        this.validator = validator;\r
+    }\r
+\r
+    public void setContentFilter(ITextContentFilter filter) {\r
+        this.editContentFilter = filter;\r
+    }\r
+\r
+    public void setRVI(RVI rvi) {\r
+        this.dataRVI = rvi;\r
+    }\r
+\r
+    private void invalidateXOffset() {\r
+        xOffsetIsDirty = true;\r
+    }\r
+\r
+    private void computeEditingXOffset() {\r
+\r
+        if(lines == null) return;\r
+        if(!xOffsetIsDirty) return;\r
+        if(fixedWidth > 0f) {\r
+\r
+            // TODO: implement\r
+//            float[] coords = textLayout.getCaretInfo(TextHitInfo.afterOffset(caret));\r
+//            if(coords != null) {\r
+//                if(coords[0] > (fixedWidth*MAX_CARET_POSITION)) xOffset = (float)((fixedWidth*MAX_CARET_POSITION)-coords[0]);\r
+//                else xOffset = 0;\r
+//            }\r
+\r
+        } else {\r
+\r
+            xOffset = 0;\r
+\r
+        }\r
+\r
+        xOffsetIsDirty = false;\r
+\r
+    }\r
+\r
+    @SyncField({"caret","selectionTail"})\r
+    protected void moveCaret(int move, boolean select) {\r
+       // prevent setting caret into line separator. \r
+       if (move > 0) {\r
+               while (text.length()> caret+move && getLineSeparator().indexOf(text.charAt(caret+move)) > 0)\r
+                       move++;\r
+       } else if (move < 0) {\r
+               while (caret+move >= 0 && text.length()> caret+move && getLineSeparator().indexOf(text.charAt(caret+move)) > 0)\r
+                       move--;\r
+       }\r
+        caret += move;\r
+        if(caret < 0)\r
+            caret = 0;\r
+        if (caret > text.length())\r
+            caret = text.length();\r
+        if(!select)\r
+            selectionTail = caret;\r
+    }\r
+    \r
+    private Line findCaretLine() {\r
+        // Find the line where caret is. Starting from first line.\r
+        for(int i = 0; i < lines.length; i++) {\r
+            Line line = lines[i];\r
+            if(caret <= line.endOffset) {\r
+                return line;\r
+            }\r
+        }\r
+        return null;\r
+    }\r
+    \r
+    /**\r
+     * Moves caret to next not letter or digit\r
+     * @param shiftDown\r
+     */\r
+    private void moveCaretCtrlLeft(boolean shiftDown) {\r
+        Line line = findCaretLine();\r
+        if(line != null) {\r
+            int i;\r
+            for(i = caret-1; i > line.startOffset; i--) {\r
+                char c = line.document.charAt(i);\r
+                if(!Character.isLetterOrDigit(c)) {\r
+                    break;\r
+                }\r
+            }\r
+            moveCaret(i - caret, shiftDown);\r
+        }\r
+    }\r
+    \r
+    /**\r
+     * Moves caret to previous non letter or digit\r
+     * @param shiftDown\r
+     */\r
+    private void moveCaretCtrlRight(boolean shiftDown) {\r
+        Line line = findCaretLine();\r
+        if(line != null) {\r
+            int i;\r
+            for(i = caret + 1; i < line.endOffset; i++) {\r
+                char c = line.document.charAt(i);\r
+                if(!Character.isLetterOrDigit(c)) {\r
+                    break;\r
+                }\r
+            }\r
+            moveCaret(i - caret, shiftDown);\r
+        }\r
+    }\r
+    \r
+    /**\r
+     * Moves caret to line end\r
+     * @param shiftDown\r
+     */\r
+    private void moveCaretEnd(boolean shiftDown) {\r
+        Line line = findCaretLine();\r
+        if(line != null)\r
+            // Move caret to the end of the line\r
+            moveCaret(line.endOffset - caret, shiftDown);\r
+    }\r
+    \r
+    /**\r
+     * Moves caret to beginning of a line\r
+     * @param shiftDown\r
+     */\r
+    private void moveCaretHome(boolean shiftDown) {\r
+        Line line = findCaretLine();\r
+        if(line != null)\r
+            // Move caret to the beginning of the line\r
+            moveCaret(line.startOffset - caret, shiftDown);\r
+    }\r
+    \r
+    /**\r
+     * Moves caret one row up and tries to maintain the location\r
+     * @param shiftDown\r
+     */\r
+    private void moveCaretRowUp(boolean shiftDown) {\r
+        // Find the line where caret is. Starting from first line.\r
+        for(int i = 0; i < lines.length; i++) {\r
+            Line line = lines[i];\r
+            if(caret <= line.endOffset) {\r
+                // caret is in this line\r
+                if(i == 0) {\r
+                    // Already on top line\r
+                    // Select the beginning of the line\r
+                    moveCaret(-caret, shiftDown);\r
+                } else {\r
+                    Line prevLine = lines[i-1];\r
+                    int prevLength = prevLine.endOffset - prevLine.startOffset;\r
+                    int posInCurRow = caret - line.startOffset;\r
+                    if(prevLength < posInCurRow)\r
+                        posInCurRow = prevLength;\r
+\r
+                    int newPos = prevLine.startOffset + posInCurRow;\r
+                    moveCaret(newPos - caret, shiftDown);\r
+                }\r
+                break;\r
+            }\r
+        }        \r
+    }\r
+    \r
+    /**\r
+     * Moves caret one row down and tries to maintain the location\r
+     * @param shiftDown\r
+     */\r
+    private void moveCaretRowDown(boolean shiftDown) {\r
+        // Find the line where caret is. Starting from last line.\r
+        for(int i = lines.length - 1; i >= 0; i--) {\r
+            Line line = lines[i];\r
+            if(caret >= line.startOffset) {\r
+                // caret is in this line\r
+                if(i == lines.length - 1) {\r
+                    // Already on bottom line, cannot go below\r
+                    // Select to the end of the line\r
+                    moveCaret(line.endOffset - caret, shiftDown);\r
+                } else {\r
+                    Line prevLine = lines[i+1]; // Previous line\r
+                    \r
+                    // Find new caret position. \r
+                    // Either it is in the same index as before, or if the row\r
+                    // is not long enough, select the end of the row.\r
+                    int prevLength = prevLine.endOffset - prevLine.startOffset;\r
+                    int posInCurRow = caret - line.startOffset;\r
+                    if(prevLength < posInCurRow)\r
+                        posInCurRow = prevLength;\r
+                    int newPos = prevLine.startOffset + posInCurRow;\r
+                    moveCaret(newPos - caret, shiftDown);\r
+                }\r
+                break;\r
+            }\r
+        }        \r
+    }\r
+\r
+    @SyncField({"caret","selectionTail"})\r
+    protected void setCaret(int pos, boolean select) {\r
+        caret = pos;\r
+        if (caret < 0)\r
+            caret = 0;\r
+        if (caret > text.length())\r
+            caret = text.length();\r
+        if (!select)\r
+            selectionTail = caret;\r
+    }\r
+\r
+    protected void setCaret(Point2D point) {\r
+       setCaret(point, false);\r
+    }\r
+\r
+    @SyncField({"caret","selectionTail"})\r
+    protected void setCaret(Point2D point, boolean select) {\r
+        double lineY = 0;\r
+        for(int i = 0; i < lines.length; i++) {\r
+            Line line = lines[i];\r
+            Rectangle2D bounds = line.abbox;\r
+            // Add heights of bboxes for determining the correct line\r
+            if(i == 0)\r
+                lineY = bounds.getY();\r
+            else\r
+                lineY += lines[i-1].abbox.getHeight();\r
+            \r
+            double lineHeight = bounds.getHeight();\r
+            double hitY = point.getY() / scale;\r
+            if(hitY >= lineY && hitY <= lineY + lineHeight) {\r
+                // Hit is in this line\r
+                float x = (float)(point.getX() / scale) - (float)line.abbox.getX();\r
+                float y = (float)(point.getY() / scale -  lineHeight * i) ;\r
+                TextHitInfo info = line.layout.hitTestChar(x, y);\r
+                caret = line.startOffset + info.getInsertionIndex();\r
+                if (caret > line.endOffset)\r
+                       caret = line.endOffset;\r
+                if (!select)\r
+                    selectionTail = caret;\r
+                repaint();\r
+                break;\r
+            }\r
+        }\r
+        invalidateXOffset();\r
+        assert (caret <= text.length());\r
+    }\r
+    \r
+    @Override\r
+    public Rectangle2D getBoundsInLocal() {\r
+        if(targetBounds != null)\r
+            return targetBounds;\r
+        else\r
+            return expandBounds( getTightAlignedBoundsInLocal(null) );\r
+    }\r
+\r
+    protected Rectangle2D expandBounds(Rectangle2D r) {\r
+        r.setRect(r.getX() * scale - paddingX, r.getY() * scale -paddingY, r.getWidth()*scale + paddingX + paddingX, r.getHeight()*scale + paddingY + paddingY);\r
+        //System.out.println("  => " + r);\r
+        return r;\r
+    }\r
+\r
+    protected Rectangle2D expandBoundsUnscaled(Rectangle2D r) {\r
+        r.setRect(r.getX() - scaleRecip*paddingX, r.getY() -scaleRecip*paddingY, r.getWidth() + scaleRecip*paddingX + scaleRecip*paddingX, r.getHeight() + scaleRecip*paddingY + scaleRecip*paddingY);\r
+        //System.out.println("  => " + r);\r
+        return r;\r
+    }\r
+\r
+    protected Rectangle2D expandBounds(Rectangle2D r, double amount) {\r
+        r.setRect(r.getX() - amount, r.getY() - amount, r.getWidth() + 2*amount, r.getHeight() + 2*amount);\r
+        return r;\r
+    }\r
+\r
+    protected Rectangle2D expandBounds(Rectangle2D r, double left, double top, double right, double bottom) {\r
+        r.setRect(r.getX() - left, r.getY() - top, r.getWidth() + left + right, r.getHeight() + top + bottom);\r
+        return r;\r
+    }\r
+\r
+    private void resetCaches() {\r
+        this.tightBoundsCache = null;\r
+        this.lines = null;\r
+        this.fontMetrics = null;\r
+    }\r
+\r
+    /**\r
+     * Returns the tight bounds around the current text using the current font\r
+     * in the specified rectangle. If the specified rectangle is\r
+     * <code>null</code> a new Rectangle2D.Double instance will be created.\r
+     * \r
+     * @param r\r
+     * @return\r
+     */\r
+    protected Rectangle2D getTightAlignedBoundsInLocal(Rectangle2D r) {\r
+        return getTightAlignedBoundsInLocal(r, FRC);\r
+    }\r
+\r
+    /**\r
+     * Returns the tight bounds around the current text using the current font\r
+     * in the specified rectangle. If the specified rectangle is\r
+     * <code>null</code> a new Rectangle2D.Double instance will be created.\r
+     * \r
+     * @param r\r
+     *            the rectangle where the result of the method is placed or\r
+     *            <code>null</code> to allocate new rectangle\r
+     * @param frc current font render context\r
+     * @return r or new Rectangle2D.Double instance containing the requested\r
+     *         text bounds\r
+     */\r
+    protected Rectangle2D getTightAlignedBoundsInLocal(Rectangle2D r, FontRenderContext frc) {\r
+        if (r == null)\r
+            r = new Rectangle2D.Double();\r
+\r
+        if (tightBoundsCache != null) {\r
+            r.setFrame(tightBoundsCache);\r
+            return r;\r
+        }\r
+\r
+        String txt = text;\r
+        if (font == null || txt == null) {\r
+            r.setFrame(0, 0, 2, 1);\r
+            return r;\r
+        }\r
+\r
+        //System.out.println("TextNode.getTightAlignedBoundsInLocal('" + txt + "')");\r
+\r
+        // Parse & layout (unaligned)\r
+        Line[] lines = null;\r
+        \r
+        if(wrapText) {\r
+            float width = fixedWidth;\r
+            if(width <= 0 && targetBounds != null)\r
+                width = (float) (((targetBounds.getWidth() - 2*paddingX)) * scaleRecip);\r
+            if(width > 0)\r
+                lines = wrapLines(txt, font, width, frc);\r
+        }\r
+         \r
+        if(lines == null)\r
+            lines = parseLines(txt);\r
+        this.lines = layoutLines(lines, frc);\r
+\r
+        // Calculate tight bounds based on unaligned layout\r
+        //System.out.println("Unaligned");\r
+        tightBoundsCache = calculateBounds(lines, Line.BBOX, null);\r
+        //System.out.println("  => " + tightBoundsCache);\r
+\r
+        this.lines = layoutLinesX(lines, tightBoundsCache);\r
+        // Align each line to the calculated tight bounds\r
+        this.lines = alignLines(this.lines, tightBoundsCache, horizontalAlignment, verticalAlignment);\r
+\r
+        // Calculate aligned bounds\r
+        //System.out.println("Aligned");\r
+        calculateBounds(lines, Line.ABBOX, tightBoundsCache);\r
+\r
+        r.setFrame(tightBoundsCache);\r
+        //System.out.println("  => " + tightBoundsCache);\r
+\r
+        return r;\r
+    }\r
+\r
+    /**\r
+     * @param lines\r
+     * @param bbox\r
+     *            the bounding box of all the whole laid out text (only bbox\r
+     *            size is used)\r
+     * @return\r
+     */\r
+    private Line[] layoutLinesX(Line[] lines, Rectangle2D bbox) {\r
+        int lineCount = lines.length;\r
+        for (int l = 0; l < lineCount; ++l) {\r
+            Line line = lines[l];\r
+            // Compute pen x position. If the paragraph is right-to-left we\r
+            // will align the TextLayouts to the right edge of the panel.\r
+            // Note: drawPosX is always where the LEFT of the text is placed.\r
+            // NOTE: This changes based on horizontal alignment\r
+            line.drawPosX = (float) (line.layout.isLeftToRight() ? 0f\r
+                    : tightBoundsCache.getWidth() - line.layout.getAdvance());\r
+        }\r
+        return lines;\r
+    }\r
+\r
+    /**\r
+     * @param lines\r
+     * @param boundsProvider\r
+     * @param result\r
+     * @return\r
+     */\r
+    private static Rectangle2D calculateBounds(Line[] lines, BoundsProcedure boundsProvider, Rectangle2D result) {\r
+        if (result == null)\r
+            result = new Rectangle2D.Double();\r
+        else\r
+            result.setFrame(0, 0, 0, 0);\r
+\r
+        for (Line line : lines) {\r
+            //System.out.println("line: " + line);\r
+            Rectangle2D bbox = boundsProvider.getBounds(line);\r
+            if (result.isEmpty())\r
+                result.setFrame(bbox);\r
+            else\r
+                Rectangle2D.union(result, bbox, result);\r
+            //System.out.println("bounds: " + result);\r
+        }\r
+        //System.out.println("final bounds: " + result);\r
+\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * @param lines\r
+     * @param bbox\r
+     * @param hAlign\r
+     * @param vAlign\r
+     * @return aligned lines\r
+     */\r
+    private Line[] alignLines(Line[] lines, Rectangle2D bbox, byte hAlign, byte vAlign) {\r
+//        System.out.println("horizontal align: " + Alignment.values()[hAlign]);\r
+//        System.out.println("vertical align  : " + Alignment.values()[vAlign]);\r
+//        System.out.println("bbox: " + bbox);\r
+        double xbase = 0;\r
+//        double ybase = 0;\r
+        if(targetBounds != null) {\r
+            /* In normal cases the bounding box moves when\r
+             * typing. If target bounds are set, the text\r
+             * is fitted into the box.\r
+             */\r
+            switch (hAlign) {\r
+            case 1: // Trailing\r
+                xbase = (targetBounds.getMaxX()-paddingX) * scaleRecip;\r
+                break;\r
+            case 2: // Center\r
+                xbase = targetBounds.getCenterX() * scaleRecip;\r
+                break;\r
+            default: // Leading / Baseline\r
+                // Do nothing\r
+                break;\r
+            }\r
+        }\r
+        \r
+        \r
+        for (Line line : lines) {\r
+            double xoffset = 0;\r
+            double yoffset = 0;\r
+\r
+            switch (hAlign) {\r
+            case 1: // Trailing\r
+                xoffset = xbase - line.bbox.getWidth();\r
+                break;\r
+            case 2: // Center\r
+                xoffset = xbase - line.bbox.getWidth() / 2;\r
+                break;\r
+            default: // Leading / Baseline\r
+                // Do nothing\r
+                break;\r
+            }\r
+\r
+            switch (vAlign) {\r
+            case 0:\r
+                yoffset = line.layout.getAscent();\r
+                break;\r
+            case 1:\r
+                yoffset = -bbox.getHeight() + line.layout.getAscent();\r
+                break;\r
+            case 2:\r
+                yoffset = -bbox.getHeight() / 2 + line.layout.getAscent();\r
+                break;\r
+            }\r
+\r
+            line.alignOffset(xoffset, yoffset);\r
+        }\r
+        return lines;\r
+    }\r
+\r
+    /**\r
+     * @param lines\r
+     * @param frc\r
+     * @return\r
+     */\r
+    private Line[] layoutLines(Line[] lines, FontRenderContext frc) {\r
+        TextLayout emptyRowLayout = null;\r
+        int lineCount = lines.length;\r
+        float y = 0;\r
+        for (int l = 0; l < lineCount; ++l) {\r
+            Line line = lines[l];\r
+            String lineText = line.getText();\r
+            // " " because TextLayout requires non-empty text and\r
+            // We don't want zero size for the text.\r
+            if (lineText.isEmpty()) {\r
+                lineText = " ";\r
+                if (emptyRowLayout == null)\r
+                    emptyRowLayout = new TextLayout(lineText, font, frc);\r
+                line.layout = emptyRowLayout;\r
+            } else {\r
+                line.layout = new TextLayout(lineText, font, frc);\r
+            }\r
+\r
+            //y += line.layout.getAscent();\r
+            line.drawPosY = y;\r
+            y += line.layout.getDescent() + line.layout.getLeading() + line.layout.getAscent();\r
+\r
+            Rectangle2D bbox = line.layout.getLogicalHighlightShape(0, lineText.length()).getBounds2D();\r
+            bbox.setFrame(bbox.getX(), bbox.getY() + line.drawPosY, bbox.getWidth(), bbox.getHeight());\r
+            line.bbox = bbox;\r
+        }\r
+\r
+        return lines;\r
+    }\r
+\r
+    /**\r
+     * Splits the specified string into {@link Line} structures, one for each\r
+     * line in the input text. The returned lines are only partially defined,\r
+     * waiting to be laid out (see\r
+     * {@link #layoutLines(Line[], FontRenderContext)})\r
+     * \r
+     * @param txt\r
+     *            input text\r
+     * @return parsed text lines as {@link Line} structures\r
+     * @see #layoutLines(Line[], FontRenderContext)\r
+     */\r
+    private static Line[] parseLines(String txt) {\r
+        int len = txt.length();\r
+        if (len == 0)\r
+            return new Line[] { new Line("", 0, 0) };\r
+\r
+        TIntArrayList lfpos = new TIntArrayList();\r
+        int pos = 0;\r
+        int lineCount = 1;\r
+        for (;pos < len; ++lineCount) {\r
+            int nextlf = txt.indexOf('\n', pos);\r
+            lfpos.add(nextlf != -1 ? nextlf : len);\r
+            if (nextlf == -1)\r
+                break;\r
+            pos = nextlf + 1;\r
+        }\r
+        Line[] lines = new Line[lineCount];\r
+        pos = 0;\r
+        for (int i = 0; i < lineCount-1; ++i) {\r
+            int lf = lfpos.getQuick(i);\r
+            int cr = (lf > 0) && (txt.charAt(lf - 1) == '\r') ? lf - 1 : lf;\r
+            lines[i] = new Line(txt, pos, cr);\r
+            pos = lf + 1;\r
+        }\r
+        lines[lineCount - 1] = new Line(txt, pos, len);\r
+\r
+        return lines;\r
+    }\r
+    \r
+    \r
+    private static Line[] wrapLines(String txt, Font font, float fixedWidth, FontRenderContext frc) {\r
+        if(txt == null || txt.isEmpty())\r
+            txt = " ";\r
+        \r
+        ArrayList<Line> lines = \r
+                new ArrayList<Line>();\r
+        \r
+        Hashtable<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();\r
+        map.put(TextAttribute.FONT, font);\r
+        AttributedString attributedText = new AttributedString(txt.isEmpty() ? "__ERROR__" : txt, map);\r
+\r
+        AttributedCharacterIterator paragraph = attributedText.getIterator();\r
+        int paragraphStart = paragraph.getBeginIndex();\r
+        int paragraphEnd = paragraph.getEndIndex();\r
+        LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc);\r
+\r
+        float breakWidth = fixedWidth;\r
+\r
+        // Force text to be vertical, by setting break width to 1, if the text area is narrower than "GGGG"\r
+\r
+        // Set position to the index of the first character in the paragraph.\r
+        lineMeasurer.setPosition(paragraphStart);\r
+\r
+        // Get lines until the entire paragraph has been displayed.\r
+        int next, limit, charat, position = 0;\r
+        \r
+        while ((position = lineMeasurer.getPosition()) < paragraphEnd) {\r
+\r
+            // Find possible line break and set it as a limit to the next layout\r
+            next = lineMeasurer.nextOffset(breakWidth);\r
+            limit = next;\r
+            charat = txt.indexOf(System.getProperty("line.separator"),position+1);\r
+            if(charat < next && charat != -1){\r
+                limit = charat;\r
+            }\r
+            \r
+            lineMeasurer.nextLayout(breakWidth, limit, false);\r
+            // Add Line\r
+            lines.add(new Line(txt, position, limit));\r
+        }\r
+\r
+        return lines.toArray(new Line[lines.size()]);\r
+    }\r
+    \r
+\r
+    public String getClipboardContent() {\r
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();\r
+        Transferable clipData = clipboard.getContents(this);\r
+        try {\r
+            return (String) (clipData.getTransferData(DataFlavor.stringFlavor));\r
+        } catch (Exception ee) {\r
+            return null;\r
+        }\r
+    }\r
+\r
+    public void setClipboardContent(String content) {\r
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();\r
+        StringSelection data = new StringSelection(content);\r
+        clipboard.setContents(data, data);\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return super.toString() + " [text=" + text + ", font=" + font + ", color=" + color + "]";\r
+    }\r
+\r
+    @Override\r
+    protected boolean handleCommand(CommandEvent e) {\r
+        if (!editing)\r
+            return false;\r
+\r
+        if (Commands.SELECT_ALL.equals(e.command)) {\r
+            selectAll();\r
+            return true;\r
+        }\r
+        return false;\r
+    }\r
+\r
+    @Override\r
+    protected boolean keyPressed(KeyPressedEvent event) {\r
+        if (!editing)\r
+            return false;\r
+\r
+        char c = event.character;\r
+        boolean ctrl = event.isControlDown();\r
+        boolean alt = event.isAltDown();\r
+\r
+//        System.out.println("Key pressed '" + (event.character == 0 ? "\\0" : "" + c) + "' " + event.keyCode + " - " + Integer.toBinaryString(event.stateMask));\r
+//        System.out.println("ctrl: " + ctrl);\r
+//        System.out.println("alt: " + alt);\r
+        if (ctrl && !alt) {\r
+            switch (event.keyCode) {\r
+                case KeyEvent.VK_C:\r
+                    if (caret != selectionTail) {\r
+                        int selectionMin = Math.min(caret, selectionTail);\r
+                        int selectionMax = Math.max(caret, selectionTail);\r
+                        setClipboardContent(text.substring(selectionMin, selectionMax));\r
+                    }\r
+                    break;\r
+                    \r
+                case KeyEvent.VK_X:\r
+                    if (caret != selectionTail) {\r
+                        int selectionMin = Math.min(caret, selectionTail);\r
+                        int selectionMax = Math.max(caret, selectionTail);\r
+                        setClipboardContent(text.substring(selectionMin, selectionMax));\r
+                        insert("");\r
+                    }\r
+                    break;\r
+\r
+                case KeyEvent.VK_RIGHT:\r
+                    if (c == '\0')  {\r
+                        // '\'' has the same keycode as VK_RIGHT but when right\r
+                        // arrow is pressed, event character is \0.\r
+                        moveCaretCtrlRight(event.isShiftDown());\r
+                    }\r
+                    break;\r
+                    \r
+                case KeyEvent.VK_LEFT:\r
+                        moveCaretCtrlLeft(event.isShiftDown());\r
+                        break;\r
+                    \r
+                case KeyEvent.VK_V:\r
+                {\r
+                    String content = getClipboardContent();\r
+                    if(content != null)\r
+                        insert(content);\r
+                    break;\r
+                }\r
+\r
+                // Replaced by #handleCommand\r
+//                case KeyEvent.VK_A:\r
+//                {\r
+//                    selectAll();\r
+//                    return true;\r
+//                }\r
+                \r
+                case KeyEvent.VK_ENTER:\r
+                {\r
+                    insert(getLineSeparator());\r
+                }\r
+                \r
+                break;\r
+\r
+                default:\r
+                    return false;\r
+            }\r
+        } else if (!ctrl && alt) {\r
+            return false;\r
+        } else {\r
+            switch (event.keyCode) {\r
+                case KeyEvent.VK_LEFT:\r
+                       moveCaret(-1, event.isShiftDown());\r
+                    break;\r
+                case KeyEvent.VK_RIGHT:\r
+                    if (c == '\0')  {\r
+                        // '\'' has the same keycode as VK_RIGHT but when right\r
+                        // arrow is pressed, event character is \0.\r
+                        moveCaret(1, event.isShiftDown());\r
+                        break;\r
+                    }\r
+                    // Intentional fallthrough to default case\r
+                case KeyEvent.VK_UP:\r
+                    moveCaretRowUp(event.isShiftDown());\r
+                    break;\r
+                case KeyEvent.VK_DOWN:\r
+                    moveCaretRowDown(event.isShiftDown());\r
+                    break;\r
+                case KeyEvent.VK_HOME:\r
+                    moveCaretHome(event.isShiftDown());\r
+                    break;\r
+                case KeyEvent.VK_END:\r
+                    moveCaretEnd(event.isShiftDown());\r
+                    break;\r
+\r
+                case KeyEvent.VK_ENTER:\r
+                    fireTextEditingEnded();\r
+                    return true;\r
+\r
+                case KeyEvent.VK_ESCAPE:\r
+                    text = textBeforeEdit;\r
+                    resetCaches();\r
+                    editing = false;\r
+                    fireTextEditingCancelled();\r
+                    return true;\r
+\r
+                case KeyEvent.VK_BACK_SPACE:\r
+                    if(caret == selectionTail && caret > 0) {\r
+                       // line separator may use multiple characters, we want to remove that with one command\r
+                       String lineSep = getLineSeparator();\r
+                       int index = lineSep.indexOf(text.charAt(caret-1));\r
+                       if (index == -1)\r
+                               --caret;\r
+                       else {\r
+                               caret-= (index+1);\r
+                               selectionTail+= (lineSep.length()-index-1);\r
+                       }\r
+                    }\r
+                    insert("");\r
+                    break;\r
+\r
+                case KeyEvent.VK_DELETE:\r
+                    if(caret == selectionTail && caret < text.length()) {\r
+                       String lineSep = getLineSeparator();\r
+                       int index = lineSep.indexOf(text.charAt(caret));\r
+                       if (index==-1)\r
+                               ++caret;\r
+                       else {\r
+                               selectionTail-= index;\r
+                               caret+= (lineSep.length()-index);\r
+                       }\r
+                    }\r
+                    insert("");\r
+                    break;\r
+\r
+                \r
+\r
+                default:\r
+                    if (c == 65535 || Character.getType(c) == Character.CONTROL) {\r
+                        return false;\r
+                    }\r
+                    //System.out.println("Char " + c + " " + Character.getType(c) + " " + text);\r
+                    insert(new String(new char[] {c}));\r
+            }\r
+        }\r
+\r
+        // FIXME This is called even if just caret was moved.\r
+        // This is currently necessary for repaints.\r
+        fireTextChanged();\r
+        invalidateXOffset();\r
+        return true;\r
+    }\r
+    \r
+    protected String getLineSeparator() {\r
+       return System.getProperty("line.separator");\r
+    }\r
+\r
+    protected void selectAll() {\r
+        setCaret(0, false);\r
+        setCaret(text.length(), true);\r
+    }\r
+\r
+    protected transient int hoverClick = 0;\r
+\r
+    @Override\r
+    protected boolean mouseClicked(MouseClickEvent event) {\r
+        if (event.button != MouseClickEvent.LEFT_BUTTON)\r
+            return false;\r
+        \r
+        if (hover) {\r
+               hoverClick++;\r
+               if (hoverClick < 2)\r
+                       return false;\r
+            ICanvasContext ctx = DiagramNodeUtil.getCanvasContext(this);\r
+            // FIXME: needed only because eventdelegator registrations are done before adding node to scene graph.\r
+            if (ctx == null)\r
+                return false;\r
+            IElement e = DiagramNodeUtil.getElement(ctx, this);\r
+            if (!editing) {\r
+               if (Boolean.TRUE.equals(setEditMode(true))) {\r
+                       editActivation = activateEdit(0, e, ctx);\r
+                       repaint();\r
+               }\r
+            } \r
+        } else {\r
+               hoverClick = 0;\r
+            if (editing) {\r
+                fireTextEditingEnded();\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+    \r
+    protected boolean mouseDoubleClicked(MouseDoubleClickedEvent event) {\r
+        if (event.button != MouseClickEvent.LEFT_BUTTON)\r
+            return false;\r
+        \r
+        if (hitTest(event, 0)) {\r
+            ICanvasContext ctx = DiagramNodeUtil.getCanvasContext(this);\r
+            // FIXME: needed only because eventdelegator registrations are done before adding node to scene graph.\r
+            if (ctx == null)\r
+                return false;\r
+            \r
+            if (text != null) {\r
+                // Select the whole text.\r
+                setCaret(0, false);\r
+                setCaret(text.length(), true);\r
+                repaint();\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+\r
+    @Override\r
+    protected boolean mouseButtonPressed(MouseButtonPressedEvent event) {\r
+        if (!editing)\r
+            return false;\r
+        \r
+        Point2D local = controlToLocal( event.controlPosition );\r
+        // FIXME: once the event coordinate systems are cleared up, remove this workaround\r
+        local = parentToLocal(local);\r
+        if (hover && this.containsLocal(local)) {\r
+            setCaret(local, event.isShiftDown());\r
+        }\r
+        return false;\r
+    }\r
+\r
+    @Override\r
+    protected boolean mouseMoved(MouseMovedEvent event) {\r
+        boolean hit = hitTest(event, 3.0);\r
+        if (hit != hover) {\r
+            hover = hit;\r
+            repaint();\r
+        }\r
+        return false;\r
+    }\r
+\r
+    private boolean isControlDown(MouseEvent e) {\r
+       return e.isControlDown() || lastMouseEvent!=null?lastMouseEvent.isControlDown():false;\r
+    }\r
+\r
+    protected boolean isShiftDown(MouseEvent e) {\r
+       return e.isShiftDown() || lastMouseEvent!=null?lastMouseEvent.isShiftDown():false;\r
+    }\r
+\r
+//    private boolean isAltDown(MouseEvent e) {\r
+//     return e.isAltDown() || lastMouseEvent!=null?lastMouseEvent.isAltDown():false;\r
+//    }\r
+\r
+    @Override\r
+    protected boolean mouseDragged(MouseDragBegin e) {\r
+        if (isHovering()\r
+                && (isControlDown(e) || isShiftDown(e))\r
+                && e.context instanceof NodeEventHandler\r
+                && dataRVI != null)\r
+        {\r
+            e.transferable = new LocalObjectTransferable(dataRVI);\r
+        }\r
+        return false;\r
+    }\r
+\r
+    protected boolean hitTest(MouseEvent event, double tolerance) {\r
+        Rectangle2D bounds = getBoundsInternal();\r
+        if (bounds == null)\r
+            return false;\r
+        Point2D localPos = NodeUtil.worldToLocal(this, event.controlPosition, new Point2D.Double());\r
+        double x = localPos.getX();\r
+        double y = localPos.getY();\r
+        boolean hit = bounds.contains(x, y);\r
+        return hit;\r
+    }\r
+\r
+    public Rectangle2D getBoundsInternal() {\r
+        Rectangle2D local = lastBounds;\r
+        if (local == null)\r
+            return null;\r
+        // TODO: potential spot for CPU/memory allocation optimization\r
+        // by using more specialized implementations\r
+        if (transform.isIdentity())\r
+            return local;\r
+        return transform.createTransformedShape(local).getBounds2D();\r
+    }\r
+\r
+    protected Color add(Color c, int r, int g, int b)  {\r
+        int nr = Math.min(255, c.getRed() + r);\r
+        int ng = Math.min(255, c.getGreen() + g);\r
+        int nb = Math.min(255, c.getBlue() + b);\r
+        return new Color(nr, ng, nb);\r
+    }\r
+\r
+    public TextEditActivation activateEdit(int mouseId, IElement e, ICanvasContext ctx) {\r
+        EditDataNode data = EditDataNode.getNode(this);\r
+        deactivateEdit(data, null);\r
+        TextEditActivation result = new TextEditActivation(mouseId, e, ctx);\r
+        data.setTextEditActivation(result);\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * @return <code>true</code> if this node is or was previously in editing\r
+     *         state\r
+     */\r
+    protected boolean deactivateEdit() {\r
+        boolean result = deactivateEdit( editActivation );\r
+        result |= editActivation != null;\r
+        editActivation = null;\r
+        return result;\r
+    }\r
+\r
+    protected boolean deactivateEdit(TextEditActivation activation) {\r
+        return deactivateEdit( EditDataNode.getNode(this), activation );\r
+    }\r
+\r
+    protected boolean deactivateEdit(EditDataNode data, TextEditActivation activation) {\r
+        TextEditActivation previous = data.getTextEditActivation();\r
+        if (previous != null && (previous == activation || activation == null)) {\r
+            previous.release();\r
+            data.setTextEditActivation(null);\r
+            return true;\r
+        }\r
+        return false;\r
+    }\r
+\r
+    @Override\r
+    public int getEventMask() {\r
+        return EventTypes.KeyPressedMask | EventTypes.MouseMovedMask | EventTypes.MouseButtonPressedMask\r
+                | EventTypes.MouseClickMask | EventTypes.CommandMask;\r
+    }\r
+\r
+    private MouseEvent lastMouseEvent = null;\r
+    \r
+    @Override\r
+    public boolean handleEvent(Event e) {\r
+       if(e instanceof MouseEvent && !(e instanceof MouseDragBegin)) lastMouseEvent = (MouseEvent)e;\r
+        return super.handleEvent(e);\r
+    }\r
+\r
+       @Override\r
+       public Function1<Object, Boolean> getPropertyFunction(String propertyName) {\r
+               return ScenegraphUtils.getMethodPropertyFunction(AWTThread.getThreadAccess(), this, propertyName);\r
+       }\r
+       \r
+       @Override\r
+       public <T> T getProperty(String propertyName) {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public void setPropertyCallback(Function2<String, Object, Boolean> callback) {\r
+       }\r
+       \r
+       public void synchronizeText(String text) {\r
+               setText(text);\r
+       }\r
+\r
+       public void synchronizeColor(RGB.Integer color) {\r
+               this.color = Colors.awt(color);\r
+       }\r
+\r
+       public void synchronizeFont(org.simantics.datatypes.literal.Font font) {\r
+               setFont(Fonts.awt(font));\r
+       }\r
+\r
+       public void synchronizeTransform(double[] data) {\r
+               this.setTransform(new AffineTransform(data));\r
+       }\r
+\r
+       public static void main(String[] args) {\r
+               Line[] lines = parseLines("\n  \n FOO  \n\nBAR\n\n\n BAZ\n\n");\r
+               System.out.println(Arrays.toString(lines));\r
+               System.out.println(GeometryUtils.pointToMillimeter(1));\r
+               System.out.println(GeometryUtils.pointToMillimeter(12));\r
+               System.out.println(GeometryUtils.pointToMillimeter(72));\r
+       }\r
+\r
+    ///////////////////////////////////////////////////////////////////////////\r
+    // LEGACY CODE NEEDED BY INHERITED CLASSES FOR NOW\r
+    ///////////////////////////////////////////////////////////////////////////\r
+\r
+    protected double getHorizontalAlignOffset(Rectangle2D r) {\r
+        switch (horizontalAlignment) {\r
+            case 0: return 0; // Leading\r
+            case 1: return -r.getWidth(); // Trailing\r
+            case 2: return -r.getCenterX(); // Center\r
+            default: return 0;\r
+        }\r
+    }\r
+\r
+    protected double getVerticalAlignOffset() {\r
+        FontMetrics fm = fontMetrics;\r
+        if (fm == null)\r
+            return 0;\r
+        switch (verticalAlignment) {\r
+            case 0: return fm.getMaxAscent(); // Leading=top=maxascent\r
+            case 1: return -fm.getMaxDescent(); // Trailing=bottom=maxdescent\r
+            case 2: return fm.getMaxAscent() / 2; // Center=maxascent / 2\r
+            case 3: return 0;\r
+            default: return 0;\r
+        }\r
+    }\r
+\r
+    ///////////////////////////////////////////////////////////////////////////\r
+    // LEGACY CODE ENDS\r
+    ///////////////////////////////////////////////////////////////////////////\r
+\r
+}\r