1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.aikiit.fotorenamer.gui;
17
18 import de.aikiit.fotorenamer.exception.InvalidDirectoryException;
19 import de.aikiit.fotorenamer.exception.NoFilesFoundException;
20 import de.aikiit.fotorenamer.image.CreationDateFromExifImageRenamer;
21 import de.aikiit.fotorenamer.util.LocalizationHelper;
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 import org.apache.logging.log4j.util.Strings;
25
26 import javax.swing.*;
27 import java.awt.*;
28 import java.awt.datatransfer.DataFlavor;
29 import java.awt.dnd.DnDConstants;
30 import java.awt.dnd.DropTarget;
31 import java.awt.dnd.DropTargetDropEvent;
32 import java.awt.event.KeyAdapter;
33 import java.awt.event.KeyEvent;
34 import java.io.File;
35 import java.io.IOException;
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 class ImageDirectorySelector extends JPanel {
47
48
49
50 private static final Logger LOG =
51 LogManager.getLogger(ImageDirectorySelector.class);
52
53
54
55
56 private JTextField textField = null;
57
58
59
60 private JButton browseButton = null;
61
62
63
64 private final ImageIcon imageIcon;
65
66
67
68
69
70
71
72
73 ImageDirectorySelector(final ImageIcon icon) {
74 super();
75 this.imageIcon = icon;
76 init();
77 }
78
79
80
81
82
83
84
85 public final void setEnabled(final boolean enable) {
86 textField.setEnabled(enable);
87 browseButton.setEnabled(enable);
88 }
89
90
91
92
93
94
95
96
97
98 public final boolean isWaiting() {
99 return Strings.isEmpty(getSelectedDirectory());
100 }
101
102
103
104
105 private void init() {
106
107 GridBagLayout grid = new GridBagLayout();
108 GridBagConstraints gbc = new GridBagConstraints();
109 gbc.insets = new Insets(0, 2, 0, 2);
110 setLayout(grid);
111
112
113 gbc.gridx = 0;
114 gbc.gridy = 0;
115 gbc.gridheight = 1;
116 gbc.gridwidth = 2;
117 gbc.anchor = GridBagConstraints.WEST;
118 textField = new JTextField(60);
119 grid.setConstraints(textField, gbc);
120 add(textField);
121
122
123 gbc.gridx = 2;
124 gbc.gridy = 0;
125 gbc.gridheight = 1;
126 gbc.gridwidth = 1;
127 gbc.anchor = GridBagConstraints.EAST;
128
129
130 browseButton = this.imageIcon == null
131 ? new JButton(
132 getBundleString(
133 "fotorenamer.ui.selector.title"))
134 : new JButton(
135 getBundleString(
136 "fotorenamer.ui.selector.title"), this.imageIcon);
137 browseButton.setMnemonic(getBundleString("fotorenamer.ui.selector.title.mnemonic").charAt(0));
138 browseButton.setMargin(new Insets(1, 1, 1, 1));
139 grid.setConstraints(browseButton, gbc);
140 add(browseButton);
141
142
143 browseButton.addActionListener(event -> {
144
145 String currentPath = getSelectedDirectory();
146 JFileChooser fileDlg = new JFileChooser(com.google.common.base.Strings.isNullOrEmpty(currentPath) ? null : currentPath);
147
148 fileDlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
149 fileDlg.setDialogTitle(
150 getBundleString(
151 "fotorenamer.ui.selector.directory"));
152 fileDlg.setApproveButtonText(getBundleString(
153 "fotorenamer.ui.selector.select"));
154
155 if (fileDlg.showOpenDialog(this)
156 == JFileChooser.APPROVE_OPTION) {
157
158
159 try {
160 textField.setText(
161 fileDlg.getSelectedFile().getCanonicalPath());
162 } catch (IOException ioe) {
163 LOG.error("Error while selecting directory, extracted text is: "
164 + textField.getText());
165 LOG.error(ioe.getMessage());
166 }
167 }
168 });
169
170
171 textField.setDropTarget(new DropTarget() {
172 public synchronized void drop(DropTargetDropEvent evt) {
173 try {
174 evt.acceptDrop(DnDConstants.ACTION_COPY);
175 Object transferData = evt
176 .getTransferable().getTransferData(
177 DataFlavor.javaFileListFlavor);
178
179 if (transferData instanceof java.util.List) {
180
181 java.util.List<File> droppedFiles = (java.util.List<File>) transferData;
182 if (!droppedFiles.isEmpty()) {
183 for (File droppedFile : droppedFiles) {
184 if (droppedFile.isDirectory()) {
185 final String path = droppedFile.getAbsolutePath();
186 LOG.info("Drag'n'drop done for file: " + path + " with " + droppedFiles.size() + " element(s) received");
187 textField.setText(path);
188 break;
189 }
190 }
191 }
192 }
193
194 } catch (Exception ex) {
195 LOG.info("Drag'd'drop did not work due to " + ex);
196 }
197 }
198 });
199
200
201 textField.addKeyListener(new KeyAdapter() {
202 @Override
203 public void keyPressed(final KeyEvent e) {
204 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
205
206 SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
207 @Override
208 protected Void doInBackground() {
209 if (isWaiting()) {
210 showErrorPopup(getBundleString("fotorenamer.ui.error.nodirectory"), getBundleString("fotorenamer.ui.error.nodirectory.title"));
211 return null;
212 }
213
214
215 try {
216 CreationDateFromExifImageRenamer renamer =
217 new CreationDateFromExifImageRenamer(getSelectedDirectory());
218 new Thread(renamer).start();
219 } catch (InvalidDirectoryException uv) {
220 LOG.info("Invalid directory selected: " + uv.getMessage());
221 showErrorPopup(getParameterizedBundleString("fotorenamer.ui.error.invaliddirectory", uv.getMessage()), getBundleString("fotorenamer.ui.error.invaliddirectory.title"));
222 } catch (NoFilesFoundException kde) {
223 LOG.info("No files found in " + kde.getMessage());
224 showErrorPopup(getParameterizedBundleString("fotorenamer.ui.error.nofiles", kde.getMessage()), getBundleString("fotorenamer.ui.error.nofiles.title"));
225 }
226 return null;
227 }
228
229 @Override
230 protected void done() {
231
232 LOG.debug("Finished working, cannot reset UI from the selector itself. Should find a way to lock the startbutton somehow.");
233 }
234 };
235
236 worker.execute();
237 }
238 }
239
240 });
241 }
242
243 void showErrorPopup(final String message, final String title) {
244 JOptionPane.showMessageDialog(null,
245 message,
246 title,
247 JOptionPane.ERROR_MESSAGE);
248 }
249
250
251
252
253
254
255 final String getSelectedDirectory() {
256 String currentSelection = LocalizationHelper.removeCrLf(this.textField.getText());
257 if (!com.google.common.base.Strings.isNullOrEmpty(currentSelection)) {
258 currentSelection = currentSelection.replaceAll("~", System.getProperty("user.home"));
259 currentSelection = currentSelection.trim();
260 LOG.debug("User input transformed into " + currentSelection);
261 }
262 return currentSelection;
263 }
264 }