diff src/name/blackcap/imageprep/MainFrame.kt @ 0:e0efe7848130

Initial commit. Untested!
author David Barts <davidb@stashtea.com>
date Thu, 16 Jul 2020 19:57:23 -0700
parents
children 5fa5d15b4a7b
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/name/blackcap/imageprep/MainFrame.kt	Thu Jul 16 19:57:23 2020 -0700
@@ -0,0 +1,78 @@
+/*
+ * The main application window.
+ */
+package name.blackcap.imageprep
+
+import java.awt.Dimension
+import java.awt.datatransfer.DataFlavor
+import java.awt.datatransfer.Transferable
+import java.awt.datatransfer.UnsupportedFlavorException
+import java.awt.event.WindowEvent
+import java.awt.event.WindowListener
+import java.io.File
+import java.io.IOException
+import java.util.logging.Level
+import java.util.logging.Logger
+import javax.swing.*
+import javax.imageio.ImageIO
+
+/* the main frame itself */
+class MainFrame : JFrame(Application.MYNAME) {
+    /* default size */
+    val WIDTH = 512
+    val HEIGHT = 384
+
+    /* does a system exit when needed */
+    private class KillIt() : WindowListener {
+        /* events we don't care about */
+        override fun windowActivated(e: WindowEvent) {}
+        override fun windowClosed(e: WindowEvent) {}
+        override fun windowDeactivated(e: WindowEvent) {}
+        override fun windowDeiconified(e: WindowEvent) {}
+        override fun windowIconified(e: WindowEvent) {}
+        override fun windowOpened(e: WindowEvent) {}
+
+        /* and the one we do */
+        override fun windowClosing(e: WindowEvent) {
+            LOGGER.log(Level.INFO, "execution complete")
+            System.exit(0)
+        }
+    }
+
+    /* acts on dragged files */
+    private class MyTransferHandler : TransferHandler() {
+        override fun canImport(support: TransferHandler.TransferSupport): Boolean {
+            return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
+        }
+
+        override fun importData(support: TransferHandler.TransferSupport): Boolean {
+            if (!canImport(support)) {
+                return false
+            }
+            val files = try {
+                support.transferable.getTransferData(DataFlavor.javaFileListFlavor) as List<File>
+            } catch (e: UnsupportedFlavorException) {
+                return false
+            } catch (e: IOException) {
+                return false
+            }
+            for (file in files) {
+                RotateDialog.makeDialog(file)
+            }
+            return true
+        }
+    }
+
+    init {
+        contentPane.add(
+            JLabel("Drag image files into this window or choose Fileā€¦ Open & Scale from menu.").apply {
+                horizontalAlignment = JLabel.CENTER
+                verticalAlignment = JLabel.CENTER
+            })
+        preferredSize = Dimension(WIDTH, HEIGHT)
+        transferHandler = MyTransferHandler()
+        pack()
+        addWindowListener(KillIt())
+    }
+}
+