001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings;
022:
023: import java.awt.Dimension;
024: import java.awt.Graphics;
025: import java.awt.Insets;
026: import java.awt.Rectangle;
027:
028: /**
029: * This class visually displays the progress of some task.
030: * <br><br>
031: * <b>This class is thread safe.</b>
032: **/
033: public class WingProgress extends WingComponent {
034: private/*final*/int orientation;
035: protected Style stBarNormal;
036: protected Style stBarDisabled;
037:
038: protected int min;
039: protected int max;
040: protected int value;
041: protected String text;
042:
043: public void loadSkin() {
044: String idt = WingSkin.catKeys(styleId, WingSkin.catKeys(
045: (orientation == HORIZONTAL) ? "h" : "v", "progress"));
046:
047: stNormal = WingSkin.getStyle(idt, null, NORMAL, null);
048: stDisabled = WingSkin.getStyle(idt, null, DISABLED, stNormal);
049: stBarNormal = WingSkin.getStyle(idt, "bar", NORMAL, null);
050: stBarDisabled = WingSkin.getStyle(idt, "bar", DISABLED,
051: stBarNormal);
052: }
053:
054: public WingProgress(int orientation) {
055: this .orientation = orientation;
056: setValues(0, 0, 100);
057: }
058:
059: public synchronized void setValues(int value, int min, int max) {
060: if (max <= min)
061: max = min + 1;
062: this .min = min;
063: this .max = max;
064: setValue(value);
065: revalidateAndRepaint();
066: }
067:
068: public void setText(String text) {
069: if (text == null) {
070: if (this .text == null)
071: return;
072: } else if (text.equals(this .text))
073: return;
074: this .text = text;
075: repaint();
076: }
077:
078: public synchronized void setValue(int value) {
079: if (value < min)
080: value = min;
081: if (value > max)
082: value = max;
083: if (value == this .value)
084: return;
085: this .value = value;
086: repaint();
087: }
088:
089: public Dimension getPreferredSize() {
090: Dimension prefSize = wingPrefSize;
091: if (prefSize == null) {
092: int range = max - min + 1;
093: String text = this .text;
094: if (text == null)
095: text = "100%";
096: prefSize = WingToolkit.calcLabelSize(null, text,
097: getWingFont(), new Insets(0, 0, 0, 0), 0, this );
098: WingImage barImage = stBarNormal.image;
099: if (orientation == HORIZONTAL) {
100: if (prefSize.width < range)
101: prefSize.width = range;
102: if (barImage != null
103: && prefSize.height < barImage.getHeight())
104: prefSize.height = barImage.getHeight();
105: } else {
106: if (barImage != null
107: && prefSize.width < barImage.getWidth())
108: prefSize.width = barImage.getWidth();
109: if (prefSize.height < range)
110: prefSize.height = range;
111: }
112: prefSize.width += stNormal.margin.left
113: + stNormal.margin.right;
114: prefSize.height += stNormal.margin.top
115: + stNormal.margin.bottom;
116: wingPrefSize = prefSize;
117: }
118: return prefSize;
119: }
120:
121: public void wingPaint(Graphics g) {
122: int min, range, value;
123: synchronized (this ) {
124: min = this .min;
125: range = this .max - min;
126: value = this .value;
127: }
128: Dimension d = getSize();
129: String text = this .text;
130: if (text == null) {
131: StringBuffer sb = new StringBuffer();
132: sb.append((int) ((value - min) * 100) / range);
133: sb.append('%');
134: text = sb.toString();
135: }
136: if (text.length() > 0) {
137: WingToolkit.drawLabel(g, 0, 0, d.width, d.height, null,
138: text, getWingFont(), getForeground(), null,
139: stNormal.margin, 0, CENTER, CENTER, this );
140: }
141: int barWidth = d.width - stNormal.margin.left
142: - stNormal.margin.right;
143: int barHeight = d.height - stNormal.margin.top
144: - stNormal.margin.bottom;
145: int y = stNormal.margin.top;
146: if (orientation == HORIZONTAL) {
147: barWidth = ((value - min) * (barWidth)) / range;
148: } else {
149: y += barHeight;
150: barHeight = ((value - min) * (barHeight)) / range;
151: y -= barHeight;
152: }
153: Style stBar = (isEnabled()) ? stBarNormal : stBarDisabled;
154: WingToolkit.drawBackground(g, stNormal.margin.left, y,
155: barWidth, barHeight, stBar, this );
156: if (text.length() > 0) {
157: Rectangle r = g.getClipBounds();
158: Rectangle r2 = new Rectangle(stNormal.margin.left, y,
159: barWidth, barHeight);
160: r2 = (r != null) ? r.intersection(r2) : r2;
161: g.setClip(r2);
162: WingToolkit.drawLabel(g, 0, 0, d.width, d.height, null,
163: text, stBar.getFont(getWingFont()), stBar
164: .getForeground(getForeground()), null,
165: stNormal.margin, 0, CENTER, CENTER, this);
166: }
167: }
168: }
|