view src/name/blackcap/exifwasher/Menus.kt @ 60:d0b83fc1d62a default tip

Remember our input directory on a per-invocation basis.
author David Barts <n5jrn@me.com>
date Sun, 26 Jul 2020 15:14:03 -0700
parents 39895d44a287
children
line wrap: on
line source

/*
 * Menus.
 */
package name.blackcap.exifwasher

import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.KeyEvent
import java.io.File
import java.util.logging.Level
import java.util.logging.Logger
import javax.swing.*
import javax.swing.filechooser.FileNameExtensionFilter

/**
 * Our menu bar. What we display depends somewhat on the system type, as
 * the Mac gives us a gratuitous menu bar entry for handling some stuff.
 */
class MyMenuBar: JMenuBar() {
    private var currentInputDirectory = File(System.getProperty("user.home"))

    init {
        add(JMenu("File").apply {
            add(JMenuItem("Wash…").apply {
                addActionListener(ActionListener { doWash() })
                makeShortcut(KeyEvent.VK_W)
            })
            if (OS.type != OS.MAC) {
                add(JMenuItem("Preferences…").apply {
                    addActionListener(ActionListener {
                        Application.settingsDialog.setVisible(true)
                    })
                    makeShortcut(KeyEvent.VK_COMMA)
                })
                add(JMenuItem("Quit").apply {
                    addActionListener(ActionListener {
                        LOGGER.log(Level.INFO, "execution complete")
                        System.exit(0)
                    })
                    makeShortcut(KeyEvent.VK_Q)
                })
            }
        })
        add(JMenu("Help").apply {
            if (OS.type != OS.MAC) {
                add(JMenuItem("About ${Application.MYNAME}…").apply {
                    addActionListener(ActionListener { showAboutDialog() })
                })
            }
            add(JMenuItem("${Application.MYNAME} Help…").apply {
                addActionListener(ActionListener { Application.helpDialog.setVisible(true) })
            })
        })
    }

    fun getMenu(name: String): JMenu? {
        subElements.forEach {
            val jmenu = it.component as? JMenu
            if (jmenu?.text == name) {
                return jmenu
            }
        }
        return null
    }

    fun doWash() {
        val fc = JFileChooser().apply {
            currentDirectory = currentInputDirectory
            fileFilter = FileNameExtensionFilter("JPEG Files", "jpg", "jpeg")
            setMultiSelectionEnabled(true)
        }
        val status = fc.showOpenDialog(Application.mainFrame)
        if (status == JFileChooser.APPROVE_OPTION) {
            val files = fc.getSelectedFiles()
            if (files.size > 0)
                currentInputDirectory = files[0].canonicalFile.parentFile
            for (file in files) {
                WashDialog().wash(file)
            }
        }
    }
}

/**
 * Show an About dialog.
 */
fun showAboutDialog() {
    JOptionPane.showMessageDialog(Application.mainFrame,
        "\nJpegWasher—Privacy for your photos.\n"
        + "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0© MMXX, David W. Barts\n",
        "About JpegWasher",
        JOptionPane.PLAIN_MESSAGE)
}