PageRenderTime 122ms CodeModel.GetById 60ms app.highlight 8ms RepoModel.GetById 52ms app.codeStats 0ms

/src/mpv5/utils/jobs/Job.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 126 lines | 81 code | 8 blank | 37 comment | 23 complexity | 6bc1957511502748687b08a2a94441e8 MD5 | raw file
  1/*
  2 *  This file is part of YaBS.
  3 *  
  4 *      YaBS 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 *      YaBS 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 YaBS.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17package mpv5.utils.jobs;
 18
 19import java.util.ArrayList;
 20import java.util.Collections;
 21import java.util.List;
 22import javax.swing.JProgressBar;
 23import javax.swing.SwingWorker;
 24import mpv5.logging.Log;
 25import mpv5.ui.frames.MPView;
 26
 27/**
 28 *
 29 *  This class helps to manage background tasks which result e.g. in the creation of a file or similar
 30 */
 31public class Job extends SwingWorker<Object, Object> {
 32
 33    private final List<Waitable> objects;
 34    private final Waiter recipient;
 35    private JProgressBar bar;
 36    private String message;
 37    private final List<Exception> code = new ArrayList<Exception>();
 38
 39    /**
 40     * Creates a new job
 41     * @param waitable
 42     * @param waiter
 43     */
 44    public Job(Waitable waitable, Waiter waiter) {
 45        this.objects = Collections.singletonList(waitable);
 46        this.recipient = waiter;
 47        this.bar = mpv5.YabsViewProxy.instance().getProgressbar();
 48    }
 49
 50    /**
 51     * Creates a new job
 52     * @param waitable
 53     * @param waiter 
 54     * @param message
 55     */
 56    public Job(Waitable waitable, Waiter waiter, String message) {
 57        this.objects = Collections.singletonList(waitable);
 58        this.recipient = waiter;
 59        this.message = message;
 60    }
 61
 62    /**
 63     * Creates a new job
 64     * @param waitables
 65     * @param waiter
 66     * @param message
 67     */
 68    public Job(List<Waitable> waitables, Waiter waiter, String message) {
 69        this.objects = waitables;
 70        this.recipient = waiter;
 71        this.message = message;
 72        this.bar = mpv5.YabsViewProxy.instance().getProgressbar();
 73    }
 74
 75    @Override
 76    public Object doInBackground() throws Exception {
 77        if (bar != null) {
 78            bar.setIndeterminate(true);
 79        }
 80        try {
 81            for (int i = 0; i < objects.size(); i++) {
 82                Waitable waitable = objects.get(i);
 83                code.add(waitable.waitFor());
 84            }
 85        } catch (Exception e) {
 86            if (bar != null) {
 87                bar.setIndeterminate(false);
 88            }
 89            throw e;
 90        } finally {
 91            if (bar != null) {
 92                bar.setIndeterminate(false);
 93            }
 94        }
 95        return objects;
 96    }
 97
 98    @Override
 99    public void done() {
100        if (recipient != null) {
101            try {
102                if (code != null && code.size() > 0) {
103                    if (objects.size() == 1) {
104                        recipient.set(objects.get(0), code.get(0));
105                    } else {
106                        recipient.set(objects, code.get(0));
107                    }
108                } else {
109                    if (objects.size() == 1) {
110                        recipient.set(objects.get(0), null);
111                    } else {
112                        recipient.set(objects, null);
113                    }
114                }
115            } catch (Exception ex) {
116                Log.Debug(ex);
117            }
118        }
119        if (bar != null) {
120            bar.setIndeterminate(false);
121        }
122        if (message != null) {
123            mpv5.YabsViewProxy.instance().addMessage(message);
124        }
125    }
126}