Example #1
0
  /** Updates the chat message panel in the Swing event thread */
  public void updateChatMessagePanel() {
    // Ensure that we operate in the Swing event thread
    if (!SwingUtilities.isEventDispatchThread()) {
      SwingUtilities.invokeLater(
          new Runnable() {
            @Override
            public void run() {
              updateChatMessagePanel();
            }
          });
      return;
    }

    // Set the title header
    titleHeader.setText(
        chatData != null ? NameUtils.getName(chatData.getId(), NameFormat.MEDIUM) : " ");
    titleHeader.setVisible(chatData != null);

    // Only enable send-function when there is chat data, and hence a target
    sendBtn.setEnabled(chatData != null);
    messageText.setEditable(chatData != null);

    // Remove all components from the messages panel
    messagesPanel.removeAll();

    Insets insets = new Insets(0, 2, 2, 2);
    Insets insets2 = new Insets(6, 2, 0, 2);

    if (chatData != null && chatData.getMessageCount() > 0) {

      // First, add a filler component
      int y = 0;
      messagesPanel.add(
          new JLabel(""),
          new GridBagConstraints(0, y++, 1, 1, 0.0, 1.0, NORTH, VERTICAL, insets, 0, 0));

      // Add the messages
      long lastMessageTime = 0;
      for (EPDChatMessage message : chatData.getMessages()) {

        // Check if we need to add a time label
        if (message.getSendDate().getTime() - lastMessageTime > PRINT_DATE_INTERVAL) {
          JLabel dateLabel =
              new JLabel(
                  String.format(
                      message.isOwnMessage() ? "Sent to %s" : "Received %s",
                      Formatter.formatShortDateTimeNoTz(
                          new Date(message.getSendDate().getTime()))));
          dateLabel.setFont(dateLabel.getFont().deriveFont(9.0f).deriveFont(Font.PLAIN));
          dateLabel.setHorizontalAlignment(SwingConstants.CENTER);
          dateLabel.setForeground(Color.LIGHT_GRAY);
          messagesPanel.add(
              dateLabel,
              new GridBagConstraints(0, y++, 1, 1, 1.0, 0.0, NORTH, HORIZONTAL, insets2, 0, 0));
        }

        // Add a chat message field
        JPanel msg = new JPanel();
        msg.setBorder(new ChatMessageBorder(message));
        JLabel msgLabel = new ChatMessageLabel(message.getMsg(), message.isOwnMessage());
        msg.add(msgLabel);
        messagesPanel.add(
            msg, new GridBagConstraints(0, y++, 1, 1, 1.0, 0.0, NORTH, HORIZONTAL, insets, 0, 0));

        lastMessageTime = message.getSendDate().getTime();
      }

      // Scroll to the bottom
      validate();
      scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
      messagesPanel.repaint();

    } else if (chatData == null && noDataComponent != null) {
      // The noDataComponent may e.g. be a message
      messagesPanel.add(
          noDataComponent, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, NORTH, BOTH, insets, 0, 0));
    }
  }