view src/name/blackcap/exifwasher/ShowDialog.kt @ 35:bcbc92ffe0d0

Makes a .deb file at long last, but Duke still shows up as an icon in the dock.
author David Barts <n5jrn@me.com>
date Thu, 30 Apr 2020 21:22:30 -0700
parents 3d9c36307704
children 40911898ed23
line wrap: on
line source

/*
 * The dialog that displays the Exif data in a single file (display only,
 * no changing). We do this after washing.
 */
package name.blackcap.exifwasher

import java.awt.Color
import java.awt.Dimension
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.io.File
import java.io.IOException
import java.util.logging.Level
import java.util.logging.Logger
import javax.swing.*
import javax.swing.table.DefaultTableModel
import javax.swing.table.TableColumn
import javax.swing.table.TableColumnModel

import name.blackcap.exifwasher.exiv2.*

class ShowDialog : JDialog(Application.mainFrame) {
    private val BW = 9
    private val BW2 = BW * 2
    private val WIDTH = 640
    private val HEIGHT = 480

    private val COLUMN_NAMES = arrayOf<String>("Key", "Type", "Value")
    private val myTable = JExifTable(arrayOf<Array<out Any?>>(), COLUMN_NAMES).apply {
        autoCreateRowSorter = false
        border = BorderFactory.createLineBorder(Color.GRAY)
        gridColor = Color.GRAY
        rowSorter = null
        setShowGrid(true)
    }

    /* deliberately not the default, because this changes a file */
    private val dismissButton = JButton("Dismiss").also {
        it.addActionListener(ActionListener { close() })
    }

    /* controls the washing of the Exif data */
    fun show(image: File) {
        title = "Washed: ${image.name}"
        Application.mainFrame.useWaitCursor()
        swingWorker<Array<Array<String>>?> {
            inBackground {
                try {
                    val openedImage = Image(image.absolutePath)
                    val meta = openedImage.metadata
                    val keys = meta.keys
                    keys.sort()
                    Array<Array<String>>(keys.size) {
                        val key = keys[it]
                        val value = meta[key]
                        arrayOf<String>(key, value.type, value.value)
                    }
                } catch (e: Exiv2Exception) {
                    LOGGER.log(Level.SEVERE, "unable to read metadata", e)
                    null
                }
            }
            whenDone {
                Application.mainFrame.useNormalCursor()
                val tableData = get()
                if (tableData == null) {
                    JOptionPane.showMessageDialog(Application.mainFrame,
                        "Unable to read metadata.",
                        "Error", JOptionPane.ERROR_MESSAGE)
                } else {
                    myTable.run {
                        model = MyTableModel(tableData, COLUMN_NAMES)
                        autoResizeMode = JTable.AUTO_RESIZE_OFF
                        setColWidth(0, 180, null)
                        setColWidth(1, 72, "Undefined")
                        setColWidth(2, 720, null)
                        setOverallWidth()
                    }
                    setVisible(true)
                }
            }
        }
    }

    private class MyTableModel(tData: Array<Array<String>>, cNames: Array<String>) : DefaultTableModel(tData, cNames) {
        override fun isCellEditable(row: Int, col: Int) = false
        override fun getColumnClass(col: Int) = java.lang.String::class.java
    }

    init {
        defaultCloseOperation = JDialog.DISPOSE_ON_CLOSE /* delete if reusing */
        title = "Untitled"
        contentPane.apply {
            layout = BoxLayout(this, BoxLayout.Y_AXIS)
            add(JScrollPane(myTable).apply {
                alignmentX = JScrollPane.CENTER_ALIGNMENT
                border = BorderFactory.createEmptyBorder(BW2, BW2, BW, BW2)
                verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
                horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
                preferredSize = Dimension(WIDTH, HEIGHT)
                background = Application.mainFrame.background
            })
            add(Box(BoxLayout.X_AXIS).apply {
                alignmentX = Box.CENTER_ALIGNMENT
                border = BorderFactory.createEmptyBorder(BW, BW2, BW2, BW2)
                add(Box.createHorizontalGlue())
                add(dismissButton)
            })
        }
        pack()
    }
}