Пример #1
0
 /**
  * Get a list of a room Messages.
  *
  * @param room - the room being queried
  * @return a List of the room Message contents
  */
 public List<String> getMessages(String room) {
   return client.getMessages(room);
 }
Пример #2
0
  /**
   * @param Packet message - the message to be processed Process messages returned from the
   *     ServerConnection by updating GUI objects.
   */
  public synchronized void processMessage(Packet message) {
    System.out.println("Message received and processing in progress");
    System.out.println(
        message.getCommand().name()
            + ": "
            + message.getChannelName()
            + " "
            + message.getMessageText());
    Packet response;
    switch (message.getCommand()) {
      case REPLY_LIST_CHANNEL_USERS:
        if (message.getChannelName().equals(gui.roomLabel.getText())) {
          DefaultListModel userListModel = (DefaultListModel) gui.userList.getModel();
          userListModel.setSize(0);
          String[] users = message.getMessageText().split(" ");
          for (String user : users) {
            userListModel.addElement(user);
          }
        }
        break;
      case REPLY_SUCCESS:
        System.out.print("Reply success");
        break;
      case REPLY_FAILURE:
        break;
      case JOIN:
        if (message.getChannelName().equals(gui.roomLabel.getText())) {
          DefaultListModel userListModel = (DefaultListModel) gui.userList.getModel();
          userListModel.addElement(message.getAuthor());
        }
        break;
      case REPLY_LIST_USERS:
        JTree tree = gui.tree;
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("All Users");
        DefaultTreeModel model = new DefaultTreeModel(root);

        String[] text = message.getMessageText().split(" ");
        for (String name : text) {
          root.add(new DefaultMutableTreeNode(name));
        }

        // Sleep until tree is available. treeLogic means we have to wait extra time for tree to get
        // setup before access
        while (!gui.visible) {
          try {
            tree = gui.tree;
            Thread.sleep(10);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }

        tree.setModel(model);
        tree.repaint();

        break;
      case MESSAGE:
        String mess = message.getAuthor() + ": " + message.getMessageText();
        if (gui.roomLabel.getText().equals(message.getChannelName())) {
          synchronized (gui.chatList) {
            JList chatList = gui.chatList;
            DefaultListModel model2 = (DefaultListModel) chatList.getModel();
            model2.add(0, mess);
            chatList.repaint();
          }
        } else {
          DefaultTableModel roomModel = (DefaultTableModel) gui.roomTable.getModel();
          synchronized (roomModel) {
            for (int i = 0; i < roomModel.getRowCount(); i++) {
              if (message.getChannelName() != gui.roomLabel.getText()
                  && message.getChannelName().equals(roomModel.getValueAt(i, 2))) {
                String missed = (String) roomModel.getValueAt(i, 3);
                try {
                  Integer numMissed = Integer.parseInt(missed);
                  numMissed += 1;
                  roomModel.setValueAt(numMissed.toString(), i, 3);
                } catch (Exception e) {
                  roomModel.setValueAt("1", i, 3);
                }

                break;
              }
            }
          }
        }
        client.getMessages(message.getChannelName()).add(0, mess);
        break;
      case QUIT:
        JTree treeCopy = gui.tree;
        DefaultTreeModel treeModel = (DefaultTreeModel) treeCopy.getModel();
        DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) treeModel.getRoot();
        for (int i = 0; i < rootNode.getChildCount(); i++) {
          System.out.println(message.getAuthor());
          System.out.println("child: " + rootNode.getChildAt(i));
          if (rootNode.getChildAt(i).equals(message.getAuthor())) {
            rootNode.remove(i);
            break;
          }
        }

        break;
      default:
        System.out.println("Fell through");
        break;
    }
  }