001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.properties.editors;
020:
021: import java.awt.Component;
022: import java.awt.Font;
023: import java.awt.FontMetrics;
024: import java.awt.Graphics;
025: import java.awt.Rectangle;
026:
027: import javax.swing.ImageIcon;
028:
029: public class ValuePainter {
030: /**
031: * The text to paint
032: */
033: private String m_value;
034:
035: /**
036: * The font to use
037: */
038: private Font m_font;
039:
040: private ImageIcon[] m_preimages;
041: private ImageIcon[] m_postimages;
042:
043: /**
044: * The component associated with this painter.
045: */
046: private Component m_comp;
047:
048: /**
049: * ctor
050: *
051: * @param comp
052: * the component associated with this painter.
053: */
054: public ValuePainter() {
055: this (null);
056: }
057:
058: /**
059: * ctor
060: */
061: public ValuePainter(String value) {
062: m_value = value;
063: m_font = javax.swing.UIManager.getFont("Table.font");
064: }
065:
066: /**
067: * Sets the images rendered to the left of the value
068: */
069: public void setPreImages(ImageIcon[] images) {
070: m_preimages = images;
071: }
072:
073: /**
074: * Sets the images rendered to the left of the value
075: */
076: public void setPreImages(ImageIcon image) {
077: ImageIcon[] images = new ImageIcon[] { image };
078: setPreImages(images);
079: }
080:
081: /**
082: * Sets the images rendered to the right of the value
083: */
084: public void setPostImages(ImageIcon[] images) {
085: m_postimages = images;
086: }
087:
088: /**
089: * Sets the images rendered to the right of the value
090: */
091: public void setPostImages(ImageIcon image) {
092: ImageIcon[] images = new ImageIcon[] { image };
093: setPostImages(images);
094: }
095:
096: /**
097: * Method that renders the text on the given graphics context
098: *
099: * @return the string width in pixels
100: */
101: public int drawString(Graphics g, Rectangle rect, int x,
102: String value) {
103: g.setFont(m_font);
104: FontMetrics fm = g.getFontMetrics();
105:
106: int line_height = fm.getHeight();
107: int y = rect.height - (rect.height - line_height) / 2
108: - fm.getDescent();
109:
110: if (value == null) {
111: g.drawString("null", x, y);
112: return fm.stringWidth("null");
113: } else {
114: g.drawString(value, x, y);
115: return fm.stringWidth(value);
116: }
117:
118: }
119:
120: /**
121: * Method that renders the text on the given graphics context
122: */
123: public void paintValue(Graphics g, Rectangle rect) {
124: assert (rect != null);
125:
126: int x = 0;
127: if (m_preimages != null) {
128: x += 4;
129: for (int index = 0; index < m_preimages.length; index++) {
130: ImageIcon icon = m_preimages[index];
131: int y = (rect.height - icon.getIconHeight()) / 2;
132: if (y < 0)
133: y = 0;
134:
135: icon.paintIcon(null, g, x, y);
136: x += icon.getIconWidth();
137: }
138: }
139:
140: x += 5;
141: x += drawString(g, rect, x, m_value);
142:
143: if (m_postimages != null) {
144: x += 5;
145: for (int index = 0; index < m_postimages.length; index++) {
146: ImageIcon icon = m_postimages[index];
147: int y = (rect.height - icon.getIconHeight()) / 2;
148: if (y < 0)
149: y = 0;
150:
151: icon.paintIcon(null, g, x, y);
152: x += icon.getIconWidth();
153: }
154: }
155: }
156:
157: /**
158: * Override setValue so we can nullify the descriptor for the font
159: */
160: public void setValue(Object value) {
161: if (value instanceof String) {
162: m_value = (String) value;
163: } else if (value == null) {
164: m_value = null;
165: } else {
166: m_value = value.toString();
167: }
168: }
169: }
|