001: /* ====================================================================
002: * The JRefactory License, Version 1.0
003: *
004: * Copyright (c) 2001 JRefactory. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by the
021: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "JRefactory" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please contact seguin@acm.org.
028: *
029: * 5. Products derived from this software may not be called "JRefactory",
030: * nor may "JRefactory" appear in their name, without prior written
031: * permission of Chris Seguin.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of JRefactory. For more information on
049: * JRefactory, please see
050: * <http://www.sourceforge.org/projects/jrefactory>.
051: */
052: package org.acm.seguin.uml.line;
053:
054: import java.awt.Color;
055: import java.awt.Component;
056: import java.awt.Dimension;
057: import java.awt.Font;
058: import java.awt.FontMetrics;
059: import java.awt.Graphics;
060: import java.awt.Graphics2D;
061: import java.awt.Point;
062: import java.awt.Rectangle;
063: import java.awt.Image;
064: import java.awt.image.BufferedImage;
065: import javax.swing.JLabel;
066: import javax.swing.JPanel;
067:
068: /**
069: * Creates a label that can be resized by setting it's scaling factor.
070: *
071: *@author Chris Seguin
072: *@created September 12, 2001
073: */
074: public class SizableLabel extends ScalablePanel {
075: private boolean bufferCreated = false;
076: private Image scaledImage;
077: private Color foreground;
078: private Color background;
079: private Font font;
080: private int height = 200;
081: private int width = 25;
082: private String text;
083: private int align;
084: private int ascent;
085:
086: private static Font defaultFont = null;
087: private static Color defaultColor = null;
088:
089: /**
090: * Constructor for the SizableLabel object
091: */
092: public SizableLabel() {
093: this (" ");
094: }
095:
096: /**
097: * Constructor for the SizableLabel object
098: *
099: *@param init Description of Parameter
100: */
101: public SizableLabel(String init) {
102: if (defaultFont == null) {
103: SizableLabel.init();
104: }
105:
106: font = defaultFont;
107: foreground = defaultColor;
108:
109: setText(init);
110: determineSize();
111: setSize(getPreferredSize());
112:
113: setDoubleBuffered(false);
114: }
115:
116: /**
117: * Sets the Foreground attribute of the SizableLabel object
118: *
119: *@param value The new Foreground value
120: */
121: public void setSLForeground(Color value) {
122: foreground = value;
123: bufferCreated = false;
124: }
125:
126: /**
127: * Sets the Font attribute of the SizableLabel object
128: *
129: *@param value The new Font value
130: */
131: public void setSLFont(Font value) {
132: font = value;
133: determineSize();
134: setSize(getPreferredSize());
135: }
136:
137: /**
138: * Sets the Text attribute of the SizableLabel object
139: *
140: *@param value The new Text value
141: */
142: public void setText(String value) {
143: if ((value == null) || (value == "")) {
144: text = " ";
145: } else {
146: text = value;
147: }
148: bufferCreated = false;
149: determineSize();
150: setSize(getPreferredSize());
151: }
152:
153: /**
154: * Sets the HorizontalAlignment attribute of the SizableLabel object
155: *
156: *@param value The new HorizontalAlignment value
157: */
158: public void setSLHorizontalAlignment(int value) {
159: align = value;
160: }
161:
162: /**
163: * Gets the Text attribute of the SizableLabel object
164: *
165: *@return The Text value
166: */
167: public String getText() {
168: return text;
169: }
170:
171: /**
172: * Gets the PreferredSize attribute of the SizableLabel object
173: *
174: *@return The PreferredSize value
175: */
176: public Dimension getPreferredSize() {
177: Dimension dim = new Dimension();
178: dim.width = scaleInteger(width);
179: dim.height = scaleInteger(height);
180:
181: bufferCreated = false;
182:
183: return dim;
184: }
185:
186: /**
187: * Paints the sizable label
188: *
189: *@param g the graphics area to print in
190: */
191: public void paint(Graphics g) {
192: print(g, 0, 0);
193: }
194:
195: /**
196: * Prints the label at the proper location
197: *
198: *@param g the graphics object
199: *@param x the x coordinate
200: *@param y the y coordinate
201: */
202: public void print(Graphics g, int x, int y) {
203: if (!bufferCreated || !getParentBackground().equals(background)) {
204: createBuffer();
205: }
206: x = unscaleInteger(x);
207: y = unscaleInteger(y);
208: Dimension dim = getSize();
209: //g.setColor(background);
210: //g.fillRect(x, y, dim.width, dim.height);
211: if (align == JLabel.LEFT) {
212: //g.drawImage(scaledImage, x, y, this);
213: } else if (align == JLabel.RIGHT) {
214: int deltax = (int) (dim.width - scaleInteger(width));
215: //g.drawImage(scaledImage, x + deltax, y, this);
216: x += deltax;
217: } else if (align == JLabel.CENTER) {
218: int deltax = (int) ((dim.width - scaleInteger(width)) * 0.5);
219: //g.drawImage(scaledImage, x + deltax, y, this);
220: x += deltax;
221: }
222: determineSize();
223: dim = new Dimension(width, height);
224:
225: Graphics2D graphics = (Graphics2D) g;
226: java.awt.geom.AffineTransform transform = graphics
227: .getTransform();
228: graphics.scale(getScale(), getScale());
229:
230: //int tempWidth = Math.max(1, scaleInteger(width + 10));
231: //int tempHeight = Math.max(1, scaleInteger(height + 10));
232: //background = getParentBackground();
233: //g.setColor(background);
234: //g.fillRect(0, 0, (int)(1+tempWidth/getScale()), (int)(1+tempHeight/getScale()));
235:
236: // Draw the text
237: g.setColor(foreground);
238: g.setFont(font);
239: g.drawString(text, x, y + ascent);
240: graphics.setTransform(transform);
241: }
242:
243: /**
244: * Scales the image
245: *
246: *@param value the amount to scale
247: */
248: public void scale(double value) {
249: if (Math.abs(getScale() - value) > 0.001) {
250: super .scale(value);
251: bufferCreated = false;
252: }
253: //setSize(getPreferredSize());
254: }
255:
256: /**
257: * Gets the ParentBackground attribute of the SizableLabel object
258: *
259: *@return The ParentBackground value
260: */
261: private Color getParentBackground() {
262: Component parent = getParent();
263: if (parent == null) {
264: return Color.gray;
265: } else {
266: return parent.getBackground();
267: }
268: }
269:
270: /**
271: * Creates the buffer from which to draw the resized text
272: */
273: private void createBuffer() {
274: bufferCreated = true;
275: determineSize();
276: Dimension dim = new Dimension(width, height);
277:
278: int tempWidth = Math.max(1, scaleInteger(width + 10));
279: int tempHeight = Math.max(1, scaleInteger(height + 10));
280: scaledImage = new BufferedImage(tempWidth, tempHeight,
281: BufferedImage.TYPE_INT_RGB);
282: Graphics2D g = (Graphics2D) scaledImage.getGraphics();
283: g.scale(getScale(), getScale());
284:
285: // Fill in the background
286: background = getParentBackground();
287: g.setColor(background);
288: g.fillRect(0, 0, (int) (1 + tempWidth / getScale()),
289: (int) (1 + tempHeight / getScale()));
290:
291: // Draw the text
292: g.setColor(foreground);
293: g.setFont(font);
294: g.drawString(text, 0, ascent);
295: }
296:
297: /**
298: * Description of the Method
299: */
300: private void determineSize() {
301: TextInfo ti = LabelSizeComputation.get().compute(text, font);
302:
303: height = ti.height;
304: width = ti.width;
305: ascent = ti.ascent;
306: }
307:
308: /**
309: * Prints some debug information about this label
310: */
311: private void debug() {
312: System.out.println("Label: " + text);
313: System.out.println(" Scale: " + getScale());
314: System.out.println(" Height: " + height);
315: System.out.println(" Width: " + width);
316: System.out.println(" Align: " + align);
317: System.out.println(" Color: " + foreground);
318: System.out.println(" Shape: " + getBounds());
319: }
320:
321: /**
322: * Initializes the static font and color
323: */
324: private static synchronized void init() {
325: if (defaultFont == null) {
326: defaultFont = new Font("Serif", Font.PLAIN, 12);
327: defaultColor = new Color(0, 80, 180);
328: }
329: }
330: }
|