Mercurial > cgi-bin > hgweb.cgi > ImagePrep
annotate src/name/blackcap/imageprep/SettingsDialog.kt @ 25:a495f9ea498b mac-open
Initial attempts to make it respond to MacOS open message (not working).
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 13 Dec 2020 10:29:08 -0800 |
parents | d3979a2155a8 |
children | 9bf3d8de6904 |
rev | line source |
---|---|
20 | 1 /* |
2 * The dialog that controls font corecion. | |
3 */ | |
4 package name.blackcap.imageprep | |
5 | |
6 import java.awt.Dimension | |
7 import java.awt.event.ActionEvent | |
8 import java.awt.event.ActionListener | |
9 import java.io.BufferedWriter | |
10 import java.io.File | |
11 import java.io.FileOutputStream | |
12 import java.io.IOException | |
13 import java.io.OutputStreamWriter | |
14 import java.util.Properties | |
15 import java.util.logging.Level | |
16 import java.util.logging.Logger | |
17 import javax.swing.* | |
18 import kotlin.text.toInt | |
19 | |
20 /* work around name shadowing */ | |
21 private val _PROPS = PROPERTIES | |
22 | |
23 class SettingsDialog: JDialog(Application.mainFrame) { | |
24 /* maximum allowed dimension in output */ | |
25 private val _maxDimension = MaxDimSpinner(_PROPS.getInt("maxDimension")) | |
26 val maxDimension: Int | |
27 get() { | |
28 return _maxDimension.value as Int | |
29 } | |
30 | |
31 /* JPEG output quality */ | |
32 private val _outputQuality = OutQualSpinner(_PROPS.getInt("outputQuality")) | |
33 val outputQuality: Int | |
34 get() { | |
35 return _outputQuality.value as Int | |
36 } | |
37 | |
38 /* file name output suffix */ | |
39 private val _outputSuffix = JTextField(_PROPS.getProperty("outputSuffix"), 24).apply { | |
40 noTaller() | |
41 } | |
42 val outputSuffix: String | |
43 get() { | |
44 return _outputSuffix.text | |
45 } | |
46 | |
47 /* output to input dir? */ | |
48 private val otid = _PROPS.getBoolean("outputToInputDir") | |
21 | 49 private val outputToInputButton = JRadioButton("Create output file in same folder as input.", otid).apply { |
50 alignmentX = LEFT_ALIGNMENT | |
51 } | |
20 | 52 val outputToInputDir: Boolean |
53 get() { | |
54 return outputToInputButton.isSelected() | |
55 } | |
56 | |
57 /* output to some other directory */ | |
21 | 58 private val outputToButton = JRadioButton("Create output file in:", !otid).apply { |
59 alignmentX = LEFT_ALIGNMENT | |
60 } | |
20 | 61 |
62 /* name of the other directory */ | |
63 private val oto = _getOutputTo() | |
64 protected val outputToText = JTextField(oto, 40).apply { | |
65 setEditable(false) | |
66 noTaller() | |
67 } | |
68 | |
69 /* chooser for other directory */ | |
70 private val _outputTo = JFileChooser(oto).apply { | |
71 fileSelectionMode = JFileChooser.DIRECTORIES_ONLY | |
72 setMultiSelectionEnabled(false) | |
73 } | |
74 val outputTo: String | |
75 get() { | |
76 return _outputTo.selectedFile?.getCanonicalPath() ?: oto | |
77 } | |
78 | |
79 /* button to change that other directory */ | |
80 protected val changeOutputTo = JButton("Change").also { | |
81 it.noTaller() | |
82 it.addActionListener(ActionListener { | |
83 val status = _outputTo.showOpenDialog(this) | |
84 if (status == JFileChooser.APPROVE_OPTION) { | |
85 val path = outputTo | |
86 outputToText.text = path | |
87 } | |
88 }) | |
89 it.setEnabled(!otid) | |
90 } | |
91 | |
92 /* radio button group */ | |
93 protected val buttonGroup = ButtonGroup().apply { | |
94 add(outputToButton) | |
95 add(outputToInputButton) | |
96 } | |
97 | |
98 /* standard spacing between elements (10 pixels ≅ 1/7") and half that */ | |
99 private val BW = 5 | |
100 private val BW2 = 10 | |
101 | |
102 /* buttons */ | |
103 private val _ok = JButton("OK").also { | |
104 it.noTaller() | |
105 it.addActionListener(ActionListener { | |
106 writeProperties() | |
107 setVisible(false) | |
108 }) | |
109 } | |
110 | |
111 private val _cancel = JButton("Cancel").also { | |
112 it.noTaller() | |
113 it.addActionListener(ActionListener { | |
114 revertValues() | |
115 setVisible(false) | |
116 }) | |
117 } | |
118 | |
119 /* initializer */ | |
120 init { | |
121 title = "Preferences" | |
122 contentPane.apply { | |
123 layout = BoxLayout(this, BoxLayout.Y_AXIS) | |
124 add(Box(BoxLayout.X_AXIS).apply { | |
21 | 125 alignmentX = LEFT_ALIGNMENT |
20 | 126 border = BorderFactory.createEmptyBorder(BW2, BW2, BW, BW2) |
22
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
127 add(leftLabel("Default maximum dimension: ")) |
20 | 128 add(_maxDimension) |
129 add(Box.createGlue()) | |
22
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
130 add(leftLabel("Default output quality: ")) |
20 | 131 add(_outputQuality) |
132 }) | |
133 add(Box(BoxLayout.X_AXIS).apply { | |
21 | 134 alignmentX = LEFT_ALIGNMENT |
20 | 135 border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2) |
21 | 136 add(leftLabel("Output filename suffix: ")) |
20 | 137 add(_outputSuffix) |
138 add(Box.createGlue()) | |
139 }) | |
140 add(Box(BoxLayout.Y_AXIS).apply { | |
21 | 141 alignmentX = LEFT_ALIGNMENT |
142 border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2) | |
20 | 143 add(outputToInputButton) |
144 add(outputToButton) | |
145 add(Box(BoxLayout.X_AXIS).apply { | |
21 | 146 alignmentX = LEFT_ALIGNMENT |
20 | 147 add(outputToText) |
148 add(changeOutputTo) | |
149 add(Box.createGlue()) | |
150 }) | |
151 }) | |
152 add(Box(BoxLayout.X_AXIS).apply { | |
21 | 153 alignmentX = LEFT_ALIGNMENT |
154 border = BorderFactory.createEmptyBorder(BW, BW2, BW2, BW2) | |
155 add(Box.createGlue()) | |
20 | 156 add(_cancel) |
157 add(Box.createGlue()) | |
158 add(_ok) | |
21 | 159 add(Box.createGlue()) |
20 | 160 }) |
161 } | |
162 pack() | |
163 setResizable(false) | |
164 } | |
165 | |
166 private fun leftLabel(text: String) = JLabel(text).apply { | |
167 alignmentX = JLabel.LEFT_ALIGNMENT | |
168 } | |
169 | |
170 private fun revertValues() | |
171 { | |
172 _maxDimension.value = _PROPS.getInt("maxDimension") | |
173 _outputQuality.value = _PROPS.getInt("outputQuality") | |
174 _outputSuffix.text = _PROPS.getProperty("outputSuffix") | |
175 val otid = _PROPS.getBoolean("outputToInputDir") | |
176 outputToInputButton.setSelected(otid) | |
177 outputToButton.setSelected(!otid) | |
178 val oto = _getOutputTo() | |
179 outputToText.text = oto | |
180 _outputTo.selectedFile = File(oto) | |
181 } | |
182 | |
183 private fun writeProperties() | |
184 { | |
185 _PROPS.setInt("maxDimension", maxDimension) | |
186 _PROPS.setInt("outputQuality", outputQuality) | |
187 _PROPS.setProperty("outputSuffix", outputSuffix) | |
188 _PROPS.setBoolean("outputToInputDir", outputToInputDir) | |
189 _PROPS.setProperty("outputTo", outputTo) | |
190 try { | |
191 BufferedWriter(OutputStreamWriter(FileOutputStream(PROP_FILE), CHARSET)).use { | |
192 _PROPS.store(it, " -*- coding: ${CHARSET} -*-") | |
193 } | |
194 } catch (e: IOException) { | |
195 LOGGER.log(Level.WARNING, "IOException writing properties file") | |
196 val message = e.message | |
197 if (message != null && !message.isEmpty()) { | |
198 LOGGER.log(Level.WARNING, message) | |
199 } | |
200 JOptionPane.showMessageDialog(Application.mainFrame, | |
201 "Unable to write settings.", | |
202 "Error", | |
203 JOptionPane.ERROR_MESSAGE) | |
204 } | |
205 } | |
206 | |
21 | 207 private fun _getOutputTo(): String { |
208 val p = _PROPS.getProperty("outputTo") | |
209 return if (p != null) tilde(p) else System.getProperty("user.dir") | |
210 } | |
20 | 211 } |
212 | |
213 fun Properties.getString(key: String): String = getProperty(key) as String | |
214 | |
215 fun Properties.getInt(key: String): Int = getString(key).toInt() | |
216 | |
217 fun Properties.setInt(key: String, value: Int): Unit { | |
218 setProperty(key, value.toString()) | |
219 } | |
220 | |
221 fun Properties.getBoolean(key: String): Boolean { | |
222 val raw = getProperty(key) | |
223 if (raw.isNullOrEmpty()) | |
224 return false | |
225 val c1 = raw[0].toLowerCase() | |
226 return c1 == 't' || c1 == 'y' | |
227 } | |
228 | |
229 fun Properties.setBoolean(key: String, value: Boolean): Unit { | |
230 setProperty(key, value.toString()) | |
231 } | |
232 | |
233 fun JComponent.noTaller() { | |
234 maximumSize = Dimension(maximumSize.width, preferredSize.height) | |
235 } |