001: /*
002: @COPYRIGHT@
003: */
004: package demo.sharededitor.models;
005:
006: import java.awt.Color;
007: import java.awt.Font;
008: import java.awt.Graphics2D;
009: import java.awt.Image;
010: import java.awt.Rectangle;
011: import java.awt.Shape;
012: import java.awt.font.FontRenderContext;
013: import java.awt.font.TextAttribute;
014: import java.awt.font.TextLayout;
015: import java.awt.geom.AffineTransform;
016: import java.awt.geom.Rectangle2D;
017: import java.text.AttributedCharacterIterator;
018: import java.text.AttributedString;
019:
020: import demo.sharededitor.ui.Fontable;
021: import demo.sharededitor.ui.Texturable;
022:
023: public class Text extends BaseObject implements Fontable, Texturable {
024: private Shape shape = null;
025:
026: protected Shape getShape() {
027: return shape;
028: }
029:
030: private Shape[] anchors = new Shape[0];
031:
032: protected Shape[] getAnchors() {
033: return anchors;
034: }
035:
036: public boolean isAt(int x, int y) {
037: if (shape == null)
038: return false;
039:
040: Shape bounds = new Rectangle2D.Double(shape.getBounds().x - 5,
041: shape.getBounds().y - 5, shape.getBounds().width + 5,
042: shape.getBounds().height + 5);
043: return bounds.contains(x, y);
044: }
045:
046: public synchronized void move(int dx, int dy) {
047: this .x += dx;
048: this .y += dy;
049: notifyListeners(this );
050: }
051:
052: public synchronized void resize(int x, int y) {
053: // we purposely don't allow the resizing operation for Text objects
054: }
055:
056: public synchronized void clearTexture() {
057: super .clearTexture();
058: }
059:
060: public synchronized void setTexture(Image image) {
061: super .setTexture(image);
062: notifyListeners(this );
063: }
064:
065: private String fontName;
066:
067: private int fontSize;
068:
069: public synchronized void setFontInfo(String name, int size,
070: String text) {
071: this .text = text;
072: this .fontName = name;
073: this .fontSize = size;
074: notifyListeners(this );
075: }
076:
077: public synchronized void setFontName(String name) {
078: this .fontName = name;
079: notifyListeners(this );
080: }
081:
082: public synchronized void setFontSize(int size) {
083: this .fontSize = size;
084: notifyListeners(this );
085: }
086:
087: private String text;
088:
089: public synchronized void setText(String text) {
090: this .text = text;
091: notifyListeners(this );
092: }
093:
094: public String getText() {
095: return this .text;
096: }
097:
098: public synchronized void appendToText(char c) {
099: if (!this .isInverted)
100: this .text += c;
101: else {
102: this .text = c + "";
103: this .isInverted = false;
104: }
105: notifyListeners(this );
106: }
107:
108: private int x, y;
109:
110: private synchronized void renderText(FontRenderContext frc,
111: boolean showCursor) {
112: String text = this .text;
113: if (showCursor || (text.length() == 0)) {
114: if (text.length() > 0)
115: text = this .isInverted ? text : text + "|";
116: else
117: text = "_";
118: }
119:
120: AttributedString as = new AttributedString(text);
121: as.addAttribute(TextAttribute.FONT, new Font(this .fontName,
122: Font.PLAIN, this .fontSize));
123: AttributedCharacterIterator aci = as.getIterator();
124: TextLayout tl = new TextLayout(aci, frc);
125: this .shape = tl.getOutline(AffineTransform
126: .getTranslateInstance(x, y));
127: }
128:
129: public void draw(Graphics2D g, boolean showAnchors) {
130: renderText(g.getFontRenderContext(), showAnchors);
131: super .draw(g, showAnchors);
132: if (showAnchors) {
133: Rectangle bounds = shape.getBounds();
134: Shape border = new Rectangle2D.Double(bounds.x - 5,
135: bounds.y - 5, bounds.width + 5, bounds.height + 5);
136: if (this .isInverted) {
137: g.setXORMode(Color.LIGHT_GRAY);
138: g.fillRect(bounds.x - 5, bounds.y - 5,
139: bounds.width + 5, bounds.height + 5);
140: g.setPaintMode();
141: }
142: g.setColor(Color.LIGHT_GRAY);
143: g.draw(border);
144: }
145: }
146:
147: private boolean isInverted;
148:
149: public synchronized void selectAction(boolean flag) {
150: if (!this .isInverted)
151: return;
152: this .isInverted = false;
153: }
154:
155: public synchronized void alternateSelectAction(boolean flag) {
156: if (this .isInverted)
157: return;
158: this .isInverted = true;
159: }
160:
161: public Text() {
162: this .isInverted = false;
163: this .text = "";
164: }
165: }
|