001: /*
002: * Copyright 2005 jWic group (http://www.jwic.de)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: * de.jwic.controls.ProgressBarControl
017: * Created on 16.11.2005
018: * $Id: ProgressBarControl.java,v 1.2 2006/08/14 10:56:37 lordsam Exp $
019: */
020: package de.jwic.controls;
021:
022: import de.jwic.base.Control;
023: import de.jwic.base.IControlContainer;
024: import de.jwic.events.ValueChangedEvent;
025: import de.jwic.events.ValueChangedListener;
026:
027: /**
028: * Displays a bar that indicates the progress of an operation. The state (or progress)
029: * is read from an IProgressMonitor implementation, that is queried when the state is
030: * rendered.
031: *
032: * @author Florian Lippisch
033: * @version $Revision: 1.2 $
034: */
035: public class ProgressBarControl extends Control {
036:
037: private static final long serialVersionUID = 1L;
038: private IProgressMonitor monitor = null;
039: private int width = 200;
040: private int height = 20;
041: private String cssClass = "progressBar";
042: private boolean smooth = false;
043: private MyChangeListener changeListener = null;
044:
045: private class MyChangeListener implements ValueChangedListener {
046: private static final long serialVersionUID = 1L;
047:
048: public void valueChanged(ValueChangedEvent event) {
049: requireRedraw();
050: }
051: }
052:
053: /**
054: * @param container
055: * @param name
056: */
057: public ProgressBarControl(IControlContainer container, String name) {
058: super (container, name);
059: changeListener = new MyChangeListener();
060: }
061:
062: /* (non-Javadoc)
063: * @see de.jwic.base.Control#actionPerformed(java.lang.String, java.lang.String)
064: */
065: public void actionPerformed(String actionId, String parameter) {
066:
067: }
068:
069: /**
070: * Returns the used ProgressMonitor.
071: * @return
072: */
073: public IProgressMonitor getMonitor() {
074: return monitor;
075: }
076:
077: /**
078: * Set the progressMonitor.
079: * @param monitor The monitor to set.
080: */
081: public void setMonitor(IProgressMonitor monitor) {
082: if (this .monitor != null) {
083: this .monitor.removeValueChangedListener(changeListener);
084: }
085: this .monitor = monitor;
086: // remove to prevent double registration when a monitor is set twice.
087: monitor.removeValueChangedListener(changeListener);
088: monitor.addValueChangedListener(changeListener);
089: requireRedraw();
090: }
091:
092: /**
093: * @return Returns the height.
094: */
095: public int getHeight() {
096: return height;
097: }
098:
099: /**
100: * @param height The height to set.
101: */
102: public void setHeight(int height) {
103: this .height = height;
104: requireRedraw();
105: }
106:
107: /**
108: * @return Returns the smooth.
109: */
110: public boolean isSmooth() {
111: return smooth;
112: }
113:
114: /**
115: * @param smooth The smooth to set.
116: */
117: public void setSmooth(boolean smooth) {
118: this .smooth = smooth;
119: requireRedraw();
120: }
121:
122: /**
123: * @return Returns the width.
124: */
125: public int getWidth() {
126: return width;
127: }
128:
129: /**
130: * @param width The width to set.
131: */
132: public void setWidth(int width) {
133: this .width = width;
134: requireRedraw();
135: }
136:
137: /**
138: * Returns the current percent value as int.
139: * @return
140: */
141: public int getPercent() {
142: double total = monitor.getMaximum() - monitor.getMinimum();
143: double value = monitor.getValue() - monitor.getMinimum();
144: if (total == 0) {
145: return 0; // avoid div 0
146: }
147: int proz = (int) (value / total * 100);
148: return proz > 100 ? 100 : proz < 0 ? 0 : proz;
149: }
150:
151: /**
152: * Creates a list of Boolean objects that indicate if a value is reached
153: * or not. Used by the renderer-template to draw 'boxes'
154: * @return
155: */
156: public boolean[] buildBlocks() {
157: int count = width / (height - 2);
158: boolean[] blocks = new boolean[count];
159: int too = (int) ((double) getPercent() / 100 * count);
160: for (int i = 0; i < count; i++) {
161: blocks[i] = i < too;
162: }
163: return blocks;
164: }
165:
166: /**
167: * @return Returns the cssClass.
168: */
169: public String getCssClass() {
170: return cssClass;
171: }
172:
173: /**
174: * @param cssClass The cssClass to set.
175: */
176: public void setCssClass(String cssClass) {
177: this.cssClass = cssClass;
178: }
179:
180: }
|