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

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

Removed the indeterminate progress bar at the start. Instead, the progress bar starts off 5% full.

  • 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.setIndeterminate(false);
70 progress_bar.setMaximum(MAX);
71 progress_bar.setMinimum(MIN);
72 progress_bar.setStringPainted(true);
73 progress_bar.setValue(MIN);
74 }
75
76 /** Method to register a new progress bar with this monitor.
77 * @param progress_bar The new <strong>JProgressBar</strong>.
78 */
79 public void addProgressBar(JProgressBar progress_bar) {
80 this.progress_bar = progress_bar;
81 progress_bar.setMaximum(MAX);
82 progress_bar.setMinimum(MIN);
83 progress_bar.setValue(MIN);
84 }
85
86 /** 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.
87 * @return A <i>int</i> with a value of zero if and only if the script was successful.
88 */
89 public int exitValue() {
90 if(num_docs > 0) {
91 return 0;
92 }
93 return 1;
94 }
95
96 /** Retrieve the number of documents recorded by the import monitor during the execution of the import scripts.
97 * @return An <i>int</i> giving the document number.
98 */
99 public int getNumberOfDocuments() {
100 return num_docs;
101 }
102
103 /** Method to retrieve whatever control is being used as the progress indicator. Usually a <strong>JProgressBar</strong> but there may be others implemented later.
104 * @return A <strong>Component</strong> on which the progress of the process is being displayed.
105 */
106 public Component getProgress() {
107 return progress_bar;
108 }
109
110 /** Method to determine the state of the stop flag, which may be set by the visual component that created this monitor.
111 * @return A <strong>boolean</strong> indicating if the process has been asked to stop.
112 */
113 public boolean hasSignalledStop() {
114 return stop;
115 }
116
117 /** 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. */
118 public void increment() {
119 if(num_docs > 0) {
120 progress_bar.setValue(progress_bar.getValue() + ((MAX / 5) / num_docs));
121 }
122 // else {
123 // progress_bar.setIndeterminate(true);
124 // }
125 }
126
127 /** This method is used to 'feed in' a line of text captured from the process.
128 * @param line A <strong>String</strong> of text captured from either standard out or standard error.
129 * TODO Everthing.
130 */
131 public void parse(String line_raw) {
132 progress_bar.setString("");
133 String line = line_raw.toLowerCase();
134 if (line.indexOf(SENTINEL) == 0) {
135 file_num = file_num + 1;
136 ///ystem.err.println("Done " + file_num + " of " + num_files + "...");
137 if (num_files > 0) {
138 int base_val = MAX / 10;
139 progress_bar.setValue(((((7 * MAX) / 10) * file_num) / num_files) + base_val);
140 }
141 }
142 else if (line.indexOf(NUM_DOCS_PROCESSED) != -1) {
143 num_docs = -1;
144 StringTokenizer tokenizer = new StringTokenizer(line);
145 while(num_docs == -1 && tokenizer.hasMoreTokens()) {
146 String pos_num_docs_str = tokenizer.nextToken();
147 try {
148 num_docs = Integer.parseInt(pos_num_docs_str);
149 }
150 catch (Exception error) {
151 }
152 }
153 ///ystem.err.println("Parsed num_docs to be " + num_docs);
154 }
155 }
156
157 public void saving() {
158 progress_bar.setString(Gatherer.dictionary.get("SaveProgressDialog.Title", (String)null));
159 progress_bar.setValue(MAX / (10 * 2));
160 }
161
162 /** 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.
163 * @param state The desired state of the stop flag as a <strong>boolean</strong>.
164 */
165 public void setStop(boolean state) {
166 stop = state;
167 }
168
169 /** This method resets this monitor to the start, reseting the process parsing and progress bar.
170 * TODO Everthing.
171 */
172 public void start() {
173 progress_bar.setString(Gatherer.dictionary.get("FileActions.Calculating_Size", (String)null));
174 progress_bar.setValue(MAX / 10);
175 num_files = Gatherer.c_man.getCollection().getDocumentCount();
176 ///ystem.err.println("Number of files: " + num_files);
177 file_num = 0;
178 }
179
180 /** This method indicates the process is complete.
181 * TODO Everthing.
182 */
183 public void stop() {
184 progress_bar.setValue(MAX);
185 }
186}
Note: See TracBrowser for help on using the repository browser.