comparison src/name/blackcap/exifwasher/ShowDialog.kt @ 3:19c381c536ec

Code to make it a proper Mac GUI app. Untested!
author David Barts <n5jrn@me.com>
date Wed, 08 Apr 2020 20:29:12 -0700
parents
children dc1f4359659d
comparison
equal deleted inserted replaced
2:efd9fe2d70d7 3:19c381c536ec
1 /*
2 * The dialog that displays the Exif data in a single file (display only,
3 * no changing). We do this after washing.
4 */
5 package name.blackcap.exifwasher
6
7 import java.awt.event.ActionEvent
8 import java.awt.event.ActionListener
9 import java.io.File
10 import java.io.IOException
11 import java.util.logging.Level
12 import java.util.logging.Logger
13 import javax.swing.*
14 import javax.swing.table.DefaultTableModel
15 import javax.swing.table.TableColumn
16 import javax.swing.table.TableColumnModel
17
18 import name.blackcap.exifwasher.exiv2.*
19
20 class ShowDialog : JDialog(Application.mainFrame) {
21 private val BW = 9
22 private val BW2 = BW * 2
23 private val WIDTH = 640
24 private val HEIGHT = 480
25
26 private val myTable = JTable().apply {
27 autoCreateRowSorter = false
28 rowSorter = null
29 columnModel.run {
30 getColumn(0).preferredWidth = 25 /* key name */
31 getColumn(1).preferredWidth = 15 /* type */
32 getColumn(2).preferredWidth = 100 /* value */
33 }
34 }
35
36 /* deliberately not the default, because this changes a file */
37 private val dismissButton = JButton("Dismiss").also {
38 it.addActionListener(ActionListener { close() })
39 it.border = BorderFactory.createEmptyBorder(0, BW, 0, BW)
40 }
41
42 /* controls the washing of the Exif data */
43 fun show(image: File) {
44 title = "Washed: ${image.name}"
45 useWaitCursor()
46 swingWorker<Array<Array<String>>?> {
47 inBackground {
48 try {
49 val image = Image(image.absolutePath)
50 val meta = image.meta
51 val keys = meta.keys
52 keys.sort()
53 Array<Array<String>>(keys.size) {
54 val key = keys[it]
55 val value = meta[key]
56 arrayOf<String>(key, value.type, value.value)
57 }
58 } catch (e: Exiv2Exception) {
59 LOGGER.log(Level.SEVERE, "unable to read metadata", e)
60 null
61 }
62 }
63 whenDone {
64 useNormalCursor()
65 val tableData = get()
66 if (tableData == null) {
67 JOptionPane.showMessageDialog(Application.mainFrame,
68 "Unable to read metadata.",
69 "Error", JOptionPane.ERROR_MESSAGE)
70 } else {
71 val colNames = arrayOf("Key", "Type", "Value")
72 myTable.apply {
73 model = MyTableModel(tableData, colNames)
74 validate()
75 }
76 setVisible(true)
77 }
78 }
79 }
80 }
81
82 private class MyTableModel : DefaultTableModel {
83 override fun isCellEditable(row: Int, col: Int) = false
84 override fun getColumnClass(col: Int) = java.lang.String::class.java
85 }
86
87 init {
88 defaultCloseOperation = JDialog.DISPOSE_ON_CLOSE /* delete if reusing */
89 title = "Untitled"
90 contentPane.apply {
91 layout = BoxLayout(this, BoxLayout.Y_AXIS)
92 add(JScrollPane(myTable).apply {
93 alignmentX = JScrollPane.CENTER_ALIGNMENT
94 border = BorderFactory.createEmptyBorder(BW2, BW2, BW, BW2)
95 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
96 horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
97 preferredSize = Dimension(WIDTH, HEIGHT)
98 background = Application.mainFrame.background
99 })
100 add(Box(BoxLayout.X_AXIS).apply {
101 alignmentX = Box.CENTER_ALIGNMENT
102 border = BorderFactory.createEmptyBorder(BW, BW, BW2, BW)
103 add(Box.createHorizontalGlue())
104 add(dismissButton)
105 })
106 }
107 pack()
108 }
109 }