MainUIWindow.java

  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. import de.aikiit.fotorenamer.exception.InvalidDirectoryException;
  18. import de.aikiit.fotorenamer.exception.NoFilesFoundException;
  19. import de.aikiit.fotorenamer.image.CreationDateFromExifImageRenamer;
  20. import de.aikiit.fotorenamer.image.RemoveExifPrefixRenamer;
  21. import de.aikiit.fotorenamer.util.ComponentGaugeUtil;
  22. import de.aikiit.fotorenamer.util.Version;
  23. import org.apache.logging.log4j.LogManager;
  24. import org.apache.logging.log4j.Logger;

  25. import javax.swing.*;
  26. import java.awt.*;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.io.File;
  30. import java.text.SimpleDateFormat;
  31. import java.util.Date;

  32. import static de.aikiit.fotorenamer.util.LocalizationHelper.getBundleString;
  33. import static de.aikiit.fotorenamer.util.LocalizationHelper.getParameterizedBundleString;

  34. /**
  35.  * This class holds the main application window and allows to select a directory
  36.  * to perform the image name processing.
  37.  *
  38.  * @author hirsch, 13.10.2003
  39.  * @version 2004-01-08
  40.  */
  41. public final class MainUIWindow extends JFrame implements ActionListener {
  42.     /**
  43.      * The logger of this class.
  44.      **/
  45.     private static final Logger LOG =
  46.             LogManager.getLogger(MainUIWindow.class);

  47.     /**
  48.      * Provide version information in the UI (taken from maven).
  49.      */
  50.     private static final String VERSION = getParameterizedBundleString("fotorenamer.ui.main.version", //
  51.             new SimpleDateFormat(getBundleString("fotorenamer.datepattern")).
  52.                     format(new Date(Long.parseLong(
  53.                             Version.TIMESTAMP))), //
  54.             Version.VERSION, //
  55.             Version.TIMESTAMP
  56.     );
  57.     /**
  58.      * Relative location of the UI's image icon.
  59.      */
  60.     private static final String IMAGE_LOCATION =
  61.             File.separator + "image" + File.separator + "miniCamera.png";

  62.     /**
  63.      * Component containing the help window of this application.
  64.      */
  65.     private static final HelpWindow helpWindow = new HelpWindow();
  66.     /**
  67.      * The UI's help button.
  68.      */
  69.     private JButton helpButton = null;
  70.     /**
  71.      * The UI's start button.
  72.      */
  73.     private JButton goButton = null;
  74.     /**
  75.      * The UI's exit button.
  76.      */
  77.     private JButton endButton = null;
  78.     /**
  79.      * The UI's info/versioning button.
  80.      */
  81.     private JButton infoButton = null;
  82.     /**
  83.      * The UI's revert renaming button.
  84.      */
  85.     private JButton revertButton = null;
  86.     /**
  87.      * Component that selects the directory to work on.
  88.      */
  89.     private ImageDirectorySelector imageDirectorySelector = null;

  90.     private MainUIWindow() {
  91.         SwingUtilities.invokeLater(this::init);
  92.     }

  93.     /**
  94.      * Creates the main UI window and initializes all internal UI-components.
  95.      */
  96.     public static void build() {
  97.         new MainUIWindow();
  98.     }

  99.     /**
  100.      * Helper class to perform the internal initialization of the UI.
  101.      */
  102.     private void init() {
  103.         String os = "[" + System.getProperty("os.name");
  104.         os += " " + System.getProperty("os.version");
  105.         os += " " + System.getProperty("os.arch") + "]";

  106.         this.setTitle(getParameterizedBundleString("fotorenamer.ui.main.title", os));
  107.         this.getContentPane().setLayout(new BorderLayout());
  108.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);

  109.         // select directory
  110.         JPanel verzeichnis = new JPanel(new BorderLayout());
  111.         this.imageDirectorySelector = new ImageDirectorySelector(
  112.                 ComponentGaugeUtil.createImageIcon(IMAGE_LOCATION));
  113.         verzeichnis.add(imageDirectorySelector);

  114.         // help
  115.         JPanel knoepfe = new JPanel(new FlowLayout());
  116.         this.helpButton = new JButton(getBundleString("fotorenamer.ui.main.menu.help"));
  117.         this.helpButton.addActionListener(this);
  118.         this.helpButton.setMnemonic(getBundleString("fotorenamer.ui.main.menu.help.mnemonic").charAt(0));
  119.         knoepfe.add(this.helpButton);

  120.         // info
  121.         this.infoButton = new JButton(getBundleString("fotorenamer.ui.main.menu.info"));
  122.         this.infoButton.addActionListener(this);
  123.         this.infoButton.setMnemonic(getBundleString("fotorenamer.ui.main.menu.info.mnemonic").charAt(0));
  124.         knoepfe.add(this.infoButton);

  125.         // exit
  126.         this.endButton = new JButton(getBundleString("fotorenamer.ui.main.menu.end"));
  127.         this.endButton.addActionListener(this);
  128.         this.endButton.setMnemonic(getBundleString("fotorenamer.ui.main.menu.end.mnemonic").charAt(0));
  129.         knoepfe.add(this.endButton);

  130.         // start
  131.         this.goButton = new JButton(getBundleString("fotorenamer.ui.main.menu.start"));
  132.         this.goButton.addActionListener(this);
  133.         this.goButton.setMnemonic(getBundleString("fotorenamer.ui.main.menu.start.mnemonic").charAt(0));
  134.         knoepfe.add(this.goButton);

  135.         // revert
  136.         this.revertButton = new JButton(getBundleString("fotorenamer.ui.main.menu.revert"));
  137.         this.revertButton.addActionListener(this);
  138.         this.revertButton.setMnemonic(getBundleString("fotorenamer.ui.main.menu.revert.mnemonic").charAt(0));
  139.         knoepfe.add(this.revertButton);

  140.         this.getContentPane().add(verzeichnis, BorderLayout.NORTH);
  141.         this.getContentPane().add(knoepfe, BorderLayout.CENTER);
  142.         this.pack();
  143.         ComponentGaugeUtil.makeCentered(this);
  144.         this.setVisible(true);
  145.     }

  146.     /**
  147.      * Action listener method to react on events.
  148.      *
  149.      * @param event Events that was fired on this component.
  150.      */
  151.     public void actionPerformed(final ActionEvent event) {
  152.         final SwingWorker<Void, Void> worker;

  153.         Object source = event.getSource();
  154.         // end
  155.         if (this.endButton.equals(source)) {
  156.             LOG.info("Bye Bye :-)");
  157.             System.exit(0);
  158.         } else if (this.helpButton.equals(source)) {
  159.             LOG.info("Displaying help window.");
  160.             showHelpWindow();
  161.         } else if (this.infoButton.equals(source)) {
  162.             // info
  163.             JOptionPane.showMessageDialog(null,
  164.                     getParameterizedBundleString("fotorenamer.ui.about", VERSION, new SimpleDateFormat("yyyy").
  165.                             format(new Date(
  166.                                     Long.parseLong(
  167.                                             Version.TIMESTAMP)))),
  168.                     getBundleString("fotorenamer.ui.main.version.title"),
  169.                     JOptionPane.INFORMATION_MESSAGE);
  170.         } else if (this.revertButton.equals(source) || this.goButton.equals(source)) {
  171.             worker = new SwingWorker<Void, Void>() {
  172.                 @Override
  173.                 protected Void doInBackground() {
  174.                     if (imageDirectorySelector.isWaiting()) {
  175.                         JOptionPane.showMessageDialog(null,
  176.                                 getBundleString("fotorenamer.ui.error.nodirectory"),
  177.                                 getBundleString("fotorenamer.ui.error.nodirectory.title"),
  178.                                 JOptionPane.ERROR_MESSAGE);
  179.                         return null;
  180.                     }

  181.                     // perform renaming
  182.                     try {
  183.                         if (goButton.equals(source)) {
  184.                             goButton.setEnabled(false);
  185.                             goButton.setText(getBundleString("fotorenamer.ui.main.progress"));
  186.                             CreationDateFromExifImageRenamer renamer =
  187.                                     new CreationDateFromExifImageRenamer(
  188.                                             imageDirectorySelector.getSelectedDirectory()
  189.                                     );
  190.                             new Thread(renamer).start();
  191.                         } else {
  192.                             revertButton.setEnabled(false);
  193.                             revertButton.setText(getBundleString("fotorenamer.ui.main.progress"));
  194.                             new RemoveExifPrefixRenamer(
  195.                                     imageDirectorySelector.getSelectedDirectory());
  196.                         }
  197.                     } catch (InvalidDirectoryException uv) {
  198.                         LOG.info("Invalid directory selected: {}", uv.getMessage());
  199.                         JOptionPane.showMessageDialog(null,
  200.                                 getParameterizedBundleString("fotorenamer.ui.error.invaliddirectory", uv.getMessage()),
  201.                                 getBundleString("fotorenamer.ui.error.invaliddirectory.title"),
  202.                                 JOptionPane.ERROR_MESSAGE);
  203.                     } catch (NoFilesFoundException kde) {
  204.                         LOG.info("No files found in {}", kde.getMessage());
  205.                         JOptionPane.showMessageDialog(null,
  206.                                 getParameterizedBundleString("fotorenamer.ui.error.nofiles", kde.getMessage()),
  207.                                 getBundleString("fotorenamer.ui.error.nofiles.title"),
  208.                                 JOptionPane.ERROR_MESSAGE);
  209.                     }
  210.                     return null;
  211.                 }

  212.                 @Override
  213.                 protected void done() {
  214.                     LOG.debug("Finished working, will reset UI.");
  215.                     // reset gui if all workers terminated
  216.                     goButton.setEnabled(true);
  217.                     goButton.setText(getBundleString("fotorenamer.ui.main.menu.start"));
  218.                     revertButton.setEnabled(true);
  219.                     revertButton.setText(getBundleString("fotorenamer.ui.main.menu.revert"));
  220.                 }
  221.             };
  222.             // Execute the SwingWorker; GUI will not freeze
  223.             worker.execute();
  224.         }
  225.     }

  226.     /**
  227.      * Helper to enable visibility of help window.
  228.      *
  229.      * @see HelpWindow
  230.      */
  231.     private static void showHelpWindow() {
  232.         helpWindow.setVisible(true);
  233.     }
  234. }