KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > diagrams > TextFigure


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 */

18
19 package org.objectweb.jac.ide.diagrams;
20
21 import CH.ifa.draw.figures.AttributeFigure;
22 import CH.ifa.draw.figures.FontSizeHandle;
23 import CH.ifa.draw.framework.Figure;
24 import CH.ifa.draw.framework.FigureChangeEvent;
25 import CH.ifa.draw.framework.FigureChangeListener;
26 import CH.ifa.draw.standard.NullHandle;
27 import CH.ifa.draw.standard.OffsetLocator;
28 import CH.ifa.draw.standard.RelativeLocator;
29 import CH.ifa.draw.standard.TextHolder;
30 import CH.ifa.draw.util.ColorMap;
31 import java.awt.Color JavaDoc;
32 import java.awt.Dimension JavaDoc;
33 import java.awt.Font JavaDoc;
34 import java.awt.FontMetrics JavaDoc;
35 import java.awt.Graphics JavaDoc;
36 import java.awt.Point JavaDoc;
37 import java.awt.Rectangle JavaDoc;
38 import java.awt.Toolkit JavaDoc;
39 import java.util.Vector JavaDoc;
40
41 /**
42  * A TextFigure whose text color can be redefined with the method <code>getTextColor()</code>
43  */

44 public class TextFigure
45     extends AttributeFigure
46     implements FigureChangeListener, TextHolder {
47
48     protected int fOriginX;
49     protected int fOriginY;
50
51     // cache of the TextFigure's size
52
transient private boolean fSizeIsDirty = true;
53     transient private int fWidth;
54     transient private int fHeight;
55
56     private String JavaDoc fText;
57     private Font JavaDoc fFont;
58     private boolean fIsReadOnly;
59
60     private Figure fObservedFigure = null;
61     private OffsetLocator fLocator = null;
62
63     private static String JavaDoc fgCurrentFontName = "Helvetica";
64     private static int fgCurrentFontSize = 12;
65     private static int fgCurrentFontStyle = Font.PLAIN;
66
67     /*
68      * Serialization support.
69      */

70     private static final long serialVersionUID = 4599820785949456124L;
71
72     public TextFigure() {
73         fOriginX = 0;
74         fOriginY = 0;
75         fFont = createCurrentFont();
76         setAttribute("FillColor", ColorMap.color("None"));
77         fText = "";
78         fSizeIsDirty = true;
79     }
80
81     public void moveBy(int x, int y) {
82         willChange();
83         basicMoveBy(x, y);
84         if (fLocator != null) {
85             fLocator.moveBy(x, y);
86         }
87         changed();
88     }
89
90     protected void basicMoveBy(int x, int y) {
91         fOriginX += x;
92         fOriginY += y;
93     }
94
95     public void basicDisplayBox(Point JavaDoc newOrigin, Point JavaDoc newCorner) {
96         fOriginX = newOrigin.x;
97         fOriginY = newOrigin.y;
98     }
99
100     public Rectangle JavaDoc displayBox() {
101         Dimension JavaDoc extent = textExtent();
102         return new Rectangle JavaDoc(fOriginX, fOriginY, extent.width, extent.height);
103     }
104
105     public Rectangle JavaDoc textDisplayBox() {
106         return displayBox();
107     }
108
109     /**
110      * Tests whether this figure is read only.
111      */

112     public boolean readOnly() {
113         return fIsReadOnly;
114     }
115
116     /**
117      * Sets the read only status of the text figure.
118      */

119     public void setReadOnly(boolean isReadOnly) {
120         fIsReadOnly = isReadOnly;
121     }
122
123     /**
124      * Gets the font.
125      */

126     public Font JavaDoc getFont() {
127         return fFont;
128     }
129
130     /**
131      * Sets the font.
132      */

133     public void setFont(Font JavaDoc newFont) {
134         willChange();
135         fFont = newFont;
136         markDirty();
137         changed();
138     }
139
140     /**
141      * Updates the location whenever the figure changes itself.
142      */

143     public void changed() {
144         super.changed();
145         updateLocation();
146     }
147
148     /**
149      * A text figure understands the "FontSize", "FontStyle", and "FontName"
150      * attributes.
151      */

152     public Object JavaDoc getAttribute(String JavaDoc name) {
153         Font JavaDoc font = getFont();
154         if (name.equals("FontSize")) {
155             return new Integer JavaDoc(font.getSize());
156         }
157         if (name.equals("FontStyle")) {
158             return new Integer JavaDoc(font.getStyle());
159         }
160         if (name.equals("FontName")) {
161             return font.getName();
162         }
163         return super.getAttribute(name);
164     }
165
166     /**
167      * A text figure understands the "FontSize", "FontStyle", and "FontName"
168      * attributes.
169      */

170     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
171         Font JavaDoc font = getFont();
172         if (name.equals("FontSize")) {
173             Integer JavaDoc s = (Integer JavaDoc)value;
174             setFont(new Font JavaDoc(font.getName(), font.getStyle(), s.intValue()) );
175         }
176         else if (name.equals("FontStyle")) {
177             Integer JavaDoc s = (Integer JavaDoc)value;
178             int style = font.getStyle();
179             if (s.intValue() == Font.PLAIN) {
180                 style = Font.PLAIN;
181             }
182             else {
183                 style = style ^ s.intValue();
184             }
185             setFont(new Font JavaDoc(font.getName(), style, font.getSize()) );
186         }
187         else if (name.equals("FontName")) {
188             String JavaDoc n = (String JavaDoc)value;
189             setFont(new Font JavaDoc(n, font.getStyle(), font.getSize()) );
190         }
191         else {
192             super.setAttribute(name, value);
193         }
194     }
195
196     /**
197      * Gets the text shown by the text figure.
198      */

199     public String JavaDoc getText() {
200         return fText;
201     }
202
203     /**
204      * Sets the text shown by the text figure.
205      */

206     public void setText(String JavaDoc newText) {
207         if (!newText.equals(fText)) {
208             willChange();
209             fText = newText;
210             markDirty();
211             changed();
212         }
213     }
214
215     /**
216      * Tests whether the figure accepts typing.
217      */

218     public boolean acceptsTyping() {
219         return !fIsReadOnly;
220     }
221
222     public void drawBackground(Graphics JavaDoc g) {
223         Rectangle JavaDoc r = displayBox();
224         g.fillRect(r.x, r.y, r.width, r.height);
225     }
226
227     public void drawFrame(Graphics JavaDoc g) {
228         g.setFont(fFont);
229         g.setColor(getTextColor());
230         FontMetrics JavaDoc metrics = g.getFontMetrics(fFont);
231         g.drawString(fText, fOriginX, fOriginY + metrics.getAscent());
232     }
233
234     protected Color JavaDoc getTextColor() {
235         return (Color JavaDoc)getAttribute("TextColor");
236     }
237
238     private Dimension JavaDoc textExtent() {
239         if (!fSizeIsDirty) {
240             return new Dimension JavaDoc(fWidth, fHeight);
241         }
242         FontMetrics JavaDoc metrics = Toolkit.getDefaultToolkit().getFontMetrics(fFont);
243         fWidth = metrics.stringWidth(fText);
244         fHeight = metrics.getHeight();
245         fSizeIsDirty = false;
246         return new Dimension JavaDoc(metrics.stringWidth(fText), metrics.getHeight());
247     }
248
249     private void markDirty() {
250         fSizeIsDirty = true;
251     }
252
253     /**
254      * Gets the number of columns to be overlaid when the figure is edited.
255      */

256     public int overlayColumns() {
257         int length = getText().length();
258         int columns = 20;
259         if (length != 0) {
260             columns = getText().length()+ 3;
261         }
262         return columns;
263     }
264
265     public Vector JavaDoc handles() {
266         Vector JavaDoc handles = new Vector JavaDoc();
267         handles.addElement(new NullHandle(this, RelativeLocator.northWest()));
268         handles.addElement(new NullHandle(this, RelativeLocator.northEast()));
269         handles.addElement(new NullHandle(this, RelativeLocator.southEast()));
270         handles.addElement(new FontSizeHandle(this, RelativeLocator.southWest()));
271         return handles;
272     }
273
274     public void connect(Figure figure) {
275         if (fObservedFigure != null) {
276             fObservedFigure.removeFigureChangeListener(this);
277         }
278
279         fObservedFigure = figure;
280         fLocator = new OffsetLocator(figure.connectedTextLocator(this));
281         fObservedFigure.addFigureChangeListener(this);
282         updateLocation();
283     }
284
285     public void figureChanged(FigureChangeEvent e) {
286         updateLocation();
287     }
288
289     public void figureRemoved(FigureChangeEvent e) {
290         if (listener() != null) {
291             listener().figureRequestRemove(new FigureChangeEvent(this));
292         }
293     }
294
295     public void figureRequestRemove(FigureChangeEvent e) {}
296     public void figureInvalidated(FigureChangeEvent e) {}
297     public void figureRequestUpdate(FigureChangeEvent e) {}
298
299     /**
300      * Updates the location relative to the connected figure.
301      * The TextFigure is centered around the located point.
302      */

303     protected void updateLocation() {
304         if (fLocator != null) {
305             Point JavaDoc p = fLocator.locate(fObservedFigure,this);
306
307             p.x -= size().width/2 + fOriginX;
308             p.y -= size().height/2 + fOriginY;
309             if (p.x != 0 || p.y != 0) {
310                 willChange();
311                 basicMoveBy(p.x, p.y);
312                 changed();
313             }
314         }
315     }
316
317     public void release() {
318         super.release();
319         disconnect(fObservedFigure);
320         fObservedFigure = null;
321     }
322
323     /**
324      * Disconnects a text holder from a connect figure.
325      */

326     public void disconnect(Figure disconnectFigure) {
327         if (disconnectFigure != null) {
328             disconnectFigure.removeFigureChangeListener(this);
329         }
330         fLocator = null;
331     }
332
333
334     /**
335      * Creates the current font to be used for new text figures.
336      */

337     static public Font JavaDoc createCurrentFont() {
338         return new Font JavaDoc(fgCurrentFontName, fgCurrentFontStyle, fgCurrentFontSize);
339     }
340
341     /**
342      * Sets the current font name
343      */

344     static public void setCurrentFontName(String JavaDoc name) {
345         fgCurrentFontName = name;
346     }
347
348     /**
349      * Sets the current font size.
350      */

351     static public void setCurrentFontSize(int size) {
352         fgCurrentFontSize = size;
353     }
354
355     /**
356      * Sets the current font style.
357      */

358     static public void setCurrentFontStyle(int style) {
359         fgCurrentFontStyle = style;
360     }
361 }
362
Popular Tags