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 createBaseLayout();
107 initTextField();
108 makeTextFieldDragAndDropable();
109 makeTextFieldReactOnEnterOrCopyPasteFromMainUI();
110 }
111
112 private void createBaseLayout() {
113 GridBagLayout grid = new GridBagLayout();
114 GridBagConstraints gbc = new GridBagConstraints();
115 gbc.insets = new Insets(0, 2, 0, 2);
116 setLayout(grid);
117
118
119 gbc.gridx = 0;
120 gbc.gridy = 0;
121 gbc.gridheight = 1;
122 gbc.gridwidth = 2;
123 gbc.anchor = GridBagConstraints.WEST;
124 textField = new JTextField(60);
125 grid.setConstraints(textField, gbc);
126 add(textField);
127
128
129 gbc.gridx = 2;
130 gbc.gridy = 0;
131 gbc.gridheight = 1;
132 gbc.gridwidth = 1;
133 gbc.anchor = GridBagConstraints.EAST;
134
135
136 browseButton = this.imageIcon == null
137 ? new JButton(
138 getBundleString(
139 "fotorenamer.ui.selector.title"))
140 : new JButton(
141 getBundleString(
142 "fotorenamer.ui.selector.title"), this.imageIcon);
143 browseButton.setMnemonic(getBundleString("fotorenamer.ui.selector.title.mnemonic").charAt(0));
144 browseButton.setMargin(new Insets(1, 1, 1, 1));
145 grid.setConstraints(browseButton, gbc);
146 add(browseButton);
147 }
148
149 private void initTextField() {
150
151 browseButton.addActionListener(event -> {
152
153 String currentPath = getSelectedDirectory();
154 JFileChooser fileDlg = new JFileChooser(com.google.common.base.Strings.isNullOrEmpty(currentPath) ? null : currentPath);
155
156 fileDlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
157 fileDlg.setDialogTitle(
158 getBundleString(
159 "fotorenamer.ui.selector.directory"));
160 fileDlg.setApproveButtonText(getBundleString(
161 "fotorenamer.ui.selector.select"));
162
163 if (fileDlg.showOpenDialog(this)
164 == JFileChooser.APPROVE_OPTION) {
165
166
167 try {
168 textField.setText(
169 fileDlg.getSelectedFile().getCanonicalPath());
170 } catch (IOException ioe) {
171 LOG.error("Error while selecting directory, extracted text is: {}", textField.getText());
172 LOG.error(ioe.getMessage());
173 }
174 }
175 });
176 }
177
178 private void makeTextFieldReactOnEnterOrCopyPasteFromMainUI() {
179
180 textField.addKeyListener(new KeyAdapter() {
181 @Override
182 public void keyPressed(final KeyEvent e) {
183 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
184
185 SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
186 @Override
187 protected Void doInBackground() {
188 if (isWaiting()) {
189 showErrorPopup(getBundleString("fotorenamer.ui.error.nodirectory"), getBundleString("fotorenamer.ui.error.nodirectory.title"));
190 return null;
191 }
192
193
194 try {
195 CreationDateFromExifImageRenamer renamer =
196 new CreationDateFromExifImageRenamer(getSelectedDirectory());
197 new Thread(renamer).start();
198 } catch (InvalidDirectoryException uv) {
199 LOG.info("Invalid directory selected: {}", uv.getMessage());
200 showErrorPopup(getParameterizedBundleString("fotorenamer.ui.error.invaliddirectory", uv.getMessage()), getBundleString("fotorenamer.ui.error.invaliddirectory.title"));
201 } catch (NoFilesFoundException kde) {
202 LOG.info("No files found in {}", kde.getMessage());
203 showErrorPopup(getParameterizedBundleString("fotorenamer.ui.error.nofiles", kde.getMessage()), getBundleString("fotorenamer.ui.error.nofiles.title"));
204 }
205 return null;
206 }
207
208 @Override
209 protected void done() {
210
211 LOG.debug("Finished working, cannot reset UI from the selector itself. Should find a way to lock the startbutton somehow.");
212 }
213 };
214
215 worker.execute();
216 }
217 }
218
219 });
220 }
221
222 private void makeTextFieldDragAndDropable() {
223
224 textField.setDropTarget(new DropTarget() {
225 public synchronized void drop(DropTargetDropEvent evt) {
226 try {
227 evt.acceptDrop(DnDConstants.ACTION_COPY);
228 Object transferData = evt
229 .getTransferable().getTransferData(
230 DataFlavor.javaFileListFlavor);
231
232 if (transferData instanceof java.util.List) {
233
234 java.util.List<File> droppedFiles = (java.util.List<File>) transferData;
235 if (!droppedFiles.isEmpty()) {
236 for (File droppedFile : droppedFiles) {
237 if (droppedFile.isDirectory()) {
238 final String path = droppedFile.getAbsolutePath();
239 LOG.info("Drag'n'drop done for file: {} with {} element(s) received", path, droppedFiles.size());
240 textField.setText(path);
241 break;
242 }
243 }
244 }
245 }
246
247 } catch (Exception ex) {
248 LOG.info("Drag'd'drop did not work due to exception", ex);
249 }
250 }
251 });
252 }
253
254 void showErrorPopup(final String message, final String title) {
255 JOptionPane.showMessageDialog(null,
256 message,
257 title,
258 JOptionPane.ERROR_MESSAGE);
259 }
260
261
262
263
264
265
266 final String getSelectedDirectory() {
267 String currentSelection = LocalizationHelper.removeCrLf(this.textField.getText());
268 if (!com.google.common.base.Strings.isNullOrEmpty(currentSelection)) {
269 currentSelection = currentSelection.replaceAll("~", System.getProperty("user.home"));
270 currentSelection = currentSelection.trim();
271 LOG.debug("User input transformed into {}", currentSelection);
272 }
273 return currentSelection;
274 }
275 }