KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > ProgressBuilder


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common;
24
25 // ToolBox imports
26
import org.enhydra.tool.common.event.CancelEvent;
27 import org.enhydra.tool.common.event.CancelListener;
28 import org.enhydra.tool.common.event.ProgressEvent;
29 import org.enhydra.tool.common.event.ProgressListener;
30
31 // Standard imports
32
import java.util.ArrayList JavaDoc;
33 import java.util.Arrays JavaDoc;
34
35 //
36
public class ProgressBuilder implements CancelListener {
37     private ProgressListener[] progressListeners = new ProgressListener[0];
38     private static boolean fresh = true;
39
40     public ProgressBuilder() {
41     }
42
43
44     /**
45      * Make the thread stale so it stops running gracefully.
46      */

47     public void setFresh(boolean b) {
48         fresh = b;
49     }
50
51     public boolean isFresh() {
52         return fresh;
53     }
54
55     public ProgressListener[] getProgressListeners() {
56         return progressListeners;
57     }
58
59     public void addProgressListener(ProgressListener l) {
60         ArrayList JavaDoc list = null;
61         list = new ArrayList JavaDoc(Arrays.asList(progressListeners));
62         if (! list.contains(l)) {
63             list.add(l);
64         }
65         list.trimToSize();
66         progressListeners = new ProgressListener[list.size()];
67         progressListeners = (ProgressListener[]) list.toArray(progressListeners);
68         list.clear();
69     }
70
71     public void removeProgressListener(ProgressListener l) {
72         ArrayList JavaDoc list = null;
73         list = new ArrayList JavaDoc(Arrays.asList(progressListeners));
74         if (list.contains(l)) {
75             list.remove(l);
76         }
77         list.trimToSize();
78         progressListeners = new ProgressListener[list.size()];
79         progressListeners = (ProgressListener[]) list.toArray(progressListeners);
80         list.clear();
81     }
82
83     public void refreshProgress(int progress, String JavaDoc message) {
84         if (progressListeners.length == 0) {
85
86             // done
87
} else {
88             ProgressEvent event = null;
89
90             event = new ProgressEvent(this, progress, message);
91             for (int i = 0; i < progressListeners.length; i++) {
92                 progressListeners[i].onProgress(event);
93             }
94         }
95     }
96
97     public void onCancel(CancelEvent event) {
98         setFresh(false);
99     }
100
101 }
102
Popular Tags