예제 #1
0
  private void createLWTGroup(Composite container) {
    // data model
    final LWT lwt = connection.getOptions().getLwt();

    final Group group = new Group(container, SWT.NONE);
    group.setText(Messages.OPT_TAB_GROUP_LWT);

    // layout
    FormLayout layout = new FormLayout();
    layout.marginBottom = 10;
    group.setLayout(layout);
    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
    group.setLayoutData(gd);

    // **********************************************
    // Row: Topic + QoS + Retain
    // **********************************************
    // Topic - label
    Label topicLabel = new Label(group, SWT.NONE);
    FormData fd = new FormData();
    fd.top = new FormAttachment(0, 10);
    fd.left = new FormAttachment(0, 10);
    topicLabel.setLayoutData(fd);
    topicLabel.setText(Messages.OPT_TAB_GROUP_LWT_TOPIC);

    // Topic - input
    final Text topicText = new Text(group, SWT.BORDER);
    fd = new FormData();
    fd.top = new FormAttachment(topicLabel, 10, SWT.CENTER);
    fd.left = new FormAttachment(topicLabel, 40);
    fd.right = new FormAttachment(100, -4);
    topicText.setLayoutData(fd);

    // QoS - label
    Label qosLabel = new Label(group, SWT.NONE);
    fd = new FormData();
    fd.top = new FormAttachment(topicLabel, 10);
    fd.left = new FormAttachment(0, 10);
    qosLabel.setLayoutData(fd);
    qosLabel.setText(Messages.OPT_TAB_GROUP_LWT_QOS);

    // QoS - selection
    CCombo combo = new CCombo(group, SWT.BORDER);
    List<String> qosList = new ArrayList<String>();
    for (QoS qos : QoS.values()) {
      qosList.add(qos.getLabel());
    }
    combo.setItems(qosList.toArray(new String[0]));
    combo.setEditable(false);
    combo.addSelectionListener(
        new SelectionAdapter() {
          public void widgetSelected(final SelectionEvent e) {
            Display.getDefault()
                .asyncExec(
                    new Runnable() {
                      @Override
                      public void run() {
                        // Note: need manually set (because NOT using databinding for combo)
                        CCombo combo = (CCombo) e.widget;
                        lwt.setQos(QoS.valueOf(combo.getSelectionIndex()));
                      }
                    });
          }
        });

    // Note: need manually set (because NOT using databinding for combo)
    combo.select(lwt.getQos().getValue());
    fd = new FormData();
    fd.top = new FormAttachment(qosLabel, 0, SWT.CENTER);
    fd.left = new FormAttachment(topicText, 0, SWT.LEFT);
    combo.setLayoutData(fd);

    // Retained
    Button retained = new Button(group, SWT.CHECK);
    retained.setText(Messages.OPT_TAB_GROUP_LWT_RETAINED);
    fd = new FormData();
    fd.top = new FormAttachment(qosLabel, 0, SWT.CENTER);
    fd.left = new FormAttachment(combo, 10);
    retained.setLayoutData(fd);

    // Hex
    final Button hex = new Button(group, SWT.CHECK);
    hex.setText(Messages.OPT_TAB_GROUP_LWT_HEX);
    fd = new FormData();
    fd.top = new FormAttachment(qosLabel, 0, SWT.CENTER);
    fd.right = new FormAttachment(100, -4);
    hex.setLayoutData(fd);

    // **********************************************
    // Row: Message - label
    // **********************************************
    // Topic - label
    Label messageLabel = new Label(group, SWT.NONE);
    fd = new FormData();
    fd.top = new FormAttachment(topicLabel, 60);
    fd.left = new FormAttachment(0, 10);
    messageLabel.setLayoutData(fd);
    messageLabel.setText(Messages.OPT_TAB_GROUP_LWT_MSG);

    // Message - input
    final Text messageText = new Text(group, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
    fd = new FormData();
    fd.top = new FormAttachment(messageLabel, 10, SWT.CENTER);
    fd.left = new FormAttachment(topicText, 0, SWT.LEFT);
    fd.right = new FormAttachment(topicText, 0, SWT.RIGHT);
    // fd.width = 300;
    fd.height = 60;
    messageText.setLayoutData(fd);

    // XXX handle input when in HEX mode
    hex.addSelectionListener(
        new SelectionAdapter() {
          public void widgetSelected(final SelectionEvent e) {
            Display.getDefault()
                .asyncExec(
                    new Runnable() {
                      @Override
                      public void run() {
                        Button button = (Button) e.widget;
                        if (button.getSelection()) {
                          messageText.setText(Strings.toHex(messageText.getText()));
                          messageText.setEditable(false);
                        } else {
                          messageText.setText(Strings.hexToString(messageText.getText()));
                          messageText.setEditable(true);
                        }
                      }
                    });
          }
        });

    // **********************************************
    // DataBinding
    // **********************************************
    final DataBinding dataBinding = DataBindings.createDataBinding();
    dataBinding.bindTextAsBytes(messageText, connection, Connection.PROP_OPT_LWT_PAYLOAD);
    dataBinding.bindSelection(retained, connection, Connection.PROP_OPT_LWT_RETAIN);

    // LWT view updater
    lwtViewUpdater =
        new ViewUpdater<Boolean>() {
          final DecoratedBinding binding;

          {
            binding =
                dataBinding
                    .bindText(
                        topicText,
                        connection,
                        Connection.PROP_OPT_LWT_TOPIC,
                        Validators.publishTopic)
                    .hideDecorations();
          }

          @Override
          public void update(Boolean selected) {
            Widgets.enable(group, selected);
            if (selected) {
              binding.showDecorations();
            } else {
              binding.hideDecorations();
            }
          }
        };
    // dispose dataBinding when UI is disposed
    addDisposeListener(dataBinding);

    // initial state
    Widgets.enable(group, connection.getOptions().isLwtEnabled());
  }