annotate src/name/blackcap/imageprep/MainFrame.kt @ 16:2bb46da74667

Detect unsupported file types on input.
author David Barts <n5jrn@me.com>
date Mon, 20 Jul 2020 12:56:07 -0700
parents e0efe7848130
children 5fa5d15b4a7b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
1 /*
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
2 * The main application window.
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
3 */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
4 package name.blackcap.imageprep
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
5
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
6 import java.awt.Dimension
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
7 import java.awt.datatransfer.DataFlavor
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
8 import java.awt.datatransfer.Transferable
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
9 import java.awt.datatransfer.UnsupportedFlavorException
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
10 import java.awt.event.WindowEvent
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
11 import java.awt.event.WindowListener
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
12 import java.io.File
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
13 import java.io.IOException
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
14 import java.util.logging.Level
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
15 import java.util.logging.Logger
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
16 import javax.swing.*
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
17 import javax.imageio.ImageIO
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
18
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
19 /* the main frame itself */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
20 class MainFrame : JFrame(Application.MYNAME) {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
21 /* default size */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
22 val WIDTH = 512
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
23 val HEIGHT = 384
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
24
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
25 /* does a system exit when needed */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
26 private class KillIt() : WindowListener {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
27 /* events we don't care about */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
28 override fun windowActivated(e: WindowEvent) {}
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
29 override fun windowClosed(e: WindowEvent) {}
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
30 override fun windowDeactivated(e: WindowEvent) {}
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
31 override fun windowDeiconified(e: WindowEvent) {}
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
32 override fun windowIconified(e: WindowEvent) {}
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
33 override fun windowOpened(e: WindowEvent) {}
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
34
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
35 /* and the one we do */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
36 override fun windowClosing(e: WindowEvent) {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
37 LOGGER.log(Level.INFO, "execution complete")
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
38 System.exit(0)
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
39 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
40 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
41
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
42 /* acts on dragged files */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
43 private class MyTransferHandler : TransferHandler() {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
44 override fun canImport(support: TransferHandler.TransferSupport): Boolean {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
45 return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
46 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
47
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
48 override fun importData(support: TransferHandler.TransferSupport): Boolean {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
49 if (!canImport(support)) {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
50 return false
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
51 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
52 val files = try {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
53 support.transferable.getTransferData(DataFlavor.javaFileListFlavor) as List<File>
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
54 } catch (e: UnsupportedFlavorException) {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
55 return false
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
56 } catch (e: IOException) {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
57 return false
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
58 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
59 for (file in files) {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
60 RotateDialog.makeDialog(file)
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
61 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
62 return true
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
63 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
64 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
65
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
66 init {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
67 contentPane.add(
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
68 JLabel("Drag image files into this window or choose Fileā€¦ Open & Scale from menu.").apply {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
69 horizontalAlignment = JLabel.CENTER
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
70 verticalAlignment = JLabel.CENTER
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
71 })
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
72 preferredSize = Dimension(WIDTH, HEIGHT)
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
73 transferHandler = MyTransferHandler()
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
74 pack()
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
75 addWindowListener(KillIt())
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
76 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
77 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
78