001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.kelp.common;
024:
025: // ToolBox imports
026: import org.enhydra.tool.common.event.CancelEvent;
027: import org.enhydra.tool.common.event.CancelListener;
028: import org.enhydra.tool.common.event.ProgressEvent;
029: import org.enhydra.tool.common.event.ProgressListener;
030:
031: // Standard imports
032: import java.util.ArrayList;
033: import java.util.Arrays;
034:
035: //
036: public class ProgressBuilder implements CancelListener {
037: private ProgressListener[] progressListeners = new ProgressListener[0];
038: private static boolean fresh = true;
039:
040: public ProgressBuilder() {
041: }
042:
043: /**
044: * Make the thread stale so it stops running gracefully.
045: */
046: public void setFresh(boolean b) {
047: fresh = b;
048: }
049:
050: public boolean isFresh() {
051: return fresh;
052: }
053:
054: public ProgressListener[] getProgressListeners() {
055: return progressListeners;
056: }
057:
058: public void addProgressListener(ProgressListener l) {
059: ArrayList list = null;
060: list = new ArrayList(Arrays.asList(progressListeners));
061: if (!list.contains(l)) {
062: list.add(l);
063: }
064: list.trimToSize();
065: progressListeners = new ProgressListener[list.size()];
066: progressListeners = (ProgressListener[]) list
067: .toArray(progressListeners);
068: list.clear();
069: }
070:
071: public void removeProgressListener(ProgressListener l) {
072: ArrayList list = null;
073: list = new ArrayList(Arrays.asList(progressListeners));
074: if (list.contains(l)) {
075: list.remove(l);
076: }
077: list.trimToSize();
078: progressListeners = new ProgressListener[list.size()];
079: progressListeners = (ProgressListener[]) list
080: .toArray(progressListeners);
081: list.clear();
082: }
083:
084: public void refreshProgress(int progress, String message) {
085: if (progressListeners.length == 0) {
086:
087: // done
088: } else {
089: ProgressEvent event = null;
090:
091: event = new ProgressEvent(this , progress, message);
092: for (int i = 0; i < progressListeners.length; i++) {
093: progressListeners[i].onProgress(event);
094: }
095: }
096: }
097:
098: public void onCancel(CancelEvent event) {
099: setFresh(false);
100: }
101:
102: }
|