1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
98
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 }