Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/CoerceDialog.kt @ 45:ca8a23bae4fa
Adjust spacing in preferences.
author | David Barts <n5jrn@me.com> |
---|---|
date | Mon, 10 Feb 2020 06:55:07 -0700 |
parents | 339e2da5bf83 |
children | 19d9da731c43 |
rev | line source |
---|---|
31 | 1 /* |
2 * The dialog that controls font corecion. | |
3 */ | |
4 package name.blackcap.clipman | |
5 | |
6 import java.awt.Color | |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
7 import java.awt.Container |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
8 import java.awt.Dimension |
31 | 9 import java.awt.Font |
10 import java.awt.GraphicsEnvironment | |
11 import java.awt.Toolkit | |
12 import java.awt.event.ActionEvent | |
13 import java.awt.event.ActionListener | |
14 import java.util.logging.Level | |
15 import java.util.logging.Logger | |
16 import javax.swing.* | |
17 import javax.swing.event.DocumentEvent | |
18 import javax.swing.event.DocumentListener | |
19 | |
43
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
20 class CoerceDialog: JDialog(frame.v), ActionListener { |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
21 private val FONTS = |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
22 GraphicsEnvironment.getLocalGraphicsEnvironment().availableFontFamilyNames.copyOf().apply { |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
23 sort() |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
24 } |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
25 private val SIZES = |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
26 arrayOf(9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 16.0f, 18.0f, |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
27 24.0f, 36.0f, 48.0f, 64.0f, 72.0f, 96.0f, 144.0f, 288.0f) |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
38
diff
changeset
|
28 |
31 | 29 /* the proportional font family */ |
30 private val _pFamily = JComboBox<String>(FONTS).apply { | |
43
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
31 selectedIndex = getFontIndex(Font.SERIF) |
32
4d87bedb3f65
Looks better, Still wrestling with the damned labels.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
32 alignmentX = JComboBox.LEFT_ALIGNMENT |
31 | 33 } |
34 val pFamily: String | |
35 get() { | |
36 return _pFamily.selectedItem as String | |
37 } | |
38 | |
39 /* the proportional font size */ | |
40 private val _pSize = JComboBox<Float>(SIZES).also { | |
43
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
41 it.selectedItem = PROP_SIZE.toFloat() |
32
4d87bedb3f65
Looks better, Still wrestling with the damned labels.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
42 it.alignmentX = JComboBox.LEFT_ALIGNMENT |
31 | 43 it.setEditable(true) |
44 } | |
45 val pSize: Float | |
46 get() { | |
47 return _pSize.selectedItem as Float | |
48 } | |
49 | |
50 /* the monospaced font family */ | |
51 private val _mFamily = JComboBox<String>(FONTS).apply { | |
43
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
52 selectedIndex = getFontIndex(Font.MONOSPACED) |
32
4d87bedb3f65
Looks better, Still wrestling with the damned labels.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
53 alignmentX = JComboBox.LEFT_ALIGNMENT |
31 | 54 } |
55 val mFamily: String | |
56 get() { | |
57 return _mFamily.selectedItem as String | |
58 } | |
59 | |
60 /* the monospaced font size */ | |
61 private val _mSize = JComboBox<Float>(SIZES).also { | |
43
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
62 it.selectedItem = MONO_SIZE.toFloat() |
32
4d87bedb3f65
Looks better, Still wrestling with the damned labels.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
63 it.alignmentX = JComboBox.LEFT_ALIGNMENT |
31 | 64 it.setEditable(true) |
65 } | |
66 val mSize: Float | |
67 get() { | |
68 return _mSize.selectedItem as Float | |
69 } | |
70 | |
71 /* standard spacing between elements (10 pixels ≅ 1/7") and half that */ | |
72 private val BW = 5 | |
73 private val BW2 = 10 | |
74 | |
75 /* buttons */ | |
76 private val _coerce = JButton("Coerce").also { | |
77 it.actionCommand = "Coerce" | |
78 it.addActionListener(this) | |
79 } | |
80 | |
81 private val _cancel = JButton("Cancel").also { | |
82 it.actionCommand = "Cancel" | |
83 it.addActionListener(this) | |
84 } | |
85 | |
86 /* initializer */ | |
87 init { | |
88 title = "Coerce Fonts" | |
89 contentPane.apply { | |
90 add(Box(BoxLayout.Y_AXIS).apply { | |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
91 add(Box(BoxLayout.Y_AXIS).apply { |
32
4d87bedb3f65
Looks better, Still wrestling with the damned labels.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
92 border = BorderFactory.createEmptyBorder(BW2, BW2, BW, BW2) |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
93 alignmentX = Box.CENTER_ALIGNMENT |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
94 add(leftLabel("Coerce proportionally-spaced text to…")) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
95 add(Box.createVerticalStrut(BW)) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
96 add(Box(BoxLayout.X_AXIS).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
97 alignmentX = Box.LEFT_ALIGNMENT |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
98 add(Box.createGlue()) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
99 add(Box(BoxLayout.Y_AXIS).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
100 add(leftLabel("Family:")) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
101 add(_pFamily) |
32
4d87bedb3f65
Looks better, Still wrestling with the damned labels.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
102 }) |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
103 add(Box.createGlue()) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
104 add(Box(BoxLayout.Y_AXIS).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
105 add(leftLabel("Size:")) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
106 add(_pSize) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
107 }) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
108 add(Box.createGlue()) |
31 | 109 }) |
110 }) | |
111 add(JSeparator()) | |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
112 add(Box(BoxLayout.Y_AXIS).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
113 alignmentX = Box.CENTER_ALIGNMENT |
33
277cbb78bc5a
A few tweaks. Piece of shit still can't left-align its labels. Sigh.
David Barts <n5jrn@me.com>
parents:
32
diff
changeset
|
114 border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2) |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
115 add(leftLabel("Coerce monospaced text to…")) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
116 add(Box.createVerticalStrut(BW)) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
117 add(Box(BoxLayout.X_AXIS).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
118 alignmentX = Box.LEFT_ALIGNMENT |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
119 add(Box.createGlue()) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
120 add(Box(BoxLayout.Y_AXIS).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
121 add(leftLabel("Family:")) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
122 add(_mFamily) |
32
4d87bedb3f65
Looks better, Still wrestling with the damned labels.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
123 }) |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
124 add(Box.createGlue()) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
125 add(Box(BoxLayout.Y_AXIS).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
126 add(leftLabel("Size:")) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
127 add(_mSize) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
128 }) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
129 add(Box.createGlue()) |
31 | 130 }) |
131 }) | |
132 add(JSeparator()) | |
133 add(Box(BoxLayout.X_AXIS).apply { | |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
134 alignmentX = Box.CENTER_ALIGNMENT |
33
277cbb78bc5a
A few tweaks. Piece of shit still can't left-align its labels. Sigh.
David Barts <n5jrn@me.com>
parents:
32
diff
changeset
|
135 border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2) |
31 | 136 add(Box.createGlue()) |
137 add(_cancel) | |
138 add(Box.createGlue()) | |
139 add(_coerce) | |
140 add(Box.createGlue()) | |
141 }) | |
142 }) | |
143 } | |
144 rootPane.setDefaultButton(_coerce) | |
145 pack() | |
38
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
146 setResizable(false) |
31 | 147 } |
148 | |
149 override fun actionPerformed(e: ActionEvent) { | |
150 when (e.actionCommand) { | |
151 "Coerce" -> { | |
152 setVisible(false) | |
153 coerce() | |
154 } | |
155 "Cancel" -> setVisible(false) | |
156 } | |
157 } | |
158 | |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
159 private fun leftLabel(text: String) = JLabel(text).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
160 alignmentX = JLabel.LEFT_ALIGNMENT |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
161 } |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
162 |
31 | 163 private fun coerce() { |
164 val selected = queue.v.getSelected() | |
165 if (selected == null) { | |
166 JOptionPane.showMessageDialog(frame.v, | |
167 "No item selected.", | |
168 "Error", | |
169 JOptionPane.ERROR_MESSAGE) | |
170 } else { | |
171 val (plain, html) = when (selected.contents) { | |
172 is PasteboardItem.Plain -> | |
173 Pair(selected.contents.plain, null) | |
174 is PasteboardItem.HTML -> | |
175 Pair(selected.contents.plain, selected.contents.html) | |
176 is PasteboardItem.RTF -> | |
177 Pair(selected.contents.plain, selected.contents.html) | |
178 } | |
179 if (html == null) { | |
180 JOptionPane.showMessageDialog(frame.v, | |
181 "Only styled texts may be coerced.", | |
182 "Error", | |
183 JOptionPane.ERROR_MESSAGE) | |
184 } else { | |
43
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
185 if (badSize(_pSize, PROP_SIZE, "proportionally-spaced") || badSize(_mSize, MONO_SIZE, "monospaced")) { |
38
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
186 return |
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
187 } |
31 | 188 PasteboardItem.write( |
189 PasteboardItem.HTML( | |
190 plain, | |
191 coerceHTML(html, normalizeFont(pFamily), pSize, | |
192 normalizeFont(mFamily), mSize))) | |
193 } | |
194 } | |
195 } | |
196 | |
43
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
197 private fun badSize(control: JComboBox<Float>, default: Int, fontType: String): Boolean { |
38
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
198 val size = control.selectedItem as? Float |
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
199 if (size == null || size < 1.0f) { |
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
200 JOptionPane.showMessageDialog(frame.v, |
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
201 "Invalid ${fontType} font size.", |
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
202 "Error", |
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
203 JOptionPane.ERROR_MESSAGE) |
43
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
204 control.selectedItem = default.toFloat() |
38
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
205 return true |
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
206 } |
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
207 return false |
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
208 } |
08eaae2aaf76
Stop dialog resizing, fix detection of bad font sizes.
David Barts <n5jrn@me.com>
parents:
34
diff
changeset
|
209 |
43
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
210 private fun getFontIndex(font: String): Int { |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
211 val found = FONTS.indexOf(font) |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
212 if (found < 0) { |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
213 LOGGER.log(Level.WARNING, "font '${font}' not found") |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
214 return 0 |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
215 } |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
216 return found |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
217 } |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
218 |
31 | 219 private fun normalizeFont(font: String): String { |
220 val lcFont = font.toLowerCase() | |
221 return when (lcFont) { | |
222 in setOf("monospace", "serif", "sans-serif") -> lcFont | |
223 "monospaced" -> "monospace" | |
224 "sansserif" -> "sans-serif" | |
225 else -> font | |
226 } | |
227 } | |
228 } | |
229 | |
230 val coerceDialog = CoerceDialog() |