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.ProgressMonitor
017: * Created on 16.11.2005
018: * $Id: ProgressMonitor.java,v 1.2 2006/08/14 10:56:37 lordsam Exp $
019: */
020: package de.jwic.controls;
021:
022: import java.io.Serializable;
023:
024: import de.jwic.events.ValueChangedEvent;
025: import de.jwic.events.ValueChangedListener;
026:
027: /**
028: * Adapter implementation of the IProgressMonitor interface.
029: * @see IProgressMonitor
030: * @author Florian Lippisch
031: * @version $Revision: 1.2 $
032: */
033: public class ProgressMonitor implements IProgressMonitor, Serializable {
034:
035: private static final long serialVersionUID = 1L;
036: private int maximum = 10;
037: private int minimum = 0;
038: private int value = 0;
039:
040: private ValueChangedListener[] listeners = null;
041:
042: /**
043: * Add a ValueChangedListener to this monitor.
044: * @param listener
045: */
046: public synchronized void addValueChangedListener(
047: ValueChangedListener listener) {
048: if (listeners == null) {
049: listeners = new ValueChangedListener[] { listener };
050: } else {
051: ValueChangedListener[] tmp = new ValueChangedListener[listeners.length + 1];
052: System.arraycopy(listeners, 0, tmp, 0, listeners.length);
053: tmp[listeners.length] = listener;
054: listeners = tmp;
055: }
056: }
057:
058: /**
059: * Removes the specified listener from the monitor.
060: * @param listener
061: */
062: public synchronized void removeValueChangedListener(
063: ValueChangedListener listener) {
064: if (listeners != null && listeners.length > 0) {
065: ValueChangedListener[] tmp = new ValueChangedListener[listeners.length - 1];
066: int idx = 0;
067: for (int i = 0; i < listeners.length; i++) {
068: if (listeners[i] != listener) {
069: if (idx == tmp.length) {
070: // the listener was not registerd
071: return; // early exit
072: }
073: tmp[idx++] = listeners[i];
074: }
075: }
076: listeners = tmp;
077: }
078: }
079:
080: /**
081: * Fires the value changed event.
082: * @param event
083: */
084: protected void fireValueChangedEvent(ValueChangedEvent event) {
085: if (listeners != null) {
086: for (int i = 0; i < listeners.length; i++) {
087: listeners[i].valueChanged(event);
088: }
089: }
090: }
091:
092: /* (non-Javadoc)
093: * @see de.jwic.controls.IProgressMonitor#getMaximum()
094: */
095: public int getMaximum() {
096: return maximum;
097: }
098:
099: /* (non-Javadoc)
100: * @see de.jwic.controls.IProgressMonitor#getMinium()
101: */
102: public int getMinimum() {
103: return minimum;
104: }
105:
106: /* (non-Javadoc)
107: * @see de.jwic.controls.IProgressMonitor#getValue()
108: */
109: public int getValue() {
110: return value;
111: }
112:
113: /**
114: * @param maximum The maximum to set.
115: */
116: public void setMaximum(int maximum) {
117: this .maximum = maximum;
118: }
119:
120: /**
121: * @param minimum The minimum to set.
122: */
123: public void setMinimum(int minimum) {
124: this .minimum = minimum;
125: }
126:
127: /**
128: * @param value The value to set.
129: */
130: public void setValue(int value) {
131: this .value = value;
132: fireValueChangedEvent(new ValueChangedEvent(this ));
133: }
134:
135: /**
136: * Increases the value by the argument value.
137: * @param worked int
138: */
139: public void worked(int worked) {
140: value += worked;
141: fireValueChangedEvent(new ValueChangedEvent(this));
142: }
143:
144: }
|