| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.tools; |
| 17 | |
| 18 | /** |
| 19 | * Base for a Task already handling progress related issues. |
| 20 | * |
| 21 | * @author Thomas Aglassinger |
| 22 | */ |
| 23 | public abstract class AbstractTask implements Task |
| 24 | { |
| 25 | private static final String EMPTY_PROGRESS = "..."; |
| 26 | private long maxProgress; |
| 27 | private long progress; |
| 28 | private ProgressChangeSupport progressChangeSupport; |
| 29 | private String progressMessage; |
| 30 | |
| 31 | protected AbstractTask() { |
| 32 | super(); |
| 33 | progressChangeSupport = new ProgressChangeSupport(); |
| 34 | progressMessage = EMPTY_PROGRESS; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Set maximum progress to <code>newMaxProgress</code>. This should only be called from the |
| 39 | * constructor. |
| 40 | */ |
| 41 | protected final void setMaxProgress(long newMaxProgress) { |
| 42 | assert newMaxProgress >= 0; |
| 43 | |
| 44 | maxProgress = newMaxProgress; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Set progress to <code>newProgress</code> and notify progress listeners of it. |
| 49 | */ |
| 50 | protected void setProgress(long newProgress) { |
| 51 | assert newProgress >= 0; |
| 52 | |
| 53 | progress = Math.min(newProgress, getMaxProgress()); |
| 54 | progressChangeSupport.fireProgressChangedEvent(this); |
| 55 | } |
| 56 | |
| 57 | protected void setProgressMessage(String newMessage) { |
| 58 | if (newMessage == null) { |
| 59 | progressMessage = EMPTY_PROGRESS; |
| 60 | } else { |
| 61 | progressMessage = newMessage; |
| 62 | } |
| 63 | progressChangeSupport.fireProgressChangedEvent(this); |
| 64 | } |
| 65 | |
| 66 | public long getMaxProgress() { |
| 67 | return maxProgress; |
| 68 | } |
| 69 | |
| 70 | public long getProgress() { |
| 71 | return progress; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get a message describing the current progress, for example "Rendering hugo.png". This can |
| 76 | * not be <code>null</code>. |
| 77 | */ |
| 78 | public String getProgressMessage() { |
| 79 | return progressMessage; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Return <code>true</code> if the current thread was interrupted. |
| 84 | * |
| 85 | * @see Thread#isInterrupted() |
| 86 | */ |
| 87 | protected boolean isInterrupted() { |
| 88 | return Thread.currentThread().isInterrupted(); |
| 89 | } |
| 90 | |
| 91 | public void addProgressChangeListener(ProgressChangeListener newListener) { |
| 92 | progressChangeSupport.addProgressChangeListener(newListener); |
| 93 | } |
| 94 | |
| 95 | public void removeProgressChangeListener( |
| 96 | ProgressChangeListener listenerToRemove) { |
| 97 | progressChangeSupport.removeProgressChangeListener(listenerToRemove); |
| 98 | } |
| 99 | |
| 100 | public abstract void start() |
| 101 | throws Exception; |
| 102 | } |