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: "
172 + textField.getText());
173 LOG.error(ioe.getMessage());
174 }
175 }
176 });
177 }
178
179 private void makeTextFieldReactOnEnterOrCopyPasteFromMainUI() {
180
181 textField.addKeyListener(new KeyAdapter() {
182 @Override
183 public void keyPressed(final KeyEvent e) {
184 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
185
186 SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
187 @Override
188 protected Void doInBackground() {
189 if (isWaiting()) {
190 showErrorPopup(getBundleString("fotorenamer.ui.error.nodirectory"), getBundleString("fotorenamer.ui.error.nodirectory.title"));
191 return null;
192 }
193
194
195 try {
196 CreationDateFromExifImageRenamer renamer =
197 new CreationDateFromExifImageRenamer(getSelectedDirectory());
198 new Thread(renamer).start();
199 } catch (InvalidDirectoryException uv) {
200 LOG.info("Invalid directory selected: {}", uv.getMessage());
201 showErrorPopup(getParameterizedBundleString("fotorenamer.ui.error.invaliddirectory", uv.getMessage()), getBundleString("fotorenamer.ui.error.invaliddirectory.title"));
202 } catch (NoFilesFoundException kde) {
203 LOG.info("No files found in {}", kde.getMessage());
204 showErrorPopup(getParameterizedBundleString("fotorenamer.ui.error.nofiles", kde.getMessage()), getBundleString("fotorenamer.ui.error.nofiles.title"));
205 }
206 return null;
207 }
208
209 @Override
210 protected void done() {
211
212 LOG.debug("Finished working, cannot reset UI from the selector itself. Should find a way to lock the startbutton somehow.");
213 }
214 };
215
216 worker.execute();
217 }
218 }
219
220 });
221 }
222
223 private void makeTextFieldDragAndDropable() {
224
225 textField.setDropTarget(new DropTarget() {
226 public synchronized void drop(DropTargetDropEvent evt) {
227 try {
228 evt.acceptDrop(DnDConstants.ACTION_COPY);
229 Object transferData = evt
230 .getTransferable().getTransferData(
231 DataFlavor.javaFileListFlavor);
232
233 if (transferData instanceof java.util.List) {
234
235 java.util.List<File> droppedFiles = (java.util.List<File>) transferData;
236 if (!droppedFiles.isEmpty()) {
237 for (File droppedFile : droppedFiles) {
238 if (droppedFile.isDirectory()) {
239 final String path = droppedFile.getAbsolutePath();
240 LOG.info("Drag'n'drop done for file: {} with {} element(s) received", path, droppedFiles.size());
241 textField.setText(path);
242 break;
243 }
244 }
245 }
246 }
247
248 } catch (Exception ex) {
249 LOG.info("Drag'd'drop did not work due to exception", ex);
250 }
251 }
252 });
253 }
254
255 void showErrorPopup(final String message, final String title) {
256 JOptionPane.showMessageDialog(null,
257 message,
258 title,
259 JOptionPane.ERROR_MESSAGE);
260 }
261
262
263
264
265
266
267 final String getSelectedDirectory() {
268 String currentSelection = LocalizationHelper.removeCrLf(this.textField.getText());
269 if (!com.google.common.base.Strings.isNullOrEmpty(currentSelection)) {
270 currentSelection = currentSelection.replaceAll("~", System.getProperty("user.home"));
271 currentSelection = currentSelection.trim();
272 LOG.debug("User input transformed into {}", currentSelection);
273 }
274 return currentSelection;
275 }
276 }