-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutocompleteGUI.java
353 lines (331 loc) · 16 KB
/
AutocompleteGUI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*************************************************************************
* @author Matthew Drabick
* Compilation: javac AutocompleteGUI.java
* Execution: java AutocompleteGUI input.txt k
* Dependencies: In.java Autocomplete.java
* Data files:
*
*
* Interactive GUI used to demonstrate the Autocomplete data type.
*
* * Reads a list of terms and weights from a file, specified as a
* command-line argument.
*
* * As the user types in a text box, display the top-k terms
* that start with the text that the user types.
*
* * Displays the result in a browser if the user selects a term
* (by pressing enter or clicking a selection).
*
*
* BUG: Selections don't autoupdate if user enter character into text box without typing it
* (e.g., by selecting it from Mac OS X Character Viewer).
* BUG: Completion list disappears when user clicks to browse.
*
* FEATURE: add more vertical space between items in suggestion box
* FEATURE: option to display weights?
*
* UPDATE: Modified to display weights on the left of auto-complete box
*
*
* % java AutocompleteGUI cities.txt 10
*
*************************************************************************/
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import javax.swing.*;
public class AutocompleteGUI extends JFrame {
private static int DEF_WIDTH = 600;
private static int DEF_HEIGHT = 400;
private static String searchURL = "https://www.google.com/search?q=";
// display top k results
private final int k;
public AutocompleteGUI(String filename, int k) {
this.k = k;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Autocomplete");
setLocationRelativeTo(null);
Container content = getContentPane();
GroupLayout layout = new GroupLayout(content);
content.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
final AutocompletePanel ap = new AutocompletePanel(filename);
JButton searchButton = new JButton("Search Google");
//searchButton.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
searchButton.addMouseListener(new MouseListener() {
public void mousePressed (MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered (MouseEvent e) { }
public void mouseExited (MouseEvent e) { }
@Override
public void mouseClicked(MouseEvent e) {
searchOnline(ap.getSelectedText());
}
});
JLabel textLabel = new JLabel("Type text:");
textLabel.setBorder(BorderFactory.createEmptyBorder(1, 4, 0, 0));
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE)
.addComponent(ap, 0, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE)
.addComponent(searchButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(textLabel)
.addComponent(ap)
.addComponent(searchButton))
);
setPreferredSize(new Dimension(DEF_WIDTH, DEF_HEIGHT));
pack();
}
private class AutocompletePanel extends JPanel {
private final JTextField searchText;
private Autocomplete auto;
private String[] results = new String[k];
private JList suggestions;
// keep these two values in sync! - used to keep the listbox the same width as the textfield
private final int DEF_COLUMNS = 30;
private final String suggListLen = "<b>Harry Potter and the Deathly Hallows: Part 1 (2010)</b>";
public AutocompletePanel(String filename) {
super();
// read in the data
In in = new In(filename);
int N = Integer.parseInt(in.readLine());
String[] terms = new String[N];
double[] weights = new double[N];
for (int i = 0; i < N; i++) {
String line = in.readLine();
int tab = line.indexOf('\t');
weights[i] = Double.parseDouble(line.substring(0, tab).trim());
terms[i] = line.substring(tab + 1);
}
// create the autocomplete object
auto = new Autocomplete(terms, weights);
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
searchText = new JTextField(DEF_COLUMNS);
searchText.setMaximumSize(new Dimension(searchText.getMaximumSize().width, searchText.getPreferredSize().height));
searchText.getInputMap().put(KeyStroke.getKeyStroke("UP"), "none");
searchText.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "none");
searchText.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
int pos = searchText.getText().length();
searchText.setCaretPosition(pos);
}
public void focusLost(FocusEvent e) { }
});
JPanel searchTextPanel = new JPanel();
searchTextPanel.add(searchText);
searchTextPanel.setBorder(BorderFactory.createEmptyBorder(2,0, 0, 0));
searchTextPanel.setLayout(new GridLayout(1,1));
suggestions = new JList(results);
suggestions.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
suggestions.setVisible(false);
suggestions.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
suggestions.setMaximumSize(new Dimension(searchText.getMaximumSize().width, suggestions.getPreferredSize().height));
suggestions.setPrototypeCellValue(suggListLen); // set to make equal to the width of the textfield
suggestions.setFont(suggestions.getFont().deriveFont(Font.PLAIN, 13));
Action makeSelection = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (!suggestions.isSelectionEmpty()) {
String selection = (String) suggestions.getSelectedValue();
selection = selection.replaceAll("\\<.*?>","");
searchText.setText(selection);
getSuggestions(selection);
}
searchOnline(searchText.getText());
}
};
Action moveSelectionUp = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (suggestions.getSelectedIndex() >= 0) {
suggestions.requestFocusInWindow();
suggestions.setSelectedIndex(suggestions.getSelectedIndex() - 1);
}
}
};
Action moveSelectionDown = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (suggestions.getSelectedIndex() != results.length) {
suggestions.requestFocusInWindow();
suggestions.setSelectedIndex(suggestions.getSelectedIndex() + 1);
}
}
};
Action moveSelectionUpFocused = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (suggestions.getSelectedIndex() == 0) {
suggestions.clearSelection();
searchText.requestFocusInWindow();
int pos = searchText.getText().length();
// searchText.select(pos, pos);
searchText.setSelectionEnd(0);
}
else if (suggestions.getSelectedIndex() >= 0) {
suggestions.setSelectedIndex(suggestions.getSelectedIndex() - 1);
}
}
};
suggestions.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("UP"), "moveSelectionUp");
suggestions.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("DOWN"), "moveSelectionDown");
suggestions.getActionMap().put("moveSelectionUp", moveSelectionUp);
suggestions.getActionMap().put("moveSelectionDown", moveSelectionDown);
suggestions.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"), "makeSelection");
suggestions.getInputMap().put(KeyStroke.getKeyStroke("UP"), "moveSelectionUpFocused");
suggestions.getActionMap().put("moveSelectionUpFocused", moveSelectionUpFocused);
suggestions.getActionMap().put("makeSelection", makeSelection);
JPanel suggestionsPanel = new JPanel();
suggestionsPanel.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
suggestionsPanel.add(suggestions);
suggestionsPanel.setLayout(new GridLayout(1,1));
this.setMaximumSize(new Dimension(searchText.getMaximumSize().width, this.getPreferredSize().height));
suggestions.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
JList theList = (JList) mouseEvent.getSource();
if (mouseEvent.getClickCount() >= 1) {
int index = theList.locationToIndex(mouseEvent.getPoint());
if (index >= 0) {
String selection = getSelectedText();
searchText.setText(selection);
String text = searchText.getText();
getSuggestions(text);
searchOnline(searchText.getText());
}
}
}
});
searchText.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) { }
@Override
public void keyReleased(KeyEvent e)
{
JTextField txtSrc = (JTextField) e.getSource();
String text = txtSrc.getText();
getSuggestions(text);
}
@Override
public void keyTyped(KeyEvent e) { }
});
searchText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String selection = getSelectedText();
searchText.setText(selection);
getSuggestions(selection);
searchOnline(searchText.getText());
}
});
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(searchTextPanel, 0, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(suggestionsPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(searchTextPanel)
.addComponent(suggestionsPanel)
);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
/**
* Makes a call to the implementation of Autocomplete to get suggestions
* for the currently entered text.
* @param text string to search for
*/
public void getSuggestions(String text) {
text = text.trim();
if (text.equals("")) {
suggestions.clearSelection();
suggestions.setVisible(false);
}
else {
int textLen = text.length();
LinkedList<String> resultQ = new LinkedList<>();
LinkedList<Double> ds = new LinkedList<Double>();
for (String term : auto.topMatches(text, k)) {
resultQ.offer(term);
ds.offer(auto.weightOf(term));
}
if (!resultQ.isEmpty()) {
results = new String[resultQ.size()];
for (int i = 0; i < results.length; i++) {
results[i] = resultQ.poll();
/*Modified to include the weights of each term and a delimiter "|" to ensure that
* the search does not include the weight.
*/
results[i] = "<html><span style=\"color:#C0C0C0;\">" + ds.poll() + "</span>" + "|"
+ results[i].substring(0, textLen) + "<b>" + results[i].substring(textLen) + "</b></html>";
}
suggestions.setListData(results);
suggestions.setVisible(true);
//suggestions.setSelectedIndex(0); // Pressing enter automatically selects the first one if nothing has been
}
else {
// No suggestions
suggestions.setVisible(false);
suggestions.clearSelection();
}
}
}
public String getSelectedText() {
if (!suggestions.isSelectionEmpty()) {
String selection = (String) suggestions.getSelectedValue();
int split = selection.indexOf("|");
selection = selection.substring(split + 1, selection.length()); // Excludes weight from search string
selection = selection.replaceAll("\\<.*?>","");
return selection;
}
else return getSearchText();
}
public String getSearchText() {
return searchText.getText();
}
}
/**
* Creates a URI from the user-defined string and searches the web with the
* selected search engine
* Opens the default web browser (or a new tab if it is already open)
* @param s string to search online for
*/
private void searchOnline(String s) {
URI searchAddress = null;
try {
URI tempAddress = new URI(searchURL + s.trim().replace(' ', '+'));
searchAddress = new URI(tempAddress.toASCIIString()); // hack to handle Unicode
} catch (URISyntaxException e2) {
e2.printStackTrace();
return;
}
try {
Desktop.getDesktop().browse(searchAddress);
} catch (IOException e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) {
final String filename = args[0];
final int k = Integer.parseInt(args[1]);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new AutocompleteGUI(filename, k).setVisible(true);
}
});
}
}