Mercurial > cgi-bin > hgweb.cgi > ImagePrep
annotate src/name/blackcap/imageprep/Menus.kt @ 19:5fa5d15b4a7b
Attempt to make it a std. app. Builds, sorta runs, needs more work.
author | David Barts <n5jrn@me.com> |
---|---|
date | Fri, 20 Nov 2020 21:38:06 -0800 |
parents | d71523cde521 |
children | 71029c9bf7cd |
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() { | |
19
5fa5d15b4a7b
Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
86 val maxDim = MaxDimSpinner(Application.settingsDialog.maxDimension) |
5fa5d15b4a7b
Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
87 val acc = JPanel().apply { |
5fa5d15b4a7b
Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
88 layout = BoxLayout(this, BoxLayout.X_AXIS) |
5fa5d15b4a7b
Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
89 add(JLabel("Max. dimension:")) |
5fa5d15b4a7b
Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
90 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
|
91 } |
0 | 92 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
|
93 accessory = acc |
5
884f1415a330
Rationalized directory management.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
94 currentDirectory = currentInputDirectory |
3 | 95 fileFilter = FileNameExtensionFilter("Image Files", *ImageIO.getReaderFileSuffixes()) |
0 | 96 } |
97 if (chooser.showOpenDialog(Application.mainFrame) == JFileChooser.APPROVE_OPTION) { | |
5
884f1415a330
Rationalized directory management.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
98 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
|
99 RotateDialog.makeDialog(chooser.selectedFile, maxDim.value as Int) |
0 | 100 } |
101 } | |
102 | |
103 private fun doDiscard() { | |
1 | 104 val w = FocusManager.getCurrentManager().activeWindow as? RotateDialog |
0 | 105 if (w == null) { |
1 | 106 Toolkit.getDefaultToolkit().beep() |
0 | 107 return |
108 } | |
9 | 109 w.close() |
0 | 110 } |
111 | |
7 | 112 /* xxx - ImageIO bug? */ |
113 private class NullOutputStream : java.io.OutputStream() { | |
114 override fun write(b: Int): Unit {} | |
115 } | |
116 | |
0 | 117 private fun doClose() { |
1 | 118 val w = FocusManager.getCurrentManager().activeWindow as? RotateDialog |
0 | 119 if (w == null) { |
7 | 120 LOGGER.log(Level.INFO, "beep!") |
1 | 121 Toolkit.getDefaultToolkit().beep() |
0 | 122 return |
123 } | |
19
5fa5d15b4a7b
Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
124 val outQual = OutQualSpinner(Application.settingsDialog.outputQuality) |
5fa5d15b4a7b
Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
125 val acc = JPanel().apply { |
5fa5d15b4a7b
Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
126 layout = BoxLayout(this, BoxLayout.X_AXIS) |
5fa5d15b4a7b
Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
127 add(JLabel("Quality:")) |
5fa5d15b4a7b
Attempt to make it a std. app. Builds, sorta runs, needs more work.
David Barts <n5jrn@me.com>
parents:
17
diff
changeset
|
128 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
|
129 } |
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 outName = splitext(w.file.name).first + Application.settingsDialog.outputSuffix + ".jpg" |
1 | 131 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
|
132 accessory = acc |
5
884f1415a330
Rationalized directory management.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
133 currentDirectory = currentOutputDirectory |
884f1415a330
Rationalized directory management.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
134 selectedFile = File(currentOutputDirectory, outName) |
0 | 135 } |
3 | 136 if (chooser.showSaveDialog(Application.mainFrame) != JFileChooser.APPROVE_OPTION) { |
0 | 137 return |
138 } | |
5
884f1415a330
Rationalized directory management.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
139 currentOutputDirectory = chooser.selectedFile.canonicalFile.parentFile |
0 | 140 val (name, ext) = splitext(chooser.selectedFile.name) |
141 val file = if (ext.toLowerCase() in setOf(".jpg", ".jpeg")) { | |
142 chooser.selectedFile | |
143 } else { | |
5
884f1415a330
Rationalized directory management.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
144 File(currentOutputDirectory, name + ".jpg") |
0 | 145 } |
146 if (file.exists() && JOptionPane.showConfirmDialog(w, "File ${file.name} already exists. Overwrite?", "Confirm Overwrite", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { | |
147 return | |
148 } | |
149 w.useWaitCursor() | |
150 swingWorker<IOException?> { | |
151 inBackground { | |
152 try { | |
7 | 153 /* xxx - ImageIO bug? */ |
154 val devNull = java.io.PrintStream(NullOutputStream()) | |
155 val oldOut = System.out | |
156 System.setOut(devNull) | |
157 val oldErr = System.err | |
158 System.setErr(devNull) | |
159 val ios = try { | |
160 ImageIO.createImageOutputStream(file) | |
161 } finally { | |
162 System.setErr(oldErr) | |
163 System.setOut(oldOut) | |
164 } | |
165 if (ios == null) | |
166 throw IOException() | |
167 /* xxx - end workaround */ | |
14 | 168 ios.use { |
169 val writer = ImageIO.getImageWritersByFormatName("jpeg").next() | |
15 | 170 try { |
171 val iwp = writer.getDefaultWriteParam().apply { | |
172 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
|
173 setCompressionQuality((outQual.value as Int).toFloat() / 100.0f) |
15 | 174 } |
175 writer.setOutput(it) | |
176 writer.write(null, IIOImage(w.image, null, null), iwp) | |
177 } finally { | |
178 writer.dispose() | |
14 | 179 } |
0 | 180 } |
181 null | |
182 } catch (e: IOException) { | |
183 e | |
184 } | |
185 } | |
186 whenDone { | |
187 w.useNormalCursor() | |
188 val error = get() | |
189 if (error == null) { | |
9 | 190 w.close() |
0 | 191 } else { |
1 | 192 ioExceptionDialog(w, file, "write", error) |
0 | 193 } |
194 } | |
195 } | |
196 } | |
197 | |
198 private fun splitext(s: String): Pair<String, String> { | |
199 val pos = s.lastIndexOf('.') | |
200 if (pos == -1) { | |
201 return Pair(s, "") | |
202 } | |
203 return Pair(s.substring(0, pos), s.substring(pos)) | |
204 } | |
205 } | |
206 | |
207 /** | |
208 * Show an About dialog. | |
209 */ | |
210 fun showAboutDialog() { | |
211 JOptionPane.showMessageDialog(Application.mainFrame, | |
212 "\nImagePrep\n" | |
213 + "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0© MMXX, David W. Barts\n", | |
214 "About ImagePrep", | |
215 JOptionPane.PLAIN_MESSAGE) | |
216 } |