1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * <BR><BR>
|
---|
9 | *
|
---|
10 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
15 | *
|
---|
16 | * <BR><BR>
|
---|
17 | *
|
---|
18 | * This program is free software; you can redistribute it and/or modify
|
---|
19 | * it under the terms of the GNU General Public License as published by
|
---|
20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
21 | * (at your option) any later version.
|
---|
22 | *
|
---|
23 | * <BR><BR>
|
---|
24 | *
|
---|
25 | * This program is distributed in the hope that it will be useful,
|
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
28 | * GNU General Public License for more details.
|
---|
29 | *
|
---|
30 | * <BR><BR>
|
---|
31 | *
|
---|
32 | * You should have received a copy of the GNU General Public License
|
---|
33 | * along with this program; if not, write to the Free Software
|
---|
34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
35 | *########################################################################
|
---|
36 | */
|
---|
37 | package org.greenstone.gatherer.download;
|
---|
38 |
|
---|
39 | import java.awt.*;
|
---|
40 | import java.io.*;
|
---|
41 | import java.net.*;
|
---|
42 | import java.util.*;
|
---|
43 | import javax.swing.*;
|
---|
44 | import javax.swing.tree.*;
|
---|
45 | import org.greenstone.gatherer.*;
|
---|
46 |
|
---|
47 | /** This class provides access to the functionality of the WGet program, either by calling it via a shell script or by the JNI. It maintains a queue of pending jobs, and the component for showing these tasks to the user.
|
---|
48 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
49 | * @version 2.3
|
---|
50 | */
|
---|
51 | public class DownloadScrollPane
|
---|
52 | extends Thread {
|
---|
53 |
|
---|
54 | /** <i>true</i> if there is a task currently being carried out, <i>false</i> otherwise. */
|
---|
55 | private boolean busy = false;
|
---|
56 | /** <i>true</i> if verbose debug messages should be displayed, <i>false</i> otherwise. */
|
---|
57 | private boolean debug = false;
|
---|
58 | /** <i>true</i> if successfully completed tasks should be automatically removed from the job queue. */
|
---|
59 | private boolean remove_complete_jobs = true;
|
---|
60 |
|
---|
61 | private JPanel filler_pane = null;
|
---|
62 | /** The panel that the task list will be shown in. */
|
---|
63 | private JPanel list_pane;
|
---|
64 | /** The job currently underway. */
|
---|
65 | private DownloadJob job;
|
---|
66 | /** A scroll pane which will be used to display the list of pending tasks. */
|
---|
67 | private JScrollPane list_scroll;
|
---|
68 | /** A queue of download tasks. */
|
---|
69 | private Vector job_queue;
|
---|
70 | static final private boolean simple = true;
|
---|
71 |
|
---|
72 | public DownloadScrollPane() {
|
---|
73 | job = null;
|
---|
74 | job_queue = new Vector();
|
---|
75 | filler_pane = new JPanel();
|
---|
76 | list_pane = new JPanel();
|
---|
77 | list_pane.setLayout(new BoxLayout(list_pane, BoxLayout.Y_AXIS));
|
---|
78 | list_scroll = new JScrollPane(list_pane);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void deleteDownloadJob(DownloadJob delete_me) {
|
---|
82 | if (delete_me == job) {
|
---|
83 | while(busy) {
|
---|
84 | }
|
---|
85 | job = null;
|
---|
86 | }
|
---|
87 | if (delete_me.hasSignalledStop()) {
|
---|
88 | list_pane.remove(delete_me.getProgressBar());
|
---|
89 | job_queue.remove(delete_me);
|
---|
90 | list_pane.remove(filler_pane);
|
---|
91 | if(job_queue.size() > 0) {
|
---|
92 | Dimension progress_bar_size = delete_me.getProgressBar().getPreferredSize();
|
---|
93 | Dimension list_pane_size = list_pane.getSize();
|
---|
94 | int height = list_pane_size.height - (job_queue.size() * progress_bar_size.height);
|
---|
95 | progress_bar_size = null;
|
---|
96 | if(height > 0) {
|
---|
97 | filler_pane.setPreferredSize(new Dimension(list_pane_size.width, height));
|
---|
98 | list_pane.add(filler_pane);
|
---|
99 | }
|
---|
100 | list_pane_size = null;
|
---|
101 | }
|
---|
102 | list_pane.updateUI();
|
---|
103 | }
|
---|
104 | else {
|
---|
105 | DebugStream.println("Somehow we're trying to delete a job that is still running.");
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | public synchronized void downloadComplete() {
|
---|
110 | job.downloadComplete();
|
---|
111 | }
|
---|
112 |
|
---|
113 | public synchronized void downloadFailed() {
|
---|
114 | job.downloadFailed();
|
---|
115 | }
|
---|
116 |
|
---|
117 | public synchronized void downloadWarning() {
|
---|
118 | job.downloadWarning();
|
---|
119 | }
|
---|
120 |
|
---|
121 | public JScrollPane getDownloadJobList() {
|
---|
122 | return list_scroll;
|
---|
123 | }
|
---|
124 |
|
---|
125 | public synchronized boolean hasSignalledStop() {
|
---|
126 | return job.hasSignalledStop();
|
---|
127 | }
|
---|
128 |
|
---|
129 | public void newDownloadJob(Download download, String mode, String proxy_url) {
|
---|
130 | // Create the job and fill in the details from gatherer.config.
|
---|
131 |
|
---|
132 | DebugStream.println("About to create a new job");
|
---|
133 |
|
---|
134 |
|
---|
135 | DownloadJob new_job = new DownloadJob(download, Configuration.proxy_pass, Configuration.proxy_user, this, mode, proxy_url);
|
---|
136 | // Tell it to run as soon as possible
|
---|
137 |
|
---|
138 |
|
---|
139 |
|
---|
140 | new_job.setState(DownloadJob.RUNNING);
|
---|
141 |
|
---|
142 | // Add to job_queue job list.
|
---|
143 | job_queue.add(new_job);
|
---|
144 |
|
---|
145 | // Now add it to the visual component, job list.
|
---|
146 |
|
---|
147 | list_pane.remove(filler_pane);
|
---|
148 |
|
---|
149 | Dimension progress_bar_size = new_job.getProgressBar().getPreferredSize();
|
---|
150 |
|
---|
151 | Dimension list_pane_size = list_pane.getSize();
|
---|
152 |
|
---|
153 | int height = list_pane_size.height - (job_queue.size() * progress_bar_size.height);
|
---|
154 |
|
---|
155 | progress_bar_size = null;
|
---|
156 |
|
---|
157 | list_pane.add(new_job.getProgressBar());
|
---|
158 |
|
---|
159 | if(height > 0) {
|
---|
160 | filler_pane.setPreferredSize(new Dimension(list_pane_size.width, height));
|
---|
161 | list_pane.add(filler_pane);
|
---|
162 | }
|
---|
163 |
|
---|
164 | list_pane_size = null;
|
---|
165 | //list_pane.setAlignmentX(Component.LEFT_ALIGNMENT);
|
---|
166 | list_pane.updateUI();
|
---|
167 | new_job = null;
|
---|
168 | synchronized(this) {
|
---|
169 | notify(); // Just incase its sleeping.
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | public synchronized void updateProgress(long current, long expected) {
|
---|
174 | job.updateProgress(current, expected);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /* There may be times when the download thread is sleeping, but the
|
---|
178 | * user has indicated that a previously paused job should now begin
|
---|
179 | * again. The flag within the job will change, so we tell the thread
|
---|
180 | * to start again.
|
---|
181 | */
|
---|
182 | public void resumeThread() {
|
---|
183 | synchronized(this) {
|
---|
184 | notify(); // Just incase its sleeping.
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | public void run() {
|
---|
189 | while(true) {
|
---|
190 | if(job_queue.size() > 0) {
|
---|
191 | int index = 0;
|
---|
192 | while(job_queue.size() > 0 && index < job_queue.size()) {
|
---|
193 | job = (DownloadJob) job_queue.get(index);
|
---|
194 | if(job.getState() == DownloadJob.RUNNING) {
|
---|
195 | DebugStream.println("DownloadJob " + job.toString() + " Begun.");
|
---|
196 | busy = true;
|
---|
197 | job.callDownload();
|
---|
198 | busy = false;
|
---|
199 | DebugStream.println("DownloadJob " + job.toString() + " complete.");
|
---|
200 | job = null;
|
---|
201 | }
|
---|
202 | index++;
|
---|
203 | }
|
---|
204 | try {
|
---|
205 | synchronized(this) {
|
---|
206 | DebugStream.println("WGet thread is waiting for DownloadJobs.");
|
---|
207 | wait();
|
---|
208 | }
|
---|
209 | } catch (InterruptedException e) {
|
---|
210 | // Time to get going again.
|
---|
211 | }
|
---|
212 | }
|
---|
213 | else {
|
---|
214 | try {
|
---|
215 | synchronized(this) {
|
---|
216 | DebugStream.println("WGet thread is waiting for DownloadJobs.");
|
---|
217 | wait();
|
---|
218 | }
|
---|
219 | } catch (InterruptedException e) {
|
---|
220 | // Time to get going again.
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|