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

129
LINES

< > BotCompany Repo | #1004472 // Snippet Search With Table [dev.]

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

Libraryless. Click here for Pure Java version (8141L/58K/209K).

1  
!7
2  
3  
static JFrame frame;
4  
static JTable table;
5  
static JTextField tfCmd;
6  
static JLabel status;
7  
static JButton btnReload, btnClear;
8  
static S submittedInput;
9  
static volatile boolean searching;
10  
static long lastSearch; // time of last search
11  
12  
static int searchDelay = 100;
13  
static int maxResults = 100;
14  
static int autoResearchInterval = 30000; // 30 secs
15  
16  
static L<S> actionHistory = synchroList();
17  
18  
p {
19  
  frame = showFrame();
20  
  table = sexyTable();
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  
  btnClear = new JButton("X");
31  
  btnClear.setToolTipText("Show latest");
32  
  btnClear.addActionListener(actionListener { tfCmd.setText(""); });
33  
34  
  tfCmd = new JTextField;
35  
  
36  
  JPanel controls = jflow(btnClear, btnReload);
37  
38  
  JPanel north = centerAndEast(
39  
    withLabel("Search term:", tfCmd),
40  
    controls);
41  
  
42  
  /*btnRun = new JButton("Run");
43  
  btnRun.addActionListener(actionListener {
44  
    run();
45  
  });*/
46  
  
47  
  status = new JLabel(" ");
48  
  JPanel panel = northCenterAndSouth(north, table, status);
49  
50  
  tfCmd.addActionListener(go);
51  
  
52  
  frame.addWindowListener(new WindowAdapter {
53  
    public void windowOpened(WindowEvent e) {
54  
      tfCmd.requestFocus();
55  
    }
56  
    
57  
    public void windowActivated(WindowEvent e) {
58  
      search(false);
59  
    }
60  
  }); 
61  
  
62  
  installTimer(tfCmd, new Runnable() {
63  
    String lastContents;
64  
    boolean autoSearchOn = true;
65  
    
66  
    public void run() {
67  
      String text = getInput();
68  
      if (text.equals(lastContents)) {
69  
        if (now() > lastSearch + autoResearchInterval ||
70  
          (!text.equals(submittedInput) && autoSearchOn && !searching))
71  
          search(false);
72  
      } else
73  
        lastContents = text;
74  
    }
75  
  }, searchDelay);
76  
77  
  frame.add(panel);
78  
  frame.setBounds(100, 100, 500, 400);
79  
80  
  search(false);
81  
}
82  
83  
static S getInput() {
84  
  ret tfCmd.getText().trim();
85  
}
86  
87  
static void search(String cmd, final boolean requestFocus) ctex {
88  
  searching = true;
89  
  try {
90  
    status("Searching " + quote(cmd) + "...");
91  
    if (empty(cmd)) cmd = "_";
92  
    //if (cbAll.isSelected())
93  
      //cmd += " type:runnable";
94  
    S page = loadPage("http://tinybrain.de:8080/tb/search.php?q=" + urlencode(cmd) + "&limit=" + maxResults + "&sort=modified");
95  
    Matcher m = Pattern.compile(">(#\\d+)</a> - (.*?)<br>").matcher(page);
96  
    
97  
    final new L<Map> data;
98  
    while (m.find()) {
99  
      String title = m.group(2);
100  
      title = join(dropAllTags(htmlcoarsetok(title)));
101  
      data.add(litorderedmap("ID - Name", m.group(1) + htmldecode(title),
102  
        "Type", "?"));
103  
    }
104  
      
105  
    dataToTable_uneditable(table, data);
106  
    
107  
    awt {
108  
      status("Found " + l(data) + (l(data) == maxResults ? "+" : "") + " snippet(s).");
109  
      if (requestFocus)
110  
        tfCmd.requestFocus();
111  
    }
112  
  } finally {
113  
    searching = false;
114  
  }
115  
}
116  
117  
static void status(final S s) {
118  
  awt { status.setText(s); }
119  
}
120  
121  
static void search(final boolean requestFocus) {
122  
  submittedInput = getInput();
123  
  lastSearch = now();
124  
  final String cmd = submittedInput;
125  
  actionHistory.add(format3("*: Searching *", now(), cmd));
126  
  thread {
127  
    search(cmd, requestFocus);
128  
  }  
129  
}

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, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004472
Snippet name: Snippet Search With Table [dev.]
Eternal ID of this version: #1004472/3
Text MD5: 17e30a8bce70b3f497aa2c61535a0e8d
Transpilation MD5: 78ee291284d6fa160162499ca77ba662
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:48:13
Source code size: 3313 bytes / 129 lines
Pitched / IR pitched: No / No
Views / Downloads: 558 / 649
Version history: 2 change(s)
Referenced in: [show references]