!636
!modern // includes string ==
!actionListener {
!x619 // standard classes (LetterLayout, CenteredLine)
!multi-line strings
!awt {
main {
static JList list;
static JTextField tfCmd;
static JLabel status;
static S submittedInput;
static int searchDelay = 100;
static volatile boolean searching;
!include #1000839 // PopupMenuHelper
psvm {
installHelloMessage("JavaX");
JFrame frame;
try {
frame = new JFrame("JavaX v3");
} catch (HeadlessException e) {
System.out.println("We're headless...");
return;
}
JPanel panel = new JPanel(new BorderLayout());
list = new JList();
panel.add(BorderLayout.CENTER, new JScrollPane(list));
new PopupMenuHelper() {
void fillMenu() {
JMenuItem mi;
int idx = list.locationToIndex(point);
final String item = cast list.getModel().getElementAt(idx);
list.setSelectedIndex(idx);
mi = new JMenuItem("RUN! (warning: no questions asked)");
mi.addActionListener(actionListener { run(item); });
menu.add(mi);
mi = new JMenuItem("Run with arguments...");
mi.addActionListener(actionListener { runWithArguments(item); });
menu.add(mi);
}
}.install(list);
tfCmd = new JTextField();
panel.add(BorderLayout.NORTH, withLabel("Search term:", tfCmd));
status = new JLabel(" ");
panel.add(BorderLayout.SOUTH, status);
tfCmd.addActionListener(actionListener {
tfCmd.selectAll();
final String cmd = tfCmd.getText();
thread {
try {
tfCmd.setEnabled(false);
search(cmd);
} finally {
tfCmd.setEnabled(true);
}
}
});
frame.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e) {
tfCmd.requestFocus();
}
});
installTimer(tfCmd, new Runnable() {
String lastContents;
boolean autoSearchOn = true;
public void run() {
String text = tfCmd.getText();
if (text.equals(lastContents)) {
if (!text.equals(submittedInput) && autoSearchOn && !searching)
doIt();
} else
lastContents = text;
}
}, searchDelay);
frame.add(panel);
frame.setBounds(100, 100, 500, 400);
frame.setVisible(true);
exitOnFrameClose(frame);
search("");
}
static void search(String cmd) ctex {
searching = true;
try {
status("Searching " + quote(cmd) + "...");
if (cmd == "") cmd = "%";
S page = loadPage("http://tinybrain.de:8080/tb/search.php?q=" + urlencode(cmd + " type:runnable"));
Matcher m = Pattern.compile(">(#\\d+) - (.*?)
").matcher(page);
final new DefaultListModel model;
while (m.find())
model.addElement(htmldecode(m.group(1) + " - " + m.group(2)));
awt {
list.setModel(model);
status("Found " + model.size() + " runnable(s).");
tfCmd.requestFocus();
}
} finally {
searching = false;
}
}
static void status(final S s) {
awt { status.setText(s); }
}
static void run(S item) {
int i = item.indexOf(' ');
if (i >= 0) item = item.substring(0, i);
long id = parseSnippetID(item);
print("Running " + id);
nohupJavax("#1000874".substring(1) + " " + id);
}
static void runWithArguments(S item) {
// TODO: run a dialog for that
}
static void doIt() {
submittedInput = tfCmd.getText();
thread {
search(submittedInput);
}
}
}