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

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

Fixed tabbing.

  • 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 /** The number of directories that are expected to be scanned by this import process. May not be correct for various reasons, such as a custom RecPlug setup, but its seems the most reliable for general cases. */
52 private int num_dirs = 0;
53 /** This holds the number of documents actually processed by the import command, as garnered from the final block of text output. */
54 private int num_docs = 0;
55 /** The progress bar this monitor updates. */
56 private JProgressBar progress_bar;
57 /** The maximum value for this progress bar. */
58 static final private int MAX = 1000000;
59 /** The minimum value for this progress bar. */
60 static final private int MIN = 0;
61 /** 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. */
62 static final private String NUM_DOCS_PROCESSED = "processed and included in the collection";
63 /** 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. */
64 static final private String SENTINEL = "recplug: getting directory ";
65 public GImportProgressMonitor() {
66 progress_bar = new JProgressBar();
67 progress_bar.setMaximum(MAX);
68 progress_bar.setMinimum(MIN);
69 progress_bar.setValue(MIN);
70 }
71 /** Method to register a new progress bar with this monitor.
72 * @param progress_bar The new <strong>JProgressBar</strong>.
73 */
74 public void addProgressBar(JProgressBar progress_bar) {
75 this.progress_bar = progress_bar;
76 progress_bar.setMaximum(MAX);
77 progress_bar.setMinimum(MIN);
78 progress_bar.setValue(MIN);
79 }
80 /** 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.
81 * @return A <i>int</i> with a value of zero if and only if the script was successful.
82 */
83 public int exitValue() {
84 if(num_docs > 0) {
85 return 0;
86 }
87 return 1;
88 }
89 /** Retrieve the number of documents recorded by the import monitor during the execution of the import scripts.
90 * @return An <i>int</i> giving the document number.
91 */
92 public int getNumberOfDocuments() {
93 return num_docs;
94 }
95
96 /** Method to retrieve whatever control is being used as the progress indicator. Usually a <strong>JProgressBar</strong> but there may be others implemented later.
97 * @return A <strong>Component</strong> on which the progress of the process is being displayed.
98 */
99 public Component getProgress() {
100 return progress_bar;
101 }
102 /** Method to determine the state of the stop flag, which may be set by the visual component that created this monitor.
103 * @return A <strong>boolean</strong> indicating if the process has been asked to stop.
104 */
105 public boolean hasSignalledStop() {
106 return stop;
107 }
108 /** 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. */
109 public void increment() {
110 if(num_docs > 0) {
111 progress_bar.setValue(progress_bar.getValue() + ((MAX / 5) / num_docs));
112 }
113 else {
114 progress_bar.setIndeterminate(true);
115 }
116 }
117 /** This method is used to 'feed in' a line of text captured from the process.
118 * @param line A <strong>String</strong> of text captured from either standard out or standard error.
119 * TODO Everthing.
120 */
121 public void parse(String line_raw) {
122 String line = line_raw.toLowerCase();
123 if(line.indexOf(SENTINEL) != -1) {
124 ///ystem.err.println("Sentinal value detected: ");
125 if(num_dirs > 0) {
126 ///ystem.err.println(count + " of " + num_dirs);
127 ///ystem.err.println("Value before: " + progress_bar.getValue());
128 progress_bar.setValue(progress_bar.getValue() + ((4 * (MAX / 5)) / num_dirs));
129 ///ystem.err.println("Value after: " + progress_bar.getValue());
130 }
131 else {
132 progress_bar.setIndeterminate(true);
133 }
134 }
135 else if(line.indexOf(NUM_DOCS_PROCESSED) != -1) {
136 // Increment the import because we have finished a directory
137
138 num_docs = -1;
139 StringTokenizer tokenizer = new StringTokenizer(line);
140 while(num_docs == -1 && tokenizer.hasMoreTokens()) {
141 String pos_num_docs_str = tokenizer.nextToken();
142 try {
143 num_docs = Integer.parseInt(pos_num_docs_str);
144 }
145 catch (Exception error) {
146 }
147 }
148 ///ystem.err.println("Parsed num_docs to be " + num_docs);
149 }
150 }
151 /** 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.
152 * @param state The desired state of the stop flag as a <strong>boolean</strong>.
153 */
154 public void setStop(boolean state) {
155 stop = state;
156 progress_bar.setIndeterminate(false);
157 }
158 /** This method resets this monitor to the start, reseting the process parsing and progress bar.
159 * TODO Everthing.
160 */
161 public void start() {
162 num_dirs = Gatherer.c_man.getCollection().getFolderCount();
163 progress_bar.setValue(MIN);
164 System.err.println("Number of directories is " + num_dirs);
165 }
166 /** This method indicates the process is complete.
167 * TODO Everthing.
168 */
169 public void stop() {
170 progress_bar.setIndeterminate(false);
171 progress_bar.setValue(MAX);
172 }
173}
Note: See TracBrowser for help on using the repository browser.