1 /*
2 * Copyright (c) 2003-2008 by Cosylab d. d.
3 *
4 * This file is part of CosyBeans-Common.
5 *
6 * CosyBeans-Common is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CosyBeans-Common is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CosyBeans-Common. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package com.cosylab.gui.components;
21
22 import java.util.ArrayList;
23
24
25 /**
26 * Convenience implementation of <code>ProgressMonitor</code>. Contains
27 * methods for firing events.
28 *
29 * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
30 * @version $id$
31 */
32 public abstract class AbstractProgressTask extends Thread
33 implements ProgressMonitor
34 {
35 private final ArrayList<ProgressListener> listeners = new ArrayList<ProgressListener>();
36
37 /**
38 * AbstractProgressTask constructor comment.
39 */
40 public AbstractProgressTask()
41 {
42 super();
43 }
44
45 /**
46 * Adds listener. Listeners will be notified when task reports progress.
47 *
48 * @param listener to add.
49 */
50 public void addProgressListener(ProgressListener listener)
51 {
52 listeners.add(listener);
53 }
54
55 /**
56 * Notifies listeners that current progress has changed. There is no
57 * prescribed frequency with which these events should be fired.
58 *
59 * @param status Description of current status.
60 * @param current progress, should be less than total
61 * @param total ammount.
62 */
63 protected void fireProgressEvent(String status, int current, int total)
64 {
65 yield();
66
67 ProgressEvent e = new ProgressEvent(this, status, current, total);
68
69 for (int i = 0; i < listeners.size(); i++) {
70 listeners.get(i).progress(e);
71 }
72
73 yield();
74 }
75
76 /**
77 * Notifies listeners that a task has been completed.
78 */
79 protected void fireTaskComplete()
80 {
81 yield();
82
83 ProgressEvent e = new ProgressEvent(this, getStatus(), getCurrent(),
84 getTotal());
85
86 for (int i = 0; i < listeners.size(); i++) {
87 listeners.get(i).taskComplete(e);
88 }
89
90 yield();
91 }
92
93 /**
94 * Notifies listeners that a task has been interrupted during execution.
95 *
96 * @param reason Description of reason for interruption.
97 */
98 protected void fireTaskInterrupted(String reason)
99 {
100 yield();
101
102 ProgressEvent e = new ProgressEvent(this, reason, 0, -1);
103
104 for (int i = 0; i < listeners.size(); i++) {
105 listeners.get(i).taskInterruped(e);
106 }
107
108 yield();
109 }
110
111 /**
112 * Notify listeners that task has started execution.
113 */
114 protected void fireTaskStarted()
115 {
116 yield();
117
118 ProgressEvent e = new ProgressEvent(this, getStatus(), getCurrent(),
119 getTotal());
120
121 for (int i = 0; i < listeners.size(); i++) {
122 listeners.get(i).taskStarted(e);
123 }
124
125 yield();
126 }
127
128 /**
129 * Force listeners to update immediately.
130 */
131 protected void immediateUpdate()
132 {
133 fireProgressEvent(getStatus(), getCurrent(), getTotal());
134 }
135
136 /**
137 * Removes listener.
138 *
139 * @param listener to remove.
140 */
141 public void removeProgressListener(ProgressListener listener)
142 {
143 listeners.remove(listener);
144 }
145 }
146
147 /* __oOo__ */