Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/CoerceDialog.kt @ 35:5f8475b37e23
Got it correctly enabling and disabling menu items.
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 30 Jan 2020 20:21:42 -0800 |
parents | 376643a09b52 |
children | 08eaae2aaf76 |
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 it.actionCommand = "Size" | |
46 it.addActionListener(this) | |
47 } | |
48 val pSize: Float | |
49 get() { | |
50 return _pSize.selectedItem as Float | |
51 } | |
52 | |
53 /* the monospaced font family */ | |
54 private val _mFamily = JComboBox<String>(FONTS).apply { | |
55 selectedIndex = getFontIndex(Font.MONOSPACED) | |
32
4d87bedb3f65
Looks better, Still wrestling with the damned labels.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
56 alignmentX = JComboBox.LEFT_ALIGNMENT |
31 | 57 } |
58 val mFamily: String | |
59 get() { | |
60 return _mFamily.selectedItem as String | |
61 } | |
62 | |
63 /* the monospaced font size */ | |
64 private val _mSize = JComboBox<Float>(SIZES).also { | |
65 it.selectedIndex = DSIZEI | |
32
4d87bedb3f65
Looks better, Still wrestling with the damned labels.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
66 it.alignmentX = JComboBox.LEFT_ALIGNMENT |
31 | 67 it.setEditable(true) |
68 it.actionCommand = "Size" | |
69 it.addActionListener(this) | |
70 } | |
71 val mSize: Float | |
72 get() { | |
73 return _mSize.selectedItem as Float | |
74 } | |
75 | |
76 /* standard spacing between elements (10 pixels ≅ 1/7") and half that */ | |
77 private val BW = 5 | |
78 private val BW2 = 10 | |
79 | |
80 /* buttons */ | |
81 private val _coerce = JButton("Coerce").also { | |
82 it.actionCommand = "Coerce" | |
83 it.addActionListener(this) | |
84 } | |
85 | |
86 private val _cancel = JButton("Cancel").also { | |
87 it.actionCommand = "Cancel" | |
88 it.addActionListener(this) | |
89 } | |
90 | |
91 /* initializer */ | |
92 init { | |
93 title = "Coerce Fonts" | |
94 contentPane.apply { | |
95 add(Box(BoxLayout.Y_AXIS).apply { | |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
96 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
|
97 border = BorderFactory.createEmptyBorder(BW2, BW2, BW, BW2) |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
98 alignmentX = Box.CENTER_ALIGNMENT |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
99 add(leftLabel("Coerce proportionally-spaced text to…")) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
100 add(Box.createVerticalStrut(BW)) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
101 add(Box(BoxLayout.X_AXIS).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
102 alignmentX = Box.LEFT_ALIGNMENT |
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("Family:")) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
106 add(_pFamily) |
32
4d87bedb3f65
Looks better, Still wrestling with the damned labels.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
107 }) |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
108 add(Box.createGlue()) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
109 add(Box(BoxLayout.Y_AXIS).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
110 add(leftLabel("Size:")) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
111 add(_pSize) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
112 }) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
113 add(Box.createGlue()) |
31 | 114 }) |
115 }) | |
116 add(JSeparator()) | |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
117 add(Box(BoxLayout.Y_AXIS).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
118 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
|
119 border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2) |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
120 add(leftLabel("Coerce monospaced text to…")) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
121 add(Box.createVerticalStrut(BW)) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
122 add(Box(BoxLayout.X_AXIS).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
123 alignmentX = Box.LEFT_ALIGNMENT |
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("Family:")) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
127 add(_mFamily) |
32
4d87bedb3f65
Looks better, Still wrestling with the damned labels.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
128 }) |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
129 add(Box.createGlue()) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
130 add(Box(BoxLayout.Y_AXIS).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
131 add(leftLabel("Size:")) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
132 add(_mSize) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
133 }) |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
134 add(Box.createGlue()) |
31 | 135 }) |
136 }) | |
137 add(JSeparator()) | |
138 add(Box(BoxLayout.X_AXIS).apply { | |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
139 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
|
140 border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2) |
31 | 141 add(Box.createGlue()) |
142 add(_cancel) | |
143 add(Box.createGlue()) | |
144 add(_coerce) | |
145 add(Box.createGlue()) | |
146 }) | |
147 }) | |
148 } | |
149 rootPane.setDefaultButton(_coerce) | |
150 pack() | |
151 } | |
152 | |
153 override fun actionPerformed(e: ActionEvent) { | |
154 when (e.actionCommand) { | |
155 "Size" -> { | |
156 val source = e.source as? JComboBox<Float> | |
157 if (source != null && (source.selectedItem as Float) < 1.0f) { | |
158 Toolkit.getDefaultToolkit().beep() | |
159 source.selectedIndex = DSIZEI | |
160 } | |
161 } | |
162 "Coerce" -> { | |
163 setVisible(false) | |
164 coerce() | |
165 } | |
166 "Cancel" -> setVisible(false) | |
167 } | |
168 } | |
169 | |
34
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
170 private fun leftLabel(text: String) = JLabel(text).apply { |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
171 alignmentX = JLabel.LEFT_ALIGNMENT |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
172 } |
376643a09b52
Finally fixed the alignment. Wotta PITA...
David Barts <n5jrn@me.com>
parents:
33
diff
changeset
|
173 |
31 | 174 private fun coerce() { |
175 val selected = queue.v.getSelected() | |
176 if (selected == null) { | |
177 JOptionPane.showMessageDialog(frame.v, | |
178 "No item selected.", | |
179 "Error", | |
180 JOptionPane.ERROR_MESSAGE) | |
181 } else { | |
182 val (plain, html) = when (selected.contents) { | |
183 is PasteboardItem.Plain -> | |
184 Pair(selected.contents.plain, null) | |
185 is PasteboardItem.HTML -> | |
186 Pair(selected.contents.plain, selected.contents.html) | |
187 is PasteboardItem.RTF -> | |
188 Pair(selected.contents.plain, selected.contents.html) | |
189 } | |
190 if (html == null) { | |
191 JOptionPane.showMessageDialog(frame.v, | |
192 "Only styled texts may be coerced.", | |
193 "Error", | |
194 JOptionPane.ERROR_MESSAGE) | |
195 } else { | |
196 PasteboardItem.write( | |
197 PasteboardItem.HTML( | |
198 plain, | |
199 coerceHTML(html, normalizeFont(pFamily), pSize, | |
200 normalizeFont(mFamily), mSize))) | |
201 } | |
202 } | |
203 } | |
204 | |
205 private fun getFontIndex(font: String): Int { | |
206 val found = FONTS.indexOf(font) | |
207 if (found < 0) { | |
208 LOGGER.log(Level.WARNING, "font '${font}' not found") | |
209 return 0 | |
210 } | |
211 return found | |
212 } | |
213 | |
214 private fun normalizeFont(font: String): String { | |
215 val lcFont = font.toLowerCase() | |
216 return when (lcFont) { | |
217 in setOf("monospace", "serif", "sans-serif") -> lcFont | |
218 "monospaced" -> "monospace" | |
219 "sansserif" -> "sans-serif" | |
220 else -> font | |
221 } | |
222 } | |
223 } | |
224 | |
225 val coerceDialog = CoerceDialog() |