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      public ProgressListenerImpl(File projectFolder, MigratorFrame frame) {
    47          this.frame = frame;
    48          this.abbr = new Abbreviator((int) projectFolder.length(), TARGET_FILE_LENGTH, File.separatorChar);
    49      }
    50
    51      public void onMigrationBegin() {
    52          frame.disableInput();
    53      }
    54
    55      boolean isTooSoon() {
    56          long now = System.currentTimeMillis();
    57          if (now - lastUpdate < UPDATE_THRESHOLD) {
    58              return true;
    59          } else {
    60              lastUpdate = now;
    61              return false;
    62          }
    63      }
    64
    65      public void onDirectory(File file) {
    66          if (isTooSoon())
    67              return;
    68
    69          String abbreviatedName = getShortName(file);
    70          frame.otherLabel.setText("<html><p>Searching folder [" + abbreviatedName + "]<p>Found " + addFileCount + " java files to scan.</html>");
    71      }
    72
    73      public void onDone() {
    74          frame.progressBar.setVisible(false);
    75          frame.otherLabel.setText("<html><font color='BLUE'>Scanned " + addFileCount + " java files, " + inplaceConversionCount
    76                          + " files were modified.</font></html>");
    77
    78          frame.migrateButton.setActionCommand(MigratorFrame.EXIT_COMMAND);
    79          frame.migrateButton.setText("Exit");
    80          frame.migrateButton.setToolTipText("Click on this button to exit this application.");
    81          frame.migrateButton.setEnabled(true);
    82
    83      }
    84
    85      public void onFileAddition(File file) {
    86          addFileCount++;
    87      }
    88
    89      public void onFileScan(File file) {
    90
    91          scanFileCount++;
    92          if (isTooSoon())
    93              return;
    94          String abbreviatedName = getShortName(file);
    95
    96          frame.otherLabel.setText("<html><p>Scanning file [" + abbreviatedName + "]<p></html>");
    97          // File + scanFileCount + " out of "+ addFileCount+" files to scan."+
    98          // inplaceConversionCount+ " files converted." +
    99
    100         frame.progressBar.setValue(scanFileCount);
    101     }
    102
    103     public void onInplaceConversion(File file) {
    104         inplaceConversionCount++;
    105     }
    106
    107     String getShortName(File file) {
    108         try {
    109             return abbr.abbreviate(file.getCanonicalPath());
    110         } catch (IOException e) {
    111             return file.toString();
    112         }
    113     }
    114
    115     public void onFileScanBegin() {
    116         frame.progressBar.setMaximum(addFileCount);
    117         frame.progressBar.setValue(0);
    118         frame.progressBar.setVisible(true);
    119     }
    120
    121 }