Esempio n. 1
0
  /**
   * This method represents the window in which the preferred parameters for the obix components can
   * be chosen.
   *
   * @param chosenComponents The list of {@link ObixObject} which have been chosen in the previous
   *     window.
   * @return The container in which the preferred parameters for the obix components can be chosen.
   */
  private Container chooseParameters(List<ObixObject> chosenComponents) {
    Container pane = new Container();
    pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    Font titelF = new Font("Courier", Font.BOLD, 30);
    title = new JLabel("Please choose the appropriate Parameters for the datapoints");
    title.setFont(titelF);

    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = 10;
    c.gridx = 0;
    c.gridy = 0;
    pane.add(title, c);

    List<String> parametersList = Configurator.getInstance().getAvailableParameterTypes();
    String[] parameterTypes = new String[parametersList.size()];
    parameterTypes = parametersList.toArray(parameterTypes);

    for (ObixObject o : chosenComponents) {
      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(10, 10, 0, 0);

      JLabel parameter1Label = new JLabel("Parameter 1 Type:");
      c.gridy++;
      pane.add(parameter1Label, c);

      JLabel parameter1ObixUnitLabel = new JLabel("OBIX unit: " + o.getObixUnitUri());
      c.gridx++;
      pane.add(parameter1ObixUnitLabel, c);

      JComboBox parameter1comboBox = new JComboBox(parameterTypes);
      c.gridx++;
      pane.add(parameter1comboBox, c);

      JButton param1AddStateButton = new JButton("Add State");
      Box vBox1 = Box.createVerticalBox();
      vBox1.setBorder(BorderFactory.createLineBorder(Color.black));

      JLabel parameter1UnitLabel = new JLabel("Set Parameter 1 Unit:");
      c.gridx++;
      pane.add(parameter1UnitLabel, c);

      JTextField parameter1UnitTextField = new JTextField(o.getParameter1().getParameterUnit());
      parameter1UnitTextField.setPreferredSize(new Dimension(500, 20));
      parameter1UnitTextField.setMinimumSize(new Dimension(500, 20));
      c.gridx++;
      pane.add(parameter1UnitTextField, c);

      JLabel parameter1ValueTypeLabel =
          new JLabel("valueType: " + o.getParameter1().getValueType());
      c.gridx++;
      pane.add(parameter1ValueTypeLabel, c);

      int param1UnitLabelxPosition = c.gridx;
      int param1UnitLabelyPosition = c.gridy;

      for (StateDescription s : o.getParameter1().getStateDescriptions()) {
        JLabel stateNameLabel = new JLabel("State Name: ");
        JTextField stateNameTextfield = new JTextField(20);
        stateNameTextfield.setText(s.getName().getName());
        vBox1.add(stateNameLabel);
        vBox1.add(stateNameTextfield);

        JLabel stateValueLabel = new JLabel("State Value: ");
        JTextField stateValueTextfield = new JTextField(20);
        stateValueTextfield.setText(s.getValue().getValue());
        vBox1.add(stateValueLabel);
        vBox1.add(stateValueTextfield);

        JLabel stateURILabel = new JLabel("State URI: ");
        JTextField stateURITextfield = new JTextField(20);
        stateURITextfield.setText(s.getStateDescriptionUri());
        vBox1.add(stateURILabel);
        vBox1.add(stateURITextfield);

        JButton deleteStateButton = new JButton("Delete State");
        vBox1.add(deleteStateButton);

        JSeparator horizontalLine = new JSeparator(JSeparator.HORIZONTAL);
        vBox1.add(horizontalLine);
        vBox1.add(horizontalLine);

        StateRepresentation stateRepresentation =
            (new StateRepresentation(
                param1AddStateButton,
                deleteStateButton,
                stateNameLabel,
                stateNameTextfield,
                stateValueLabel,
                stateValueTextfield,
                stateURILabel,
                stateURITextfield,
                horizontalLine,
                vBox1,
                o.getParameter1()));

        addDeleteStateListener(stateRepresentation);
        listOfStateRepresentations.add(stateRepresentation);

        pane.revalidate();
        pane.repaint();
      }

      addParameterBoxListener(
          parameter1comboBox,
          param1UnitLabelxPosition,
          param1UnitLabelyPosition,
          parameter1UnitLabel,
          parameter1UnitTextField,
          pane,
          param1AddStateButton,
          vBox1);

      /** Add state to parameter 1 function listener */
      param1AddStateButton.addActionListener(
          new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
              JLabel stateNameLabel = new JLabel("State Name: ");
              JTextField stateNameTextfield = new JTextField(20);
              vBox1.add(stateNameLabel);
              vBox1.add(stateNameTextfield);
              JLabel stateValueLabel = new JLabel("State Value: ");

              JTextField stateValueTextfield = new JTextField(20);
              vBox1.add(stateValueLabel);
              vBox1.add(stateValueTextfield);

              JLabel stateURILabel = new JLabel("State URI: ");
              JTextField stateURITextfield = new JTextField(20);
              vBox1.add(stateURILabel);
              vBox1.add(stateURITextfield);

              JButton deleteStateButton = new JButton("Delete State");
              vBox1.add(deleteStateButton);

              JSeparator horizontalLine = new JSeparator(JSeparator.HORIZONTAL);
              vBox1.add(horizontalLine);
              vBox1.add(horizontalLine);

              StateRepresentation stateRepresentation =
                  (new StateRepresentation(
                      param1AddStateButton,
                      deleteStateButton,
                      stateNameLabel,
                      stateNameTextfield,
                      stateValueLabel,
                      stateValueTextfield,
                      stateURILabel,
                      stateURITextfield,
                      horizontalLine,
                      vBox1,
                      o.getParameter1()));

              addDeleteStateListener(stateRepresentation);
              listOfStateRepresentations.add(stateRepresentation);
              pane.revalidate();
              pane.repaint();
            }
          });

      c.gridy++;
      c.gridx = 0;
      JLabel parameter2Label = new JLabel("Parameter 2 Type:");
      pane.add(parameter2Label, c);

      JComboBox parameter2comboBox = new JComboBox(parameterTypes);
      c.gridx++;
      c.gridx++;
      pane.add(parameter2comboBox, c);

      JLabel parameter2UnitLabel = new JLabel("Set Parameter 2 Unit: ");
      c.gridx++;
      pane.add(parameter2UnitLabel, c);

      JTextField parameter2UnitTextField = new JTextField(o.getParameter2().getParameterUnit());
      parameter2UnitTextField.setPreferredSize(new Dimension(500, 20));
      parameter2UnitTextField.setMinimumSize(new Dimension(500, 20));
      c.gridx++;
      pane.add(parameter2UnitTextField, c);

      JLabel parameter2ValueTypeLabel =
          new JLabel("valueType: " + o.getParameter2().getValueType());
      c.gridx++;
      pane.add(parameter2ValueTypeLabel, c);

      JButton param2AddStateButton = new JButton("Add State");
      Box vBox2 = Box.createVerticalBox();
      vBox2.setBorder(BorderFactory.createLineBorder(Color.black));

      int param2UnitLabelxPosition = c.gridx;
      int param2UnitLabelyPosition = c.gridy;

      addParameterBoxListener(
          parameter2comboBox,
          param2UnitLabelxPosition,
          param2UnitLabelyPosition,
          parameter2UnitLabel,
          parameter2UnitTextField,
          pane,
          param2AddStateButton,
          vBox2);

      /** Add state to parameter 2 function listener */
      param2AddStateButton.addActionListener(
          new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
              JLabel stateNameLabel = new JLabel("State Name: ");
              JTextField stateNameTextfield = new JTextField(20);
              vBox2.add(stateNameLabel);
              vBox2.add(stateNameTextfield);
              JLabel stateValueLabel = new JLabel("State Value: ");

              JTextField stateValueTextfield = new JTextField(20);
              vBox2.add(stateValueLabel);
              vBox2.add(stateValueTextfield);

              JLabel stateURILabel = new JLabel("State URI: ");
              JTextField stateURITextfield = new JTextField(20);
              vBox2.add(stateURILabel);
              vBox2.add(stateURITextfield);

              JButton deleteStateButton = new JButton("Delete State");
              vBox2.add(deleteStateButton);

              JSeparator horizontalLine = new JSeparator(JSeparator.HORIZONTAL);
              vBox2.add(horizontalLine);
              vBox2.add(horizontalLine);

              StateRepresentation stateRepresentation =
                  (new StateRepresentation(
                      param2AddStateButton,
                      deleteStateButton,
                      stateNameLabel,
                      stateNameTextfield,
                      stateValueLabel,
                      stateValueTextfield,
                      stateURILabel,
                      stateURITextfield,
                      horizontalLine,
                      vBox2,
                      o.getParameter2()));

              addDeleteStateListener(stateRepresentation);
              listOfStateRepresentations.add(stateRepresentation);

              pane.revalidate();
              pane.repaint();
            }
          });

      parameter1comboBox.setSelectedItem(o.getParameter1().getParameterType());
      parameter2comboBox.setSelectedItem(o.getParameter2().getParameterType());

      representationRows.add(
          new RepresentationRow(
              o,
              parameter1comboBox,
              parameter2comboBox,
              parameter1UnitTextField,
              parameter2UnitTextField));
    }

    JButton acceptButton = new JButton("Accept");
    c.insets = new Insets(50, 0, 0, 0);
    c.gridwidth = 10;
    c.gridx = 0;
    c.gridy++;
    pane.add(acceptButton, c);

    /** Accept button listener */
    Action acceptAction =
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            for (StateRepresentation s : listOfStateRepresentations) {
              if (s.getStateNameTextField().getText().isEmpty()
                  || s.getStateUriTextField().getText().isEmpty()
                  || s.getStateValueTextField().getText().isEmpty()) {
                JOptionPane.showMessageDialog(
                    null,
                    "Each state parameter field must contain a text! "
                        + "There are some empty parameter fields, please change them before proceeding.");
                return;
              }
            }

            for (ObixObject o : chosenComponents) {
              o.getParameter1().getStateDescriptions().clear();
              o.getParameter2().getStateDescriptions().clear();
            }

            for (StateRepresentation s : listOfStateRepresentations) {
              // Save created State
              ArrayList<String> types = new ArrayList<String>();
              types.add("&colibri;AbsoluteState");
              types.add("&colibri;DiscreteState");

              Value val = new Value();
              val.setValue(s.getStateValueTextField().getText());
              val.setDatatype(s.getParameter().getValueType());

              Name name = new Name();
              name.setName(s.getStateNameTextField().getText());

              StateDescription state =
                  new StateDescription(s.getStateUriTextField().getText(), types, val, name);

              s.getParameter().addStateDescription(state);
            }
            List<ObixObject> chosenObjects = Collections.synchronizedList(new ArrayList<>());
            for (RepresentationRow r : GuiUtility.this.getRepresentationRows()) {
              r.getObixObject()
                  .getParameter1()
                  .setParameterType((String) r.getParam1TypeComboBox().getSelectedItem());
              r.getObixObject()
                  .getParameter2()
                  .setParameterType((String) r.getParam2TypeComboBox().getSelectedItem());
              chosenObjects.add(r.getObixObject());
              if (!r.getParam1UnitTextField().getText().isEmpty()) {
                r.getObixObject()
                    .getParameter1()
                    .setParameterUnit(r.getParam1UnitTextField().getText());
              }
              if (!r.getParam2UnitTextField().getText().isEmpty()) {
                r.getObixObject()
                    .getParameter2()
                    .setParameterUnit(r.getParam2UnitTextField().getText());
              }
            }
            representationRows.clear();
            cards.removeAll();
            JScrollPane scrollPane = new JScrollPane(interactionWindow(chosenObjects));
            scrollPane.getVerticalScrollBar().setUnitIncrement(16);
            scrollPane.setBorder(new EmptyBorder(20, 20, 20, 20));
            cards.add(scrollPane);
            // Display the window.
            mainFrame.pack();
          }
        };
    acceptButton.addActionListener(acceptAction);

    return pane;
  }
/** This class represents the header of a {@link ColibriMessage}. */
public class ColibriMessageHeader {

  /**
   * **************************************************************** Variables *
   * ****************************************************************
   */

  /** The message-id. */
  private String id;

  /** The {@link ContentType} of the message. */
  private ContentType contentType;

  /** The date when the message was sent. */
  private Date date;

  /** The message-id to which this message header is referencing. */
  private String refenceId;

  /** The date when the message expires. */
  private Date expires;

  /** The message-header as a String. */
  private String headerAsString;

  private static final String newLine = Configurator.getInstance().getNewlineString();

  /**
   * **************************************************************** Constructors *
   * ****************************************************************
   */
  public ColibriMessageHeader(String headerAsString) {
    this.headerAsString = headerAsString;
  }

  public ColibriMessageHeader(ContentType contentType) {
    this.id = UUID.randomUUID().toString();
    this.id = id.replace("-", "");
    this.contentType = contentType;
    this.date = new Date();
  }

  public ColibriMessageHeader(ContentType contentType, String refenceId) {
    this.id = UUID.randomUUID().toString();
    this.id = id.replace("-", "");
    this.contentType = contentType;
    this.date = new Date();
    this.refenceId = refenceId;
  }

  /**
   * **************************************************************** Getter and Setter *
   * ****************************************************************
   */
  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public Date getDate() {
    return date;
  }

  public void setDate(Date date) {
    this.date = date;
  }

  public String getMessageHeaderAsString() {
    headerAsString = "";
    if (this.hasDate()) {
      headerAsString += "Date: " + TimeDurationConverter.date2Ical(this.getDate()) + newLine;
    }
    headerAsString +=
        "Content-Type: "
            + this.getContentType().getType()
            + newLine
            + "Message-Id: "
            + this.getId()
            + newLine;
    if (this.hasExpires()) {
      headerAsString += "Expires: " + TimeDurationConverter.date2Ical(this.getExpires()) + newLine;
    }
    if (this.hasReferenceId()) {
      headerAsString += "Reference-Id: " + this.getRefenceId();
    }
    return headerAsString;
  }

  public String getRefenceId() {
    return refenceId;
  }

  public void setRefenceId(String refenceId) {
    this.refenceId = refenceId;
  }

  public Boolean hasReferenceId() {
    return refenceId != null;
  }

  public Boolean hasDate() {
    return date != null;
  }

  public ContentType getContentType() {
    return contentType;
  }

  public void setContentType(ContentType contentType) {
    this.contentType = contentType;
  }

  public Date getExpires() {
    return expires;
  }

  public void setExpires(Date expires) {
    this.expires = expires;
  }

  public Boolean hasExpires() {
    return expires != null;
  }
}