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

111
LINES

< > BotCompany Repo | #1016198 // Snippet Search [Dyn Module, dev.]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Libraryless. Compilation Failed (14779L/104K).

!7

sclass SnippetSearch extends DynModule {
  transient JList list;
  transient JTextField tfCmd;
  transient JLabel status;
  transient JButton btnReload;
  transient S submittedInput;
  transient volatile boolean searching;

  int searchDelay = 100;
  int maxResults = 100;
  int autoResearchInterval = 30000; // 30 secs
  
  *() {}
  *(VF1<S> *onSelected) {}
  
  JComponent visualize() {
    list = new JList;
    //onDoubleClickAndEnter(list, r { ok() });
  
    ActionListener go = actionListener {
      tfCmd.selectAll();
      search(true);
    };
   
    btnReload = new JButton(isWindows() ? "Reload" : "\u27F3");
    btnReload.addActionListener(go);
  
    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);
    
    frame.addWindowListener(new WindowAdapter {
      public void windowOpened(WindowEvent e) {
        tfCmd.requestFocus();
      }
      
      public void windowActivated(WindowEvent e) {
        search(false);
      }
    }); 
    
    ownTimer(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));
   
    search(false);
    ret panel;
  }
  
  S getInput() {
    ret tfCmd.getText().trim();
  }
  
  void search(S cmd) {
    searching = true;
    try {
      status("Searching " + quote(cmd) + "...");
      if (empty(cmd)) cmd = "_";
      if (nempty(forceType)) cmd += " type:" + forceType;

      L<Snippet> l = tbSearch_url("http://tinybrain.de:8080/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);

      swing {
        list.setModel(model);
        if (model.size() != 0) list.setSelectedIndex(0);
        status("Found " + model.size() + (model.size() == maxResults ? "+" : "") + " snippet(s).");
      }
    } finally {
      searching = false;
    }
  }
  
  void status(final S s) {
    setText(status, s);
  }
  
  void search() {
    submittedInput = getInput();
    lastSearch = now();
    final String cmd = submittedInput;
    thread { search(cmd); }  
  }
}

Author comment

Began life as a copy of #1014823

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: #1016198
Snippet name: Snippet Search [Dyn Module, dev.]
Eternal ID of this version: #1016198/1
Text MD5: e65e18f482d78c658143c8ee4efef2f5
Transpilation MD5: 457020ef9b6d4476e21e241ef18f1ed4
Author: stefan
Category: javax / gui
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-06-18 16:20:15
Source code size: 2900 bytes / 111 lines
Pitched / IR pitched: No / No
Views / Downloads: 345 / 425
Referenced in: