comparison src/name/blackcap/clipman/SettingsDialog.kt @ 43:339e2da5bf83

Simplified the settings. Compiles, not tested.
author David Barts <n5jrn@me.com>
date Sun, 09 Feb 2020 19:23:16 -0700
parents 33fbe3a78d84
children d14298ef8b0a
comparison
equal deleted inserted replaced
42:99220aa136d9 43:339e2da5bf83
1 /* 1 /*
2 * The dialog that controls font corecion. 2 * The dialog that controls font corecion.
3 */ 3 */
4 package name.blackcap.clipman 4 package name.blackcap.clipman
5 5
6 import java.awt.Color
7 import java.awt.Container
8 import java.awt.Dimension
9 import java.awt.Font
10 import java.awt.GraphicsEnvironment
11 import java.awt.Toolkit
12 import java.awt.event.ActionEvent 6 import java.awt.event.ActionEvent
13 import java.awt.event.ActionListener 7 import java.awt.event.ActionListener
14 import java.io.BufferedWriter 8 import java.io.BufferedWriter
15 import java.io.FileOutputStream 9 import java.io.FileOutputStream
16 import java.io.IOException 10 import java.io.IOException
23 import javax.swing.event.ChangeEvent 17 import javax.swing.event.ChangeEvent
24 import javax.swing.event.ChangeListener 18 import javax.swing.event.ChangeListener
25 import kotlin.math.log10 19 import kotlin.math.log10
26 import kotlin.math.pow 20 import kotlin.math.pow
27 import kotlin.math.roundToInt 21 import kotlin.math.roundToInt
28 import kotlin.text.toFloat
29 import kotlin.text.toInt 22 import kotlin.text.toInt
30 23
31 /* work around name shadowing */ 24 /* work around name shadowing */
32 private val _PROPS = PROPERTIES 25 private val _PROPS = PROPERTIES
33 26
34 class SettingsDialog: JDialog(frame.v), ActionListener, ChangeListener { 27 class SettingsDialog: JDialog(frame.v), ActionListener, ChangeListener {
35 /* the proportional font family */
36 private val _pFamily = JComboBox<String>(FONTS).apply {
37 selectedItem = _PROPS.getString("prop.family")
38 alignmentX = JComboBox.LEFT_ALIGNMENT
39 }
40 val pFamily: String
41 get() {
42 return _pFamily.selectedItem as String
43 }
44
45 /* the proportional font size */
46 private val _pSize = JComboBox<Float>(SIZES).also {
47 it.selectedItem = _PROPS.getFloat("prop.size")
48 it.alignmentX = JComboBox.LEFT_ALIGNMENT
49 it.setEditable(true)
50 }
51 val pSize: Float
52 get() {
53 return _pSize.selectedItem as Float
54 }
55
56 /* the monospaced font family */
57 private val _mFamily = JComboBox<String>(FONTS).apply {
58 selectedItem = _PROPS.getString("mono.family")
59 alignmentX = JComboBox.LEFT_ALIGNMENT
60 }
61 val mFamily: String
62 get() {
63 return _mFamily.selectedItem as String
64 }
65
66 /* the monospaced font size */
67 private val _mSize = JComboBox<Float>(SIZES).also {
68 it.selectedItem = _PROPS.getFloat("mono.size")
69 it.alignmentX = JComboBox.LEFT_ALIGNMENT
70 it.setEditable(true)
71 }
72 val mSize: Float
73 get() {
74 return _mSize.selectedItem as Float
75 }
76
77 /* max queue length */ 28 /* max queue length */
78 private val _qLength = _PROPS.getInt("queue.length") 29 private val _qLength = _PROPS.getInt("queue.length")
79 private val _qlSlider = JSlider(10000, 30000, spinToSlide(_qLength)).also { 30 private val _qlSlider = JSlider(10000, 30000, spinToSlide(_qLength)).also {
80 it.majorTickSpacing = 10000 31 it.majorTickSpacing = 10000
81 it.paintTicks = true 32 it.paintTicks = true
107 private val _cancel = JButton("Cancel").also { 58 private val _cancel = JButton("Cancel").also {
108 it.actionCommand = "Cancel" 59 it.actionCommand = "Cancel"
109 it.addActionListener(this) 60 it.addActionListener(this)
110 } 61 }
111 62
112 private val _rad = JButton("Restore All Defaults").also {
113 it.actionCommand = "Restore"
114 it.addActionListener(this)
115 }
116
117 /* initializer */ 63 /* initializer */
118 init { 64 init {
119 title = "Preferences" 65 title = "Preferences"
120 contentPane.apply { 66 contentPane.apply {
121 add(Box(BoxLayout.Y_AXIS).apply { 67 add(Box(BoxLayout.Y_AXIS).apply {
122 add(Box(BoxLayout.Y_AXIS).apply {
123 border = BorderFactory.createEmptyBorder(BW2, BW2, BW, BW2)
124 alignmentX = Box.CENTER_ALIGNMENT
125 add(leftLabel("Default proportionally-spaced font:"))
126 add(Box.createVerticalStrut(BW))
127 add(Box(BoxLayout.X_AXIS).apply {
128 alignmentX = Box.LEFT_ALIGNMENT
129 add(Box.createGlue())
130 add(Box(BoxLayout.Y_AXIS).apply {
131 add(leftLabel("Family:"))
132 add(_pFamily)
133 })
134 add(Box.createGlue())
135 add(Box(BoxLayout.Y_AXIS).apply {
136 add(leftLabel("Size:"))
137 add(_pSize)
138 })
139 add(Box.createGlue())
140 })
141 })
142 add(JSeparator())
143 add(Box(BoxLayout.Y_AXIS).apply {
144 alignmentX = Box.CENTER_ALIGNMENT
145 border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2)
146 add(leftLabel("Default monospaced font:"))
147 add(Box.createVerticalStrut(BW))
148 add(Box(BoxLayout.X_AXIS).apply {
149 alignmentX = Box.LEFT_ALIGNMENT
150 add(Box.createGlue())
151 add(Box(BoxLayout.Y_AXIS).apply {
152 add(leftLabel("Family:"))
153 add(_mFamily)
154 })
155 add(Box.createGlue())
156 add(Box(BoxLayout.Y_AXIS).apply {
157 add(leftLabel("Size:"))
158 add(_mSize)
159 })
160 add(Box.createGlue())
161 })
162 })
163 add(JSeparator())
164 add(Box(BoxLayout.Y_AXIS).apply { 68 add(Box(BoxLayout.Y_AXIS).apply {
165 alignmentX = Box.CENTER_ALIGNMENT 69 alignmentX = Box.CENTER_ALIGNMENT
166 border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2) 70 border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2)
167 add(leftLabel("Maximum queue size:")) 71 add(leftLabel("Maximum queue size:"))
168 add(Box.createVerticalStrut(BW)) 72 add(Box.createVerticalStrut(BW))
177 }) 81 })
178 add(JSeparator()) 82 add(JSeparator())
179 add(Box(BoxLayout.X_AXIS).apply { 83 add(Box(BoxLayout.X_AXIS).apply {
180 alignmentX = Box.CENTER_ALIGNMENT 84 alignmentX = Box.CENTER_ALIGNMENT
181 border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2) 85 border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2)
182 add(Box.createGlue())
183 add(_rad)
184 add(Box.createGlue()) 86 add(Box.createGlue())
185 add(_cancel) 87 add(_cancel)
186 add(Box.createGlue()) 88 add(Box.createGlue())
187 add(_ok) 89 add(_ok)
188 add(Box.createGlue()) 90 add(Box.createGlue())
199 "OK" -> { 101 "OK" -> {
200 writeProperties() 102 writeProperties()
201 queue.v.maxSize = qLength 103 queue.v.maxSize = qLength
202 setVisible(false) 104 setVisible(false)
203 } 105 }
204 "Cancel" -> setVisible(false) 106 "Cancel" -> {
205 "Restore" -> revertProperties() 107 revertValues()
108 setVisible(false)
109 }
206 } 110 }
207 } 111 }
208 112
209 override fun stateChanged(e: ChangeEvent) { 113 override fun stateChanged(e: ChangeEvent) {
210 when (val source = e.source) { 114 when (val source = e.source) {
220 124
221 private fun leftLabel(text: String) = JLabel(text).apply { 125 private fun leftLabel(text: String) = JLabel(text).apply {
222 alignmentX = JLabel.LEFT_ALIGNMENT 126 alignmentX = JLabel.LEFT_ALIGNMENT
223 } 127 }
224 128
225 private fun badSize(control: JComboBox<Float>, default: Float, fontType: String): Boolean { 129 private fun revertValues()
226 val size = control.selectedItem as? Float
227 if (size == null || size < 1.0f) {
228 JOptionPane.showMessageDialog(frame.v,
229 "Invalid ${fontType} font size.",
230 "Error",
231 JOptionPane.ERROR_MESSAGE)
232 control.selectedItem = default
233 return true
234 }
235 return false
236 }
237
238 private fun revertProperties()
239 { 130 {
240 val params = arrayOf("mono.family", "mono.size", "prop.family", "prop.size", "queue.length")
241 for (param in params) {
242 _PROPS.put(param, DPROPERTIES.get(param))
243 }
244 _mFamily.selectedItem = _PROPS.getString("mono.family")
245 _mSize.selectedItem = _PROPS.getFloat("mono.size")
246 _pFamily.selectedItem = _PROPS.getString("prop.family")
247 _pSize.selectedItem = _PROPS.getFloat("prop.size")
248 val ql = _PROPS.getInt("queue.length") 131 val ql = _PROPS.getInt("queue.length")
249 _qlSpinner.value = ql 132 _qlSpinner.value = ql
250 _qlSlider.value = spinToSlide(ql) 133 _qlSlider.value = spinToSlide(ql)
251 } 134 }
252 135
253 private fun writeProperties() 136 private fun writeProperties()
254 { 137 {
255 try { 138 try {
256 BufferedWriter(OutputStreamWriter(FileOutputStream(PROP_FILE), CHARSET)).use { 139 BufferedWriter(OutputStreamWriter(FileOutputStream(PROP_FILE), CHARSET)).use {
257 _PROPS.put("mono.family", mFamily)
258 _PROPS.put("mono.size", mSize.toString())
259 _PROPS.put("prop.family", pFamily)
260 _PROPS.put("prop.size", pSize.toString())
261 _PROPS.put("queue.length", qLength.toString()) 140 _PROPS.put("queue.length", qLength.toString())
262 _PROPS.store(it, null) 141 _PROPS.store(it, null)
263 } 142 }
264 } catch (e: IOException) { 143 } catch (e: IOException) {
265 LOGGER.log(Level.WARNING, "IOException writing properties file") 144 LOGGER.log(Level.WARNING, "IOException writing properties file")
278 val settingsDialog = SettingsDialog() 157 val settingsDialog = SettingsDialog()
279 158
280 fun Properties.getString(key: String): String = get(key) as String 159 fun Properties.getString(key: String): String = get(key) as String
281 160
282 fun Properties.getInt(key: String): Int = getString(key).toInt() 161 fun Properties.getInt(key: String): Int = getString(key).toInt()
283
284 fun Properties.getFloat(key: String): Float = getString(key).toFloat()