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