001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018:
019: package org.columba.core.command;
020:
021: import java.util.List;
022: import java.util.Vector;
023:
024: import javax.swing.event.EventListenerList;
025:
026: import org.columba.core.base.Mutex;
027: import org.columba.core.base.SwingWorker.ThreadVar;
028:
029: /**
030: * TaskManager keeps a list of currently running {@link Worker} objects.
031: * <p>
032: * The <code> StatusBar</code> listens for {@link TaskManagerEvent} to provide
033: * visual feedback. This includes a status message text and the progress bar.
034: * <p>
035: * In a model/view/controller pattern, the statusbar is the view, the
036: * taskmanager the underlying model.
037: *
038: * @author fdietz
039: */
040: public class TaskManager {
041: /**
042: * List of currently running {@link Worker} objects
043: */
044: private List<Worker> workerList;
045:
046: /**
047: * We need a mutex to be sure that modifying the workerList is
048: * thread-safe at all time
049: */
050: protected Mutex workerListMutex;
051:
052: /**
053: * Listeners which are interested in status changes
054: */
055: protected EventListenerList listenerList = new EventListenerList();
056:
057: private static TaskManager instance = new TaskManager();
058:
059: /**
060: * Default constructor
061: */
062: private TaskManager() {
063: workerList = new Vector<Worker>();
064: workerListMutex = new Mutex();
065: }
066:
067: public static TaskManager getInstance() {
068: return instance;
069: }
070:
071: /**
072: * Get list of currently running workers.
073: *
074: * @return list of workers
075: */
076: public Worker[] getWorkers() {
077: return workerList.toArray(new Worker[0]);
078: }
079:
080: public boolean exists(Worker worker) {
081: try {
082: workerListMutex.lock();
083:
084: if (workerList.contains(worker))
085: return true;
086:
087: } finally {
088: workerListMutex.release();
089: }
090:
091: return false;
092: }
093:
094: /**
095: * Get number of workers
096: *
097: * @return number of currenlty running workers
098: */
099: 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[] 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[] 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])
188: .workerRemoved(e);
189: }
190: }
191: }
192: }
|