001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: Task.java,v $ 023 Revision 1.5 2004/05/05 21:22:45 markl 024 Comment header updates. 025 026 Revision 1.4 2003/01/19 09:42:39 markl 027 Javadoc & comment header updates. 028 029 Revision 1.3 2001/03/12 03:16:51 markl 030 *** empty log message *** 031 032 Revision 1.2 1999/01/10 03:56:22 markl 033 added GPL header & RCS tag 034 ---------------------------------------------------------------------------- 035*/ 036 037package kiwi.util; 038 039import java.util.*; 040 041/** This class represents an asynchronous task whose progress can be tracked 042 * by a <code>ProgressObserver</code>. 043 * 044 * @author Mark Lindner 045 * 046 * @see kiwi.util.ProgressObserver 047 * @see kiwi.ui.dialog.ProgressDialog 048 * @see java.lang.Runnable 049 */ 050 051public abstract class Task implements Runnable 052 { 053 private Vector observers; 054 055 /** Construct a new <code>Task</code>. */ 056 057 public Task() 058 { 059 observers = new Vector(); 060 } 061 062 /** Run the task. This method is the body of the thread for this task. */ 063 064 public abstract void run(); 065 066 /** Add a progress observer to this task's list of observers. Observers are 067 * notified by the task of progress made. 068 * 069 * @param observer The observer to add. 070 */ 071 072 public final void addProgressObserver(ProgressObserver observer) 073 { 074 observers.addElement(observer); 075 } 076 077 /** Remove a progress observer from this task's list of observers. 078 * 079 * @param observer The observer to remove. 080 */ 081 082 public final void removeProgressObserver(ProgressObserver observer) 083 { 084 observers.removeElement(observer); 085 } 086 087 /** Notify all observers about the percentage of the task completed. 088 * 089 * @param percent The percentage of the task completed, an integer value 090 * between 0 and 100 inclusive. Values outside of this range are silently 091 * clipped. 092 */ 093 094 protected final void notifyObservers(int percent) 095 { 096 if(percent < 0) percent = 0; 097 else if(percent > 100) percent = 100; 098 099 Enumeration e = observers.elements(); 100 while(e.hasMoreElements()) 101 ((ProgressObserver)e.nextElement()).setProgress(percent); 102 } 103 104 } 105 106/* end of source file */