sclass SnippetSelectForm { VF1 onSelected; S snippetTitle; S forceType; JDesktopPane desktop = mainDesktopPane(); Container frame; JList list; JTextField tfCmd; JLabel status; JButton btnReload, btnRun, btnClear, btnOK; S submittedInput; volatile boolean searching; long lastSearch; // time of last search int searchDelay = 100; int maxResults = 100; int autoResearchInterval = 30000; // 30 secs L actionHistory = synchroList(); *() {} *(VF1 *onSelected) {} void go() swing { frame = showFramePossiblyInternal(desktop, "Select Snippet - " + programTitle()); list = new JList; onDoubleClickAndEnter(list, r { ok() }); ActionListener go = actionListener { tfCmd.selectAll(); search(true); }; btnReload = new JButton(isWindows() ? "Reload" : "\u27F3"); btnReload.addActionListener(go); btnClear = new JButton("X"); btnClear.setToolTipText("Show latest"); btnClear.addActionListener(actionListener { tfCmd.setText(""); }); tfCmd = new JTextField; btnOK = listDependentButton(list, "OK", r { ok(); }); JPanel controls = jflow(btnClear, btnReload, btnOK); JPanel north = centerAndEast( withLabel("Search term:", tfCmd), controls); status = new JLabel(" "); JPanel panel = northCenterAndSouth(north, list, status); onEnter(tfCmd, r ok); if (frame instanceof JFrame) onWindowActivated((JFrame) frame, r { search(false) }); installTimer(tfCmd, new Runnable { String lastContents; boolean autoSearchOn = true; public void run() { String text = getInput(); if (text.equals(lastContents)) { if (now() > lastSearch + autoResearchInterval || (!text.equals(submittedInput) && autoSearchOn && !searching)) search(false); } else lastContents = text; } }, searchDelay); frame.add(panel); frame.setBounds(100, 100, 500, 400); search(false); focusLater(tfCmd); } S getInput() { ret tfCmd.getText().trim(); } void search(String cmd, final boolean requestFocus) ctex { searching = true; try { status("Searching " + quote(cmd) + "..."); if (empty(cmd)) cmd = "_"; if (nempty(forceType)) cmd += " type:" + forceType; //if (cbAll.isSelected()) //cmd += " type:runnable"; L l = tbSearch_url(tb_mainServer() + "/tb/search.php?q=" + urlencode(cmd) + "&limit=" + maxResults + "&sort=modified" + standardCredentials()); final new DefaultListModel model; for (Snippet s : l) model.addElement(s.id + " - " + s.title); awt { list.setModel(model); if (model.size() != 0) list.setSelectedIndex(0); status("Found " + model.size() + (model.size() == maxResults ? "+" : "") + " snippet(s)."); if (requestFocus) tfCmd.requestFocus(); } } finally { searching = false; } } void status(final S s) { awt { status.setText(s); } } void search(final boolean requestFocus) { submittedInput = getInput(); lastSearch = now(); final String cmd = submittedInput; actionHistory.add(format3("*: Searching *", now(), cmd)); thread { search(cmd, requestFocus); } } void ok() { fS s = getSelectedItem(list); final int i = indexOf(s, " - "); if (i < 0) ret; snippetTitle = substring(s, i+3); thread { disposePossiblyInternalFrame(frame); pcall-messagebox { temp holdInstance(SnippetSelectForm.this); callF(onSelected, takeFirst(s, i)); } } } }