view src/name/blackcap/exifwasher/Menus.kt @ 25:5a5a7de27eb4

ExifWasher -> JpegWasher
author David Barts <n5jrn@me.com>
date Thu, 16 Apr 2020 19:41:29 -0700
parents 5cac95c17fef
children ee580450d45a
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.util.logging.Level
import java.util.logging.Logger
import javax.swing.*

/**
 * 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() {
    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)
                })
            }
        })
        if (OS.type != OS.MAC) {
            add(JMenu("Help").apply {
                add(JMenuItem("About ${Application.MYNAME}…").apply {
                    addActionListener(ActionListener { showAboutDialog() })
                })
            })
        }
    }

    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 {
            setMultiSelectionEnabled(true)
        }
        val status = fc.showOpenDialog(Application.mainFrame)
        if (status == JFileChooser.APPROVE_OPTION) {
            for (file in fc.getSelectedFiles()) {
                WashDialog().wash(file)
            }
        }
    }
}

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