Mercurial > cgi-bin > hgweb.cgi > ImagePrep
annotate src/name/blackcap/imageprep/Menus.kt @ 31:99a0eb385c9a default tip
Work around annoying Swing glitch.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sat, 20 Aug 2022 09:19:49 -0700 |
parents | 098c4f5507c7 |
children |
rev | line source |
---|---|
0 | 1 /* |
2 * Menus. | |
3 */ | |
4 package name.blackcap.imageprep | |
5 | |
6 import java.awt.Graphics2D | |
7 import java.awt.RenderingHints | |
1 | 8 import java.awt.Toolkit |
0 | 9 import java.awt.Window |
10 import java.awt.event.ActionEvent | |
11 import java.awt.event.ActionListener | |
12 import java.awt.event.KeyEvent | |
13 import java.awt.image.BufferedImage | |
14 import java.io.File | |
1 | 15 import java.io.IOException |
0 | 16 import java.util.logging.Level |
17 import java.util.logging.Logger | |
18 import javax.imageio.IIOImage | |
19 import javax.imageio.ImageIO | |
20 import javax.imageio.ImageWriteParam | |
21 import javax.imageio.ImageWriter | |
22 import javax.imageio.stream.ImageOutputStream | |
23 import javax.swing.* | |
1 | 24 import javax.swing.filechooser.FileNameExtensionFilter |
0 | 25 |
26 /** | |
27 * Our menu bar. What we display depends somewhat on the system type, as | |
28 * the Mac gives us a gratuitous menu bar entry for handling some stuff. | |
29 */ | |
30 class MyMenuBar: JMenuBar() { | |
17 | 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 | 34 init { |
35 add(JMenu("File").apply { | |
36 add(JMenuItem("Open & Scale…").apply { | |
37 addActionListener(ActionListener { doOpen() }) | |
38 makeShortcut(KeyEvent.VK_O) | |
39 }) | |
40 add(JMenuItem("Discard").apply { | |
41 addActionListener(ActionListener { doDiscard() }) | |
42 }) | |
43 add(JMenuItem("Save & Close…").apply { | |
44 addActionListener(ActionListener { doClose() }) | |
45 makeShortcut(KeyEvent.VK_S) | |
46 }) | |
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 | 54 add(JMenuItem("Quit").apply { |
55 addActionListener(ActionListener { | |
56 LOGGER.log(Level.INFO, "execution complete") | |
57 System.exit(0) | |
58 }) | |
59 makeShortcut(KeyEvent.VK_Q) | |
60 }) | |
61 } | |
62 }) | |
63 add(JMenu("Help").apply { | |
64 if (OS.type != OS.MAC) { | |
65 add(JMenuItem("About ${Application.MYNAME}…").apply { | |
66 addActionListener(ActionListener { showAboutDialog() }) | |
67 }) | |
68 } | |
69 add(JMenuItem("${Application.MYNAME} Help…").apply { | |
70 addActionListener(ActionListener { Application.helpDialog.setVisible(true) }) | |
71 }) | |
72 }) | |
73 } | |
74 | |
75 fun getMenu(name: String): JMenu? { | |
76 subElements.forEach { | |
77 val jmenu = it.component as? JMenu | |
78 if (jmenu?.text == name) { | |
79 return jmenu | |
80 } | |
81 } | |
82 return null | |
83 } | |
84 | |
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 | 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 | 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 | 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 | 98 fileFilter = FileNameExtensionFilter("Image Files", *ImageIO.getReaderFileSuffixes()) |
0 | 99 } |
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 |
30 | 102 RotateDialog.makeDialog(chooser.selectedFile, maxDim.value) |
0 | 103 } |
104 } | |
105 | |
106 private fun doDiscard() { | |
1 | 107 val w = FocusManager.getCurrentManager().activeWindow as? RotateDialog |
0 | 108 if (w == null) { |
1 | 109 Toolkit.getDefaultToolkit().beep() |
0 | 110 return |
111 } | |
9 | 112 w.close() |
0 | 113 } |
114 | |
7 | 115 /* xxx - ImageIO bug? */ |
116 private class NullOutputStream : java.io.OutputStream() { | |
117 override fun write(b: Int): Unit {} | |
118 } | |
119 | |
0 | 120 private fun doClose() { |
1 | 121 val w = FocusManager.getCurrentManager().activeWindow as? RotateDialog |
0 | 122 if (w == null) { |
7 | 123 LOGGER.log(Level.INFO, "beep!") |
1 | 124 Toolkit.getDefaultToolkit().beep() |
0 | 125 return |
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 | 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 | 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 | 141 } |
31
99a0eb385c9a
Work around annoying Swing glitch.
David Barts <n5jrn@me.com>
parents:
30
diff
changeset
|
142 /* swing glitch workaround from https://stackoverflow.com/questions/14640103/jfilechooser-not-showing-up */ |
99a0eb385c9a
Work around annoying Swing glitch.
David Barts <n5jrn@me.com>
parents:
30
diff
changeset
|
143 if (!chooser.isVisible()) { |
99a0eb385c9a
Work around annoying Swing glitch.
David Barts <n5jrn@me.com>
parents:
30
diff
changeset
|
144 chooser.setVisible(true) |
99a0eb385c9a
Work around annoying Swing glitch.
David Barts <n5jrn@me.com>
parents:
30
diff
changeset
|
145 chooser.requestFocus() |
99a0eb385c9a
Work around annoying Swing glitch.
David Barts <n5jrn@me.com>
parents:
30
diff
changeset
|
146 } |
3 | 147 if (chooser.showSaveDialog(Application.mainFrame) != JFileChooser.APPROVE_OPTION) { |
0 | 148 return |
149 } | |
5
884f1415a330
Rationalized directory management.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
150 currentOutputDirectory = chooser.selectedFile.canonicalFile.parentFile |
0 | 151 val (name, ext) = splitext(chooser.selectedFile.name) |
30 | 152 val file = if (ext.lowercase() in setOf(".jpg", ".jpeg")) { |
0 | 153 chooser.selectedFile |
154 } else { | |
5
884f1415a330
Rationalized directory management.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
155 File(currentOutputDirectory, name + ".jpg") |
0 | 156 } |
157 if (file.exists() && JOptionPane.showConfirmDialog(w, "File ${file.name} already exists. Overwrite?", "Confirm Overwrite", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { | |
158 return | |
159 } | |
160 w.useWaitCursor() | |
161 swingWorker<IOException?> { | |
162 inBackground { | |
163 try { | |
7 | 164 /* xxx - ImageIO bug? */ |
165 val devNull = java.io.PrintStream(NullOutputStream()) | |
166 val oldOut = System.out | |
167 System.setOut(devNull) | |
168 val oldErr = System.err | |
169 System.setErr(devNull) | |
170 val ios = try { | |
171 ImageIO.createImageOutputStream(file) | |
172 } finally { | |
173 System.setErr(oldErr) | |
174 System.setOut(oldOut) | |
175 } | |
176 if (ios == null) | |
177 throw IOException() | |
178 /* xxx - end workaround */ | |
14 | 179 ios.use { |
180 val writer = ImageIO.getImageWritersByFormatName("jpeg").next() | |
15 | 181 try { |
182 val iwp = writer.getDefaultWriteParam().apply { | |
183 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
|
184 setCompressionQuality((outQual.value as Int).toFloat() / 100.0f) |
15 | 185 } |
186 writer.setOutput(it) | |
187 writer.write(null, IIOImage(w.image, null, null), iwp) | |
188 } finally { | |
189 writer.dispose() | |
14 | 190 } |
0 | 191 } |
192 null | |
193 } catch (e: IOException) { | |
194 e | |
195 } | |
196 } | |
197 whenDone { | |
198 w.useNormalCursor() | |
199 val error = get() | |
200 if (error == null) { | |
9 | 201 w.close() |
0 | 202 } else { |
1 | 203 ioExceptionDialog(w, file, "write", error) |
0 | 204 } |
205 } | |
206 } | |
207 } | |
208 | |
209 private fun splitext(s: String): Pair<String, String> { | |
210 val pos = s.lastIndexOf('.') | |
211 if (pos == -1) { | |
212 return Pair(s, "") | |
213 } | |
214 return Pair(s.substring(0, pos), s.substring(pos)) | |
215 } | |
216 } | |
217 | |
218 /** | |
219 * Show an About dialog. | |
220 */ | |
221 fun showAboutDialog() { | |
222 JOptionPane.showMessageDialog(Application.mainFrame, | |
223 "\nImagePrep\n" | |
224 + "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0© MMXX, David W. Barts\n", | |
225 "About ImagePrep", | |
226 JOptionPane.PLAIN_MESSAGE) | |
227 } |