ProgressListenerImpl xref

View Javadoc

    1   /**
    2    * Copyright (c) 2004-2011 QOS.ch
    3    * All rights reserved.
    4    *
    5    * Permission is hereby granted, free  of charge, to any person obtaining
    6    * a  copy  of this  software  and  associated  documentation files  (the
    7    * "Software"), to  deal in  the Software without  restriction, including
    8    * without limitation  the rights to  use, copy, modify,  merge, publish,
    9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
    10   * permit persons to whom the Software  is furnished to do so, subject to
    11   * the following conditions:
    12   *
    13   * The  above  copyright  notice  and  this permission  notice  shall  be
    14   * included in all copies or substantial portions of the Software.
    15   *
    16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
    19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
    22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    23   *
    24   */
    25  package org.slf4j.migrator.internal;
    26
    27  import java.io.File;
    28  import java.io.IOException;
    29
    30  import org.slf4j.migrator.helper.Abbreviator;
    31
    32  public class ProgressListenerImpl implements ProgressListener {
    33
    34    static final int TARGET_FILE_LENGTH = 85;
    35    static final int UPDATE_THRESHOLD = 100;
    36
    37    int addFileCount = 0;
    38    int scanFileCount = 0;
    39    int inplaceConversionCount = 0;
    40    final MigratorFrame frame;
    41
    42    Abbreviator abbr;
    43
    44    long lastUpdate = 0;
    45
    46
    47
    48    public ProgressListenerImpl(File projectFolder, MigratorFrame frame) {
    49      this.frame = frame;
    50      this.abbr = new Abbreviator((int) projectFolder.length(),
    51          TARGET_FILE_LENGTH, File.separatorChar);
    52    }
    53
    54    public void onMigrationBegin() {
    55      frame.disableInput();
    56    }
    57
    58    boolean isTooSoon() {
    59      long now = System.currentTimeMillis();
    60      if(now-lastUpdate < UPDATE_THRESHOLD) {
    61        return true;
    62      } else {
    63        lastUpdate = now;
    64        return false;
    65      }
    66    }
    67
    68    public void onDirectory(File file) {
    69      if(isTooSoon()) return;
    70
    71      String abbreviatedName = getShortName(file);
    72      frame.otherLabel.setText("<html><p>Searching folder [" + abbreviatedName
    73          + "]<p>Found " + addFileCount + " java files to scan.</html>");
    74    }
    75
    76    public void onDone() {
    77      frame.progressBar.setVisible(false);
    78      frame.otherLabel.setText("<html><font color='BLUE'>Scanned " + addFileCount
    79          + " java files, " + inplaceConversionCount
    80          + " files were modified.</font></html>");
    81
    82      frame.migrateButton.setActionCommand(MigratorFrame.EXIT_COMMAND);
    83      frame.migrateButton.setText("Exit");
    84      frame.migrateButton
    85          .setToolTipText("Click on this button to exit this application.");
    86      frame.migrateButton.setEnabled(true);
    87
    88    }
    89
    90    public void onFileAddition(File file) {
    91      addFileCount++;
    92    }
    93
    94    public void onFileScan(File file) {
    95
    96      scanFileCount++;
    97      if(isTooSoon()) return;
    98      String abbreviatedName = getShortName(file);
    99
    100     frame.otherLabel.setText("<html><p>Scanning file [" + abbreviatedName
    101         + "]<p></html>");
    102     // File + scanFileCount + " out of "+ addFileCount+" files to scan."+
    103     // inplaceConversionCount+ " files converted." +
    104
    105     frame.progressBar.setValue(scanFileCount);
    106   }
    107
    108   public void onInplaceConversion(File file) {
    109     inplaceConversionCount++;
    110   }
    111
    112   String getShortName(File file) {
    113     try {
    114       return abbr.abbreviate(file.getCanonicalPath());
    115     } catch (IOException e) {
    116       return file.toString();
    117     }
    118   }
    119
    120   public void onFileScanBegin() {
    121     frame.progressBar.setMaximum(addFileCount);
    122     frame.progressBar.setValue(0);
    123     frame.progressBar.setVisible(true);
    124   }
    125
    126 }