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

130
LINES

< > BotCompany Repo | #1000985 // swing-top - List running programs & bots (old)

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

Libraryless. Click here for Pure Java version (1907L/14K/46K).

!747
!actionListener {
!x619 // standard classes (LetterLayout, CenteredLine)
!multi-line strings
!1000805 // awt {

main {
  static JList list;
  static JTextField tfCmd; // unused
  static JLabel status;
  static JButton btnScan;
  static S ip;
  static int fromPort = 10000, toPort = 10999;
  
  !include #1000804 // ProgramScan

  static S menuSnippet = "#1000910";
  static S menuScript;
  
  !include #1000839 // PopupMenuHelper
  
  psvm {
    if (args.length != 0) ip = args[0];
    
    S title = "Running programs and bots";
    if (ip != null) title += " on " + ip;
    installHelloMessage(title);
    JFrame frame = new JFrame(title);
    
    JPanel panel = new JPanel(new BorderLayout());
    
    list = new JList();
    panel.add(BorderLayout.CENTER, new JScrollPane(list));
    
    new PopupMenuHelper() {
      void fillMenu() {
        try {
          if (menuScript == null)
            menuScript = loadSnippet(menuSnippet);
            
          JMenuItem mi;
          int idx = list.locationToIndex(point);
          final String item = cast list.getModel().getElementAt(idx);
          list.setSelectedIndex(idx);
          
          L<S> tok = javaTok(menuScript);
          for (int i = 1; i+4 < tok.size(); i += 2)
            if (tok.get(i+2).equals("=")) {
              final S snip = unquote(tok.get(i+4));
              S text = unquote(tok.get(i));
              mi = new JMenuItem(text);
              mi.addActionListener(actionListener {
                runCustom(snip, item);
              });
              menu.add(mi);
            }
        } catch (Throwable e) {
          popup(e);
        }
          
      }
    }.install(list);    
    
    tfCmd = new JTextField();
    //panel.add(BorderLayout.SOUTH, tfCmd)
    
    status = new JLabel(" ");
    btnScan = new JButton("Scan again");
    btnScan.addActionListener(actionListener { scan(); });
    JPanel bottom = new JPanel(new BorderLayout());
    bottom.add(BorderLayout.CENTER, status);
    bottom.add(BorderLayout.EAST, btnScan);
    panel.add(BorderLayout.SOUTH, bottom);
    
    /*tfCmd.addActionListener(actionListener {
      tfCmd.selectAll();
      String cmd = tfCmd.getText();
      runCmd(cmd);
    });*/
    
    /*frame.addWindowListener(new WindowAdapter() {
      public void windowOpened(WindowEvent e) {
        tfCmd.requestFocus();
      }
    });*/
  
    frame.add(panel);
    frame.setBounds(100, 100, 500, 400);
    frame.setVisible(true);
    exitOnFrameClose(frame);

    scan();
  }
  
  static void scan() {
    thread {
      status("Scanning " + (ip != null ? ip + " " : "") + " for programs...");
      btnScan.setEnabled(false);
      try {
        if (ip != null) ProgramScan.ip = ip;
        final List<ProgramScan.Program> programs = ProgramScan.scan(fromPort, toPort);
        programs.addAll(ProgramScan.quickBotScan());
        status("Done. " + programs.size() + " programs or bots found.");
        
        awt {
          new DefaultListModel<S> model;
          for (ProgramScan.Program p : programs)
            model.addElement("Port " + p.port + ": " + p.helloString);
          list.setModel(model);
        }
      } catch (Throwable e) {
        e.printStackTrace();
        status("Scan error: " + e);
      }
      btnScan.setEnabled(true);
   }
  }
  
  static void status(final S s) {
    awt { status.setText(s); }
  }
  
  static void runCustom(S worker, S item) {
    item = item.replaceAll("^[^0-9]*", "");
    int i = item.indexOf(":");
    nohupJavax(worker + " " + (ip == null ? "" : ip + " ") + item.substring(0, i) + " by the name of " + quote(item.substring(i+1).trim()));
  }
  
  !include #1001139 // quickBotScan included for technical reasons
}

Author comment

Began life as a copy of #751

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, dhtvkmknsjym, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
1137 stefan Use #1001437 instead 2015-10-22 21:12:31

add comment

Snippet ID: #1000985
Snippet name: swing-top - List running programs & bots (old)
Eternal ID of this version: #1000985/1
Text MD5: bae0ffb905d7524a42d4242961b6e2d2
Transpilation MD5: e8361c3454405886bc1cfec0f2c2371f
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-22 21:12:46
Source code size: 3845 bytes / 130 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1004 / 1269
Referenced in: [show references]