source: trunk/gli/src/org/greenstone/gatherer/shell/GImportProgressMonitor.java@ 5292

Last change on this file since 5292 was 5292, checked in by mdewsnip, 21 years ago

Improved the accuracy of the progress bar, especially when importing one folder containing many documents.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1package org.greenstone.gatherer.shell;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import java.awt.Component;
39import java.util.StringTokenizer;
40import javax.swing.JProgressBar;
41import org.greenstone.gatherer.Gatherer;
42import org.greenstone.gatherer.shell.GShellProgressMonitor;
43/** This implementation of <i>GShellProgressMonitor</i> is designed to parse and translate the progress of a import.pl call.
44 * @author John Thompson, Greenstone Digital Library, University of Waikato
45 * @version 2.1
46 */
47public class GImportProgressMonitor
48 implements GShellProgressMonitor {
49 /** Indicates if the GUI has asked the process this object monitors to stop. */
50 private boolean stop = false;
51 private int file_num = 0;
52 /** The number of files expected to be scanned by this import process. */
53 private int num_files = 0;
54 /** This holds the number of documents actually processed by the import command, as garnered from the final block of text output. */
55 private int num_docs = 0;
56 /** The progress bar this monitor updates. */
57 private JProgressBar progress_bar;
58 /** The maximum value for this progress bar. */
59 static final private int MAX = 1000000;
60 /** The minimum value for this progress bar. */
61 static final private int MIN = 0;
62 /** A String fragment which is used to determine if the current output process line is referring to the number of documents actually processed by the import script. */
63 static final private String NUM_DOCS_PROCESSED = "processed and included in the collection";
64 /** The Sentinel value is the String fragment an output process line must start with to be considered a milestone. In this case we are counting directories, so the fragment refers to a directory scan. */
65 static final private String SENTINEL = "recplug - ";
66
67 public GImportProgressMonitor() {
68 progress_bar = new JProgressBar();
69 progress_bar.setMaximum(MAX);
70 progress_bar.setMinimum(MIN);
71 progress_bar.setStringPainted(true);
72 progress_bar.setValue(MIN);
73 }
74
75 /** Method to register a new progress bar with this monitor.
76 * @param progress_bar The new <strong>JProgressBar</strong>.
77 */
78 public void addProgressBar(JProgressBar progress_bar) {
79 this.progress_bar = progress_bar;
80 progress_bar.setMaximum(MAX);
81 progress_bar.setMinimum(MIN);
82 progress_bar.setValue(MIN);
83 }
84
85 /** Determine the script exit value according to the progress monitor. This gets around a problem where several script failures actually result with a successful exit value.
86 * @return A <i>int</i> with a value of zero if and only if the script was successful.
87 */
88 public int exitValue() {
89 if(num_docs > 0) {
90 return 0;
91 }
92 return 1;
93 }
94
95 /** Retrieve the number of documents recorded by the import monitor during the execution of the import scripts.
96 * @return An <i>int</i> giving the document number.
97 */
98 public int getNumberOfDocuments() {
99 return num_docs;
100 }
101
102 /** Method to retrieve whatever control is being used as the progress indicator. Usually a <strong>JProgressBar</strong> but there may be others implemented later.
103 * @return A <strong>Component</strong> on which the progress of the process is being displayed.
104 */
105 public Component getProgress() {
106 return progress_bar;
107 }
108
109 /** Method to determine the state of the stop flag, which may be set by the visual component that created this monitor.
110 * @return A <strong>boolean</strong> indicating if the process has been asked to stop.
111 */
112 public boolean hasSignalledStop() {
113 return stop;
114 }
115
116 /** Inform the progress bar that it should programatically increment progress by one step. This is only called during the metadata archive extraction so each step should be (100 / 5) / num_docs. */
117 public void increment() {
118 if(num_docs > 0) {
119 progress_bar.setValue(progress_bar.getValue() + ((MAX / 5) / num_docs));
120 }
121 else {
122 progress_bar.setIndeterminate(true);
123 }
124 }
125
126 /** This method is used to 'feed in' a line of text captured from the process.
127 * @param line A <strong>String</strong> of text captured from either standard out or standard error.
128 * TODO Everthing.
129 */
130 public void parse(String line_raw) {
131 progress_bar.setString("");
132 String line = line_raw.toLowerCase();
133 if (line.indexOf(SENTINEL) == 0) {
134 file_num = file_num + 1;
135 ///ystem.err.println("Done " + file_num + " of " + num_files + "...");
136 if (num_files > 0) {
137 progress_bar.setValue((4 * MAX * file_num) / (5 * num_files));
138 }
139 }
140 else if (line.indexOf(NUM_DOCS_PROCESSED) != -1) {
141 num_docs = -1;
142 StringTokenizer tokenizer = new StringTokenizer(line);
143 while(num_docs == -1 && tokenizer.hasMoreTokens()) {
144 String pos_num_docs_str = tokenizer.nextToken();
145 try {
146 num_docs = Integer.parseInt(pos_num_docs_str);
147 }
148 catch (Exception error) {
149 }
150 }
151 ///ystem.err.println("Parsed num_docs to be " + num_docs);
152 }
153 }
154
155 public void saving() {
156 progress_bar.setIndeterminate(true);
157 progress_bar.setString(Gatherer.dictionary.get("SaveProgressDialog.Title", (String)null));
158 }
159
160 /** Since the creator of this process monitor is actually in the GUI, this class provides the simpliest way to send a cancel process message between the two.
161 * @param state The desired state of the stop flag as a <strong>boolean</strong>.
162 */
163 public void setStop(boolean state) {
164 stop = state;
165 progress_bar.setIndeterminate(false);
166 }
167
168 /** This method resets this monitor to the start, reseting the process parsing and progress bar.
169 * TODO Everthing.
170 */
171 public void start() {
172 progress_bar.setIndeterminate(true);
173 progress_bar.setString(Gatherer.dictionary.get("FileActions.Calculating_Size", (String)null));
174 progress_bar.setIndeterminate(false);
175 progress_bar.setValue(MIN);
176 num_files = Gatherer.c_man.getCollection().getDocumentCount();
177 ///ystem.err.println("Number of files: " + num_files);
178 file_num = 0;
179 }
180
181 /** This method indicates the process is complete.
182 * TODO Everthing.
183 */
184 public void stop() {
185 progress_bar.setIndeterminate(false);
186 progress_bar.setValue(MAX);
187 }
188}
Note: See TracBrowser for help on using the repository browser.