Mercurial > cgi-bin > hgweb.cgi > ImagePrep
annotate src/name/blackcap/imageprep/Menus.kt @ 24:a4737a525af8
Fix icons a bit.
author | David Barts <n5jrn@me.com> |
---|---|
date | Wed, 25 Nov 2020 08:40:45 -0800 |
parents | 92afaa27f40a |
children | 098c4f5507c7 |
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 |
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 | 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 } |
3 | 142 if (chooser.showSaveDialog(Application.mainFrame) != JFileChooser.APPROVE_OPTION) { |
0 | 143 return |
144 } | |
5
884f1415a330
Rationalized directory management.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
145 currentOutputDirectory = chooser.selectedFile.canonicalFile.parentFile |
0 | 146 val (name, ext) = splitext(chooser.selectedFile.name) |
147 val file = if (ext.toLowerCase() in setOf(".jpg", ".jpeg")) { | |
148 chooser.selectedFile | |
149 } else { | |
5
884f1415a330
Rationalized directory management.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
150 File(currentOutputDirectory, name + ".jpg") |
0 | 151 } |
152 if (file.exists() && JOptionPane.showConfirmDialog(w, "File ${file.name} already exists. Overwrite?", "Confirm Overwrite", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { | |
153 return | |
154 } | |
155 w.useWaitCursor() | |
156 swingWorker<IOException?> { | |
157 inBackground { | |
158 try { | |
7 | 159 /* xxx - ImageIO bug? */ |
160 val devNull = java.io.PrintStream(NullOutputStream()) | |
161 val oldOut = System.out | |
162 System.setOut(devNull) | |
163 val oldErr = System.err | |
164 System.setErr(devNull) | |
165 val ios = try { | |
166 ImageIO.createImageOutputStream(file) | |
167 } finally { | |
168 System.setErr(oldErr) | |
169 System.setOut(oldOut) | |
170 } | |
171 if (ios == null) | |
172 throw IOException() | |
173 /* xxx - end workaround */ | |
14 | 174 ios.use { |
175 val writer = ImageIO.getImageWritersByFormatName("jpeg").next() | |
15 | 176 try { |
177 val iwp = writer.getDefaultWriteParam().apply { | |
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 | 180 } |
181 writer.setOutput(it) | |
182 writer.write(null, IIOImage(w.image, null, null), iwp) | |
183 } finally { | |
184 writer.dispose() | |
14 | 185 } |
0 | 186 } |
187 null | |
188 } catch (e: IOException) { | |
189 e | |
190 } | |
191 } | |
192 whenDone { | |
193 w.useNormalCursor() | |
194 val error = get() | |
195 if (error == null) { | |
9 | 196 w.close() |
0 | 197 } else { |
1 | 198 ioExceptionDialog(w, file, "write", error) |
0 | 199 } |
200 } | |
201 } | |
202 } | |
203 | |
204 private fun splitext(s: String): Pair<String, String> { | |
205 val pos = s.lastIndexOf('.') | |
206 if (pos == -1) { | |
207 return Pair(s, "") | |
208 } | |
209 return Pair(s.substring(0, pos), s.substring(pos)) | |
210 } | |
211 } | |
212 | |
213 /** | |
214 * Show an About dialog. | |
215 */ | |
216 fun showAboutDialog() { | |
217 JOptionPane.showMessageDialog(Application.mainFrame, | |
218 "\nImagePrep\n" | |
219 + "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0© MMXX, David W. Barts\n", | |
220 "About ImagePrep", | |
221 JOptionPane.PLAIN_MESSAGE) | |
222 } |