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 = LogManager.getLogger(AbstractImageRenamer.class);
55
56
57
58
59 private File currentDirectory = null;
60
61
62
63 private List<File> imageList = null;
64
65
66
67
68 private ProgressBar progressBar = null;
69
70
71
72 private int amountOfFiles = 0;
73
74
75
76
77
78
79
80
81
82
83
84 AbstractImageRenamer(final String directory) throws InvalidDirectoryException, NoFilesFoundException {
85
86 if (directory == null) {
87 throw new InvalidDirectoryException("null is not a directory");
88 }
89
90 this.currentDirectory = new File(directory);
91 if (!this.currentDirectory.isDirectory()) {
92 throw new InvalidDirectoryException(this.currentDirectory);
93 }
94
95
96 File[] files = this.currentDirectory.listFiles(new ImageFilenameFilter());
97 if (files == null || files.length == 0) {
98 throw new NoFilesFoundException(this.currentDirectory);
99 }
100 this.imageList = Lists.newArrayList(files);
101 this.amountOfFiles = this.imageList.size();
102 }
103
104
105
106
107 private void renameFiles() {
108 LOG.info("Starting to rename {} files.", this.amountOfFiles);
109
110 Consumer<File> consumer = file -> {
111
112 String targetFilename = renameImage(file);
113
114
115 progressBar.setProgress();
116 progressBar.setText(file.getName());
117 progressBar.updateUI();
118
119
120 try {
121 Files.move(file.toPath(), new File(file.getParent(), targetFilename).toPath());
122 } catch (IOException e) {
123 LOG.error("Unable to rename '{}' to '{}'", file.getName(), targetFilename);
124 }
125 };
126 Predicate<File> fileOnly = file -> file != null && file.isFile();
127
128 this.imageList.parallelStream().filter(fileOnly).forEach(consumer);
129 }
130
131
132
133
134
135
136
137
138
139
140 protected abstract String renameImage(File imageFile);
141
142
143
144
145
146
147
148 public final void run() {
149 this.progressBar = new ProgressBar(this.amountOfFiles);
150
151 try {
152 renameFiles();
153 } catch (Exception e) {
154 JOptionPane.showMessageDialog(null, getParameterizedBundleString("fotorenamer.ui.rename.error", MoreObjects.firstNonNull(e.getMessage(), e.getClass().getSimpleName())), getBundleString("fotorenamer.ui.rename.error.title"), JOptionPane.ERROR_MESSAGE);
155
156 this.amountOfFiles = 0;
157 } finally {
158 this.progressBar.dispose();
159 }
160
161
162 StringBuilder notification = new StringBuilder();
163 switch (this.amountOfFiles) {
164 case 0:
165 notification.append(getParameterizedBundleString("fotorenamer.ui.rename.success.message.none", this.currentDirectory.getName()));
166 break;
167 case 1:
168 notification.append(getParameterizedBundleString("fotorenamer.ui.rename.success.message.one", this.currentDirectory.getName()));
169 break;
170 default:
171 notification.append(getParameterizedBundleString("fotorenamer.ui.rename.success.message", this.amountOfFiles, this.currentDirectory.getName()));
172 break;
173 }
174
175 notification.append("\n\n");
176 JOptionPane.showMessageDialog(null, notification.toString(), getBundleString("fotorenamer.ui.rename.success.title"), JOptionPane.INFORMATION_MESSAGE);
177 }
178 }