KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > api > impl > 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
20 package org.netbeans.modules.refactoring.api.impl;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import org.netbeans.modules.refactoring.api.ProgressEvent;
25 import org.netbeans.modules.refactoring.api.ProgressListener;
26 import org.openide.ErrorManager;
27
28 /**
29  * Support class for progress notifications
30  * @author Martin Matula, Jan Becicka
31  */

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

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

59     public void fireProgressListenerStart(Object JavaDoc source, int type, int count) {
60         counter = -1;
61         ArrayList JavaDoc list;
62         ProgressEvent event = new ProgressEvent(source, ProgressEvent.START, type, count);
63         synchronized (this) {
64             list = (ArrayList JavaDoc) progressListenerList.clone();
65         }
66         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
67             try {
68                 ((ProgressListener) it.next()).start(event);
69             } catch (RuntimeException JavaDoc e) {
70                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
71             }
72         }
73     }
74     
75     /** Notifies all registered listeners about the event.
76      *
77      * @param type Type of operation that is starting.
78      * @param count Number of steps the operation consists of.
79      *
80      */

81     public void fireProgressListenerStart(int type, int count) {
82         fireProgressListenerStart(this, type, count);
83     }
84     
85     
86     /** Notifies all registered listeners about the event.
87      */

88     public void fireProgressListenerStep(Object JavaDoc source, int count) {
89         counter = count;
90         ArrayList JavaDoc list;
91         ProgressEvent event = new ProgressEvent(source, ProgressEvent.STEP, 0, count);
92         synchronized (this) {
93             list = (ArrayList JavaDoc) progressListenerList.clone();
94         }
95         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
96             try {
97                 ((ProgressListener) it.next()).step(event);
98             } catch (RuntimeException JavaDoc e) {
99                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
100             }
101         }
102     }
103     /** Notifies all registered listeners about the event.
104      */

105     public void fireProgressListenerStep(Object JavaDoc source) {
106         fireProgressListenerStep(source, counter+1);
107     }
108     /** Notifies all registered listeners about the event.
109      */

110     public void fireProgressListenerStop(Object JavaDoc source) {
111         ArrayList JavaDoc list;
112         ProgressEvent event = new ProgressEvent(source, ProgressEvent.STOP);
113         synchronized (this) {
114             list = (ArrayList JavaDoc) progressListenerList.clone();
115         }
116         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
117             try {
118                 ((ProgressListener) it.next()).stop(event);
119             } catch (RuntimeException JavaDoc e) {
120                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
121             }
122         }
123     }
124     
125     /** Notifies all registered listeners about the event.
126      */

127     public void fireProgressListenerStop() {
128         fireProgressListenerStop(this);
129     }
130 }
131
Popular Tags