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.concurrent.atomic.AtomicInteger;
34 import java.util.function.Consumer;
35 import java.util.function.Predicate;
36
37 import static de.aikiit.fotorenamer.util.LocalizationHelper.getBundleString;
38 import static de.aikiit.fotorenamer.util.LocalizationHelper.getParameterizedBundleString;
39
40
41
42
43
44
45
46
47
48
49 @NoArgsConstructor(access = AccessLevel.PRIVATE)
50 abstract class AbstractImageRenamer implements Runnable {
51
52
53
54
55 private static final Logger LOG = 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 AtomicInteger amountOfFiles = new AtomicInteger(0);
74
75
76
77
78
79
80
81
82
83
84
85 AbstractImageRenamer(final String directory) throws InvalidDirectoryException, NoFilesFoundException {
86
87 if (directory == null) {
88 throw new InvalidDirectoryException("null is not a directory");
89 }
90
91 this.currentDirectory = new File(directory);
92 if (!this.currentDirectory.isDirectory()) {
93 throw new InvalidDirectoryException(this.currentDirectory);
94 }
95
96
97 File[] files = this.currentDirectory.listFiles(new ImageFilenameFilter());
98 if (files == null || files.length == 0) {
99 throw new NoFilesFoundException(this.currentDirectory);
100 }
101 this.imageList = Lists.newArrayList(files);
102 this.amountOfFiles = new AtomicInteger(this.imageList.size());
103 }
104
105
106
107
108 private void renameFiles() {
109 LOG.info("Starting to rename {} files.", this.amountOfFiles);
110
111 Consumer<File> consumer = file -> {
112
113 String targetFilename = renameImage(file);
114
115
116 progressBar.setProgress();
117 progressBar.setText(file.getName());
118 progressBar.updateUI();
119
120
121 try {
122 Files.move(file.toPath(), new File(file.getParent(), targetFilename).toPath());
123 } catch (IOException e) {
124 LOG.error("Unable to rename '{}' to '{}'", file.getName(), targetFilename);
125 }
126 };
127 Predicate<File> fileOnly = file -> file != null && file.isFile();
128
129 this.imageList.parallelStream().filter(fileOnly).forEach(consumer);
130 }
131
132
133
134
135
136
137
138
139
140
141 protected abstract String renameImage(File imageFile);
142
143
144
145
146
147
148
149 public final void run() {
150 this.progressBar = new ProgressBar(this.amountOfFiles.get());
151
152 try {
153 renameFiles();
154 } catch (Exception e) {
155 JOptionPane.showMessageDialog(null, getParameterizedBundleString("fotorenamer.ui.rename.error", MoreObjects.firstNonNull(e.getMessage(), e.getClass().getSimpleName())), getBundleString("fotorenamer.ui.rename.error.title"), JOptionPane.ERROR_MESSAGE);
156
157 this.amountOfFiles = new AtomicInteger(0);
158 } finally {
159 this.progressBar.dispose();
160 }
161
162
163 StringBuilder notification = new StringBuilder();
164 switch (this.amountOfFiles.get()) {
165 case 0:
166 notification.append(getParameterizedBundleString("fotorenamer.ui.rename.success.message.none", this.currentDirectory.getName()));
167 break;
168 case 1:
169 notification.append(getParameterizedBundleString("fotorenamer.ui.rename.success.message.one", this.currentDirectory.getName()));
170 break;
171 default:
172 notification.append(getParameterizedBundleString("fotorenamer.ui.rename.success.message", this.amountOfFiles, this.currentDirectory.getName()));
173 break;
174 }
175
176 notification.append("\n\n");
177 JOptionPane.showMessageDialog(null, notification.toString(), getBundleString("fotorenamer.ui.rename.success.title"), JOptionPane.INFORMATION_MESSAGE);
178 }
179 }