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