conferenceroombrowser.java.svn-base

来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 726 行 · 第 1/2 页

SVN-BASE
726
字号
                        dlg.dispose();
                    }
                }
                try {
                    Iterator iter = rooms.iterator();
                    while (iter.hasNext()) {
                        HostedRoom hostedRoom = (HostedRoom)iter.next();
                        String roomName = hostedRoom.getName();
                        String roomJID = hostedRoom.getJid();

                        int numberOfOccupants = -1;

                        // Need to handle the case where the room info does not contain the number of occupants. If that is the case,
                        // we should not continue to request this info from the service.
                        if (!partialDiscovery) {
                            RoomInfo roomInfo = null;
                            try {
                                roomInfo = MultiUserChat.getRoomInfo(SparkManager.getConnection(), roomJID);
                            }
                            catch (Exception e) {
                            }

                            if (roomInfo != null) {
                                numberOfOccupants = roomInfo.getOccupantsCount();
                            }
                            if (roomInfo == null || numberOfOccupants == -1) {
                                partialDiscovery = true;
                            }
                        }
                        addRoomToTable(roomJID, roomName, numberOfOccupants);
                    }
                }
                catch (Exception e) {
                    Log.error("Error setting up GroupChatTable", e);
                }
            }
        };

        worker.start();
        // Find Initial Rooms


        final JOptionPane pane;


        TitlePanel titlePanel;

        // Create the title panel for this dialog
        titlePanel = new TitlePanel(Res.getString("title.create.or.bookmark.room"), Res.getString("message.add.favorite.room"), SparkRes.getImageIcon(SparkRes.BLANK_IMAGE), true);

        // Construct main panel w/ layout.
        final JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(titlePanel, BorderLayout.NORTH);

        // The user should only be able to close this dialog.
        Object[] options = {Res.getString("close")};
        pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);

        mainPanel.add(pane, BorderLayout.CENTER);

        final JOptionPane p = new JOptionPane();

        dlg = p.createDialog(SparkManager.getMainWindow(), Res.getString("title.browse.room.service", serviceName));
        dlg.setModal(false);

        dlg.pack();
        dlg.setSize(500, 400);
        dlg.setResizable(true);
        dlg.setContentPane(mainPanel);
        dlg.setLocationRelativeTo(SparkManager.getMainWindow());

        PropertyChangeListener changeListener = new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent e) {
                String value = (String)pane.getValue();
                if (Res.getString("close").equals(value)) {
                    pane.removePropertyChangeListener(this);
                    dlg.dispose();
                }
                else if (Res.getString("close").equals(value)) {
                    pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                }
            }
        };

        pane.addPropertyChangeListener(changeListener);

        dlg.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
                    dlg.dispose();
                }
            }
        });

        dlg.setVisible(true);
        dlg.toFront();
        dlg.requestFocus();
    }

    private final class RoomList extends Table {
        public RoomList() {
            super(new String[]{" ", Res.getString("title.name"), Res.getString("title.address"), Res.getString("title.occupants")});
            getColumnModel().setColumnMargin(0);
            getColumnModel().getColumn(0).setMaxWidth(30);
            getColumnModel().getColumn(3).setMaxWidth(80);

            setSelectionBackground(Table.SELECTION_COLOR);
            setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            setRowSelectionAllowed(true);
//            setSortable(true);

            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
                        enterRoom();
                    }
                }

                public void mouseReleased(MouseEvent e) {
                    checkPopup(e);
                }

                public void mousePressed(MouseEvent e) {
                    checkPopup(e);
                }
            });

        }

        // Handle image rendering correctly
        public TableCellRenderer getCellRenderer(int row, int column) {
            Object o = getValueAt(row, column);
            if (o != null) {
                if (o instanceof JLabel) {
                    return new JLabelRenderer(false);
                }
            }

            if (column == 3) {
                return new CenterRenderer();
            }

            return super.getCellRenderer(row, column);
        }

        private void checkPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                final JPopupMenu popupMenu = new JPopupMenu();

                Action roomInfoAction = new AbstractAction() {
                    public void actionPerformed(ActionEvent actionEvent) {
                        int selectedRow = roomsTable.getSelectedRow();
                        if (selectedRow != -1) {
                            String roomJID = (String)roomsTable.getValueAt(selectedRow, 2) + "@" + serviceName;
                            RoomBrowser roomBrowser = new RoomBrowser();
                            roomBrowser.displayRoomInformation(roomJID);
                        }
                    }
                };

                roomInfoAction.putValue(Action.NAME, Res.getString("menuitem.view.room.info"));
                roomInfoAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_DATA_FIND_IMAGE));

                popupMenu.add(roomInfoAction);
                popupMenu.show(roomsTable, e.getX(), e.getY());
            }
        }

    }


    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == createButton) {
            createRoom();
        }
    }

    private void enterRoom() {
        int selectedRow = roomsTable.getSelectedRow();
        if (-1 == selectedRow) {
            JOptionPane.showMessageDialog(dlg, Res.getString("message.select.room.to.enter"), Res.getString("title.group.chat"), JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        final String roomJID = (String)roomsTable.getValueAt(selectedRow, 2) + "@" + serviceName;
        final String roomDescription = (String)roomsTable.getValueAt(selectedRow, 1);

        try {
            chatManager.getChatContainer().getChatRoom(roomJID);
        }
        catch (ChatRoomNotFoundException e1) {
            ConferenceUtils.joinConferenceOnSeperateThread(roomDescription, roomJID, null);
        }
    }

    /**
     * Returns a Collection of all rooms in the specified Conference Service.
     *
     * @param serviceName the name of the conference service.
     * @return a Collection of all rooms in the Conference service.
     * @throws Exception
     */
    private static Collection getRoomList(String serviceName) throws Exception {
        return MultiUserChat.getHostedRooms(SparkManager.getConnection(), serviceName);
    }


    /**
     * Create a new room based on room table selection.
     */
    private void createRoom() {
        RoomCreationDialog mucRoomDialog = new RoomCreationDialog();
        final MultiUserChat groupChat = mucRoomDialog.createGroupChat(SparkManager.getMainWindow(), serviceName);
        LocalPreferences pref = SettingsManager.getLocalPreferences();

        if (null != groupChat) {

            // Join Room
            try {
                GroupChatRoom room = new GroupChatRoom(groupChat);


                groupChat.create(pref.getNickname());
                chatManager.getChatContainer().addChatRoom(room);
                chatManager.getChatContainer().activateChatRoom(room);

                // Send Form
                Form form = groupChat.getConfigurationForm().createAnswerForm();
                if (mucRoomDialog.isPasswordProtected()) {
                    String password = mucRoomDialog.getPassword();
                    form.setAnswer("muc#roomconfig_passwordprotectedroom", true);
                    form.setAnswer("muc#roomconfig_roomsecret", password);
                }
                form.setAnswer("muc#roomconfig_roomname", mucRoomDialog.getRoomName());

                if (mucRoomDialog.isPermanent()) {
                    form.setAnswer("muc#roomconfig_persistentroom", true);
                }

                List owners = new ArrayList();
                owners.add(SparkManager.getSessionManager().getBareAddress());
                form.setAnswer("muc#roomconfig_roomowners", owners);

                // new DataFormDialog(groupChat, form);
                groupChat.sendConfigurationForm(form);
                addRoomToTable(groupChat.getRoom(), StringUtils.parseName(groupChat.getRoom()), 1);
            }
            catch (XMPPException e1) {
                Log.error("Error creating new room.", e1);
                JOptionPane.showMessageDialog(this, Res.getString("message.room.creation.error"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    /**
     * Adds a room to the room table.
     *
     * @param jid               the jid of the conference room.
     * @param roomName          the name of the conference room.
     * @param numberOfOccupants the number of occupants in the conference room. If -1 is specified,
     *                          the the occupant count will show as n/a.
     */
    private void addRoomToTable(String jid, String roomName, int numberOfOccupants) {
        JLabel bookmarkedLabel = new JLabel();
        if (isBookmarked(jid)) {
            bookmarkedLabel.setIcon(SparkRes.getImageIcon(SparkRes.BOOKMARK_ICON));
        }

        String occupants = Integer.toString(numberOfOccupants);
        if (numberOfOccupants == -1) {
            occupants = "n/a";
        }

        final Object[] insertRoom = new Object[]{bookmarkedLabel, roomName, StringUtils.parseName(jid), occupants};
        roomsTable.getTableModel().addRow(insertRoom);
    }

    /**
     * Returns true if the room specified is bookmarked.
     *
     * @param roomJID the jid of the room to check.
     * @return true if the room is bookmarked.
     */
    private boolean isBookmarked(String roomJID) {
        final Iterator bookmarks = conferences.getBookmarks().iterator();
        while (bookmarks.hasNext()) {
            BookmarkedConference bk = (BookmarkedConference)bookmarks.next();
            String jid = bk.getJid();
            if (jid != null && roomJID.equals(jid)) {
                return true;
            }
        }

        return false;

    }

    /**
     * Toggles the bookmark room button depending on it's state.
     *
     * @param addBookmark true if the button should display itself as bookmarkable :)
     */
    private void addBookmarkUI(boolean addBookmark) {
        if (!addBookmark) {
            addRoomButton.setText(Res.getString("button.remove.bookmark"));
            addRoomButton.setIcon(SparkRes.getImageIcon(SparkRes.DELETE_BOOKMARK_ICON));
        }
        else {
            ResourceUtils.resButton(addRoomButton, Res.getString("button.bookmark.room"));
            addRoomButton.setIcon(SparkRes.getImageIcon(SparkRes.ADD_BOOKMARK_ICON));
        }
    }

    /*
    **  Center the text
    */
    static class CenterRenderer extends DefaultTableCellRenderer {
        public CenterRenderer() {
            setHorizontalAlignment(CENTER);
        }

        public Component getTableCellRendererComponent(
                JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            return this;
        }
    }

    private class RoomObject {
        private String roomName;
        private String roomJID;

        int numberOfOccupants;

        public String getRoomName() {
            return roomName;
        }

        public void setRoomName(String roomName) {
            this.roomName = roomName;
        }

        public String getRoomJID() {
            return roomJID;
        }

        public void setRoomJID(String roomJID) {
            this.roomJID = roomJID;
        }

        public int getNumberOfOccupants() {
            return numberOfOccupants;
        }

        public void setNumberOfOccupants(int numberOfOccupants) {
            this.numberOfOccupants = numberOfOccupants;
        }
    }


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?