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.base.MoreObjects;
19 import com.google.common.collect.Lists;
20 import de.aikiit.fotorenamer.exception.InvalidDirectoryException;
21 import de.aikiit.fotorenamer.exception.NoFilesFoundException;
22 import de.aikiit.fotorenamer.gui.ProgressBar;
23 import lombok.AccessLevel;
24 import lombok.NoArgsConstructor;
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27
28 import javax.swing.*;
29 import java.io.File;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.util.List;
33 import java.util.function.Consumer;
34 import java.util.function.Predicate;
35
36 import static de.aikiit.fotorenamer.util.LocalizationHelper.getBundleString;
37 import static de.aikiit.fotorenamer.util.LocalizationHelper.getParameterizedBundleString;
38
39
40
41
42
43
44
45
46
47
48 @NoArgsConstructor(access = AccessLevel.PRIVATE)
49 abstract class AbstractImageRenamer implements Runnable {
50
51
52
53
54 private static final Logger LOG =
55 LogManager.getLogger(AbstractImageRenamer.class);
56
57
58
59
60 private File currentDirectory = null;
61
62
63
64 private List<File> imageList = null;
65
66
67
68
69 private ProgressBar progressBar = null;
70
71
72
73 private int amountOfFiles = 0;
74
75
76
77
78
79
80
81
82
83
84
85 AbstractImageRenamer(final String directory)
86 throws InvalidDirectoryException, NoFilesFoundException {
87
88 if (directory == null) {
89 throw new InvalidDirectoryException("null is not a directory");
90 }
91
92 this.currentDirectory = new File(directory);
93 if (!this.currentDirectory.isDirectory()) {
94 throw new InvalidDirectoryException(this.currentDirectory);
95 }
96
97
98 File[] files = this.currentDirectory.listFiles(
99 new ImageFilenameFilter());
100 if (files == null || files.length == 0) {
101 throw new NoFilesFoundException(this.currentDirectory);
102 }
103 this.imageList = Lists.newArrayList(files);
104 this.amountOfFiles = this.imageList.size();
105 }
106
107
108
109
110 private void renameFiles() {
111 LOG.info("Starting to rename " + this.amountOfFiles + " files.");
112
113 Consumer<File> consumer = file -> {
114
115 String targetFilename = renameImage(file);
116
117
118 progressBar.setProgress();
119 progressBar.setText(file.getName());
120 progressBar.updateUI();
121
122
123 try {
124 Files.move(file.toPath(), new File(file.getParent(),
125 targetFilename).toPath());
126 } catch (IOException e) {
127 LOG.error("Unable to rename '"
128 + file.getName() + "' to '"
129 + targetFilename + "'");
130 }
131 };
132 Predicate<File> fileOnly = file -> file != null && file.isFile();
133
134 this.imageList.parallelStream().filter(fileOnly).forEach(consumer);
135 }
136
137
138
139
140
141
142
143
144
145
146 protected abstract String renameImage(File imageFile);
147
148
149
150
151
152
153
154 public final void run() {
155 String notification;
156 this.progressBar = new ProgressBar(this.amountOfFiles);
157
158 try {
159 renameFiles();
160 } catch (Exception e) {
161 JOptionPane.showMessageDialog(null,
162 getParameterizedBundleString("fotorenamer.ui.rename.error", MoreObjects.firstNonNull(e.getMessage(), e.getClass().getSimpleName())),
163 getBundleString("fotorenamer.ui.rename.error.title"),
164 JOptionPane.ERROR_MESSAGE);
165
166 this.amountOfFiles = 0;
167 } finally {
168 this.progressBar.dispose();
169 }
170
171
172 switch (this.amountOfFiles) {
173 case 0:
174 notification = getParameterizedBundleString("fotorenamer.ui.rename.success.message.none", this.currentDirectory.getName());
175 break;
176 case 1:
177 notification = getParameterizedBundleString("fotorenamer.ui.rename.success.message.one", this.currentDirectory.getName());
178 break;
179 default:
180 notification = getParameterizedBundleString("fotorenamer.ui.rename.success.message", this.amountOfFiles, this.currentDirectory.getName());
181 break;
182 }
183
184 notification += "\n\n";
185 JOptionPane.showMessageDialog(null, notification, getBundleString("fotorenamer.ui.rename.success.title"),
186 JOptionPane.INFORMATION_MESSAGE);
187 }
188 }