1 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 ; 32 import java.awt.Dimension ; 33 import java.awt.Font ; 34 import java.awt.FontMetrics ; 35 import java.awt.Graphics ; 36 import java.awt.Point ; 37 import java.awt.Rectangle ; 38 import java.awt.Toolkit ; 39 import java.util.Vector ; 40 41 44 public class TextFigure 45 extends AttributeFigure 46 implements FigureChangeListener, TextHolder { 47 48 protected int fOriginX; 49 protected int fOriginY; 50 51 transient private boolean fSizeIsDirty = true; 53 transient private int fWidth; 54 transient private int fHeight; 55 56 private String fText; 57 private Font fFont; 58 private boolean fIsReadOnly; 59 60 private Figure fObservedFigure = null; 61 private OffsetLocator fLocator = null; 62 63 private static String fgCurrentFontName = "Helvetica"; 64 private static int fgCurrentFontSize = 12; 65 private static int fgCurrentFontStyle = Font.PLAIN; 66 67 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 newOrigin, Point newCorner) { 96 fOriginX = newOrigin.x; 97 fOriginY = newOrigin.y; 98 } 99 100 public Rectangle displayBox() { 101 Dimension extent = textExtent(); 102 return new Rectangle (fOriginX, fOriginY, extent.width, extent.height); 103 } 104 105 public Rectangle textDisplayBox() { 106 return displayBox(); 107 } 108 109 112 public boolean readOnly() { 113 return fIsReadOnly; 114 } 115 116 119 public void setReadOnly(boolean isReadOnly) { 120 fIsReadOnly = isReadOnly; 121 } 122 123 126 public Font getFont() { 127 return fFont; 128 } 129 130 133 public void setFont(Font newFont) { 134 willChange(); 135 fFont = newFont; 136 markDirty(); 137 changed(); 138 } 139 140 143 public void changed() { 144 super.changed(); 145 updateLocation(); 146 } 147 148 152 public Object getAttribute(String name) { 153 Font font = getFont(); 154 if (name.equals("FontSize")) { 155 return new Integer (font.getSize()); 156 } 157 if (name.equals("FontStyle")) { 158 return new Integer (font.getStyle()); 159 } 160 if (name.equals("FontName")) { 161 return font.getName(); 162 } 163 return super.getAttribute(name); 164 } 165 166 170 public void setAttribute(String name, Object value) { 171 Font font = getFont(); 172 if (name.equals("FontSize")) { 173 Integer s = (Integer )value; 174 setFont(new Font (font.getName(), font.getStyle(), s.intValue()) ); 175 } 176 else if (name.equals("FontStyle")) { 177 Integer s = (Integer )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 (font.getName(), style, font.getSize()) ); 186 } 187 else if (name.equals("FontName")) { 188 String n = (String )value; 189 setFont(new Font (n, font.getStyle(), font.getSize()) ); 190 } 191 else { 192 super.setAttribute(name, value); 193 } 194 } 195 196 199 public String getText() { 200 return fText; 201 } 202 203 206 public void setText(String newText) { 207 if (!newText.equals(fText)) { 208 willChange(); 209 fText = newText; 210 markDirty(); 211 changed(); 212 } 213 } 214 215 218 public boolean acceptsTyping() { 219 return !fIsReadOnly; 220 } 221 222 public void drawBackground(Graphics g) { 223 Rectangle r = displayBox(); 224 g.fillRect(r.x, r.y, r.width, r.height); 225 } 226 227 public void drawFrame(Graphics g) { 228 g.setFont(fFont); 229 g.setColor(getTextColor()); 230 FontMetrics metrics = g.getFontMetrics(fFont); 231 g.drawString(fText, fOriginX, fOriginY + metrics.getAscent()); 232 } 233 234 protected Color getTextColor() { 235 return (Color )getAttribute("TextColor"); 236 } 237 238 private Dimension textExtent() { 239 if (!fSizeIsDirty) { 240 return new Dimension (fWidth, fHeight); 241 } 242 FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(fFont); 243 fWidth = metrics.stringWidth(fText); 244 fHeight = metrics.getHeight(); 245 fSizeIsDirty = false; 246 return new Dimension (metrics.stringWidth(fText), metrics.getHeight()); 247 } 248 249 private void markDirty() { 250 fSizeIsDirty = true; 251 } 252 253 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 handles() { 266 Vector handles = new Vector (); 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 303 protected void updateLocation() { 304 if (fLocator != null) { 305 Point 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 326 public void disconnect(Figure disconnectFigure) { 327 if (disconnectFigure != null) { 328 disconnectFigure.removeFigureChangeListener(this); 329 } 330 fLocator = null; 331 } 332 333 334 337 static public Font createCurrentFont() { 338 return new Font (fgCurrentFontName, fgCurrentFontStyle, fgCurrentFontSize); 339 } 340 341 344 static public void setCurrentFontName(String name) { 345 fgCurrentFontName = name; 346 } 347 348 351 static public void setCurrentFontSize(int size) { 352 fgCurrentFontSize = size; 353 } 354 355 358 static public void setCurrentFontStyle(int style) { 359 fgCurrentFontStyle = style; 360 } 361 } 362 | Popular Tags |