View Javadoc
1   /**
2    * Copyright 2011, Aiki IT, FotoRenamer
3    * <p/>
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * <p/>
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * <p/>
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.image.RemoveExifPrefixRenamer;
22  import de.aikiit.fotorenamer.util.ComponentGaugeUtil;
23  import de.aikiit.fotorenamer.util.Version;
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  
27  import javax.swing.*;
28  import java.awt.*;
29  import java.awt.event.ActionEvent;
30  import java.awt.event.ActionListener;
31  import java.io.File;
32  import java.text.SimpleDateFormat;
33  import java.util.Date;
34  
35  import static de.aikiit.fotorenamer.util.LocalizationHelper.getBundleString;
36  import static de.aikiit.fotorenamer.util.LocalizationHelper.getParameterizedBundleString;
37  
38  /**
39   * This class holds the main application window and allows to select a directory
40   * to perform the image name processing.
41   *
42   * @author hirsch, 13.10.2003
43   * @version 2004-01-08
44   */
45  public final class MainUIWindow extends JFrame implements ActionListener {
46      /**
47       * The logger of this class.
48       **/
49      private static final Logger LOG =
50              LogManager.getLogger(MainUIWindow.class);
51  
52      /**
53       * Provide version information in the UI (taken from maven).
54       */
55      private static final String VERSION = getParameterizedBundleString("fotorenamer.ui.main.version", //
56              new SimpleDateFormat(getBundleString("fotorenamer.datepattern")).
57                      format(new Date(Long.parseLong(
58                              Version.TIMESTAMP))), //
59              Version.VERSION, //
60              Version.TIMESTAMP
61      );
62      /**
63       * Relative location of the UI's image icon.
64       */
65      private static final String IMAGE_LOCATION =
66              File.separator + "image" + File.separator + "miniCamera.png";
67  
68      /**
69       * Component containing the help window of this application.
70       */
71      private static final HelpWindow helpWindow = new HelpWindow();
72      /**
73       * The UI's help button.
74       */
75      private JButton helpButton = null;
76      /**
77       * The UI's start button.
78       */
79      private JButton goButton = null;
80      /**
81       * The UI's exit button.
82       */
83      private JButton endButton = null;
84      /**
85       * The UI's info/versioning button.
86       */
87      private JButton infoButton = null;
88      /**
89       * The UI's revert renaming button.
90       */
91      private JButton revertButton = null;
92      /**
93       * Component that selects the directory to work on.
94       */
95      private ImageDirectorySelector imageDirectorySelector = null;
96  
97      private MainUIWindow() {
98          SwingUtilities.invokeLater(this::init);
99      }
100 
101     /**
102      * Creates the main UI window and initializes all internal UI-components.
103      */
104     public static void build() {
105         new MainUIWindow();
106     }
107 
108     /**
109      * Helper class to perform the internal initialization of the UI.
110      */
111     private void init() {
112         String os = "[" + System.getProperty("os.name");
113         os += " " + System.getProperty("os.version");
114         os += " " + System.getProperty("os.arch") + "]";
115 
116         this.setTitle(getParameterizedBundleString("fotorenamer.ui.main.title", os));
117         this.getContentPane().setLayout(new BorderLayout());
118         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
119 
120         // select directory
121         JPanel verzeichnis = new JPanel(new BorderLayout());
122         this.imageDirectorySelector = new ImageDirectorySelector(
123                 ComponentGaugeUtil.createImageIcon(IMAGE_LOCATION));
124         verzeichnis.add(imageDirectorySelector);
125 
126         // help
127         JPanel knoepfe = new JPanel(new FlowLayout());
128         this.helpButton = new JButton(getBundleString("fotorenamer.ui.main.menu.help"));
129         this.helpButton.addActionListener(this);
130         this.helpButton.setMnemonic(getBundleString("fotorenamer.ui.main.menu.help.mnemonic").charAt(0));
131         knoepfe.add(this.helpButton);
132 
133         // info
134         this.infoButton = new JButton(getBundleString("fotorenamer.ui.main.menu.info"));
135         this.infoButton.addActionListener(this);
136         this.infoButton.setMnemonic(getBundleString("fotorenamer.ui.main.menu.info.mnemonic").charAt(0));
137         knoepfe.add(this.infoButton);
138 
139         // exit
140         this.endButton = new JButton(getBundleString("fotorenamer.ui.main.menu.end"));
141         this.endButton.addActionListener(this);
142         this.endButton.setMnemonic(getBundleString("fotorenamer.ui.main.menu.end.mnemonic").charAt(0));
143         knoepfe.add(this.endButton);
144 
145         // start
146         this.goButton = new JButton(getBundleString("fotorenamer.ui.main.menu.start"));
147         this.goButton.addActionListener(this);
148         this.goButton.setMnemonic(getBundleString("fotorenamer.ui.main.menu.start.mnemonic").charAt(0));
149         knoepfe.add(this.goButton);
150 
151         // revert
152         this.revertButton = new JButton(getBundleString("fotorenamer.ui.main.menu.revert"));
153         this.revertButton.addActionListener(this);
154         this.revertButton.setMnemonic(getBundleString("fotorenamer.ui.main.menu.revert.mnemonic").charAt(0));
155         knoepfe.add(this.revertButton);
156 
157         this.getContentPane().add(verzeichnis, BorderLayout.NORTH);
158         this.getContentPane().add(knoepfe, BorderLayout.CENTER);
159         this.pack();
160         ComponentGaugeUtil.makeCentered(this);
161         this.setVisible(true);
162     }
163 
164     /**
165      * Action listener method to react on events.
166      *
167      * @param event Events that was fired on this component.
168      */
169     public void actionPerformed(final ActionEvent event) {
170         final SwingWorker<Void, Void> worker;
171 
172         Object source = event.getSource();
173         // end
174         if (this.endButton.equals(source)) {
175             LOG.info("Bye Bye :-)");
176             System.exit(0);
177         } else if (this.helpButton.equals(source)) {
178             LOG.info("Displaying help window.");
179             showHelpWindow();
180         } else if (this.infoButton.equals(source)) {
181             // info
182             JOptionPane.showMessageDialog(null,
183                     getParameterizedBundleString("fotorenamer.ui.about", VERSION, new SimpleDateFormat("yyyy").
184                             format(new Date(
185                                     Long.parseLong(
186                                             Version.TIMESTAMP)))),
187                     getBundleString("fotorenamer.ui.main.version.title"),
188                     JOptionPane.INFORMATION_MESSAGE);
189         } else if (this.revertButton.equals(source) || this.goButton.equals(source)) {
190             worker = new SwingWorker<Void, Void>() {
191                 @Override
192                 protected Void doInBackground() {
193                     if (imageDirectorySelector.isWaiting()) {
194                         JOptionPane.showMessageDialog(null,
195                                 getBundleString("fotorenamer.ui.error.nodirectory"),
196                                 getBundleString("fotorenamer.ui.error.nodirectory.title"),
197                                 JOptionPane.ERROR_MESSAGE);
198                         return null;
199                     }
200 
201                     // perform renaming
202                     try {
203                         if (goButton.equals(source)) {
204                             goButton.setEnabled(false);
205                             goButton.setText(getBundleString("fotorenamer.ui.main.progress"));
206                             CreationDateFromExifImageRenamer renamer =
207                                     new CreationDateFromExifImageRenamer(
208                                             imageDirectorySelector.getSelectedDirectory()
209                                     );
210                             new Thread(renamer).start();
211                         } else {
212                             revertButton.setEnabled(false);
213                             revertButton.setText(getBundleString("fotorenamer.ui.main.progress"));
214                             new RemoveExifPrefixRenamer(
215                                     imageDirectorySelector.getSelectedDirectory());
216                         }
217                     } catch (InvalidDirectoryException uv) {
218                         LOG.info("Invalid directory selected: {}", uv.getMessage());
219                         JOptionPane.showMessageDialog(null,
220                                 getParameterizedBundleString("fotorenamer.ui.error.invaliddirectory", uv.getMessage()),
221                                 getBundleString("fotorenamer.ui.error.invaliddirectory.title"),
222                                 JOptionPane.ERROR_MESSAGE);
223                     } catch (NoFilesFoundException kde) {
224                         LOG.info("No files found in {}", kde.getMessage());
225                         JOptionPane.showMessageDialog(null,
226                                 getParameterizedBundleString("fotorenamer.ui.error.nofiles", kde.getMessage()),
227                                 getBundleString("fotorenamer.ui.error.nofiles.title"),
228                                 JOptionPane.ERROR_MESSAGE);
229                     }
230                     return null;
231                 }
232 
233                 @Override
234                 protected void done() {
235                     LOG.debug("Finished working, will reset UI.");
236                     // reset gui if all workers terminated
237                     goButton.setEnabled(true);
238                     goButton.setText(getBundleString("fotorenamer.ui.main.menu.start"));
239                     revertButton.setEnabled(true);
240                     revertButton.setText(getBundleString("fotorenamer.ui.main.menu.revert"));
241                 }
242             };
243             // Execute the SwingWorker; GUI will not freeze
244             worker.execute();
245         }
246     }
247 
248     /**
249      * Helper to enable visibility of help window.
250      *
251      * @see HelpWindow
252      */
253     private static void showHelpWindow() {
254         helpWindow.setVisible(true);
255     }
256 }