]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/TextNode.java
G2DParentNode handles "undefined" child bounds separately
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / TextNode.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.scenegraph.g2d.nodes;
13
14 import java.awt.Color;
15 import java.awt.Font;
16 import java.awt.FontMetrics;
17 import java.awt.Graphics2D;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.Point2D;
20 import java.awt.geom.Rectangle2D;
21 import java.io.IOException;
22
23 import org.simantics.scenegraph.ExportableWidget.OutputWidget;
24 import org.simantics.scenegraph.g2d.G2DNode;
25 import org.simantics.scenegraph.g2d.G2DPDFRenderingHints;
26
27 import com.lowagie.text.DocumentException;
28 import com.lowagie.text.Element;
29 import com.lowagie.text.Rectangle;
30 import com.lowagie.text.pdf.FontMapper;
31 import com.lowagie.text.pdf.PdfFormField;
32 import com.lowagie.text.pdf.PdfWriter;
33 import com.lowagie.text.pdf.TextField;
34
35 @OutputWidget("text")
36 public class TextNode extends G2DNode {
37
38     private static final long serialVersionUID = 8508750881358776559L;
39
40     protected Rectangle2D bounds = null;
41     protected String      text   = null;
42     protected Font        font   = null;
43     protected Color       color  = Color.BLACK;
44
45     @SyncField("bounds")
46     public void setBounds(Rectangle2D bounds) {
47         this.bounds = bounds;
48     }
49
50     @SyncField("font")
51     public void setFont(Font font) {
52         this.font = font;
53     }
54     
55     @SyncField("text")
56     public void setText(String text) {
57         this.text = text;
58     }
59     
60     public String getText() {
61         return this.text;
62     }
63
64     @SyncField("color")
65     public void setColor(Color color) {
66         this.color = color;
67     }
68
69     @Override
70     public void render(Graphics2D g) {
71         if (text == null || bounds == null || font == null || color == null)
72             return;
73         
74 //        PdfWriter writer = (PdfWriter) g.getRenderingHint(G2DPDFRenderingHints.KEY_PDF_WRITER);
75 //        if ( writer == null ) {
76                 // Write to Graphics2D
77                 g.setFont(font);
78                 g.setColor(color);
79         
80                 FontMetrics fm =        g.getFontMetrics(font);
81                 Rectangle2D rect =      fm.getStringBounds(text, g);
82                 float x = (float) ((bounds.getWidth()  - rect.getWidth())/2 + bounds.getMinX());
83                 float y = (float) ((bounds.getHeight() - rect.getHeight())/2 + bounds.getMinY());
84                 
85                 AffineTransform at = g.getTransform();
86                 g.transform(getTransform());
87                 g.drawString(text, x, (float)(y+rect.getHeight()));
88                 g.setTransform(at);
89 //        } else {
90                 /*
91                         try {
92                         // Write to PDF
93                     FontMapper mapper = (FontMapper) g.getRenderingHint(G2DPDFRenderingHints.KEY_PDF_FONTMAPPER);
94                     //text.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
95                     AffineTransform at = g.getTransform();
96                         FontMetrics fm =        g.getFontMetrics(font);
97                         Rectangle2D rect =      fm.getStringBounds(text, g);
98                         float x = (float) ((bounds.getWidth()  - rect.getWidth())/2 + bounds.getMinX());
99                         float y = (float) ((bounds.getHeight() - rect.getHeight())/2 + bounds.getMinY());
100                         
101                     Point2D pt1 = at.transform(new Point2D.Float(x, y+(float)rect.getHeight()), null);
102                     Point2D pt2 = at.transform(new Point2D.Float(x+(float)rect.getWidth(), y), null);
103                         Rectangle rectangle = new Rectangle(
104                                         (float) pt1.getX(), 
105                                         (float) pt1.getY(), 
106                                         (float) pt2.getX(), 
107                                         (float) pt2.getY()); 
108                     TextField text = new TextField(writer, rectangle, "approvedBy");
109                     text.setText(this.text);
110                     text.setFont(mapper.awtToPdf(font));
111                     text.setFontSize(0);
112                     text.setAlignment(Element.ALIGN_LEFT);
113                     text.setRotation(90);
114                     text.setOptions(TextField.READ_ONLY);
115                     PdfFormField field = text.getTextField();
116                     writer.addAnnotation(field);
117                         } catch (IOException e) {
118                                 e.printStackTrace();
119                         } catch (DocumentException e) {
120                                 e.printStackTrace();
121                         }
122                 */
123 //        }
124     }
125
126 //    @Override
127 //    public Rectangle2D getBoundsInLocal() {
128 //        return bounds;
129 //    }
130     
131     @Override
132     public Rectangle2D getBoundsInLocal() {
133         return bounds;
134     }
135     
136     @Override
137     public Rectangle2D getBounds() {
138         return null;
139     }
140     
141 }