KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > command > TaskManager


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18

19 package org.columba.core.command;
20
21 import java.util.List JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import javax.swing.event.EventListenerList JavaDoc;
25
26 import org.columba.core.base.Mutex;
27 import org.columba.core.base.SwingWorker.ThreadVar;
28
29 /**
30  * TaskManager keeps a list of currently running {@link Worker} objects.
31  * <p>
32  * The <code> StatusBar</code> listens for {@link TaskManagerEvent} to provide
33  * visual feedback. This includes a status message text and the progress bar.
34  * <p>
35  * In a model/view/controller pattern, the statusbar is the view, the
36  * taskmanager the underlying model.
37  *
38  * @author fdietz
39  */

40 public class TaskManager {
41     /**
42          * List of currently running {@link Worker} objects
43          */

44     private List JavaDoc<Worker> workerList;
45
46     /**
47          * We need a mutex to be sure that modifying the workerList is
48          * thread-safe at all time
49          */

50     protected Mutex workerListMutex;
51
52     /**
53          * Listeners which are interested in status changes
54          */

55     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
56
57     private static TaskManager instance = new TaskManager();
58
59     /**
60          * Default constructor
61          */

62     private TaskManager() {
63     workerList = new Vector JavaDoc<Worker>();
64     workerListMutex = new Mutex();
65     }
66
67     public static TaskManager getInstance() {
68     return instance;
69     }
70
71     /**
72          * Get list of currently running workers.
73          *
74          * @return list of workers
75          */

76     public Worker[] getWorkers() {
77     return workerList.toArray(new Worker[0]);
78     }
79
80     public boolean exists(Worker worker) {
81     try {
82         workerListMutex.lock();
83
84         if (workerList.contains(worker))
85         return true;
86
87     } finally {
88         workerListMutex.release();
89     }
90
91     return false;
92     }
93
94     /**
95          * Get number of workers
96          *
97          * @return number of currenlty running workers
98          */

99     public int count() {
100     return workerList.size();
101     }
102
103     /*
104          * (non-Javadoc)
105          *
106          * @see org.columba.core.taskmanager.ITaskManager#register(org.columba.core.command.Worker)
107          */

108     public void register(Worker t) {
109     try {
110         workerListMutex.lock();
111
112         workerList.add(t);
113     } finally {
114         workerListMutex.release();
115     }
116
117     fireWorkerAdded(t);
118     }
119
120     /*
121          * (non-Javadoc)
122          *
123          * @see org.columba.core.taskmanager.ITaskManager#unregister(org.columba.core.util.SwingWorker.ThreadVar)
124          */

125     public void unregister(ThreadVar tvar) {
126     try {
127         workerListMutex.lock();
128         for (Worker _worker : workerList) {
129         if (tvar == _worker.getThreadVar()) {
130             workerList.remove(_worker);
131             fireWorkerRemoved(_worker);
132             break;
133         }
134         }
135     } finally {
136         workerListMutex.release();
137     }
138     }
139
140     /*
141          * (non-Javadoc)
142          *
143          * @see org.columba.core.taskmanager.ITaskManager#addTaskManagerListener(org.columba.core.taskmanager.TaskManagerListener)
144          */

145     public void addTaskManagerListener(TaskManagerListener l) {
146     listenerList.add(TaskManagerListener.class, l);
147     }
148
149     /*
150          * (non-Javadoc)
151          *
152          * @see org.columba.core.taskmanager.ITaskManager#removeTaskManagerListener(org.columba.core.taskmanager.TaskManagerListener)
153          */

154     public void removeTaskManagerListener(TaskManagerListener l) {
155     listenerList.remove(TaskManagerListener.class, l);
156     }
157
158     /**
159          * Notify all listeners that something has changed.
160          *
161          * @param e
162          * event
163          */

164     protected void fireWorkerAdded(Worker w) {
165     TaskManagerEvent e = new TaskManagerEvent(this, w);
166     // Guaranteed to return a non-null array
167
Object JavaDoc[] listeners = listenerList.getListenerList();
168
169     // Process the listeners last to first, notifying
170
// those that are interested in this event
171
for (int i = listeners.length - 2; i >= 0; i -= 2) {
172         if (listeners[i] == TaskManagerListener.class) {
173         ((TaskManagerListener) listeners[i + 1]).workerAdded(e);
174         }
175     }
176     }
177
178     protected void fireWorkerRemoved(Worker w) {
179     TaskManagerEvent e = new TaskManagerEvent(this, w);
180     // Guaranteed to return a non-null array
181
Object JavaDoc[] listeners = listenerList.getListenerList();
182
183     // Process the listeners last to first, notifying
184
// those that are interested in this event
185
for (int i = listeners.length - 2; i >= 0; i -= 2) {
186         if (listeners[i] == TaskManagerListener.class) {
187         ((TaskManagerListener) listeners[i + 1]).workerRemoved(e);
188         }
189     }
190     }
191 }
192
Popular Tags