001: package com.xoetrope.awt;
002:
003: import java.awt.Color;
004: import java.awt.Dimension;
005: import java.awt.Font;
006: import java.awt.FontMetrics;
007: import java.awt.Graphics;
008: import java.awt.geom.Rectangle2D;
009:
010: import net.xoetrope.awt.XLabel;
011: import net.xoetrope.xui.XProject;
012: import net.xoetrope.xui.XProjectManager;
013: import net.xoetrope.xui.style.XStyleFactory;
014:
015: /**
016: * A graphical button that is drawn with rounded edges.
017: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
018: * the GNU Public License (GPL), please see license.txt for more details. If
019: * you make commercial use of this software you must purchase a commercial
020: * license from Xoetrope.</p>
021: * $Revision: 1.3 $ not attributable
022: */
023: public class XGraphicButton extends XLabel {
024: /**
025: * Rounded edges, but no fill
026: */
027: public static final int LINE_STYLE = 0;
028:
029: /**
030: * Rounded edges and filled
031: */
032: public static final int SOLID_STYLE = 1;
033:
034: /**
035: * Rounded corner on the bottom right corner
036: */
037: public static final int BOTTOM_RIGHT_STYLE = 2;
038:
039: /**
040: * Rounded corner on the bottom left corner
041: */
042: public static final int BOTTOM_LEFT_STYLE = 3;
043:
044: private int drawStyle;
045: private boolean drawArrow = true;
046: private boolean center = false;
047: private XStyleFactory componentFactory;
048:
049: public XGraphicButton() {
050: XProject currentProject = XProjectManager.getCurrentProject();
051: componentFactory = new XStyleFactory(currentProject,
052: "net.xoetrope.awt");
053: componentFactory.setResourceBundle(currentProject
054: .getStartupParam("Language"));
055: }
056:
057: public XGraphicButton(String title) {
058: this (title, LINE_STYLE);
059: }
060:
061: public XGraphicButton(String title, int style) {
062: super ();
063: drawStyle = style;
064: setText(title);
065: }
066:
067: /**
068: * Set the button style
069: * @param style "0"=LINE_STYLE, "1"=SOLID_STYLE, "2"=BOTTOM_RIGHT_STYLE, "3"=BOTTOM_LEFT_STYLE
070: */
071: public void setDrawStyle(String style) {
072: drawStyle = Integer.parseInt(style);
073: }
074:
075: /**
076: * Set the button style
077: * @param style LINE_STYLE, SOLID_STYLE, BOTTOM_RIGHT_STYLE, BOTTOM_LEFT_STYLE
078: */
079: public void setDrawStyle(int style) {
080: drawStyle = style;
081: }
082:
083: /**
084: * Set the button caption
085: * @param title the text to display
086: */
087: public void setTitle(String title) {
088: setText(componentFactory.translate(title));
089: }
090:
091: /**
092: * Set the attributes of the button, see setDrawStyle
093: * @param attribName the attribute name
094: * @param attribValue the attribute value
095: */
096: public void setAttribute(String attribName, String attribValue) {
097: String attribNameLwr = attribName.toLowerCase();
098: String attribValueLwr = attribValue.toLowerCase();
099: if (attribNameLwr.compareTo("drawstyle") == 0)
100: setDrawStyle(attribValueLwr);
101: else
102: super .setAttribute(attribName, attribValue);
103: }
104:
105: public void paint(Graphics g) {
106: switch (drawStyle) {
107: case LINE_STYLE:
108: drawLineStyle(g);
109: break;
110: case SOLID_STYLE:
111: drawSolidStyle(g);
112: drawRightArrow(g);
113: break;
114: case BOTTOM_RIGHT_STYLE:
115: drawSolidStyle(g);
116: completeBotRightStyle(g);
117: drawRightArrow(g);
118: break;
119: case BOTTOM_LEFT_STYLE:
120: drawSolidStyle(g);
121: completeBotLeftStyle(g);
122: drawLeftArrow(g);
123: break;
124: }
125: drawText(g);
126: }
127:
128: private void drawText(Graphics g) {
129: Font f = new Font("Arial", Font.BOLD, 12);
130: g.setFont(f);
131:
132: if (drawStyle == BOTTOM_LEFT_STYLE) {
133: FontMetrics fm = g.getFontMetrics(f);
134: Rectangle2D rect = fm.getStringBounds(getText(), g);
135: g
136: .drawString(
137: getText(),
138: 34/*getSize().width - rect.getBounds().width - 8*/,
139: 14);
140: } else if (center) {
141: FontMetrics fm = g.getFontMetrics(f);
142: Rectangle2D rect = fm.getStringBounds(getText(), g);
143: int mid = getSize().width / 2;
144: mid = mid - (rect.getBounds().width / 2);
145: g.drawString(getText(), mid, 14);
146: } else {
147: g.drawString(getText(), 10, 14);
148: }
149: }
150:
151: private void drawLineStyle(Graphics g) {
152: Dimension size = getSize();
153: int w = size.width;
154: g.setColor(Color.red);
155: g.drawRoundRect(0, 0, w - 1, size.height - 1, 16, 16);
156: drawRightArrow(g);
157: g.setColor(Color.red);
158: }
159:
160: private void drawSolidStyle(Graphics g) {
161: Dimension size = getSize();
162: int w = size.width;
163: g.setColor(Color.red);
164: g.fillRoundRect(0, 0, w, size.height - 1, 16, 16);
165: g.setColor(Color.white);
166: }
167:
168: private void completeBotRightStyle(Graphics g) {
169: g.setColor(Color.red);
170: int w = getSize().width;
171: g.fillRect(0, 0, w, 10);
172: g.setColor(Color.white);
173: // drawRightArrow( g );
174: g.fillRect(w - 27, 0, 3, getSize().height);
175: }
176:
177: private void completeBotLeftStyle(Graphics g) {
178: g.setColor(Color.red);
179: int w = getSize().width;
180: g.fillRect(0, 0, w, 10);
181: g.setColor(Color.white);
182: // drawLeftArrow( g );
183: g.fillRect(27, 0, 3, getSize().height);
184: }
185:
186: private void drawRightArrow(Graphics g) {
187: if (!drawArrow)
188: return;
189:
190: int w = getSize().width;
191:
192: g.fillOval(w - 8, 9, 2, 2);
193: g.fillOval(w - 11, 9, 2, 2);
194: g.fillOval(w - 14, 9, 2, 2);
195: g.fillOval(w - 17, 9, 2, 2);
196: g.fillOval(w - 20, 9, 2, 2);
197:
198: g.fillOval(w - 12, 5, 2, 2);
199: g.fillOval(w - 10, 7, 2, 2);
200: g.fillOval(w - 10, 11, 2, 2);
201: g.fillOval(w - 12, 13, 2, 2);
202: }
203:
204: private void drawLeftArrow(Graphics g) {
205: if (!drawArrow)
206: return;
207:
208: int w = getSize().width;
209:
210: g.fillOval(8, 9, 2, 2);
211: g.fillOval(11, 9, 2, 2);
212: g.fillOval(14, 9, 2, 2);
213: g.fillOval(17, 9, 2, 2);
214: g.fillOval(20, 9, 2, 2);
215:
216: g.fillOval(12, 5, 2, 2);
217: g.fillOval(10, 7, 2, 2);
218: g.fillOval(10, 11, 2, 2);
219: g.fillOval(12, 13, 2, 2);
220: }
221:
222: public void update(Graphics g) {
223: paint(g);
224: }
225:
226: public void doLayout() {
227: super .doLayout();
228: repaint();
229: }
230:
231: /**
232: * Center the text
233: * @param c true to center the text
234: */
235: public void setCentered(boolean c) {
236: center = c;
237: }
238:
239: /**
240: * Turn on drawing of the arrow
241: * @param draw true to draw the arrow
242: */
243: public void setDrawArrow(boolean draw) {
244: drawArrow = draw;
245: }
246: }
|