Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

127
LINES

< > BotCompany Repo | #1004469 // Simple Snippet Search

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 3874K of libraries. Click here for Pure Java version (7134L/50K/177K).

!7

static JFrame frame;
static JList list;
static JTextField tfCmd;
static JLabel status;
static JButton btnReload, btnRun, btnClear;
static S submittedInput;
static volatile boolean searching;
static long lastSearch; // time of last search

static int searchDelay = 100;
static int maxResults = 100;
static int autoResearchInterval = 30000; // 30 secs

static L<S> actionHistory = synchroList();

p-substance {
  frame = showFrame();
  list = new JList;

  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;
  
  JPanel controls = jflow(btnClear, btnReload);

  JPanel north = centerAndEast(
    withLabel("Search term:", tfCmd),
    controls);
  
  /*btnRun = new JButton("Run");
  btnRun.addActionListener(actionListener {
    run();
  });*/
  
  status = new JLabel(" ");
  JPanel panel = northCenterAndSouth(north, list, status);

  tfCmd.addActionListener(go);
  
  frame.addWindowListener(new WindowAdapter {
    public void windowOpened(WindowEvent e) {
      tfCmd.requestFocus();
    }
    
    public void windowActivated(WindowEvent e) {
      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);
}

static S getInput() {
  ret tfCmd.getText().trim();
}

static void search(String cmd, final boolean requestFocus) ctex {
  searching = true;
  try {
    status("Searching " + quote(cmd) + "...");
    if (empty(cmd)) cmd = "_";
    //if (cbAll.isSelected())
      //cmd += " type:runnable";
    S page = loadPage("http://tinybrain.de:8080/tb/search.php?q=" + urlencode(cmd) + "&limit=" + maxResults + "&sort=modified");
    Matcher m = Pattern.compile(">(#\\d+)</a> - (.*?)<br>").matcher(page);
    
    final new DefaultListModel model;
    while (m.find()) {
      String title = m.group(2);
      title = join(dropAllTags(htmlcoarsetok(title)));
      model.addElement(htmldecode(m.group(1) + " - " + title));
    }
      
    awt {
      list.setModel(model);
      status("Found " + model.size() + (model.size() == maxResults ? "+" : "") + " snippet(s).");
      if (requestFocus)
        tfCmd.requestFocus();
    }
  } finally {
    searching = false;
  }
}

static void status(final S s) {
  awt { status.setText(s); }
}

static void search(final boolean requestFocus) {
  submittedInput = getInput();
  lastSearch = now();
  final String cmd = submittedInput;
  actionHistory.add(format3("*: Searching *", now(), cmd));
  thread {
    search(cmd, requestFocus);
  }  
}

Author comment

Began life as a copy of #1000825

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004469
Snippet name: Simple Snippet Search
Eternal ID of this version: #1004469/5
Text MD5: 85d842c1d8ef92fa0553ad9f3b5e3724
Transpilation MD5: 8e4037f4affd8c7a4ff8a5256ec714cf
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-04 09:51:29
Source code size: 3293 bytes / 127 lines
Pitched / IR pitched: No / No
Views / Downloads: 588 / 691
Version history: 4 change(s)
Referenced in: [show references]