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