comparison src/name/blackcap/imageprep/Menus.kt @ 5:884f1415a330

Rationalized directory management.
author David Barts <n5jrn@me.com>
date Fri, 17 Jul 2020 17:11:43 -0700
parents 09dcd475d1bf
children 801cdc780ca8
comparison
equal deleted inserted replaced
4:5234e4500d45 5:884f1415a330
26 /** 26 /**
27 * Our menu bar. What we display depends somewhat on the system type, as 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. 28 * the Mac gives us a gratuitous menu bar entry for handling some stuff.
29 */ 29 */
30 class MyMenuBar: JMenuBar() { 30 class MyMenuBar: JMenuBar() {
31 var currentInputDirectory = File(System.getProperty("user.dir"))
32 var currentOutputDirectory = File(Settings.outputTo)
33
31 init { 34 init {
32 add(JMenu("File").apply { 35 add(JMenu("File").apply {
33 add(JMenuItem("Open & Scale…").apply { 36 add(JMenuItem("Open & Scale…").apply {
34 addActionListener(ActionListener { doOpen() }) 37 addActionListener(ActionListener { doOpen() })
35 makeShortcut(KeyEvent.VK_O) 38 makeShortcut(KeyEvent.VK_O)
73 return null 76 return null
74 } 77 }
75 78
76 private fun doOpen() { 79 private fun doOpen() {
77 val chooser = JFileChooser().apply { 80 val chooser = JFileChooser().apply {
81 currentDirectory = currentInputDirectory
78 fileFilter = FileNameExtensionFilter("Image Files", *ImageIO.getReaderFileSuffixes()) 82 fileFilter = FileNameExtensionFilter("Image Files", *ImageIO.getReaderFileSuffixes())
79 } 83 }
80 if (chooser.showOpenDialog(Application.mainFrame) == JFileChooser.APPROVE_OPTION) { 84 if (chooser.showOpenDialog(Application.mainFrame) == JFileChooser.APPROVE_OPTION) {
85 currentInputDirectory = chooser.selectedFile.canonicalFile.parentFile
81 RotateDialog.makeDialog(chooser.selectedFile) 86 RotateDialog.makeDialog(chooser.selectedFile)
82 } 87 }
83 } 88 }
84 89
85 private fun doDiscard() { 90 private fun doDiscard() {
98 Toolkit.getDefaultToolkit().beep() 103 Toolkit.getDefaultToolkit().beep()
99 return 104 return
100 } 105 }
101 val outName = splitext(w.file.name).first + Settings.outputSuffix + ".jpg" 106 val outName = splitext(w.file.name).first + Settings.outputSuffix + ".jpg"
102 val chooser = JFileChooser().apply { 107 val chooser = JFileChooser().apply {
103 selectedFile = File( 108 currentDirectory = currentOutputDirectory
104 if (Settings.outputToInputDir) w.file.parent else Settings.outputTo, 109 selectedFile = File(currentOutputDirectory, outName)
105 outName)
106 } 110 }
107 if (chooser.showSaveDialog(Application.mainFrame) != JFileChooser.APPROVE_OPTION) { 111 if (chooser.showSaveDialog(Application.mainFrame) != JFileChooser.APPROVE_OPTION) {
108 return 112 return
109 } 113 }
114 currentOutputDirectory = chooser.selectedFile.canonicalFile.parentFile
110 val (name, ext) = splitext(chooser.selectedFile.name) 115 val (name, ext) = splitext(chooser.selectedFile.name)
111 val file = if (ext.toLowerCase() in setOf(".jpg", ".jpeg")) { 116 val file = if (ext.toLowerCase() in setOf(".jpg", ".jpeg")) {
112 chooser.selectedFile 117 chooser.selectedFile
113 } else { 118 } else {
114 File(chooser.selectedFile.parent, name + ".jpg") 119 File(currentOutputDirectory, name + ".jpg")
115 } 120 }
116 if (file.exists() && JOptionPane.showConfirmDialog(w, "File ${file.name} already exists. Overwrite?", "Confirm Overwrite", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { 121 if (file.exists() && JOptionPane.showConfirmDialog(w, "File ${file.name} already exists. Overwrite?", "Confirm Overwrite", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
117 return 122 return
118 } 123 }
119 w.useWaitCursor() 124 w.useWaitCursor()