Ejemplo n.º 1
0
  /** Create a new telnet plugin. */
  public Telnet(final PluginBus bus, String id) {
    super(bus, id);

    // create a new telnet protocol handler
    handler =
        new TelnetProtocolHandler() {
          /** get the current terminal type */
          @Override
          public String getTerminalType() {
            return (String) bus.broadcast(new TerminalTypeRequest());
          }

          /** get the current window size */
          @Override
          public Dimension getWindowSize() {
            return (Dimension) bus.broadcast(new WindowSizeRequest());
          }

          /** notify about EOR end of record */
          @Override
          public void notifyEndOfRecord() {
            bus.broadcast(new EndOfRecordRequest());
          }

          /** notify about local echo */
          @Override
          public void setLocalEcho(boolean echo) {
            bus.broadcast(new LocalEchoRequest(echo));
          }

          @Override
          public void setMCCPRequest(boolean mccp) {
            bus.broadcast(new MCCPRequest(mccp));
            log.debug("got MCCP request");
          }

          /** write data to our back end */
          @Override
          public void write(byte[] b) throws IOException {
            source.write(b);
          }
        };

    // reset the telnet protocol handler just in case :-)
    bus.registerPluginListener(
        new OnlineStatusListener() {
          @Override
          public void offline() {
            handler.reset();
            bus.broadcast(new LocalEchoRequest(true));
          }

          @Override
          public void online() {
            handler.reset();
            try {
              handler.startup();
            } catch (java.io.IOException e) {
            }

            bus.broadcast(new LocalEchoRequest(true));
          }
        });

    bus.registerPluginListener(
        new SetWindowSizeListener() {
          @Override
          public void setWindowSize(int columns, int rows) {
            try {
              handler.setWindowSize(columns, rows);
            } catch (java.io.IOException e) {
              log.error("IO Exception in set window size");
            }
          }
        });

    bus.registerPluginListener(
        new ConfigurationListener() {
          @Override
          public void setConfiguration(PluginConfig config) {
            configure(config);
          }
        });
    bus.registerPluginListener(
        new TelnetCommandListener() {
          @Override
          public void sendTelnetCommand(byte command) throws IOException {
            handler.sendTelnetControl(command);
          }
        });
  }
Ejemplo n.º 2
0
  /** Initialize the button bar and register plugin listeners */
  public ButtonBar(PluginBus bus, final String id) {
    super(bus, id);

    // configure the button bar
    bus.registerPluginListener(
        new ConfigurationListener() {
          public void setConfiguration(PluginConfig cfg) {
            String file = cfg.getProperty("ButtonBar", id, "setup");
            clearFields =
                (new Boolean(cfg.getProperty("ButtonBar", id, "clearFields"))).booleanValue();

            // check for the setup file
            if (file == null) {
              ButtonBar.this.error("no setup file");
              return;
            }

            StreamTokenizer setup = null;
            InputStream is = null;

            try {
              is = getClass().getResourceAsStream(file);
            } catch (Exception e) {
              // ignore any errors here
            }

            // if the resource access fails, try URL
            if (is == null)
              try {
                is = new URL(file).openStream();
              } catch (Exception ue) {
                ButtonBar.this.error("could not find: " + file);
                return;
              }

            // create a new stream tokenizer to read the file
            try {
              InputStreamReader ir = new InputStreamReader(is);
              setup = new StreamTokenizer(new BufferedReader(ir));
            } catch (Exception e) {
              ButtonBar.this.error("cannot load " + file + ": " + e);
              return;
            }

            setup.commentChar('#');
            setup.quoteChar('"');

            fields = new HashMap();
            buttons = new HashMap();
            choices = new HashMap();

            GridBagLayout l = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
            panel.setLayout(l);
            c.fill = GridBagConstraints.BOTH;

            int token;
            int ChoiceCount = 0;
            JList list;
            // parse the setup file
            try {
              while ((token = setup.nextToken()) != StreamTokenizer.TT_EOF) {
                switch (token) {
                  case StreamTokenizer.TT_WORD:
                    // reset the constraints
                    c.gridwidth = 1;
                    c.weightx = 0.0;
                    c.weighty = 0.0;
                    // keyword found, parse arguments
                    if (setup.sval.equals("button")) {
                      if ((token = setup.nextToken()) != StreamTokenizer.TT_EOF) {
                        String descr = setup.sval;
                        if ((token = setup.nextToken()) != StreamTokenizer.TT_EOF) {
                          JButton b = new JButton(descr);
                          buttons.put(b, setup.sval);
                          b.addActionListener(ButtonBar.this);
                          l.setConstraints(b, constraints(c, setup));
                          panel.add(b);
                        } else ButtonBar.this.error(descr + ": missing button command");
                      } else ButtonBar.this.error("unexpected end of file");
                    } else if (setup.sval.equals("label")) {
                      if ((token = setup.nextToken()) != StreamTokenizer.TT_EOF) {
                        String descr = setup.sval;
                        JLabel b = new JLabel(descr);
                        l.setConstraints(b, constraints(c, setup));
                        panel.add(b);
                      } else ButtonBar.this.error("unexpected end of file");
                      /* choice - new stuff added APS 07-dec-2001 for Choice
                       * buttons
                       * Choice info is held in the choices hash. There are two
                       * sorts of hash entry:
                       * 1) for each choices button on the terminal, key=choice
                       *    object, value=ID ("C1.", "C2.", etc)
                       * 2) for each item, key=ID+user's text (eg "C1.opt1"),
                       *    value=user's command
                       */

                    } else if (setup.sval.equals("choice")) {
                      ChoiceCount++;
                      String ident = "C" + ChoiceCount + ".";
                      list = new JList();
                      choices.put(list, ident);
                      list.addListSelectionListener(
                          ButtonBar.this); // Choices use ItemListener, not Action
                      l.setConstraints(list, constraints(c, setup));
                      panel.add(list);
                      while ((token = setup.nextToken()) != StreamTokenizer.TT_EOF) {
                        if (isKeyword(setup.sval)) { // Got command ... Back off.
                          setup.pushBack();
                          break;
                        }
                        String descr = setup.sval; // This is the hash key.
                        token = setup.nextToken();
                        if (token == StreamTokenizer.TT_EOF) {
                          ButtonBar.this.error("unexpected end of file");
                        } else {
                          String value = setup.sval;
                          if (isKeyword(value)) { // Missing command - complain but continue
                            System.err.println(descr + ": missing choice command");
                            setup.pushBack();
                            break;
                          }
                          System.out.println("choice: name='" + descr + "', value='" + value);
                          list.add(descr, new JLabel(descr));
                          choices.put(ident + descr, value);
                        }
                      }
                      ButtonBar.this.error("choices hash: " + choices);
                    } else if (setup.sval.equals("input")) {
                      if ((token = setup.nextToken()) != StreamTokenizer.TT_EOF) {
                        String descr = setup.sval;
                        if ((token = setup.nextToken()) == StreamTokenizer.TT_NUMBER) {
                          int size = (int) setup.nval;
                          String init = "", command = "";
                          token = setup.nextToken();
                          if (isKeyword(setup.sval)) setup.pushBack();
                          else command = setup.sval;
                          token = setup.nextToken();
                          if (isKeyword(setup.sval)) {
                            setup.pushBack();
                            init = command;
                          } else init = setup.sval;
                          JTextField t = new JTextField(init, size);
                          if (!init.equals(command)) {
                            buttons.put(t, command);
                            t.addActionListener(ButtonBar.this);
                          }
                          fields.put(descr, t);
                          l.setConstraints(t, constraints(c, setup));
                          panel.add(t);
                        } else ButtonBar.this.error(descr + ": missing field size");
                      } else ButtonBar.this.error("unexpected end of file");
                    }
                    break;
                  default:
                    ButtonBar.this.error("syntax error at line " + setup.lineno());
                }
              }
            } catch (IOException e) {
              ButtonBar.this.error("unexpected error while reading setup: " + e);
            }
            panel.validate();
          }
        });
  }