001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.migrator.internal;
026
027import java.io.File;
028import java.io.IOException;
029
030import org.slf4j.migrator.helper.Abbreviator;
031
032public class ProgressListenerImpl implements ProgressListener {
033
034    static final int TARGET_FILE_LENGTH = 85;
035    static final int UPDATE_THRESHOLD = 100;
036
037    int addFileCount = 0;
038    int scanFileCount = 0;
039    int inplaceConversionCount = 0;
040    final MigratorFrame frame;
041
042    Abbreviator abbr;
043
044    long lastUpdate = 0;
045
046    public ProgressListenerImpl(File projectFolder, MigratorFrame frame) {
047        this.frame = frame;
048        this.abbr = new Abbreviator((int) projectFolder.length(), TARGET_FILE_LENGTH, File.separatorChar);
049    }
050
051    public void onMigrationBegin() {
052        frame.disableInput();
053    }
054
055    boolean isTooSoon() {
056        long now = System.currentTimeMillis();
057        if (now - lastUpdate < UPDATE_THRESHOLD) {
058            return true;
059        } else {
060            lastUpdate = now;
061            return false;
062        }
063    }
064
065    public void onDirectory(File file) {
066        if (isTooSoon())
067            return;
068
069        String abbreviatedName = getShortName(file);
070        frame.otherLabel.setText("<html><p>Searching folder [" + abbreviatedName + "]<p>Found " + addFileCount + " java files to scan.</html>");
071    }
072
073    public void onDone() {
074        frame.progressBar.setVisible(false);
075        frame.otherLabel.setText("<html><font color='BLUE'>Scanned " + addFileCount + " java files, " + inplaceConversionCount
076                        + " files were modified.</font></html>");
077
078        frame.migrateButton.setActionCommand(MigratorFrame.EXIT_COMMAND);
079        frame.migrateButton.setText("Exit");
080        frame.migrateButton.setToolTipText("Click on this button to exit this application.");
081        frame.migrateButton.setEnabled(true);
082
083    }
084
085    public void onFileAddition(File file) {
086        addFileCount++;
087    }
088
089    public void onFileScan(File file) {
090
091        scanFileCount++;
092        if (isTooSoon())
093            return;
094        String abbreviatedName = getShortName(file);
095
096        frame.otherLabel.setText("<html><p>Scanning file [" + abbreviatedName + "]<p></html>");
097        // File + scanFileCount + " out of "+ addFileCount+" files to scan."+
098        // inplaceConversionCount+ " files converted." +
099
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}