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

140
LINES

< > BotCompany Repo | #1014823 // SnippetSelectForm - TODO: search in non-AWT thread?

JavaX fragment (include)

sclass SnippetSelectForm {
  VF1<S> 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<S> actionHistory = synchroList();
  
  *() {}
  *(VF1<S> *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<Snippet> 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));
      }
    }
  }
}

Author comment

Began life as a copy of #1004469

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1014823
Snippet name: SnippetSelectForm - TODO: search in non-AWT thread?
Eternal ID of this version: #1014823/39
Text MD5: b923963b05599f816a756cfb2c78fc8d
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-07-13 16:03:42
Source code size: 3854 bytes / 140 lines
Pitched / IR pitched: No / No
Views / Downloads: 452 / 1050
Version history: 38 change(s)
Referenced in: [show references]