KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > sharededitor > models > Text


1 /*
2 @COPYRIGHT@
3 */

4 package demo.sharededitor.models;
5
6 import java.awt.Color JavaDoc;
7 import java.awt.Font JavaDoc;
8 import java.awt.Graphics2D JavaDoc;
9 import java.awt.Image JavaDoc;
10 import java.awt.Rectangle JavaDoc;
11 import java.awt.Shape JavaDoc;
12 import java.awt.font.FontRenderContext JavaDoc;
13 import java.awt.font.TextAttribute JavaDoc;
14 import java.awt.font.TextLayout JavaDoc;
15 import java.awt.geom.AffineTransform JavaDoc;
16 import java.awt.geom.Rectangle2D JavaDoc;
17 import java.text.AttributedCharacterIterator JavaDoc;
18 import java.text.AttributedString JavaDoc;
19
20 import demo.sharededitor.ui.IFontable;
21 import demo.sharededitor.ui.ITexturable;
22
23 public class Text extends BaseObject implements IFontable, ITexturable
24 {
25     private Shape JavaDoc shape = null;
26
27     protected Shape JavaDoc getShape()
28     {
29         return shape;
30     }
31
32     protected Shape JavaDoc[] getAnchors()
33     {
34         return new Shape JavaDoc[0];
35     }
36
37     public boolean isAt(int x, int y)
38     {
39         if (shape == null)
40             return false;
41         
42         Shape JavaDoc bounds = new Rectangle2D.Double JavaDoc(shape.getBounds().x - 5,
43             shape.getBounds().y - 5,
44             shape.getBounds().width + 5,
45             shape.getBounds().height + 5);
46         return bounds.contains(x, y);
47     }
48
49     public synchronized void move(int dx, int dy)
50     {
51         this.x += dx;
52         this.y += dy;
53         notifyListeners(this);
54     }
55
56     public synchronized void resize(int x, int y)
57     {
58         // we purposely don't allow the resizing operation for Text objects
59
}
60
61     public synchronized void setTexture(Image image)
62     {
63         super.setTexture(image);
64         notifyListeners(this);
65     }
66
67     private String JavaDoc fontName;
68
69     private int fontSize;
70
71     public synchronized void setFontInfo(String JavaDoc name, int size, String JavaDoc text)
72     {
73         this.text = text;
74         this.fontName = name;
75         this.fontSize = size;
76         notifyListeners(this);
77     }
78
79     public synchronized void setFontName(String JavaDoc name)
80     {
81         this.fontName = name;
82         notifyListeners(this);
83     }
84
85     public synchronized void setFontSize(int size)
86     {
87         this.fontSize = size;
88         notifyListeners(this);
89     }
90
91     private String JavaDoc text;
92
93     public synchronized void setText(String JavaDoc text)
94     {
95         this.text = text;
96         notifyListeners(this);
97     }
98
99     public String JavaDoc getText()
100     {
101         return this.text;
102     }
103
104     public synchronized void appendToText(char c)
105     {
106        if (!this.isInverted)
107           this.text += c;
108        else
109        {
110           this.text = c + "";
111           this.isInverted = false;
112        }
113         notifyListeners(this);
114     }
115
116     private int x, y;
117
118     private synchronized void renderText(FontRenderContext JavaDoc frc, boolean showCursor)
119     {
120         String JavaDoc text = this.text;
121         if (showCursor || (text.length() == 0))
122         {
123            if (text.length() > 0) text = this.isInverted ? text : text + "|";
124            else text = "_";
125        }
126         
127         AttributedString JavaDoc as = new AttributedString JavaDoc(text);
128         as.addAttribute(TextAttribute.FONT, new Font JavaDoc(
129             this.fontName,
130             Font.PLAIN,
131             this.fontSize));
132         AttributedCharacterIterator JavaDoc aci = as.getIterator();
133         TextLayout JavaDoc tl = new TextLayout JavaDoc(aci, frc);
134         this.shape = tl.getOutline(AffineTransform.getTranslateInstance(x, y));
135     }
136
137     public void draw(Graphics2D JavaDoc g, boolean showAnchors)
138     {
139         renderText(g.getFontRenderContext(), showAnchors);
140         super.draw(g, showAnchors);
141         if (showAnchors)
142         {
143             Rectangle JavaDoc bounds = shape.getBounds();
144             Shape JavaDoc border = new Rectangle2D.Double JavaDoc(bounds.x - 5, bounds.y - 5, bounds.width + 5, bounds.height + 5);
145             if (this.isInverted)
146             {
147                g.setXORMode(Color.LIGHT_GRAY);
148             g.fillRect(bounds.x - 5, bounds.y - 5, bounds.width + 5, bounds.height + 5);
149                g.setPaintMode();
150            }
151             g.setColor(Color.LIGHT_GRAY);
152             g.draw(border);
153         }
154     }
155
156    private boolean isInverted;
157    
158     public synchronized void selectAction(boolean flag)
159     {
160        if (!this.isInverted)
161           return;
162        this.isInverted = false;
163     }
164     
165     public synchronized void alternateSelectAction(boolean flag)
166     {
167        if (this.isInverted)
168           return;
169        this.isInverted = true;
170     }
171
172     public Text()
173     {
174        this.isInverted = false;
175         this.text = "";
176     }
177 }
178
Popular Tags