annotate src/name/blackcap/imageprep/Menus.kt @ 28:c310ec097194

Add Linux support.
author David Barts <n5jrn@me.com>
date Fri, 25 Dec 2020 19:07:47 -0800
parents 92afaa27f40a
children 098c4f5507c7
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 * Menus.
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.Graphics2D
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
7 import java.awt.RenderingHints
1
0bded24f746e Compiles, still untested.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
8 import java.awt.Toolkit
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
9 import java.awt.Window
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
10 import java.awt.event.ActionEvent
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
11 import java.awt.event.ActionListener
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
12 import java.awt.event.KeyEvent
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
13 import java.awt.image.BufferedImage
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
14 import java.io.File
1
0bded24f746e Compiles, still untested.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
15 import java.io.IOException
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
16 import java.util.logging.Level
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
17 import java.util.logging.Logger
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
18 import javax.imageio.IIOImage
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
19 import javax.imageio.ImageIO
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
20 import javax.imageio.ImageWriteParam
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
21 import javax.imageio.ImageWriter
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
22 import javax.imageio.stream.ImageOutputStream
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
23 import javax.swing.*
1
0bded24f746e Compiles, still untested.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
24 import javax.swing.filechooser.FileNameExtensionFilter
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
25
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
26 /**
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
27 * Our menu bar. What we display depends somewhat on the system type, as
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
28 * the Mac gives us a gratuitous menu bar entry for handling some stuff.
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
29 */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
30 class MyMenuBar: JMenuBar() {
17
d71523cde521 Should be private. Make it so.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
31 private var currentInputDirectory = File(System.getProperty("user.dir"))
19
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
32 private var currentOutputDirectory = File(Application.settingsDialog.outputTo)
5
884f1415a330 Rationalized directory management.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
33
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
34 init {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
35 add(JMenu("File").apply {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
36 add(JMenuItem("Open & Scale…").apply {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
37 addActionListener(ActionListener { doOpen() })
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
38 makeShortcut(KeyEvent.VK_O)
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 add(JMenuItem("Discard").apply {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
41 addActionListener(ActionListener { doDiscard() })
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
42 })
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
43 add(JMenuItem("Save & Close…").apply {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
44 addActionListener(ActionListener { doClose() })
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
45 makeShortcut(KeyEvent.VK_S)
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 if (OS.type != OS.MAC) {
19
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
48 add(JMenuItem("Preferences…").apply {
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
49 addActionListener(ActionListener {
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
50 Application.settingsDialog.setVisible(true)
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
51 })
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
52 makeShortcut(KeyEvent.VK_COMMA)
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
53 })
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
54 add(JMenuItem("Quit").apply {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
55 addActionListener(ActionListener {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
56 LOGGER.log(Level.INFO, "execution complete")
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
57 System.exit(0)
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 makeShortcut(KeyEvent.VK_Q)
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
60 })
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 })
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
63 add(JMenu("Help").apply {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
64 if (OS.type != OS.MAC) {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
65 add(JMenuItem("About ${Application.MYNAME}…").apply {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
66 addActionListener(ActionListener { showAboutDialog() })
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
67 })
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
68 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
69 add(JMenuItem("${Application.MYNAME} Help…").apply {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
70 addActionListener(ActionListener { Application.helpDialog.setVisible(true) })
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 })
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
73 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
74
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
75 fun getMenu(name: String): JMenu? {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
76 subElements.forEach {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
77 val jmenu = it.component as? JMenu
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
78 if (jmenu?.text == name) {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
79 return jmenu
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
80 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
81 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
82 return null
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
83 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
84
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
85 private fun doOpen() {
22
d3979a2155a8 Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents: 20
diff changeset
86 val maxDim = MaxDimSpinner(Application.settingsDialog.maxDimension).apply {
d3979a2155a8 Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents: 20
diff changeset
87 alignmentX = LEFT_ALIGNMENT
d3979a2155a8 Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents: 20
diff changeset
88 }
19
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
89 val acc = JPanel().apply {
20
71029c9bf7cd Commit overlooked files.
David Barts <n5jrn@me.com>
parents: 19
diff changeset
90 layout = BoxLayout(this, BoxLayout.Y_AXIS)
22
d3979a2155a8 Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents: 20
diff changeset
91 add(Box.createGlue())
23
92afaa27f40a Mac app support.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
92 add(JLabel("Max. dim.:").apply { alignmentX = LEFT_ALIGNMENT })
19
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
93 add(maxDim)
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
94 }
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
95 val chooser = JFileChooser().apply {
19
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
96 accessory = acc
5
884f1415a330 Rationalized directory management.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
97 currentDirectory = currentInputDirectory
3
09dcd475d1bf Works (prelim tests only).
David Barts <n5jrn@me.com>
parents: 1
diff changeset
98 fileFilter = FileNameExtensionFilter("Image Files", *ImageIO.getReaderFileSuffixes())
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
99 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
100 if (chooser.showOpenDialog(Application.mainFrame) == JFileChooser.APPROVE_OPTION) {
5
884f1415a330 Rationalized directory management.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
101 currentInputDirectory = chooser.selectedFile.canonicalFile.parentFile
19
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
102 RotateDialog.makeDialog(chooser.selectedFile, maxDim.value as Int)
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
103 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
104 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
105
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
106 private fun doDiscard() {
1
0bded24f746e Compiles, still untested.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
107 val w = FocusManager.getCurrentManager().activeWindow as? RotateDialog
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
108 if (w == null) {
1
0bded24f746e Compiles, still untested.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
109 Toolkit.getDefaultToolkit().beep()
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
110 return
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
111 }
9
9e9fe34052a6 Use .close().
David Barts <n5jrn@me.com>
parents: 7
diff changeset
112 w.close()
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
113 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
114
7
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
115 /* xxx - ImageIO bug? */
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
116 private class NullOutputStream : java.io.OutputStream() {
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
117 override fun write(b: Int): Unit {}
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
118 }
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
119
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
120 private fun doClose() {
1
0bded24f746e Compiles, still untested.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
121 val w = FocusManager.getCurrentManager().activeWindow as? RotateDialog
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
122 if (w == null) {
7
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
123 LOGGER.log(Level.INFO, "beep!")
1
0bded24f746e Compiles, still untested.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
124 Toolkit.getDefaultToolkit().beep()
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
125 return
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
126 }
22
d3979a2155a8 Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents: 20
diff changeset
127 val outQual = OutQualSpinner(Application.settingsDialog.outputQuality).apply {
d3979a2155a8 Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents: 20
diff changeset
128 alignmentX = LEFT_ALIGNMENT
d3979a2155a8 Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents: 20
diff changeset
129 }
19
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
130 val acc = JPanel().apply {
20
71029c9bf7cd Commit overlooked files.
David Barts <n5jrn@me.com>
parents: 19
diff changeset
131 layout = BoxLayout(this, BoxLayout.Y_AXIS)
22
d3979a2155a8 Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents: 20
diff changeset
132 add(Box.createGlue())
d3979a2155a8 Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents: 20
diff changeset
133 add(JLabel("Quality:").apply { alignmentX = LEFT_ALIGNMENT })
19
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
134 add(outQual)
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
135 }
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
136 val outName = splitext(w.file.name).first + Application.settingsDialog.outputSuffix + ".jpg"
1
0bded24f746e Compiles, still untested.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
137 val chooser = JFileChooser().apply {
19
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
138 accessory = acc
5
884f1415a330 Rationalized directory management.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
139 currentDirectory = currentOutputDirectory
884f1415a330 Rationalized directory management.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
140 selectedFile = File(currentOutputDirectory, outName)
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
141 }
3
09dcd475d1bf Works (prelim tests only).
David Barts <n5jrn@me.com>
parents: 1
diff changeset
142 if (chooser.showSaveDialog(Application.mainFrame) != JFileChooser.APPROVE_OPTION) {
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
143 return
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
144 }
5
884f1415a330 Rationalized directory management.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
145 currentOutputDirectory = chooser.selectedFile.canonicalFile.parentFile
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
146 val (name, ext) = splitext(chooser.selectedFile.name)
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
147 val file = if (ext.toLowerCase() in setOf(".jpg", ".jpeg")) {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
148 chooser.selectedFile
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
149 } else {
5
884f1415a330 Rationalized directory management.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
150 File(currentOutputDirectory, name + ".jpg")
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
151 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
152 if (file.exists() && JOptionPane.showConfirmDialog(w, "File ${file.name} already exists. Overwrite?", "Confirm Overwrite", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
153 return
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
154 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
155 w.useWaitCursor()
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
156 swingWorker<IOException?> {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
157 inBackground {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
158 try {
7
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
159 /* xxx - ImageIO bug? */
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
160 val devNull = java.io.PrintStream(NullOutputStream())
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
161 val oldOut = System.out
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
162 System.setOut(devNull)
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
163 val oldErr = System.err
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
164 System.setErr(devNull)
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
165 val ios = try {
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
166 ImageIO.createImageOutputStream(file)
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
167 } finally {
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
168 System.setErr(oldErr)
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
169 System.setOut(oldOut)
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
170 }
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
171 if (ios == null)
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
172 throw IOException()
801cdc780ca8 Work around ImageIO bug.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
173 /* xxx - end workaround */
14
bed255e4c2dc Fix file leak on save.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
174 ios.use {
bed255e4c2dc Fix file leak on save.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
175 val writer = ImageIO.getImageWritersByFormatName("jpeg").next()
15
fad32eda667f Fix ImageWriter leak.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
176 try {
fad32eda667f Fix ImageWriter leak.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
177 val iwp = writer.getDefaultWriteParam().apply {
fad32eda667f Fix ImageWriter leak.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
178 setCompressionMode(ImageWriteParam.MODE_EXPLICIT)
19
5fa5d15b4a7b Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents: 17
diff changeset
179 setCompressionQuality((outQual.value as Int).toFloat() / 100.0f)
15
fad32eda667f Fix ImageWriter leak.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
180 }
fad32eda667f Fix ImageWriter leak.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
181 writer.setOutput(it)
fad32eda667f Fix ImageWriter leak.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
182 writer.write(null, IIOImage(w.image, null, null), iwp)
fad32eda667f Fix ImageWriter leak.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
183 } finally {
fad32eda667f Fix ImageWriter leak.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
184 writer.dispose()
14
bed255e4c2dc Fix file leak on save.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
185 }
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
186 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
187 null
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
188 } catch (e: IOException) {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
189 e
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
190 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
191 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
192 whenDone {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
193 w.useNormalCursor()
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
194 val error = get()
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
195 if (error == null) {
9
9e9fe34052a6 Use .close().
David Barts <n5jrn@me.com>
parents: 7
diff changeset
196 w.close()
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
197 } else {
1
0bded24f746e Compiles, still untested.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
198 ioExceptionDialog(w, file, "write", error)
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
199 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
200 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
201 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
202 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
203
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
204 private fun splitext(s: String): Pair<String, String> {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
205 val pos = s.lastIndexOf('.')
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
206 if (pos == -1) {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
207 return Pair(s, "")
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
208 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
209 return Pair(s.substring(0, pos), s.substring(pos))
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
210 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
211 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
212
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
213 /**
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
214 * Show an About dialog.
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
215 */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
216 fun showAboutDialog() {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
217 JOptionPane.showMessageDialog(Application.mainFrame,
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
218 "\nImagePrep\n"
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
219 + "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0© MMXX, David W. Barts\n",
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
220 "About ImagePrep",
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
221 JOptionPane.PLAIN_MESSAGE)
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
222 }