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).

1  
!7
2  
3  
sclass SnippetSearch extends DynModule {
4  
  transient JList list;
5  
  transient JTextField tfCmd;
6  
  transient JLabel status;
7  
  transient JButton btnReload;
8  
  transient S submittedInput;
9  
  transient volatile boolean searching;
10  
11  
  int searchDelay = 100;
12  
  int maxResults = 100;
13  
  int autoResearchInterval = 30000; // 30 secs
14  
  
15  
  *() {}
16  
  *(VF1<S> *onSelected) {}
17  
  
18  
  JComponent visualize() {
19  
    list = new JList;
20  
    //onDoubleClickAndEnter(list, r { ok() });
21  
  
22  
    ActionListener go = actionListener {
23  
      tfCmd.selectAll();
24  
      search(true);
25  
    };
26  
   
27  
    btnReload = new JButton(isWindows() ? "Reload" : "\u27F3");
28  
    btnReload.addActionListener(go);
29  
  
30  
    tfCmd = new JTextField;
31  
    
32  
    btnOK = listDependentButton(list, "OK", r { ok(); });
33  
    
34  
    JPanel controls = jflow(btnClear, btnReload, btnOK);
35  
  
36  
    JPanel north = centerAndEast(
37  
      withLabel("Search term:", tfCmd),
38  
      controls);
39  
    
40  
    status = new JLabel(" ");
41  
    JPanel panel = northCenterAndSouth(north, list, status);
42  
  
43  
    onEnter(tfCmd, r ok);
44  
    
45  
    frame.addWindowListener(new WindowAdapter {
46  
      public void windowOpened(WindowEvent e) {
47  
        tfCmd.requestFocus();
48  
      }
49  
      
50  
      public void windowActivated(WindowEvent e) {
51  
        search(false);
52  
      }
53  
    }); 
54  
    
55  
    ownTimer(installTimer(tfCmd, new Runnable {
56  
      String lastContents;
57  
      boolean autoSearchOn = true;
58  
      
59  
      public void run() {
60  
        String text = getInput();
61  
        if (text.equals(lastContents)) {
62  
          if (now() > lastSearch + autoResearchInterval ||
63  
            (!text.equals(submittedInput) && autoSearchOn && !searching))
64  
            search(false);
65  
        } else
66  
          lastContents = text;
67  
      }
68  
    }, searchDelay));
69  
   
70  
    search(false);
71  
    ret panel;
72  
  }
73  
  
74  
  S getInput() {
75  
    ret tfCmd.getText().trim();
76  
  }
77  
  
78  
  void search(S cmd) {
79  
    searching = true;
80  
    try {
81  
      status("Searching " + quote(cmd) + "...");
82  
      if (empty(cmd)) cmd = "_";
83  
      if (nempty(forceType)) cmd += " type:" + forceType;
84  
85  
      L<Snippet> l = tbSearch_url("http://tinybrain.de:8080/tb/search.php?q=" + urlencode(cmd) + "&limit=" + maxResults + "&sort=modified" + standardCredentials());
86  
      
87  
      final new DefaultListModel model;
88  
      for (Snippet s : l)
89  
        model.addElement(s.id + " - " + s.title);
90  
91  
      swing {
92  
        list.setModel(model);
93  
        if (model.size() != 0) list.setSelectedIndex(0);
94  
        status("Found " + model.size() + (model.size() == maxResults ? "+" : "") + " snippet(s).");
95  
      }
96  
    } finally {
97  
      searching = false;
98  
    }
99  
  }
100  
  
101  
  void status(final S s) {
102  
    setText(status, s);
103  
  }
104  
  
105  
  void search() {
106  
    submittedInput = getInput();
107  
    lastSearch = now();
108  
    final String cmd = submittedInput;
109  
    thread { search(cmd); }  
110  
  }
111  
}

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: 344 / 425
Referenced in: [show references]