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)

1  
sclass SnippetSelectForm {
2  
  VF1<S> onSelected;
3  
  S snippetTitle;
4  
  S forceType;
5  
  JDesktopPane desktop = mainDesktopPane();
6  
  
7  
  Container frame;
8  
  JList list;
9  
  JTextField tfCmd;
10  
  JLabel status;
11  
  JButton btnReload, btnRun, btnClear, btnOK;
12  
  S submittedInput;
13  
  volatile boolean searching;
14  
  long lastSearch; // time of last search
15  
  
16  
  int searchDelay = 100;
17  
  int maxResults = 100;
18  
  int autoResearchInterval = 30000; // 30 secs
19  
  
20  
  L<S> actionHistory = synchroList();
21  
  
22  
  *() {}
23  
  *(VF1<S> *onSelected) {}
24  
  
25  
  void go() swing {
26  
    frame = showFramePossiblyInternal(desktop, "Select Snippet - " + programTitle());
27  
    list = new JList;
28  
    onDoubleClickAndEnter(list, r { ok() });
29  
  
30  
    ActionListener go = actionListener {
31  
      tfCmd.selectAll();
32  
      search(true);
33  
    };
34  
   
35  
    btnReload = new JButton(isWindows() ? "Reload" : "\u27F3");
36  
    btnReload.addActionListener(go);
37  
  
38  
    btnClear = new JButton("X");
39  
    btnClear.setToolTipText("Show latest");
40  
    btnClear.addActionListener(actionListener { tfCmd.setText(""); });
41  
  
42  
    tfCmd = new JTextField;
43  
    
44  
    btnOK = listDependentButton(list, "OK", r { ok(); });
45  
    
46  
    JPanel controls = jflow(btnClear, btnReload, btnOK);
47  
  
48  
    JPanel north = centerAndEast(
49  
      withLabel("Search term:", tfCmd),
50  
      controls);
51  
    
52  
    status = new JLabel(" ");
53  
    JPanel panel = northCenterAndSouth(north, list, status);
54  
  
55  
    onEnter(tfCmd, r ok);
56  
    
57  
    if (frame instanceof JFrame) 
58  
      onWindowActivated((JFrame) frame, r { search(false) });
59  
60  
    installTimer(tfCmd, new Runnable {
61  
      String lastContents;
62  
      boolean autoSearchOn = true;
63  
      
64  
      public void run() {
65  
        String text = getInput();
66  
        if (text.equals(lastContents)) {
67  
          if (now() > lastSearch + autoResearchInterval ||
68  
            (!text.equals(submittedInput) && autoSearchOn && !searching))
69  
            search(false);
70  
        } else
71  
          lastContents = text;
72  
      }
73  
    }, searchDelay);
74  
  
75  
    frame.add(panel);
76  
    frame.setBounds(100, 100, 500, 400);
77  
  
78  
    search(false);
79  
    focusLater(tfCmd);
80  
  }
81  
  
82  
  S getInput() {
83  
    ret tfCmd.getText().trim();
84  
  }
85  
  
86  
  void search(String cmd, final boolean requestFocus) ctex {
87  
    searching = true;
88  
    try {
89  
      status("Searching " + quote(cmd) + "...");
90  
      if (empty(cmd)) cmd = "_";
91  
      if (nempty(forceType)) cmd += " type:" + forceType;
92  
      //if (cbAll.isSelected())
93  
        //cmd += " type:runnable";
94  
        
95  
      L<Snippet> l = tbSearch_url(tb_mainServer() + "/tb/search.php?q=" + urlencode(cmd) + "&limit=" + maxResults + "&sort=modified" + standardCredentials());
96  
      
97  
      final new DefaultListModel model;
98  
      for (Snippet s : l)
99  
        model.addElement(s.id + " - " + s.title);
100  
101  
      awt {
102  
        list.setModel(model);
103  
        if (model.size() != 0) list.setSelectedIndex(0);
104  
        status("Found " + model.size() + (model.size() == maxResults ? "+" : "") + " snippet(s).");
105  
        if (requestFocus)
106  
          tfCmd.requestFocus();
107  
      }
108  
    } finally {
109  
      searching = false;
110  
    }
111  
  }
112  
  
113  
  void status(final S s) {
114  
    awt { status.setText(s); }
115  
  }
116  
  
117  
  void search(final boolean requestFocus) {
118  
    submittedInput = getInput();
119  
    lastSearch = now();
120  
    final String cmd = submittedInput;
121  
    actionHistory.add(format3("*: Searching *", now(), cmd));
122  
    thread {
123  
      search(cmd, requestFocus);
124  
    }  
125  
  }
126  
  
127  
  void ok() {
128  
    fS s = getSelectedItem(list);
129  
    final int i = indexOf(s, " - ");
130  
    if (i < 0) ret;
131  
    snippetTitle = substring(s, i+3);
132  
    thread {
133  
      disposePossiblyInternalFrame(frame);
134  
      pcall-messagebox {
135  
        temp holdInstance(SnippetSelectForm.this);
136  
        callF(onSelected, takeFirst(s, i));
137  
      }
138  
    }
139  
  }
140  
}

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: 453 / 1051
Version history: 38 change(s)
Referenced in: [show references]