view src/name/blackcap/exifwasher/ShowDialog.kt @ 6:aafc9c127c7b

Fix many bugs; get settings (apparently) working.
author David Barts <n5jrn@me.com>
date Thu, 09 Apr 2020 22:29:48 -0700
parents dc1f4359659d
children 65d14d44bc3f
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.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 myTable = JTable().apply {
        autoCreateRowSorter = false
        rowSorter = null
        columnModel.run {
            getColumn(0).preferredWidth = 25  /* key name */
            getColumn(1).preferredWidth = 15  /* type */
            getColumn(2).preferredWidth = 100  /* value */
        }
    }

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

    /* controls the washing of the Exif data */
    fun show(image: File) {
        title = "Washed: ${image.name}"
        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 {
                useNormalCursor()
                val tableData = get()
                if (tableData == null) {
                    JOptionPane.showMessageDialog(Application.mainFrame,
                        "Unable to read metadata.",
                        "Error", JOptionPane.ERROR_MESSAGE)
                } else {
                    val colNames = arrayOf("Key", "Type", "Value")
                    myTable.apply {
                        model = MyTableModel(tableData, colNames)
                        validate()
                    }
                    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) = 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, BW, BW2, BW)
                add(Box.createHorizontalGlue())
                add(dismissButton)
            })
        }
        pack()
    }
}