diff src/name/blackcap/exifwasher/Misc.kt @ 19:39b977021ea1

Tool tips in case key and type cols truncate.
author David Barts <n5jrn@me.com>
date Sat, 11 Apr 2020 16:12:59 -0700
parents 0a106e9b91b4
children 965435b85a69
line wrap: on
line diff
--- a/src/name/blackcap/exifwasher/Misc.kt	Sat Apr 11 15:01:23 2020 -0700
+++ b/src/name/blackcap/exifwasher/Misc.kt	Sat Apr 11 16:12:59 2020 -0700
@@ -9,7 +9,9 @@
 import java.awt.Font
 import java.awt.FontMetrics
 import java.awt.Graphics
+import java.awt.Point
 import java.awt.Toolkit
+import java.awt.event.MouseEvent
 import javax.swing.*
 import javax.swing.table.TableColumnModel
 import kotlin.annotation.*
@@ -193,3 +195,25 @@
     }
     preferredSize = Dimension(total, preferredSize.height)
 }
+
+/**
+ * A JTable for displaying metadata. Columns that might get harmfully
+ * truncated have tooltips when truncation happens.
+ */
+class JExifTable(rowData: Array<Array<out Any?>>, colNames: Array<out Any?>): JTable(rowData, colNames) {
+    override fun getToolTipText(e: MouseEvent): String? {
+        val pos = e.point
+        val col = columnAtPoint(pos)
+        if (!setOf("Key", "Type").contains(getColumnName(col))) {
+            return null
+        }
+        val contents = getValueAt(rowAtPoint(pos), col) as String?
+        if (contents == null) {
+            return null
+        }
+        val needed = graphics.fontMetrics.stringWidth(contents)
+        val actual = columnModel.getColumn(col).width
+        return if (needed > actual) contents else null
+    }
+}
+