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() {
|
|
31 init {
|
|
32 add(JMenu("File").apply {
|
|
33 add(JMenuItem("Open & Scale…").apply {
|
|
34 addActionListener(ActionListener { doOpen() })
|
|
35 makeShortcut(KeyEvent.VK_O)
|
|
36 })
|
|
37 add(JMenuItem("Discard").apply {
|
|
38 addActionListener(ActionListener { doDiscard() })
|
|
39 })
|
|
40 add(JMenuItem("Save & Close…").apply {
|
|
41 addActionListener(ActionListener { doClose() })
|
|
42 makeShortcut(KeyEvent.VK_S)
|
|
43 })
|
|
44 if (OS.type != OS.MAC) {
|
|
45 add(JMenuItem("Quit").apply {
|
|
46 addActionListener(ActionListener {
|
|
47 LOGGER.log(Level.INFO, "execution complete")
|
|
48 System.exit(0)
|
|
49 })
|
|
50 makeShortcut(KeyEvent.VK_Q)
|
|
51 })
|
|
52 }
|
|
53 })
|
|
54 add(JMenu("Help").apply {
|
|
55 if (OS.type != OS.MAC) {
|
|
56 add(JMenuItem("About ${Application.MYNAME}…").apply {
|
|
57 addActionListener(ActionListener { showAboutDialog() })
|
|
58 })
|
|
59 }
|
|
60 add(JMenuItem("${Application.MYNAME} Help…").apply {
|
|
61 addActionListener(ActionListener { Application.helpDialog.setVisible(true) })
|
|
62 })
|
|
63 })
|
|
64 }
|
|
65
|
|
66 fun getMenu(name: String): JMenu? {
|
|
67 subElements.forEach {
|
|
68 val jmenu = it.component as? JMenu
|
|
69 if (jmenu?.text == name) {
|
|
70 return jmenu
|
|
71 }
|
|
72 }
|
|
73 return null
|
|
74 }
|
|
75
|
|
76 private fun doOpen() {
|
|
77 val chooser = JFileChooser().apply {
|
3
|
78 fileFilter = FileNameExtensionFilter("Image Files", *ImageIO.getReaderFileSuffixes())
|
0
|
79 }
|
|
80 if (chooser.showOpenDialog(Application.mainFrame) == JFileChooser.APPROVE_OPTION) {
|
|
81 RotateDialog.makeDialog(chooser.selectedFile)
|
|
82 }
|
|
83 }
|
|
84
|
|
85 private fun doDiscard() {
|
1
|
86 val w = FocusManager.getCurrentManager().activeWindow as? RotateDialog
|
0
|
87 if (w == null) {
|
1
|
88 Toolkit.getDefaultToolkit().beep()
|
0
|
89 return
|
|
90 }
|
|
91 w.setVisible(false)
|
|
92 w.dispose()
|
|
93 }
|
|
94
|
|
95 private fun doClose() {
|
1
|
96 val w = FocusManager.getCurrentManager().activeWindow as? RotateDialog
|
0
|
97 if (w == null) {
|
1
|
98 Toolkit.getDefaultToolkit().beep()
|
0
|
99 return
|
|
100 }
|
|
101 val outName = splitext(w.file.name).first + Settings.outputSuffix + ".jpg"
|
1
|
102 val chooser = JFileChooser().apply {
|
0
|
103 selectedFile = File(
|
|
104 if (Settings.outputToInputDir) w.file.parent else Settings.outputTo,
|
|
105 outName)
|
|
106 }
|
3
|
107 if (chooser.showSaveDialog(Application.mainFrame) != JFileChooser.APPROVE_OPTION) {
|
0
|
108 return
|
|
109 }
|
|
110 val (name, ext) = splitext(chooser.selectedFile.name)
|
|
111 val file = if (ext.toLowerCase() in setOf(".jpg", ".jpeg")) {
|
|
112 chooser.selectedFile
|
|
113 } else {
|
|
114 File(chooser.selectedFile.parent, name + ".jpg")
|
|
115 }
|
|
116 if (file.exists() && JOptionPane.showConfirmDialog(w, "File ${file.name} already exists. Overwrite?", "Confirm Overwrite", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
|
|
117 return
|
|
118 }
|
|
119 w.useWaitCursor()
|
|
120 swingWorker<IOException?> {
|
|
121 inBackground {
|
|
122 try {
|
|
123 val ios = ImageIO.createImageOutputStream(file)
|
|
124 val writer = ImageIO.getImageWritersByFormatName("jpeg").next()
|
|
125 val iwp = writer.getDefaultWriteParam().apply {
|
|
126 setCompressionMode(ImageWriteParam.MODE_EXPLICIT)
|
|
127 setCompressionQuality(Settings.outputQuality.toFloat() / 100.0f)
|
|
128 }
|
|
129 writer.setOutput(ios)
|
|
130 writer.write(null, IIOImage(w.image, null, null), iwp)
|
|
131 null
|
|
132 } catch (e: IOException) {
|
|
133 e
|
|
134 }
|
|
135 }
|
|
136 whenDone {
|
|
137 w.useNormalCursor()
|
|
138 val error = get()
|
|
139 if (error == null) {
|
|
140 w.setVisible(false)
|
|
141 w.dispose()
|
|
142 } else {
|
1
|
143 ioExceptionDialog(w, file, "write", error)
|
0
|
144 }
|
|
145 }
|
|
146 }
|
|
147 }
|
|
148
|
|
149 private fun splitext(s: String): Pair<String, String> {
|
|
150 val pos = s.lastIndexOf('.')
|
|
151 if (pos == -1) {
|
|
152 return Pair(s, "")
|
|
153 }
|
|
154 return Pair(s.substring(0, pos), s.substring(pos))
|
|
155 }
|
|
156 }
|
|
157
|
|
158 /**
|
|
159 * Show an About dialog.
|
|
160 */
|
|
161 fun showAboutDialog() {
|
|
162 JOptionPane.showMessageDialog(Application.mainFrame,
|
|
163 "\nImagePrep\n"
|
|
164 + "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0© MMXX, David W. Barts\n",
|
|
165 "About ImagePrep",
|
|
166 JOptionPane.PLAIN_MESSAGE)
|
|
167 }
|