KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > internalapi > ProgressSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore.internalapi;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import org.openide.ErrorManager;
24
25 /**
26  * @author Martin Matula, Jan Becicka
27  */

28 public final class ProgressSupport {
29     /** Utility field holding list of ProgressListeners. */
30     private transient ArrayList JavaDoc progressListenerList = null;
31     private int counter;
32
33
34     public ProgressSupport() {
35         progressListenerList = new ArrayList JavaDoc();
36     }
37     public synchronized void addProgressListener(ProgressListener listener) {
38         progressListenerList.add(listener);
39     }
40     
41     /** Removes ProgressListener from the list of listeners.
42      * @param listener The listener to remove.
43      *
44      */

45     public synchronized void removeProgressListener(ProgressListener listener) {
46         progressListenerList.remove(listener);
47     }
48     
49     /** Notifies all registered listeners about the event.
50      *
51      * @param type Type of operation that is starting.
52      * @param count Number of steps the operation consists of.
53      *
54      */

55     public void fireProgressListenerStart(int type, int count) {
56         counter = -1;
57         ArrayList JavaDoc list;
58         ProgressEvent event = new ProgressEvent(this, ProgressEvent.START, type, count);
59         synchronized (this) {
60             list = (ArrayList JavaDoc) progressListenerList.clone();
61         }
62         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
63             try {
64                 ((ProgressListener) it.next()).start(event);
65             } catch (RuntimeException JavaDoc e) {
66                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
67             }
68         }
69     }
70     
71     /** Notifies all registered listeners about the event.
72      */

73     public void fireProgressListenerStep(int count) {
74         counter = count;
75         ArrayList JavaDoc list;
76         ProgressEvent event = new ProgressEvent(this, ProgressEvent.STEP, 0, count);
77         synchronized (this) {
78             list = (ArrayList JavaDoc) progressListenerList.clone();
79         }
80         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
81             try {
82                 ((ProgressListener) it.next()).step(event);
83             } catch (RuntimeException JavaDoc e) {
84                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
85             }
86         }
87     }
88       /** Notifies all registered listeners about the event.
89      */

90     public void fireProgressListenerStep() {
91          fireProgressListenerStep(counter+1);
92     }
93     /** Notifies all registered listeners about the event.
94      */

95     public void fireProgressListenerStop() {
96         ArrayList JavaDoc list;
97         ProgressEvent event = new ProgressEvent(this, ProgressEvent.STOP);
98         synchronized (this) {
99             list = (ArrayList JavaDoc) progressListenerList.clone();
100         }
101         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
102             try {
103                 ((ProgressListener) it.next()).stop(event);
104             } catch (RuntimeException JavaDoc e) {
105                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
106             }
107         }
108     }
109 }
110
Popular Tags