Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/SearchDialog.kt @ 29:c4f53bc01732
Fix searching (and main display).
author | David Barts <n5jrn@me.com> |
---|---|
date | Wed, 29 Jan 2020 14:36:16 -0800 |
parents | f1fcc1281dad |
children | 0c6c18a733b7 |
rev | line source |
---|---|
27 | 1 /* |
2 * The dialog that controls a search. | |
3 */ | |
4 package name.blackcap.clipman | |
5 | |
6 import java.awt.Color | |
7 import java.awt.Toolkit | |
8 import java.awt.event.ActionEvent | |
9 import java.awt.event.ActionListener | |
10 import javax.swing.* | |
11 import javax.swing.border.* | |
12 import javax.swing.event.DocumentEvent | |
13 import javax.swing.event.DocumentListener | |
14 | |
15 class SearchDialog: JDialog(frame.v), ActionListener, DocumentListener { | |
16 /* the search term */ | |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
17 private val _searchFor = JTextField(25).also { |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
18 it.border = BorderFactory.createLineBorder(Color.GRAY, 1) |
27 | 19 it.horizontalAlignment = JTextField.LEFT |
20 it.alignmentX = JTextField.LEFT_ALIGNMENT | |
21 it.text = "" | |
22 it.document.addDocumentListener(this) | |
23 } | |
24 val searchFor: String | |
25 get() { | |
26 return _searchFor.text | |
27 } | |
28 | |
29 /* whether or not we should ignore case */ | |
30 private val _ignoreCase = JCheckBox("Ignore case", true) | |
31 val ignoreCase: Boolean | |
32 get() { | |
33 return _ignoreCase.isSelected() | |
34 } | |
35 | |
36 /* whether or not searches should wrap around */ | |
37 private val _autoWrap = JCheckBox("Auto wrap", false) | |
38 val autoWrap: Boolean | |
39 get() { | |
40 return _autoWrap.isSelected() | |
41 } | |
42 | |
43 /* which direction to search */ | |
44 private val _forwards = JRadioButton("Forward", true) | |
45 private val _backwards = JRadioButton("Backward", false) | |
46 private val _direction = ButtonGroup().apply { | |
47 add(_forwards) | |
48 add(_backwards) | |
49 } | |
50 val direction: PasteboardQueue.Direction | |
51 get() { | |
52 if (_forwards.isSelected()) { | |
53 return PasteboardQueue.Direction.FORWARDS | |
54 } | |
55 if (_backwards.isSelected()) { | |
56 return PasteboardQueue.Direction.BACKWARDS | |
57 } | |
58 throw RuntimeException("impossible button state!") | |
59 } | |
60 | |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
61 /* standard spacing between elements (18 pixels = 1/4") and half that */ |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
62 private val BW = 9 |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
63 private val BW2 = 2 * BW |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
64 |
27 | 65 /* where to begin searching from. unlike the other properties, this |
66 one is read/write. null means to start from the beginning on | |
67 forward searches, and from the end on backward searches (i.e. | |
68 search everything) */ | |
69 var origin: PasteboardQueue.Offset? = null | |
70 | |
71 private val _find = JButton("Find").also { | |
72 it.actionCommand = "Find" | |
73 it.addActionListener(this) | |
74 } | |
75 | |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
76 private val _cancel = JButton("Cancel").also { |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
77 it.actionCommand = "Cancel" |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
78 it.addActionListener(this) |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
79 } |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
80 |
27 | 81 /* initializer */ |
82 init { | |
83 title = "Find" | |
84 contentPane.apply { | |
85 add(Box(BoxLayout.Y_AXIS).apply { | |
86 add(Box(BoxLayout.Y_AXIS).apply { | |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
87 add(JLabel("Search for:").apply { |
27 | 88 horizontalAlignment = JLabel.LEFT |
89 alignmentX = JLabel.LEFT_ALIGNMENT | |
90 }) | |
91 add(_searchFor) | |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
92 alignmentX = Box.CENTER_ALIGNMENT |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
93 border = BorderFactory.createEmptyBorder(BW2, BW2, BW, BW2) |
27 | 94 }) |
95 add(Box(BoxLayout.X_AXIS).apply { | |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
96 add(Box.createGlue()) |
27 | 97 add(Box(BoxLayout.Y_AXIS).apply { |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
98 add(JLabel("Settings:").apply { |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
99 horizontalAlignment = JLabel.CENTER |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
100 alignmentX = JLabel.LEFT_ALIGNMENT |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
101 }) |
27 | 102 add(_ignoreCase) |
103 add(_autoWrap) | |
104 }) | |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
105 add(Box.createGlue()) |
27 | 106 add(Box(BoxLayout.Y_AXIS).apply { |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
107 add(JLabel("Direction:").apply { |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
108 horizontalAlignment = JLabel.CENTER |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
109 alignmentX = JLabel.LEFT_ALIGNMENT |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
110 }) |
27 | 111 add(_forwards) |
112 add(_backwards) | |
113 }) | |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
114 add(Box.createGlue()) |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
115 alignmentX = Box.CENTER_ALIGNMENT |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
116 border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2) |
27 | 117 }) |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
118 add(Box(BoxLayout.X_AXIS).apply { |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
119 add(Box.createGlue()) |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
120 add(_cancel) |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
121 add(Box.createGlue()) |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
122 add(_find) |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
123 add(Box.createGlue()) |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
124 border = BorderFactory.createEmptyBorder(BW, BW2, BW2, BW2) |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
125 }) |
27 | 126 }) |
127 } | |
128 rootPane.setDefaultButton(_find) | |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
129 pack() |
27 | 130 } |
131 | |
132 override fun actionPerformed(e: ActionEvent) { | |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
133 when (e.actionCommand) { |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
134 "Find" -> { |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
135 setVisible(false) |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
136 find() |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
137 } |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
138 "Cancel" -> setVisible(false) |
27 | 139 } |
140 } | |
141 | |
142 override fun setVisible(visible: Boolean) { | |
143 if (visible) { | |
144 _searchFor.run { | |
145 requestFocusInWindow() | |
146 selectAll() | |
147 } | |
148 } | |
149 super.setVisible(visible) | |
150 } | |
151 | |
152 fun find(): Unit { | |
28
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
153 if (searchFor.isEmpty()) { |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
154 Toolkit.getDefaultToolkit().beep() |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
155 origin = null |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
156 return |
f1fcc1281dad
Use BorderFactory; clean up Find dialog.
David Barts <n5jrn@me.com>
parents:
27
diff
changeset
|
157 } |
27 | 158 fun doFind(o: PasteboardQueue.Offset?) = queue.v.find(searchFor, |
159 direction = direction, foldCase = ignoreCase, origin = o) | |
160 var result = doFind(origin) | |
161 if (result == null && origin != null && autoWrap) { | |
162 result = doFind(null) | |
163 } | |
164 if (result == null) { | |
165 Toolkit.getDefaultToolkit().beep() | |
29
c4f53bc01732
Fix searching (and main display).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
166 origin = null |
c4f53bc01732
Fix searching (and main display).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
167 } else { |
c4f53bc01732
Fix searching (and main display).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
168 origin = when(direction) { |
c4f53bc01732
Fix searching (and main display).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
169 PasteboardQueue.Direction.FORWARDS -> |
c4f53bc01732
Fix searching (and main display).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
170 PasteboardQueue.Offset(result.inQueue, result.inItem + 1) |
c4f53bc01732
Fix searching (and main display).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
171 PasteboardQueue.Direction.BACKWARDS -> |
c4f53bc01732
Fix searching (and main display).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
172 if (result.inItem == 0) { |
c4f53bc01732
Fix searching (and main display).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
173 if (result.inQueue == 0) null else PasteboardQueue.Offset(result.inQueue - 1, -1) |
c4f53bc01732
Fix searching (and main display).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
174 } else { |
c4f53bc01732
Fix searching (and main display).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
175 PasteboardQueue.Offset(result.inQueue, result.inItem - 1) |
c4f53bc01732
Fix searching (and main display).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
176 } |
c4f53bc01732
Fix searching (and main display).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
177 } |
27 | 178 } |
179 } | |
180 | |
181 /* changing the search string resets the search origin */ | |
182 override fun changedUpdate(e: DocumentEvent) { | |
183 if (e.document === _searchFor.document) { | |
184 origin = null | |
185 } | |
186 } | |
187 override fun insertUpdate(e: DocumentEvent) = changedUpdate(e) | |
188 override fun removeUpdate(e: DocumentEvent) = changedUpdate(e) | |
189 } | |
190 | |
191 val searchDialog = SearchDialog() |