Ignore:
Timestamp:
2004-01-09T15:00:44+13:00 (20 years ago)
Author:
jmt12
Message:

Completely rewrote monitors to be language independant - fixed several bugs including a decreasing progress bar and added a new reset() method to ensure that progress bars are pristine now that they can be onscreen at all times (lower detail modes)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/shell/GImportProgressMonitor.java

    r6325 r6388  
    3838
    3939import java.awt.Component;
     40import java.util.ArrayList;
    4041import java.util.StringTokenizer;
    4142import javax.swing.JProgressBar;
     43import org.greenstone.gatherer.Configuration;
    4244import org.greenstone.gatherer.Dictionary;
    4345import org.greenstone.gatherer.Gatherer;
     46import org.greenstone.gatherer.shell.GShellElement;
    4447import org.greenstone.gatherer.shell.GShellProgressMonitor;
    4548import org.greenstone.gatherer.util.StaticStrings;
     
    6568    /** This holds the number of documents actually processed by the import command, as garnered from the final block of text output. */
    6669    private int num_docs;
     70
     71    private int threshold = Configuration.SYSTEMS_MODE;
    6772    /** The progress bar this monitor updates. */
    6873    private JProgressBar progress_bar;
    6974    /** */
    70     static final private String C = "c";
     75    static final private String BLOCKED_ATTRIBUTE = "blocked";
     76    static final private String CONSIDERED_ATTRIBUTE = "considered";
     77    static final private String IGNORED_ATTRIBUTE = "ignored";
     78    static final private String IMPORT_ELEMENT = "Import";
     79    static final private String PROCESSED_ATTRIBUTE = "processed";   
    7180    /** */
    72     static final private String N = "n";
     81    static final private String NAME_ATTRIBUTE = "n";
    7382    /** */
    74     static final private String P = "p";
     83    static final private String PLUGIN_ATTRIBUTE = "p";
    7584    /** The fixed portion of the progress bar used for the calculating of file size and other pre-import functions. */
    7685    static final private int CALCULATION = 50000;
     
    8291    static final private int PROCESSED = 750000;
    8392    /** The element name of a file detected message. */
    84     static final private String FILE = "File";
     93    static final private String FILE_ELEMENT = "File";
    8594    /** The element name of a file processing message. */
    86     static final private String FILE_PROCESSING = "Processing";
     95    static final private String FILEPROCESSING_ELEMENT = "Processing";
    8796    /** The element name of an import complete message. */
    88     static final private String IMPORT_COMPLETE = "ImportComplete";
     97    static final private String IMPORTCOMPLETE_ELEMENT = "ImportComplete";
    8998
    9099    public GImportProgressMonitor() {
     
    93102    progress_bar.setMaximum(CALCULATION + PROCESSED + EXTRACTED);
    94103    progress_bar.setMinimum(MIN);
     104    progress_bar.setString(StaticStrings.SPACE_CHARACTER);
    95105    progress_bar.setStringPainted(true);
    96     progress_bar.setValue(MIN);
     106    setValue(MIN);
    97107    next_progress_value = CALCULATION;
    98108    }
     
    105115    progress_bar.setMaximum(CALCULATION + PROCESSED + EXTRACTED);
    106116    progress_bar.setMinimum(MIN);
    107     progress_bar.setValue(MIN);
     117    setValue(MIN);
    108118    next_progress_value = CALCULATION;
    109119    }
     
    145155    // The current progress is calculated to be:
    146156    // The fixed calculation value plus the fixed processed value plus some portion of the fixed extracted value. This portion is the extracted_file_count over the total number of documents available. Note that this breaks badly for bibliographical files (for now).
    147     progress_bar.setValue(CALCULATION + PROCESSED + ((EXTRACTED * extracted_file_count) / num_docs));
     157    setValue(CALCULATION + PROCESSED + ((EXTRACTED * extracted_file_count) / num_docs));
    148158    }
    149159
    150160    /** This method is used to 'feed in' a line of text captured from the process.
    151      * @param event the GShellEvent containing a message or other important shell information
    152      */
    153     public void process(GShellEvent event) {
    154     // We're into parsing output, so we don't need the 'calculating file size' etc string.
    155     if(showing_string) {
    156         progress_bar.setString("");
    157         showing_string = false;
    158     }
     161     * @param queue a queue which at the moment should contain a single GShellEvent
     162     */
     163    public void process(ArrayList queue) {
     164    // Retrieve the first event.
     165    GShellEvent event = (GShellEvent) queue.get(0);
    159166    // Remove 'import.pl> ' bit
    160167    String line = event.getMessage();
    161168    line = line.substring(line.indexOf(StaticStrings.GREATER_THAN_CHARACTER) + 1);
    162169    line = line.trim();
    163     ///ystem.err.println("message = " + line);
    164170    if(line.startsWith(StaticStrings.LESS_THAN_CHARACTER) && line.endsWith(StaticStrings.GREATER_THAN_CHARACTER)) {
    165         line = line.substring(1, line.length() - 1);
     171        // No original event is passed on, even in the lower modes
     172        event.veto();
     173        // Create a new element from it
     174        GShellElement element = new GShellElement(line);
     175        String name = element.getElementName();
    166176        // We may be reading a file. Remember we have to offset process as we recieve this message 'before' a document is processed. Hence the use of 'next_progress_value'
    167         if(line.startsWith(FILE)) {
     177        if(name.equals(IMPORT_ELEMENT)) {
     178        // We're into parsing output, so we don't need the 'calculating file size' etc string.
     179        if(showing_string) {
     180            progress_bar.setString("");
     181            showing_string = false;
     182        }
     183        if(Gatherer.config.getMode() <= threshold) {
     184            queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportBegun1"), event.getStatus()));
     185        }
     186        }
     187        else if(name.equals(FILE_ELEMENT)) {
    168188        file_count++;
    169         ///ystem.err.println("File #" + file_count);
    170189        // Set the next progress
    171         progress_bar.setValue(next_progress_value);
     190        if(next_progress_value > 0) {
     191            setValue(next_progress_value);
     192        }
    172193        // Now we calculate the next progress value
    173194        next_progress_value = CALCULATION + ((PROCESSED * file_count) / num_expected_docs);
     195        event.veto(); // Unconditional veto
    174196        }
    175197        // Or we're being told what plugin is actually processing the file
    176         else if(line.startsWith(FILE_PROCESSING)) {
     198        else if(name.equals(FILEPROCESSING_ELEMENT)) {
    177199        // If we are at lower mode settings fire a new 'dumbed down' event
    178         /** @todo */
     200        if(Gatherer.config.getMode() <= threshold) {
     201            String args[] = new String[2];
     202            args[0] = element.getAttribute(NAME_ATTRIBUTE);
     203            args[1] = element.getAttribute(PLUGIN_ATTRIBUTE);
     204            if(!args[1].equals("")) {
     205            queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.FileProcessing", args), event.getStatus()));
     206            }
     207            else {
     208            queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.FileNotProcessed", args), event.getStatus()));
     209            }
     210            args = null;
     211        }
    179212        }
    180213        // Or the import complete element
    181         else if(line.startsWith(IMPORT_COMPLETE)) {
     214        else if(name.equals(IMPORTCOMPLETE_ELEMENT)) {
    182215        // Set the next progress
    183         progress_bar.setValue(next_progress_value);
    184         // Determine the number of documents processed by using the 'p' attribute
    185         StringTokenizer tokenizer = new StringTokenizer(line);
    186         boolean found = false;
    187         String token = tokenizer.nextToken(); // Drop the element name
    188         while(tokenizer.hasMoreTokens() && !found) {
    189             token = tokenizer.nextToken();
    190             if(token.startsWith(P)) {
    191             found = true;
    192             try {
    193                 String num_docs_string = token.substring(3, token.length() - 1);
    194                 num_docs = Integer.parseInt(num_docs_string);
    195             }
    196             catch(Exception exception) {
    197                 Gatherer.println("Exception in GImportProgressMonitor.process() - Expected");
    198                 Gatherer.printStackTrace(exception);
    199             }
    200             }
    201         }
    202         tokenizer = null;
     216        setValue(next_progress_value);
    203217        // If we are at lower mode settings fire a new 'dumbed down' event
    204         /** @todo */
    205         }
    206 
    207 
    208         // We also veto this event to prevent other classes handling it (we ensure progress monitors are called first).
     218        if(Gatherer.config.getMode() <= threshold) {
     219            queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete1"), event.getStatus()));
     220            String args[] = new String[1];
     221            args[0] = element.getAttribute(CONSIDERED_ATTRIBUTE);
     222            queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete2", args), event.getStatus()));
     223            // The number of documents processed
     224            String processed_str = element.getAttribute(PROCESSED_ATTRIBUTE);
     225            if(!processed_str.equals(StaticStrings.ZERO_CHARACTER)) {
     226            args = new String[3];
     227            args[0] = element.getAttribute(PROCESSED_ATTRIBUTE);
     228            if(args[0].equals(StaticStrings.ONE_CHARACTER)) {
     229                args[1] = Dictionary.get("GShell.Import.Was");
     230                args[2] = Dictionary.get("GShell.Import.Is");
     231            }
     232            else {
     233                args[1] = Dictionary.get("GShell.Import.Were");
     234                args[2] = Dictionary.get("GShell.Import.Are");
     235            }
     236            queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete.Processed", args), event.getStatus()));
     237            }
     238            // Try to set num docs
     239            try {
     240            num_docs = Integer.parseInt(processed_str);
     241            }
     242            catch(Exception exception) {
     243            num_docs = 0;
     244            }
     245            processed_str = null;
     246            // The number of documents blocked
     247            String blocked_str = element.getAttribute(BLOCKED_ATTRIBUTE);
     248            if(!blocked_str.equals(StaticStrings.ZERO_CHARACTER)) {
     249            args = new String[2];
     250            args[0] = element.getAttribute(BLOCKED_ATTRIBUTE);
     251            if(args[0].equals(StaticStrings.ONE_CHARACTER)) {
     252                args[1] = Dictionary.get("GShell.Import.Was");
     253            }
     254            else {
     255                args[1] = Dictionary.get("GShell.Import.Were");
     256            }
     257            queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete.Blocked", args), event.getStatus()));
     258            }
     259            blocked_str = null;
     260            // The number of documents ignored
     261            String ignored_str = element.getAttribute(IGNORED_ATTRIBUTE);
     262            if(!ignored_str.equals(StaticStrings.ZERO_CHARACTER)) {
     263            args = new String[3];
     264            args[0] = element.getAttribute(IGNORED_ATTRIBUTE);
     265            if(args[0].equals(StaticStrings.ONE_CHARACTER)) {
     266                args[1] = Dictionary.get("GShell.Import.Was");
     267                args[2] = Dictionary.get("GShell.Import.Is");
     268            }
     269            else {
     270                args[1] = Dictionary.get("GShell.Import.Were");
     271                args[2] = Dictionary.get("GShell.Import.Are");
     272            }
     273            queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete.Ignored", args), event.getStatus()));
     274            }
     275            ignored_str = null;
     276            args = null;
     277            // Message is finally ready
     278            queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete3"), event.getStatus()));
     279        }
     280        }
     281        else {
     282        // Veto it
     283        event.veto();
     284        }
     285    }
     286    else if(Gatherer.config.getMode() <= threshold) {
    209287        event.veto();
    210288    }
     289    }
     290
     291    public void reset() {
     292    setValue(MIN);
     293    progress_bar.setString(StaticStrings.SPACE_CHARACTER);
     294    progress_bar.updateUI();
    211295    }
    212296
     
    226310     */
    227311    public void start() {
     312    setValue(MIN);
    228313    progress_bar.setString(Dictionary.get("FileActions.Calculating_Size"));
    229314    showing_string = true;
     
    238323     */
    239324    public void stop() {
    240     progress_bar.setValue(CALCULATION + PROCESSED + EXTRACTED);
     325    setValue(CALCULATION + PROCESSED + EXTRACTED);
     326    }
     327
     328    int previous_value = -1;
     329    private void setValue(int value) {
     330    progress_bar.setValue(value);
     331    if(value != 0 && value < previous_value) {
     332        Gatherer.println("Progress is decreasing! " + previous_value + " -> " + value);
     333    }
     334    previous_value = value;
    241335    }
    242336}
     337
Note: See TracChangeset for help on using the changeset viewer.