コード例 #1
0
ファイル: GuiUtility.java プロジェクト: dschachinger/colibri
  /**
   * This method is used to display the window to interact with obix as well as colibri.
   *
   * @param chosenComponents The {@link ObixObject} which have been chosen in the previous windows.
   * @return The container which contains all elemtents that are used for interacting with obix as
   *     well as colibri.
   */
  private Container interactionWindow(List<ObixObject> chosenComponents) {
    Container pane = new Container();
    pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = 10;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(30, 20, 0, 0);

    c.gridy++;
    pane.add(registeredColibriChannelCheckBox, c);

    JLabel label = new JLabel("OBIX Components");
    Font headerF = new Font("Courier", Font.BOLD, 25);
    label.setFont(headerF);
    c.gridy++;
    pane.add(label, c);

    /*
       Print lobby Data
    */
    for (ObixObject o : chosenComponents) {
      if (connector.getColibriChannel().getRegistered()) {
        connector.getColibriChannel().send(ColibriMessage.createAddServiceMessage(o));
      }

      c.gridy++;
      c.insets = new Insets(30, 10, 0, 0);

      JLabel uriLabel = new JLabel(o.getObixUri());
      uriLabel.setFont(new Font("Courier", Font.ITALIC, 15));
      c.gridx = 0;
      c.gridwidth = 10;
      pane.add(uriLabel, c);

      c.gridwidth = 1;
      c.insets = new Insets(5, 10, 0, 0);

      final JTextField textField = new JTextField("NOT OBSERVED", 20);
      Font tempF = new Font("Courier", Font.PLAIN, 15);
      textField.setFont(tempF);
      c.gridy++;
      pane.add(textField, c);

      JLabel unitLabel = new JLabel();
      if (o.hasUnit()) {
        String unitString = o.getUnit().symbol().get();
        int unitCode = unitString.codePointAt(0);
        if (unitCode == 65533) {
          unitString = "\u2103";
        }
        unitLabel.setText(unitString);
      }
      c.gridx++;
      pane.add(unitLabel, c);

      final JButton getObixButton = new JButton("GET from OBIX");
      c.gridx++;
      pane.add(getObixButton, c);

      final JButton getColibriButton = new JButton("GET from Colibri");
      c.gridx++;
      pane.add(getColibriButton, c);

      final JCheckBox writableCheckBox = new JCheckBox("Writable");
      writableCheckBox.setSelected(o.getObj().isWritable());
      writableCheckBox.setEnabled(false);
      c.gridx++;
      pane.add(writableCheckBox, c);

      final JCheckBox observeObixCheckBox = new JCheckBox("observe Obix Data");
      observeObixCheckBox.setMargin(new Insets(0, 20, 0, 20));
      c.gridx++;
      pane.add(observeObixCheckBox, c);

      final JCheckBox observedByColibriCheckBox = new JCheckBox("Colibri observes Data");
      observedByColibriCheckBox.setEnabled(false);
      commandFactory.addCommand(
          () -> observedByColibriCheckBox.setSelected(o.getObservedByColibri()));
      c.gridx++;
      pane.add(observedByColibriCheckBox, c);

      final JCheckBox addServiceCheckbox = new JCheckBox("Service Added to Colibri");
      commandFactory.addCommand(() -> addServiceCheckbox.setSelected(o.getAddedAsService()));
      commandFactory.addCommand(
          () -> addServiceCheckbox.setEnabled(connector.getColibriChannel().getRegistered()));
      c.gridx++;
      pane.add(addServiceCheckbox, c);

      final JCheckBox observeColibriActionsCheckbox = new JCheckBox("Observe Colibri Actions");
      if (o.getObj().isWritable()) {
        commandFactory.addCommand(
            () -> observeColibriActionsCheckbox.setEnabled(o.getAddedAsService()));
        commandFactory.addCommand(
            () -> observeColibriActionsCheckbox.setSelected(o.getObservesColibriActions()));
      } else {
        observeColibriActionsCheckbox.setEnabled(false);
      }
      c.gridx++;
      pane.add(observeColibriActionsCheckbox, c);

      representationRows.add(
          new RepresentationRow(
              uriLabel,
              observeObixCheckBox,
              textField,
              o,
              writableCheckBox,
              getObixButton,
              getColibriButton,
              addServiceCheckbox,
              observedByColibriCheckBox,
              observeColibriActionsCheckbox));
      PutToObixTask putToObixTask =
          new PutToObixTask(o, connector.getColibriChannel(), connector.getObixChannel(), null);
      connector.getColibriChannel().addPutToObixTask(o.getServiceUri(), putToObixTask);
      ObixObservationUpdates observationUpdates =
          new ObixObservationUpdates(observeObixCheckBox, textField, o, connector);

      /**
       * Listener for the checkbox which indicates, if an {@link ObixObject] is observed by the obix connector.
       */
      observeObixCheckBox.addItemListener(
          new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
              ObixObject object = new ObixObject("", o.getObixChannelPort());
              for (RepresentationRow r : GuiUtility.this.getRepresentationRows()) {
                if (r.getObservedCheckBox().equals(observeObixCheckBox)) {
                  object = r.getObixObject();
                }
              }
              if (e.getStateChange() == ItemEvent.SELECTED) {
                obixChannel.observe(object);
                commandFactory.addCommand(
                    object.getObixUri() + "ObserveCommand", observationUpdates::run);
              } else {
                commandFactory.removeCommand(object.getObixUri() + "ObserveCommand");
                object.getRelation().proactiveCancel();
              }
            }
          });

      /**
       * Listener for the checkbox which indicates, if an {@link ObixObject] is writeable.
       */
      writableCheckBox.addItemListener(
          new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
              ObixObject object = new ObixObject("", o.getObixChannelPort());
              for (RepresentationRow r : GuiUtility.this.getRepresentationRows()) {
                if (r.getWritableCheckbox().equals(writableCheckBox)) {
                  object = r.getObixObject();
                }
              }

              if (e.getStateChange() == ItemEvent.SELECTED) {

              } else {
                object.getObj().setWritable(false);
              }
              object = obixChannel.put(object);
              writableCheckBox.setSelected(object.getObj().isWritable());
            }
          });

      /**
       * Listener for the checkbox which indicates, if an {@link ObixObject] is added as a service at colibri.
       */
      addServiceCheckbox.addItemListener(
          new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
              ObixObject object = new ObixObject("", o.getObixChannelPort());
              for (RepresentationRow r : GuiUtility.this.getRepresentationRows()) {
                if (r.getAddedAsServiceCheckBox().equals(addServiceCheckbox)) {
                  object = r.getObixObject();
                }
              }
              if (e.getStateChange() == ItemEvent.SELECTED) {
                if (!object.getAddedAsService()) {
                  connector
                      .getColibriChannel()
                      .send(ColibriMessage.createAddServiceMessage(object));
                }
              } else {
                if (object.getAddedAsService()) {
                  connector
                      .getColibriChannel()
                      .send(ColibriMessage.createRemoveServiceMessage(object));
                }
              }
            }
          });

      /**
       * Listener for the checkbox which indicates, if the connector observes the actions that the colibri
       * semantic core performs on an {@link ObixObject].
       */
      observeColibriActionsCheckbox.addItemListener(
          new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
              ObixObject object = new ObixObject("", o.getObixChannelPort());
              for (RepresentationRow r : GuiUtility.this.getRepresentationRows()) {
                if (r.getObserveColibriActionsCheckbox().equals(observeColibriActionsCheckbox)) {
                  object = r.getObixObject();
                }
              }
              if (e.getStateChange() == ItemEvent.SELECTED) {
                if (!object.getObservesColibriActions()) {
                  connector
                      .getColibriChannel()
                      .send(ColibriMessage.createObserveServiceMessage(object));
                }
              } else {
                if (object.getObservesColibriActions()) {
                  connector
                      .getColibriChannel()
                      .send(ColibriMessage.createDetachServiceMessage(object));
                }
              }
            }
          });

      /** GET Obix button listener */
      Action getObixAction =
          new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
              ObixObject object = new ObixObject("", o.getObixChannelPort());
              JTextField textF = null;
              for (RepresentationRow r : GuiUtility.this.getRepresentationRows()) {
                if (r.getGetObixButton().equals(getObixButton)) {
                  object = r.getObixObject();
                  textF = r.getValueTextField();
                }
              }
              textField.setText("");
              object = obixChannel.get(object.getObixUri());
              textF.setText(object.toString());
              if (o.getObservedByColibri()) {
                o.getPutMessageToColibriTask().execute(o);
              }
            }
          };
      getObixButton.addActionListener(getObixAction);

      /** GET Colibri button listener */
      Action getColibriAction =
          new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
              ObixObject object = new ObixObject("", o.getObixChannelPort());
              JTextField textF = null;
              for (RepresentationRow r : GuiUtility.this.getRepresentationRows()) {
                if (r.getGetColibriButton().equals(getColibriButton)) {
                  object = r.getObixObject();
                  textF = r.getValueTextField();
                }
              }
              connector.getColibriChannel().send(ColibriMessage.createGetMessage(object));
            }
          };
      getColibriButton.addActionListener(getColibriAction);

      /**
       * Listener for the textfield connected with an {@link ObixObject}, to send PUT messages to
       * obix on <Enter>.
       */
      textField.addKeyListener(
          new KeyListener() {
            public void keyTyped(KeyEvent e) {
              // intentionally empty
            }

            public void keyPressed(KeyEvent e) {
              // intentionally empty
            }

            public void keyReleased(KeyEvent e) {
              if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                ObixObject object = new ObixObject("", o.getObixChannelPort());
                for (RepresentationRow r : GuiUtility.this.getRepresentationRows()) {
                  if (r.getValueTextField().equals(textField)) {
                    object = r.getObixObject();
                  }
                }
                object.setValueParameter1(textField.getText());
                textField.setText("");
                object = obixChannel.put(object);
                textField.setText(object.toString());
              }
            }
          });
    }

    JTextArea receivedMessagesTextArea = new JTextArea("Received Messages");
    receivedMessagesTextArea.setLineWrap(true);
    receivedMessagesTextArea.setWrapStyleWord(true);
    c.gridy++;
    c.gridx = 0;
    c.insets = new Insets(50, 0, 0, 0);
    c.gridwidth = 10;
    pane.add(receivedMessagesTextArea, c);
    receivedMessagesTextArea.setEnabled(false);
    commandFactory.addCommand(
        () ->
            receivedMessagesTextArea.setText(
                connector.getColibriChannel().getLastMessageReceived()));

    c.gridy++;
    c.gridwidth = 1;

    JLabel sendMessageLabel = new JLabel("Send Message to Colibri Semantic Core:");
    pane.add(sendMessageLabel, c);

    c.gridy++;
    c.gridwidth = 10;
    JTextArea sendMessageArea = new JTextArea("");
    pane.add(sendMessageArea, c);

    c.gridy++;
    c.gridwidth = 2;
    JButton sendQueMessageButton = new JButton("Send Query Message");
    pane.add(sendQueMessageButton, c);
    c.gridx++;
    c.gridx++;
    c.gridwidth = 2;
    JButton sendUpdMessageButton = new JButton("Send Update Message");
    pane.add(sendUpdMessageButton, c);

    commandFactory.addCommand(
        () -> sendQueMessageButton.setEnabled(connector.getColibriChannel().getRegistered()));
    commandFactory.addCommand(
        () -> sendUpdMessageButton.setEnabled(connector.getColibriChannel().getRegistered()));

    /** QUE to Colibri button listener */
    Action sendQueMessageAction =
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            connector
                .getColibriChannel()
                .send(
                    ColibriMessage.createQueryMessage(
                        sendMessageArea.getText(), new ArrayList<>()));
          }
        };
    sendQueMessageButton.addActionListener(sendQueMessageAction);

    /** UPD to Colibri button listener */
    Action sendUpdMessageAction =
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            connector
                .getColibriChannel()
                .send(ColibriMessage.createUpdateMessage(sendMessageArea.getText()));
          }
        };
    sendUpdMessageButton.addActionListener(sendUpdMessageAction);
    return pane;
  }
コード例 #2
0
ファイル: GuiUtility.java プロジェクト: dschachinger/colibri
  /**
   * This method represents the window in which the preferred obix components can be chosen.
   *
   * @return The container in which the preferred obix components can be chosen.
   */
  private Container chooseComponents() {
    Container pane = new Container();
    pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = 1;
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 1;
    c.weighty = 1;
    c.insets = new Insets(5, 5, 5, 5);

    registeredColibriChannelCheckBox = new JCheckBox("IS REGISTERD ON COLIBRI SEMANTIC CORE");
    commandFactory.addCommand(
        () ->
            registeredColibriChannelCheckBox.setSelected(
                connector.getColibriChannel().getRegistered()));
    Font regF = new Font("Courier", Font.BOLD, 40);
    registeredColibriChannelCheckBox.setFont(regF);

    /**
     * Listener for the checkbox which indicates, if the obix connector is registered at colibri.
     */
    registeredColibriChannelCheckBox.addItemListener(
        new ItemListener() {
          public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
              if (!connector.getColibriChannel().getRegistered()) {
                connector.getColibriChannel().send(ColibriMessage.createRegisterMessage(connector));
              }
            } else {
              if (connector.getColibriChannel().getRegistered()) {
                connector
                    .getColibriChannel()
                    .send(ColibriMessage.createDeregisterMessage(connector));
              }
            }
          }
        });

    pane.add(registeredColibriChannelCheckBox, c);

    Font titelF = new Font("Courier", Font.BOLD, 30);
    title = new JLabel("Please choose the components you want to work with");
    title.setFont(titelF);

    c.gridy++;
    pane.add(title, c);

    JTextField searchTextField = new JTextField("Search for a component");
    c.weightx = 1;
    c.weighty = 1;
    c.gridx++;
    pane.add(searchTextField, c);

    c.weightx = 0.25;
    c.weighty = 0.25;
    c.gridwidth = 1;
    c.gridx++;
    JButton searchButton = new JButton("Search");
    pane.add(searchButton, c);

    JCheckBox markAllCheckbox = new JCheckBox("Mark all components");
    c.gridy++;
    c.gridy++;
    pane.add(markAllCheckbox, c);

    for (String s : lobby.getObservedObjectsLists().keySet()) {
      if (!s.equals("all")) {
        List<ObixObject> objects = lobby.getObservedObjectsLists().get(s);
        JLabel header = new JLabel(s);
        Font headerF = new Font("Courier", Font.BOLD, 25);
        header.setFont(headerF);
        c.gridx = 0;
        c.gridy++;
        pane.add(header, c);
        for (ObixObject o : objects) {
          JCheckBox chooseCheckBox = new JCheckBox(o.getObixUri());
          JPanel innerPanel = new JPanel();
          innerPanel.setLayout(new FlowLayout(0, 0, 0));
          innerPanel.add(chooseCheckBox);
          c.gridx = 0;
          c.gridwidth = 10;
          c.gridy++;
          pane.add(innerPanel, c);
          representationRows.add(new RepresentationRow(o, chooseCheckBox));
        }
      }
    }
    JButton acceptButton = new JButton("Accept");
    c.gridx = 0;
    c.gridy++;
    pane.add(acceptButton, c);

    /** Accept button listener */
    Action acceptAction =
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            List<ObixObject> chosenObjects = Collections.synchronizedList(new ArrayList<>());
            ;
            for (RepresentationRow r : GuiUtility.this.getRepresentationRows()) {
              if (r.getChooseCheckbox().isSelected()) {
                chosenObjects.add(r.getObixObject());
              }
            }
            representationRows.clear();
            cards.removeAll();
            JScrollPane scrollPane = new JScrollPane(chooseParameters(chosenObjects));
            scrollPane.getVerticalScrollBar().setUnitIncrement(16);
            scrollPane.setBorder(new EmptyBorder(20, 20, 20, 20));
            cards.add(scrollPane);
            // Display the window.
            mainFrame.pack();
          }
        };
    acceptButton.addActionListener(acceptAction);

    /** Search function listener */
    Action searchAction =
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            String searchText = searchTextField.getText();
            for (RepresentationRow r : GuiUtility.this.getRepresentationRows()) {
              if (r.getChooseCheckbox().getText().contains(searchText)) {
                r.getChooseCheckbox().setForeground(Color.blue);
              } else {
                r.getChooseCheckbox().setForeground(Color.black);
              }
              r.getChooseCheckbox().revalidate();
              r.getChooseCheckbox().repaint();
            }
          }
        };
    searchTextField.addActionListener(searchAction);
    searchButton.addActionListener(searchAction);

    /** Mark all components function listener */
    Action markAllAction =
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            for (RepresentationRow r : GuiUtility.this.getRepresentationRows()) {
              r.getChooseCheckbox().setSelected(true);
            }
          }
        };
    markAllCheckbox.addActionListener(markAllAction);

    return pane;
  }