Last active
December 10, 2015 09:12
-
-
Save clarkttfu/11ddb89861b0c1e03b8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package prototypes; | |
import java.awt.Dimension; | |
import java.awt.GridBagConstraints; | |
import java.awt.GridBagLayout; | |
import java.util.Random; | |
import javax.swing.JFrame; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTextArea; | |
import javax.swing.JTextField; | |
import javax.swing.event.DocumentEvent; | |
import javax.swing.event.DocumentListener; | |
@SuppressWarnings("serial") | |
public class InstantSearchExample extends JFrame { | |
private JTextArea console; | |
private JTextField input; | |
private Thread pre; | |
private Random rand = new Random(2344); | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
InstantSearchExample test = new InstantSearchExample(); | |
test.pack(); | |
test.setVisible(true); | |
} | |
public InstantSearchExample() { | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
initUI(); | |
} | |
private void initUI() { | |
setLayout(new GridBagLayout()); | |
input = new JTextField(); | |
input.getDocument().addDocumentListener(new DocumentListener() { | |
@Override | |
public void removeUpdate(DocumentEvent e) { | |
startSearchJob(); | |
} | |
@Override | |
public void insertUpdate(DocumentEvent e) { | |
startSearchJob(); | |
} | |
@Override | |
public void changedUpdate(DocumentEvent e) { | |
startSearchJob(); | |
} | |
}); | |
console = new JTextArea(); | |
console.setEditable(false); | |
JScrollPane scroller = new JScrollPane(console, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); | |
scroller.setPreferredSize(new Dimension(320, 400)); | |
GridBagConstraints cons = new GridBagConstraints(); | |
cons.fill = GridBagConstraints.HORIZONTAL; | |
cons.weightx = 1.0; | |
cons.gridx = 0; | |
cons.gridy = 0; | |
add(input, cons); | |
cons.gridx = 0; | |
cons.gridy = 1; | |
cons.fill = GridBagConstraints.BOTH; | |
cons.weighty = 1.0; | |
add(scroller, cons); | |
} | |
private void startSearchJob() { | |
new Thread() { | |
@Override | |
public void run() { | |
pre = this; | |
String text = input.getText(); | |
try { | |
Thread.sleep(600); | |
} catch (Exception e) { | |
} finally { | |
if (pre == this) { | |
int cost = search(text); | |
if (pre == this) { | |
console.append(String.format("Cost: %1dms, %2s\r\n", cost, text)); | |
} else { | |
console.append(String.format("Cost: %1dms, %2s dropped\r\n", cost, text)); | |
} | |
} | |
} | |
} | |
}.start(); | |
} | |
private int search(String text) { | |
int cost = rand.nextInt(4000); | |
try { | |
Thread.sleep(cost); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return cost; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment