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