1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.aikiit.fotorenamer.image;
17
18 import com.google.common.collect.Lists;
19 import de.aikiit.fotorenamer.exception.InvalidDirectoryException;
20 import de.aikiit.fotorenamer.exception.NoFilesFoundException;
21 import de.aikiit.fotorenamer.exception.RenamingErrorException;
22 import de.aikiit.fotorenamer.gui.ProgressBar;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25
26 import javax.swing.*;
27 import java.io.File;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.concurrent.atomic.AtomicInteger;
31
32 import static de.aikiit.fotorenamer.util.LocalizationHelper.getBundleString;
33 import static de.aikiit.fotorenamer.util.LocalizationHelper.getParameterizedBundleString;
34
35
36
37
38
39
40
41
42 public class RemoveExifPrefixRenamer implements Runnable {
43 private static final Logger LOG =
44 LogManager.getLogger(RemoveExifPrefixRenamer.class);
45
46
47
48
49 private static final String REPLACE_PATTERN = "\\d{8}[_]\\d{4}[_]";
50
51 private final File currentDirectory;
52 private List<File> listOfFiles = Lists.newArrayList();
53 private ProgressBar progressBar = null;
54 private final AtomicInteger done = new AtomicInteger(0);
55
56
57
58
59
60
61
62
63
64 public RemoveExifPrefixRenamer(final String directory)
65 throws InvalidDirectoryException, NoFilesFoundException {
66 this.currentDirectory = new File(directory);
67 checkInputAndInitialize();
68 new Thread(this).start();
69 }
70
71
72
73
74
75
76
77
78 private void rename() throws RenamingErrorException {
79 for (final File listOfFile : this.listOfFiles) {
80 String name = listOfFile.getName();
81 String nameNeu = name.replaceFirst(REPLACE_PATTERN, "");
82
83
84 if (!nameNeu.equalsIgnoreCase(name)) {
85 done.incrementAndGet();
86 }
87
88
89 this.progressBar.setProgress();
90 this.progressBar.setText(name);
91 this.progressBar.updateUI();
92
93
94 if (listOfFile.isFile() && !listOfFile.renameTo(
95 new File(listOfFile.getParent()
96 + File.separatorChar + nameNeu))) {
97 LOG.error("Problem with file " + listOfFile.getName());
98 throw new RenamingErrorException(getParameterizedBundleString("fotorenamer.ui.rerename.error.detail",
99 listOfFile.getName()));
100 }
101 }
102 }
103
104
105
106
107
108
109
110
111
112 private void checkInputAndInitialize()
113 throws NoFilesFoundException, InvalidDirectoryException {
114
115 if (this.currentDirectory == null || !this.currentDirectory.isDirectory()) {
116 throw new InvalidDirectoryException("" + this.currentDirectory);
117 }
118
119 final File[] files = this.currentDirectory.listFiles(
120 new ImageFilenameFilter());
121
122
123 if(files == null || files.length == 0) {
124 throw new NoFilesFoundException(this.currentDirectory);
125 }
126
127 this.listOfFiles = Arrays.asList(files);
128 }
129
130
131
132
133
134
135
136 public final void run() {
137 this.progressBar = new ProgressBar(this.listOfFiles.size());
138
139 try {
140 rename();
141 } catch (RenamingErrorException uf) {
142 JOptionPane.showMessageDialog(null, getParameterizedBundleString("fotorenamer.ui.rename.error", uf.getMessage()),
143 getBundleString("fotorenamer.ui.rerename.error.title"),
144 JOptionPane.ERROR_MESSAGE);
145 return;
146 }
147 this.progressBar.dispose();
148
149 String statusMessage;
150 if (this.done.get() == 0) {
151 statusMessage = getParameterizedBundleString("fotorenamer.ui.rename.success.message.none", this.currentDirectory.getName());
152 } else if (this.done.get() == 1) {
153 statusMessage = getParameterizedBundleString("fotorenamer.ui.rename.success.message.one", this.currentDirectory.getName());
154 } else {
155 statusMessage = getParameterizedBundleString("fotorenamer.ui.rename.success.message", this.done, this.listOfFiles.size(), this.currentDirectory.getName());
156 }
157 JOptionPane.showMessageDialog(null, statusMessage, getBundleString("fotorenamer.ui.rerename.success.title"),
158 JOptionPane.INFORMATION_MESSAGE);
159 }
160 }