diff --git a/src/java/org/jivesoftware/spark/ui/conferences/ConferenceRoomBrowser.java b/src/java/org/jivesoftware/spark/ui/conferences/ConferenceRoomBrowser.java index 65c07b79..d9739938 100644 --- a/src/java/org/jivesoftware/spark/ui/conferences/ConferenceRoomBrowser.java +++ b/src/java/org/jivesoftware/spark/ui/conferences/ConferenceRoomBrowser.java @@ -1,922 +1,923 @@ -/** - * $RCSfile: ,v $ - * $Revision: $ - * $Date: $ - * - * Copyright (C) 2004-2010 Jive Software. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jivesoftware.spark.ui.conferences; - -import java.awt.BorderLayout; -import java.awt.Color; -import java.awt.Component; -import java.awt.FlowLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.ComponentEvent; -import java.awt.event.ComponentListener; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import javax.swing.AbstractAction; -import javax.swing.Action; -import javax.swing.JDialog; -import javax.swing.JLabel; -import javax.swing.JMenuItem; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JPopupMenu; -import javax.swing.JScrollPane; -import javax.swing.JTable; -import javax.swing.ListSelectionModel; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; -import javax.swing.table.DefaultTableCellRenderer; -import javax.swing.table.TableCellRenderer; -import javax.swing.tree.DefaultTreeModel; -import javax.swing.tree.TreePath; - -import org.jivesoftware.resource.Res; -import org.jivesoftware.resource.SparkRes; -import org.jivesoftware.smack.XMPPException; -import org.jivesoftware.smack.util.StringUtils; -import org.jivesoftware.smackx.Form; -import org.jivesoftware.smackx.bookmark.BookmarkedConference; -import org.jivesoftware.smackx.muc.HostedRoom; -import org.jivesoftware.smackx.muc.MultiUserChat; -import org.jivesoftware.smackx.muc.RoomInfo; -import org.jivesoftware.spark.ChatManager; -import org.jivesoftware.spark.SparkManager; -import org.jivesoftware.spark.component.JiveTreeNode; -import org.jivesoftware.spark.component.RolloverButton; -import org.jivesoftware.spark.component.Table; -import org.jivesoftware.spark.component.TitlePanel; -import org.jivesoftware.spark.component.Tree; -import org.jivesoftware.spark.ui.ChatRoomNotFoundException; -import org.jivesoftware.spark.ui.rooms.GroupChatRoom; -import org.jivesoftware.spark.util.ResourceUtils; -import org.jivesoftware.spark.util.SwingWorker; -import org.jivesoftware.spark.util.log.Log; -import org.jivesoftware.sparkimpl.settings.local.LocalPreferences; -import org.jivesoftware.sparkimpl.settings.local.SettingsManager; - -/** - * A UI that handles all Group Rooms contained in an XMPP Messenger server. This handles - * creation and joining of rooms for group chat discussions as well as the listing - * of the creation times, number of occupants in a room, and the room name itself. - */ -public class ConferenceRoomBrowser extends JPanel implements ActionListener, ComponentListener{ - private static final long serialVersionUID = -4483998189117467048L; - private final RoomList roomsTable; - private final RolloverButton createButton = new RolloverButton("", SparkRes.getImageIcon(SparkRes.SMALL_USER1_NEW)); - private final RolloverButton joinRoomButton = new RolloverButton("", SparkRes.getImageIcon(SparkRes.DOOR_IMAGE)); - private final RolloverButton refreshButton = new RolloverButton("", SparkRes.getImageIcon(SparkRes.REFRESH_IMAGE)); - private final RolloverButton addRoomButton = new RolloverButton("", SparkRes.getImageIcon(SparkRes.ADD_BOOKMARK_ICON)); - - private final RolloverButton showHiddenButtons = new RolloverButton(SparkRes.getImageIcon(SparkRes.PANE_UP_ARROW_IMAGE)); - - private JMenuItem joinRoomItem; - private JMenuItem addRoomItem; - private JMenuItem createItem; - private JMenuItem refreshItem; - - private ChatManager chatManager; - - private JDialog dlg; - - private BookmarksUI conferences; - private String serviceName; - - private int allButtonWidth; - private int threeButtonWidth; - private int twoButtonWidth; - private int oneButtonWidth; - - private boolean partialDiscovery = false; - - private JPopupMenu popup; - /** - * Creates a new instance of ConferenceRooms. - * - * @param conferences the conference ui. - * @param serviceName the name of the conference service. - * //TODO This needs to be refactored. - */ - public ConferenceRoomBrowser(BookmarksUI conferences, final String serviceName) { - - this.setLayout(new BorderLayout()); - - this.conferences = conferences; - this.serviceName = serviceName; - - - - popup = new JPopupMenu(); - - joinRoomItem = new JMenuItem(Res.getString("menuitem.join.room")); - addRoomItem = new JMenuItem(Res.getString("menuitem.bookmark.room")); - createItem = new JMenuItem(Res.getString("menuitem.create.room")); - refreshItem = new JMenuItem(Res.getString("menuitem.refresh")); - - joinRoomItem.setIcon(SparkRes.getImageIcon(SparkRes.DOOR_IMAGE)); - addRoomItem.setIcon(SparkRes.getImageIcon(SparkRes.ADD_BOOKMARK_ICON)); - createItem.setIcon(SparkRes.getImageIcon(SparkRes.SMALL_USER1_NEW)); - refreshItem.setIcon(SparkRes.getImageIcon(SparkRes.REFRESH_IMAGE)); - - popup.add(joinRoomItem); - popup.add(addRoomItem); - popup.add(createItem); - popup.add(refreshItem); - - - // Add Toolbar - final JPanel Hauptpanel = new JPanel(new BorderLayout()); - JPanel toolbar = new JPanel(new FlowLayout(FlowLayout.LEFT)); - JPanel pane_hiddenButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT)); - toolbar.add(joinRoomButton); - toolbar.add(addRoomButton); - toolbar.add(createButton); - toolbar.add(refreshButton); - pane_hiddenButtons.add(showHiddenButtons); - - Hauptpanel.add(toolbar, BorderLayout.WEST); - Hauptpanel.add(pane_hiddenButtons, BorderLayout.EAST); - this.add(Hauptpanel, BorderLayout.NORTH); - - createButton.addActionListener(this); - createItem.addActionListener(this); - joinRoomButton.addActionListener(this); - refreshButton.addActionListener(this); - - ResourceUtils.resButton(createButton, Res.getString("button.create.room")); - ResourceUtils.resButton(joinRoomButton, Res.getString("button.join.room")); - ResourceUtils.resButton(refreshButton, Res.getString("button.refresh")); - ResourceUtils.resButton(addRoomButton, Res.getString("button.bookmark.room")); - - refreshButton.setToolTipText(Res.getString("message.update.room.list")); - joinRoomButton.setToolTipText(Res.getString("message.join.conference.room")); - createButton.setToolTipText(Res.getString("message.create.or.join.room")); - - // Add Group Chat Table - roomsTable = new RoomList(); - - final JScrollPane pane = new JScrollPane(roomsTable); - pane.setBackground(Color.white); - pane.setForeground(Color.white); - this.setBackground(Color.white); - this.setForeground(Color.white); - pane.getViewport().setBackground(Color.white); - this.add(pane, BorderLayout.CENTER); - - chatManager = SparkManager.getChatManager(); - - joinRoomButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent actionEvent) { - joinSelectedRoom(); - } - }); - - addRoomButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent actionEvent) { - bookmarkRoom(serviceName); - } - }); - - refreshButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent actionEvent) { - refreshRoomList(serviceName); - } - }); - - joinRoomItem.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent actionEvent) { - joinSelectedRoom(); - } - }); - - addRoomItem.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent actionEvent) { - bookmarkRoom(serviceName); - } - }); - - refreshItem.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent actionEvent) { - refreshRoomList(serviceName); - } - }); - - showHiddenButtons.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent actionEvent) { - popup.show(showHiddenButtons, 0, showHiddenButtons.getHeight()); - } - }); - - - - joinRoomButton.setEnabled(false); - addRoomButton.setEnabled(false); - joinRoomItem.setEnabled(false); - addRoomItem.setEnabled(false); - - addTableListener(); - } - - private void refreshRoomList(final String serviceName) { - roomsTable.clearTable(); - SwingWorker worker = new SwingWorker() { - Collection result; - - public Object construct() { - result = getRoomsAndInfo(serviceName); - return result; - } - - public void finished() { - try { - for (Object aResult : result) { - RoomObject obj = (RoomObject) aResult; - addRoomToTable(obj.getRoomJID(), obj.getRoomName(), obj.getNumberOfOccupants()); - } - } - catch (Exception e) { - Log.error("Unable to retrieve room list and info.", e); - } - } - }; - - worker.start(); - - - } - - private Collection getRoomsAndInfo(final String serviceName) { - List roomList = new ArrayList(); - boolean stillSearchForOccupants = true; - try { - Collection result = getRoomList(serviceName); - try { - for (Object aResult : result) { - HostedRoom hostedRoom = (HostedRoom) aResult; - String roomName = hostedRoom.getName(); - String roomJID = hostedRoom.getJid(); - int numberOfOccupants = -1; - if (stillSearchForOccupants) { - RoomInfo roomInfo = null; - try { - roomInfo = MultiUserChat.getRoomInfo(SparkManager.getConnection(), roomJID); - } - catch (Exception e) { - // Nothing to do - } - - if (roomInfo != null) { - numberOfOccupants = roomInfo.getOccupantsCount(); - if (numberOfOccupants == -1) { - stillSearchForOccupants = false; - } - } else { - stillSearchForOccupants = false; - } - } - - RoomObject obj = new RoomObject(); - obj.setRoomJID(roomJID); - obj.setRoomName(roomName); - obj.setNumberOfOccupants(numberOfOccupants); - roomList.add(obj); - } - } - catch (Exception e) { - Log.error("Error setting up GroupChatTable", e); - } - } - catch (Exception e) { - System.err.println(e); - } - return roomList; - } - - private void bookmarkRoom(String serviceName) { - int selectedRow = roomsTable.getSelectedRow(); - if (-1 == selectedRow) { - JOptionPane.showMessageDialog(dlg, Res.getString("message.select.add.room.to.add"), Res.getString("title.group.chat"), JOptionPane.INFORMATION_MESSAGE); - return; - } - - final String roomJID = roomsTable.getValueAt(selectedRow, 2) + "@" + serviceName; - final String roomName = roomsTable.getValueAt(selectedRow, 1).toString(); - - // Check to see what type of room this is. - try { - final RoomInfo roomInfo = MultiUserChat.getRoomInfo(SparkManager.getConnection(), roomJID); - if (!roomInfo.isPersistent()) { - JOptionPane.showMessageDialog(dlg, Res.getString("message.bookmark.temporary.room.error"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE); - return; - } - } - catch (Exception e) { - // Do not return - } - - Tree serviceTree = conferences.getTree(); - JiveTreeNode rootNode = (JiveTreeNode)serviceTree.getModel().getRoot(); - - TreePath rootPath = serviceTree.findByName(serviceTree, new String[]{rootNode.toString(), serviceName}); - - - boolean isBookmarked = isBookmarked(roomJID); - - - if (!isBookmarked) { - JiveTreeNode node = (JiveTreeNode)serviceTree.getLastSelectedPathComponent(); - if (node == null) { - TreePath path = serviceTree.findByName(serviceTree, new String[]{rootNode.toString(), ConferenceServices.getDefaultServiceName()}); - node = (JiveTreeNode)path.getLastPathComponent(); - } - JiveTreeNode roomNode = new JiveTreeNode(roomName, false, SparkRes.getImageIcon(SparkRes.BOOKMARK_ICON)); - roomNode.setAssociatedObject(roomJID); - node.add(roomNode); - final DefaultTreeModel model = (DefaultTreeModel)serviceTree.getModel(); - model.nodeStructureChanged(node); - serviceTree.expandPath(rootPath); - roomsTable.getTableModel().setValueAt(new JLabel(SparkRes.getImageIcon(SparkRes.BOOKMARK_ICON)), selectedRow, 0); - addBookmarkUI(false); - - conferences.addBookmark(roomName, roomJID, false); - } - else { - // Remove bookmark - TreePath path = serviceTree.findByName(serviceTree, new String[]{rootNode.toString(), serviceName, roomName}); - JiveTreeNode node = (JiveTreeNode)path.getLastPathComponent(); - final DefaultTreeModel model = (DefaultTreeModel)serviceTree.getModel(); - model.removeNodeFromParent(node); - roomsTable.getTableModel().setValueAt(new JLabel(SparkRes.getImageIcon(SparkRes.BLANK_IMAGE)), selectedRow, 0); - addBookmarkUI(true); - - String jid = (String)node.getAssociatedObject(); - conferences.removeBookmark(jid); - } - } - - private void joinSelectedRoom() { - int selectedRow = roomsTable.getSelectedRow(); - if (-1 == selectedRow) { - JOptionPane.showMessageDialog(dlg, Res.getString("message.select.room.to.join"), Res.getString("title.group.chat"), JOptionPane.INFORMATION_MESSAGE); - return; - } - enterRoom(); - } - - private void addTableListener() { - roomsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { - public void valueChanged(ListSelectionEvent e) { - if (e.getValueIsAdjusting()) return; - - int selectedRow = roomsTable.getSelectedRow(); - if (selectedRow != -1) { - joinRoomButton.setEnabled(true); - joinRoomItem.setEnabled(true); - String roomJID = roomsTable.getValueAt(selectedRow, 2) + "@" + serviceName; - addRoomButton.setEnabled(true); - addRoomItem.setEnabled(true); - if (isBookmarked(roomJID)) { - addBookmarkUI(false); - } - else { - addBookmarkUI(true); - } - } - else { - joinRoomButton.setEnabled(false); - addRoomButton.setEnabled(false); - joinRoomItem.setEnabled(false); - addRoomItem.setEnabled(false); - addBookmarkUI(true); - } - } - }); - } - - /** - * Displays the ConferenceRoomBrowser. - */ - public void invoke() { - SwingWorker worker = new SwingWorker() { - Collection rooms; - - public Object construct() { - try { - rooms = getRoomList(serviceName); - } - catch (Exception e) { - Log.error("Unable to retrieve list of rooms.", e); - } - - return "OK"; - } - - public void finished() { - if (rooms == null) { - JOptionPane.showMessageDialog(conferences, Res.getString("message.conference.info.error"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE); - if (dlg != null) { - dlg.dispose(); - } - } - try { - for (Object room : rooms) { - HostedRoom hostedRoom = (HostedRoom) room; - 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) { - // Nothing to do - } - - 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.addComponentListener(this); - - /* looking up which bundle is used to set the size of the Window - (not using Localpreferences getLanguage() because sometimes language is not saved - in the properties file and so the method only returns an empty String) */ - if(Res.getBundle().getLocale().toString().equals("de")) - dlg.setSize(700, 400); - else - 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(); - } - } - }); - - // will need that, when the window is smaller then the buttons width... - setButtonsWidth(); - - showHiddenButtons.setVisible(false); - - 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 = 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 || e.getSource() == createItem) { - 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 = 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 if a problem occurs while getting the room list - */ - 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) { - for (Object o : conferences.getBookmarks()) { - BookmarkedConference bk = (BookmarkedConference) o; - 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; - } - } - - - public void componentHidden(ComponentEvent e) - { - // TODO Auto-generated method stub - - } - - public void componentMoved(ComponentEvent e) - { - // TODO Auto-generated method stub - - } - - public void componentResized(ComponentEvent e) - { - if(this.getWidth() <= (oneButtonWidth + 19)) - { - joinRoomButton.setVisible(false); - addRoomButton.setVisible(false); - createButton.setVisible(false); - refreshButton.setVisible(false); - - joinRoomItem.setVisible(true); - addRoomItem.setVisible(true); - createItem.setVisible(true); - refreshItem.setVisible(true); - - showHiddenButtons.setVisible(true); - } - - else if(this.getWidth() <= (twoButtonWidth + 19)) - { - joinRoomButton.setVisible(true); - addRoomButton.setVisible(false); - createButton.setVisible(false); - refreshButton.setVisible(false); - - joinRoomItem.setVisible(false); - addRoomItem.setVisible(true); - createItem.setVisible(true); - refreshItem.setVisible(true); - - showHiddenButtons.setVisible(true); - } - - else if(this.getWidth() <= (threeButtonWidth + 19)) - { - joinRoomButton.setVisible(true); - addRoomButton.setVisible(true); - createButton.setVisible(false); - refreshButton.setVisible(false); - - joinRoomItem.setVisible(false); - addRoomItem.setVisible(false); - createItem.setVisible(true); - refreshItem.setVisible(true); - - showHiddenButtons.setVisible(true); - } - - else if(this.getWidth() <= (allButtonWidth + 19)) - { - joinRoomButton.setVisible(true); - addRoomButton.setVisible(true); - createButton.setVisible(true); - refreshButton.setVisible(false); - - joinRoomItem.setVisible(false); - addRoomItem.setVisible(false); - createItem.setVisible(false); - refreshItem.setVisible(true); - - showHiddenButtons.setVisible(true); - } - - else if(this.getWidth() > (allButtonWidth + 19)) - { - joinRoomButton.setVisible(true); - addRoomButton.setVisible(true); - createButton.setVisible(true); - refreshButton.setVisible(true); - - joinRoomItem.setVisible(false); - addRoomItem.setVisible(false); - createItem.setVisible(false); - refreshItem.setVisible(false); - - showHiddenButtons.setVisible(false); - } - } - - public void componentShown(ComponentEvent e) - { - // TODO Auto-generated method stub - - } - - // will need that, when the window is smaller than the buttons width... - private void setButtonsWidth() - { - allButtonWidth = createButton.getWidth() + refreshButton.getWidth() + addRoomButton.getWidth() + joinRoomButton.getWidth(); - threeButtonWidth = createButton.getWidth() + addRoomButton.getWidth() + joinRoomButton.getWidth() + showHiddenButtons.getWidth(); - twoButtonWidth = addRoomButton.getWidth() + joinRoomButton.getWidth() + showHiddenButtons.getWidth(); - oneButtonWidth = joinRoomButton.getWidth() + showHiddenButtons.getWidth(); - } - -} - - +/** + * $RCSfile: ,v $ + * $Revision: $ + * $Date: $ + * + * Copyright (C) 2004-2010 Jive Software. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.spark.ui.conferences; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Component; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentEvent; +import java.awt.event.ComponentListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JPopupMenu; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.ListSelectionModel; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import javax.swing.table.DefaultTableCellRenderer; +import javax.swing.table.TableCellRenderer; +import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.TreePath; + +import org.jivesoftware.resource.Res; +import org.jivesoftware.resource.SparkRes; +import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smack.util.StringUtils; +import org.jivesoftware.smackx.Form; +import org.jivesoftware.smackx.bookmark.BookmarkedConference; +import org.jivesoftware.smackx.muc.HostedRoom; +import org.jivesoftware.smackx.muc.MultiUserChat; +import org.jivesoftware.smackx.muc.RoomInfo; +import org.jivesoftware.spark.ChatManager; +import org.jivesoftware.spark.SparkManager; +import org.jivesoftware.spark.component.JiveTreeNode; +import org.jivesoftware.spark.component.RolloverButton; +import org.jivesoftware.spark.component.Table; +import org.jivesoftware.spark.component.TitlePanel; +import org.jivesoftware.spark.component.Tree; +import org.jivesoftware.spark.ui.ChatRoomNotFoundException; +import org.jivesoftware.spark.ui.rooms.GroupChatRoom; +import org.jivesoftware.spark.util.ResourceUtils; +import org.jivesoftware.spark.util.SwingWorker; +import org.jivesoftware.spark.util.log.Log; +import org.jivesoftware.sparkimpl.settings.local.LocalPreferences; +import org.jivesoftware.sparkimpl.settings.local.SettingsManager; + +/** + * A UI that handles all Group Rooms contained in an XMPP Messenger server. This handles + * creation and joining of rooms for group chat discussions as well as the listing + * of the creation times, number of occupants in a room, and the room name itself. + */ +public class ConferenceRoomBrowser extends JPanel implements ActionListener, ComponentListener{ + private static final long serialVersionUID = -4483998189117467048L; + private final RoomList roomsTable; + private final RolloverButton createButton = new RolloverButton("", SparkRes.getImageIcon(SparkRes.SMALL_USER1_NEW)); + private final RolloverButton joinRoomButton = new RolloverButton("", SparkRes.getImageIcon(SparkRes.DOOR_IMAGE)); + private final RolloverButton refreshButton = new RolloverButton("", SparkRes.getImageIcon(SparkRes.REFRESH_IMAGE)); + private final RolloverButton addRoomButton = new RolloverButton("", SparkRes.getImageIcon(SparkRes.ADD_BOOKMARK_ICON)); + + private final RolloverButton showHiddenButtons = new RolloverButton(SparkRes.getImageIcon(SparkRes.PANE_UP_ARROW_IMAGE)); + + private JMenuItem joinRoomItem; + private JMenuItem addRoomItem; + private JMenuItem createItem; + private JMenuItem refreshItem; + + private ChatManager chatManager; + + private JDialog dlg; + + private BookmarksUI conferences; + private String serviceName; + + private int allButtonWidth; + private int threeButtonWidth; + private int twoButtonWidth; + private int oneButtonWidth; + + private boolean partialDiscovery = false; + + private JPopupMenu popup; + /** + * Creates a new instance of ConferenceRooms. + * + * @param conferences the conference ui. + * @param serviceName the name of the conference service. + * //TODO This needs to be refactored. + */ + public ConferenceRoomBrowser(BookmarksUI conferences, final String serviceName) { + + this.setLayout(new BorderLayout()); + + this.conferences = conferences; + this.serviceName = serviceName; + + + + popup = new JPopupMenu(); + + joinRoomItem = new JMenuItem(Res.getString("menuitem.join.room")); + addRoomItem = new JMenuItem(Res.getString("menuitem.bookmark.room")); + createItem = new JMenuItem(Res.getString("menuitem.create.room")); + refreshItem = new JMenuItem(Res.getString("menuitem.refresh")); + + joinRoomItem.setIcon(SparkRes.getImageIcon(SparkRes.DOOR_IMAGE)); + addRoomItem.setIcon(SparkRes.getImageIcon(SparkRes.ADD_BOOKMARK_ICON)); + createItem.setIcon(SparkRes.getImageIcon(SparkRes.SMALL_USER1_NEW)); + refreshItem.setIcon(SparkRes.getImageIcon(SparkRes.REFRESH_IMAGE)); + + popup.add(joinRoomItem); + popup.add(addRoomItem); + popup.add(createItem); + popup.add(refreshItem); + + + // Add Toolbar + final JPanel Hauptpanel = new JPanel(new BorderLayout()); + JPanel toolbar = new JPanel(new FlowLayout(FlowLayout.LEFT)); + JPanel pane_hiddenButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT)); + toolbar.add(joinRoomButton); + toolbar.add(addRoomButton); + toolbar.add(createButton); + toolbar.add(refreshButton); + pane_hiddenButtons.add(showHiddenButtons); + + Hauptpanel.add(toolbar, BorderLayout.WEST); + Hauptpanel.add(pane_hiddenButtons, BorderLayout.EAST); + this.add(Hauptpanel, BorderLayout.NORTH); + + createButton.addActionListener(this); + createItem.addActionListener(this); + joinRoomButton.addActionListener(this); + refreshButton.addActionListener(this); + + ResourceUtils.resButton(createButton, Res.getString("button.create.room")); + ResourceUtils.resButton(joinRoomButton, Res.getString("button.join.room")); + ResourceUtils.resButton(refreshButton, Res.getString("button.refresh")); + ResourceUtils.resButton(addRoomButton, Res.getString("button.bookmark.room")); + + refreshButton.setToolTipText(Res.getString("message.update.room.list")); + joinRoomButton.setToolTipText(Res.getString("message.join.conference.room")); + createButton.setToolTipText(Res.getString("message.create.or.join.room")); + + // Add Group Chat Table + roomsTable = new RoomList(); + + final JScrollPane pane = new JScrollPane(roomsTable); + pane.setBackground(Color.white); + pane.setForeground(Color.white); + this.setBackground(Color.white); + this.setForeground(Color.white); + pane.getViewport().setBackground(Color.white); + this.add(pane, BorderLayout.CENTER); + + chatManager = SparkManager.getChatManager(); + + joinRoomButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent actionEvent) { + joinSelectedRoom(); + } + }); + + addRoomButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent actionEvent) { + bookmarkRoom(serviceName); + } + }); + + refreshButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent actionEvent) { + refreshRoomList(serviceName); + } + }); + + joinRoomItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent actionEvent) { + joinSelectedRoom(); + } + }); + + addRoomItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent actionEvent) { + bookmarkRoom(serviceName); + } + }); + + refreshItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent actionEvent) { + refreshRoomList(serviceName); + } + }); + + showHiddenButtons.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent actionEvent) { + popup.show(showHiddenButtons, 0, showHiddenButtons.getHeight()); + } + }); + + + + joinRoomButton.setEnabled(false); + addRoomButton.setEnabled(false); + joinRoomItem.setEnabled(false); + addRoomItem.setEnabled(false); + + addTableListener(); + } + + private void refreshRoomList(final String serviceName) { + roomsTable.clearTable(); + SwingWorker worker = new SwingWorker() { + Collection result; + + public Object construct() { + result = getRoomsAndInfo(serviceName); + return result; + } + + public void finished() { + try { + for (Object aResult : result) { + RoomObject obj = (RoomObject) aResult; + addRoomToTable(obj.getRoomJID(), obj.getRoomName(), obj.getNumberOfOccupants()); + } + } + catch (Exception e) { + Log.error("Unable to retrieve room list and info.", e); + } + } + }; + + worker.start(); + + + } + + private Collection getRoomsAndInfo(final String serviceName) { + List roomList = new ArrayList(); + boolean stillSearchForOccupants = true; + try { + Collection result = getRoomList(serviceName); + try { + for (Object aResult : result) { + HostedRoom hostedRoom = (HostedRoom) aResult; + String roomName = hostedRoom.getName(); + String roomJID = hostedRoom.getJid(); + int numberOfOccupants = -1; + if (stillSearchForOccupants) { + RoomInfo roomInfo = null; + try { + roomInfo = MultiUserChat.getRoomInfo(SparkManager.getConnection(), roomJID); + } + catch (Exception e) { + // Nothing to do + } + + if (roomInfo != null) { + numberOfOccupants = roomInfo.getOccupantsCount(); + if (numberOfOccupants == -1) { + stillSearchForOccupants = false; + } + } else { + stillSearchForOccupants = false; + } + } + + RoomObject obj = new RoomObject(); + obj.setRoomJID(roomJID); + obj.setRoomName(roomName); + obj.setNumberOfOccupants(numberOfOccupants); + roomList.add(obj); + } + } + catch (Exception e) { + Log.error("Error setting up GroupChatTable", e); + } + } + catch (Exception e) { + System.err.println(e); + } + return roomList; + } + + private void bookmarkRoom(String serviceName) { + int selectedRow = roomsTable.getSelectedRow(); + if (-1 == selectedRow) { + JOptionPane.showMessageDialog(dlg, Res.getString("message.select.add.room.to.add"), Res.getString("title.group.chat"), JOptionPane.INFORMATION_MESSAGE); + return; + } + + final String roomJID = roomsTable.getValueAt(selectedRow, 2) + "@" + serviceName; + final String roomName = roomsTable.getValueAt(selectedRow, 1).toString(); + + // Check to see what type of room this is. + try { + final RoomInfo roomInfo = MultiUserChat.getRoomInfo(SparkManager.getConnection(), roomJID); + if (!roomInfo.isPersistent()) { + JOptionPane.showMessageDialog(dlg, Res.getString("message.bookmark.temporary.room.error"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE); + return; + } + } + catch (Exception e) { + // Do not return + } + + Tree serviceTree = conferences.getTree(); + JiveTreeNode rootNode = (JiveTreeNode)serviceTree.getModel().getRoot(); + + TreePath rootPath = serviceTree.findByName(serviceTree, new String[]{rootNode.toString(), serviceName}); + + + boolean isBookmarked = isBookmarked(roomJID); + + + if (!isBookmarked) { + JiveTreeNode node = (JiveTreeNode)serviceTree.getLastSelectedPathComponent(); + if (node == null) { + TreePath path = serviceTree.findByName(serviceTree, new String[]{rootNode.toString(), ConferenceServices.getDefaultServiceName()}); + node = (JiveTreeNode)path.getLastPathComponent(); + } + JiveTreeNode roomNode = new JiveTreeNode(roomName, false, SparkRes.getImageIcon(SparkRes.BOOKMARK_ICON)); + roomNode.setAssociatedObject(roomJID); + node.add(roomNode); + final DefaultTreeModel model = (DefaultTreeModel)serviceTree.getModel(); + model.nodeStructureChanged(node); + serviceTree.expandPath(rootPath); + roomsTable.getTableModel().setValueAt(new JLabel(SparkRes.getImageIcon(SparkRes.BOOKMARK_ICON)), selectedRow, 0); + addBookmarkUI(false); + + conferences.addBookmark(roomName, roomJID, false); + } + else { + // Remove bookmark + TreePath path = serviceTree.findByName(serviceTree, new String[]{rootNode.toString(), serviceName, roomName}); + JiveTreeNode node = (JiveTreeNode)path.getLastPathComponent(); + final DefaultTreeModel model = (DefaultTreeModel)serviceTree.getModel(); + model.removeNodeFromParent(node); + roomsTable.getTableModel().setValueAt(new JLabel(SparkRes.getImageIcon(SparkRes.BLANK_IMAGE)), selectedRow, 0); + addBookmarkUI(true); + + String jid = (String)node.getAssociatedObject(); + conferences.removeBookmark(jid); + } + } + + private void joinSelectedRoom() { + int selectedRow = roomsTable.getSelectedRow(); + if (-1 == selectedRow) { + JOptionPane.showMessageDialog(dlg, Res.getString("message.select.room.to.join"), Res.getString("title.group.chat"), JOptionPane.INFORMATION_MESSAGE); + return; + } + enterRoom(); + } + + private void addTableListener() { + roomsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { + public void valueChanged(ListSelectionEvent e) { + if (e.getValueIsAdjusting()) return; + + int selectedRow = roomsTable.getSelectedRow(); + if (selectedRow != -1) { + joinRoomButton.setEnabled(true); + joinRoomItem.setEnabled(true); + String roomJID = roomsTable.getValueAt(selectedRow, 2) + "@" + serviceName; + addRoomButton.setEnabled(true); + addRoomItem.setEnabled(true); + if (isBookmarked(roomJID)) { + addBookmarkUI(false); + } + else { + addBookmarkUI(true); + } + } + else { + joinRoomButton.setEnabled(false); + addRoomButton.setEnabled(false); + joinRoomItem.setEnabled(false); + addRoomItem.setEnabled(false); + addBookmarkUI(true); + } + } + }); + } + + /** + * Displays the ConferenceRoomBrowser. + */ + public void invoke() { + SwingWorker worker = new SwingWorker() { + Collection rooms; + + public Object construct() { + try { + rooms = getRoomList(serviceName); + } + catch (Exception e) { + Log.error("Unable to retrieve list of rooms.", e); + } + + return "OK"; + } + + public void finished() { + if (rooms == null) { + JOptionPane.showMessageDialog(conferences, Res.getString("message.conference.info.error"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE); + if (dlg != null) { + dlg.dispose(); + } + } + try { + for (Object room : rooms) { + HostedRoom hostedRoom = (HostedRoom) room; + 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) { + // Nothing to do + } + + 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.addComponentListener(this); + + /* looking up which bundle is used to set the size of the Window + (not using Localpreferences getLanguage() because sometimes language is not saved + in the properties file and so the method only returns an empty String) */ + if(Res.getBundle().getLocale().toString().equals("de")) + dlg.setSize(700, 400); + else + 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(); + } + } + }); + + // will need that, when the window is smaller then the buttons width... + setButtonsWidth(); + + showHiddenButtons.setVisible(false); + + 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 = 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 || e.getSource() == createItem) { + 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 = 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 if a problem occurs while getting the room list + */ + 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()); + form.setAnswer("muc#roomconfig_roomdesc", mucRoomDialog.getRoomTopic()); + + 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) { + for (Object o : conferences.getBookmarks()) { + BookmarkedConference bk = (BookmarkedConference) o; + 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; + } + } + + + public void componentHidden(ComponentEvent e) + { + // TODO Auto-generated method stub + + } + + public void componentMoved(ComponentEvent e) + { + // TODO Auto-generated method stub + + } + + public void componentResized(ComponentEvent e) + { + if(this.getWidth() <= (oneButtonWidth + 19)) + { + joinRoomButton.setVisible(false); + addRoomButton.setVisible(false); + createButton.setVisible(false); + refreshButton.setVisible(false); + + joinRoomItem.setVisible(true); + addRoomItem.setVisible(true); + createItem.setVisible(true); + refreshItem.setVisible(true); + + showHiddenButtons.setVisible(true); + } + + else if(this.getWidth() <= (twoButtonWidth + 19)) + { + joinRoomButton.setVisible(true); + addRoomButton.setVisible(false); + createButton.setVisible(false); + refreshButton.setVisible(false); + + joinRoomItem.setVisible(false); + addRoomItem.setVisible(true); + createItem.setVisible(true); + refreshItem.setVisible(true); + + showHiddenButtons.setVisible(true); + } + + else if(this.getWidth() <= (threeButtonWidth + 19)) + { + joinRoomButton.setVisible(true); + addRoomButton.setVisible(true); + createButton.setVisible(false); + refreshButton.setVisible(false); + + joinRoomItem.setVisible(false); + addRoomItem.setVisible(false); + createItem.setVisible(true); + refreshItem.setVisible(true); + + showHiddenButtons.setVisible(true); + } + + else if(this.getWidth() <= (allButtonWidth + 19)) + { + joinRoomButton.setVisible(true); + addRoomButton.setVisible(true); + createButton.setVisible(true); + refreshButton.setVisible(false); + + joinRoomItem.setVisible(false); + addRoomItem.setVisible(false); + createItem.setVisible(false); + refreshItem.setVisible(true); + + showHiddenButtons.setVisible(true); + } + + else if(this.getWidth() > (allButtonWidth + 19)) + { + joinRoomButton.setVisible(true); + addRoomButton.setVisible(true); + createButton.setVisible(true); + refreshButton.setVisible(true); + + joinRoomItem.setVisible(false); + addRoomItem.setVisible(false); + createItem.setVisible(false); + refreshItem.setVisible(false); + + showHiddenButtons.setVisible(false); + } + } + + public void componentShown(ComponentEvent e) + { + // TODO Auto-generated method stub + + } + + // will need that, when the window is smaller than the buttons width... + private void setButtonsWidth() + { + allButtonWidth = createButton.getWidth() + refreshButton.getWidth() + addRoomButton.getWidth() + joinRoomButton.getWidth(); + threeButtonWidth = createButton.getWidth() + addRoomButton.getWidth() + joinRoomButton.getWidth() + showHiddenButtons.getWidth(); + twoButtonWidth = addRoomButton.getWidth() + joinRoomButton.getWidth() + showHiddenButtons.getWidth(); + oneButtonWidth = joinRoomButton.getWidth() + showHiddenButtons.getWidth(); + } + +} + + diff --git a/src/java/org/jivesoftware/spark/ui/conferences/RoomCreationDialog.java b/src/java/org/jivesoftware/spark/ui/conferences/RoomCreationDialog.java index ac2939d9..094810af 100644 --- a/src/java/org/jivesoftware/spark/ui/conferences/RoomCreationDialog.java +++ b/src/java/org/jivesoftware/spark/ui/conferences/RoomCreationDialog.java @@ -1,251 +1,263 @@ -/** - * $RCSfile: ,v $ - * $Revision: $ - * $Date: $ - * - * Copyright (C) 2004-2010 Jive Software. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jivesoftware.spark.ui.conferences; - -import org.jivesoftware.resource.SparkRes; -import org.jivesoftware.resource.Res; -import org.jivesoftware.smack.XMPPException; -import org.jivesoftware.smackx.muc.MultiUserChat; -import org.jivesoftware.spark.SparkManager; -import org.jivesoftware.spark.component.TitlePanel; -import org.jivesoftware.spark.util.ModelUtil; -import org.jivesoftware.spark.util.ResourceUtils; -import org.jivesoftware.spark.util.log.Log; - -import javax.swing.JCheckBox; -import javax.swing.JDialog; -import javax.swing.JLabel; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JPasswordField; -import javax.swing.JTextField; - -import java.awt.BorderLayout; -import java.awt.Component; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; - -public class RoomCreationDialog extends JPanel { - private static final long serialVersionUID = -8391698290385575601L; - private JLabel nameLabel = new JLabel(); - private JLabel topicLabel = new JLabel(); - private JLabel passwordLabel = new JLabel(); - private JLabel confirmPasswordLabel = new JLabel(); - private JCheckBox permanentCheckBox = new JCheckBox(); - private JCheckBox privateCheckbox = new JCheckBox(); - private JTextField nameField = new JTextField(); - private JTextField topicField = new JTextField(); - private JPasswordField passwordField = new JPasswordField(); - private JPasswordField confirmPasswordField = new JPasswordField(); - private GridBagLayout gridBagLayout1 = new GridBagLayout(); - private MultiUserChat groupChat = null; - - public RoomCreationDialog() { - try { - jbInit(); - } - catch (Exception e) { - Log.error(e); - } - } - - private void jbInit() throws Exception { - this.setLayout(gridBagLayout1); - this.add(confirmPasswordField, new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); - this.add(passwordField, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); - this.add(topicField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); - this.add(nameField, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); - this.add(privateCheckbox, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); - this.add(permanentCheckBox, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); - this.add(confirmPasswordLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); - this.add(passwordLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); - this.add(topicLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 0)); - this.add(nameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); - - ResourceUtils.resLabel(nameLabel, nameField, Res.getString("label.room.name")); - ResourceUtils.resLabel(topicLabel, topicField, Res.getString("label.room.topic") + ":"); - ResourceUtils.resLabel(passwordLabel, passwordField, Res.getString("label.password") + ":"); - ResourceUtils.resLabel(confirmPasswordLabel, confirmPasswordField, Res.getString("label.confirm.password") + ":"); - ResourceUtils.resButton(permanentCheckBox, Res.getString("checkbox.permanent")); - ResourceUtils.resButton(privateCheckbox, Res.getString("checkbox.private.room")); - } - - public MultiUserChat createGroupChat(Component parent, final String serviceName) { - final JOptionPane pane; - final JDialog dlg; - - TitlePanel titlePanel; - - // Create the title panel for this dialog - titlePanel = new TitlePanel(Res.getString("title.create.or.join"), Res.getString("message.create.or.join.room"), SparkRes.getImageIcon(SparkRes.BLANK_24x24), 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("create"), Res.getString("close")}; - pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); - - mainPanel.add(pane, BorderLayout.CENTER); - - JOptionPane p = new JOptionPane(); - dlg = p.createDialog(parent, Res.getString("title.conference.rooms")); - dlg.pack(); - dlg.setSize(400, 350); - dlg.setContentPane(mainPanel); - dlg.setLocationRelativeTo(parent); - - - PropertyChangeListener changeListener = new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent e) { - Object o = pane.getValue(); - if (o instanceof Integer) { - dlg.setVisible(false); - return; - } - - String value = (String)pane.getValue(); - if (Res.getString("close").equals(value)) { - dlg.setVisible(false); - } - else if (Res.getString("create").equals(value)) { - boolean isValid = validatePanel(); - if (isValid) { - String room = nameField.getText().replaceAll(" ", "_") + "@" + serviceName; - try { - MultiUserChat.getRoomInfo(SparkManager.getConnection(), room); - //JOptionPane.showMessageDialog(dlg, "Room already exists. Please specify a unique room name.", "Room Exists", JOptionPane.ERROR_MESSAGE); - //pane.setValue(JOptionPane.UNINITIALIZED_VALUE); - pane.removePropertyChangeListener(this); - dlg.setVisible(false); - ConferenceUtils.joinConferenceRoom(room, room); - return; - } - catch (XMPPException e1) { - // Nothing to do - } - - groupChat = createGroupChat(nameField.getText(), serviceName); - if (groupChat == null) { - showError("Could not join chat " + nameField.getText()); - pane.setValue(JOptionPane.UNINITIALIZED_VALUE); - - } - else { - pane.removePropertyChangeListener(this); - dlg.setVisible(false); - } - } - else { - pane.setValue(JOptionPane.UNINITIALIZED_VALUE); - } - } - } - }; - - pane.addPropertyChangeListener(changeListener); - nameField.requestFocusInWindow(); - - dlg.setVisible(true); - dlg.toFront(); - dlg.requestFocus(); - - return groupChat; - } - - private boolean validatePanel() { - String roomName = nameField.getText(); - String password = new String(passwordField.getPassword()); - String confirmPassword = new String(confirmPasswordField.getPassword()); - boolean isPrivate = privateCheckbox.isSelected(); - - // Check for valid information - if (!ModelUtil.hasLength(roomName)) { - showError(Res.getString("message.specify.name.error")); - nameField.requestFocus(); - return false; - } - - if (isPrivate) { - if (!ModelUtil.hasLength(password)) { - showError(Res.getString("message.password.private.room.error")); - passwordField.requestFocus(); - return false; - } - - if (!ModelUtil.hasLength(confirmPassword)) { - showError(Res.getString("message.confirmation.password.error")); - confirmPasswordField.requestFocus(); - return false; - } - - if (!ModelUtil.areEqual(password, confirmPassword)) { - showError(Res.getString("message.passwords.no.match")); - passwordField.requestFocus(); - return false; - } - } - - return true; - } - - private MultiUserChat createGroupChat(String roomName, String serviceName) { - String room = roomName.replaceAll(" ", "_") + "@" + serviceName; - - // Create a group chat with valid information - return new MultiUserChat(SparkManager.getConnection(), room.toLowerCase()); - } - - - private void showError(String errorMessage) { - JOptionPane.showMessageDialog(this, errorMessage, Res.getString("title.error"), JOptionPane.ERROR_MESSAGE); - } - - public boolean isPrivate() { - return privateCheckbox.isSelected(); - } - - public boolean isPermanent() { - return permanentCheckBox.isSelected(); - } - - public boolean isPasswordProtected() { - String password = new String(passwordField.getPassword()); - if (password.length() > 0) { - return true; - } - - return false; - } - - public String getPassword() { - return new String(confirmPasswordField.getPassword()); - } - - public String getRoomName() { - return nameField.getText(); - } - +/** + * $RCSfile: ,v $ + * $Revision: $ + * $Date: $ + * + * Copyright (C) 2004-2010 Jive Software. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.spark.ui.conferences; + +import org.jivesoftware.resource.SparkRes; +import org.jivesoftware.resource.Res; +import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smackx.muc.MultiUserChat; +import org.jivesoftware.spark.SparkManager; +import org.jivesoftware.spark.component.TitlePanel; +import org.jivesoftware.spark.util.ModelUtil; +import org.jivesoftware.spark.util.ResourceUtils; +import org.jivesoftware.spark.util.log.Log; + +import javax.swing.JCheckBox; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JPasswordField; +import javax.swing.JTextField; + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; + +public class RoomCreationDialog extends JPanel { + private static final long serialVersionUID = -8391698290385575601L; + private JLabel nameLabel = new JLabel(); + private JLabel topicLabel = new JLabel(); + private JLabel passwordLabel = new JLabel(); + private JLabel confirmPasswordLabel = new JLabel(); + private JCheckBox permanentCheckBox = new JCheckBox(); + private JCheckBox privateCheckbox = new JCheckBox(); + private JTextField nameField = new JTextField(); + private JTextField topicField = new JTextField(); + private JPasswordField passwordField = new JPasswordField(); + private JPasswordField confirmPasswordField = new JPasswordField(); + private GridBagLayout gridBagLayout1 = new GridBagLayout(); + private MultiUserChat groupChat = null; + + public RoomCreationDialog() { + try { + jbInit(); + } + catch (Exception e) { + Log.error(e); + } + } + + private void jbInit() throws Exception { + this.setLayout(gridBagLayout1); + this.add(confirmPasswordField, new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); + this.add(passwordField, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); + this.add(topicField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); + this.add(nameField, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); + this.add(privateCheckbox, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); + this.add(permanentCheckBox, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); + this.add(confirmPasswordLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); + this.add(passwordLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); + this.add(topicLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 0)); + this.add(nameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); + + ResourceUtils.resLabel(nameLabel, nameField, Res.getString("label.room.name")); + ResourceUtils.resLabel(topicLabel, topicField, Res.getString("label.room.topic") + ":"); + ResourceUtils.resLabel(passwordLabel, passwordField, Res.getString("label.password") + ":"); + ResourceUtils.resLabel(confirmPasswordLabel, confirmPasswordField, Res.getString("label.confirm.password") + ":"); + ResourceUtils.resButton(permanentCheckBox, Res.getString("checkbox.permanent")); + ResourceUtils.resButton(privateCheckbox, Res.getString("checkbox.private.room")); + } + + public MultiUserChat createGroupChat(Component parent, final String serviceName) { + final JOptionPane pane; + final JDialog dlg; + + TitlePanel titlePanel; + + // Create the title panel for this dialog + titlePanel = new TitlePanel(Res.getString("title.create.or.join"), Res.getString("message.create.or.join.room"), SparkRes.getImageIcon(SparkRes.BLANK_24x24), 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("create"), Res.getString("close")}; + pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); + + mainPanel.add(pane, BorderLayout.CENTER); + + JOptionPane p = new JOptionPane(); + dlg = p.createDialog(parent, Res.getString("title.conference.rooms")); + dlg.pack(); + dlg.setSize(400, 350); + dlg.setContentPane(mainPanel); + dlg.setLocationRelativeTo(parent); + + + PropertyChangeListener changeListener = new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent e) { + Object o = pane.getValue(); + if (o instanceof Integer) { + dlg.setVisible(false); + return; + } + + String value = (String)pane.getValue(); + if (Res.getString("close").equals(value)) { + dlg.setVisible(false); + } + else if (Res.getString("create").equals(value)) { + boolean isValid = validatePanel(); + if (isValid) { + String room = nameField.getText().replaceAll(" ", "_") + "@" + serviceName; + try { + MultiUserChat.getRoomInfo(SparkManager.getConnection(), room); + //JOptionPane.showMessageDialog(dlg, "Room already exists. Please specify a unique room name.", "Room Exists", JOptionPane.ERROR_MESSAGE); + //pane.setValue(JOptionPane.UNINITIALIZED_VALUE); + pane.removePropertyChangeListener(this); + dlg.setVisible(false); + ConferenceUtils.joinConferenceRoom(room, room); + return; + } + catch (XMPPException e1) { + // Nothing to do + } + + groupChat = createGroupChat(nameField.getText(), serviceName); + if (groupChat == null) { + showError("Could not join chat " + nameField.getText()); + pane.setValue(JOptionPane.UNINITIALIZED_VALUE); + + } + else { + pane.removePropertyChangeListener(this); + dlg.setVisible(false); + } + } + else { + pane.setValue(JOptionPane.UNINITIALIZED_VALUE); + } + } + } + }; + + pane.addPropertyChangeListener(changeListener); + nameField.requestFocusInWindow(); + + dlg.setVisible(true); + dlg.toFront(); + dlg.requestFocus(); + + return groupChat; + } + + private boolean validatePanel() { + String roomName = nameField.getText(); + String password = new String(passwordField.getPassword()); + String confirmPassword = new String(confirmPasswordField.getPassword()); + boolean isPrivate = privateCheckbox.isSelected(); + + // Check for valid information + if (!ModelUtil.hasLength(roomName)) { + showError(Res.getString("message.specify.name.error")); + nameField.requestFocus(); + return false; + } + + if (isPrivate) { + if (!ModelUtil.hasLength(password)) { + showError(Res.getString("message.password.private.room.error")); + passwordField.requestFocus(); + return false; + } + + if (!ModelUtil.hasLength(confirmPassword)) { + showError(Res.getString("message.confirmation.password.error")); + confirmPasswordField.requestFocus(); + return false; + } + + if (!ModelUtil.areEqual(password, confirmPassword)) { + showError(Res.getString("message.passwords.no.match")); + passwordField.requestFocus(); + return false; + } + } + + return true; + } + + private MultiUserChat createGroupChat(String roomName, String serviceName) { + String room = roomName.replaceAll(" ", "_") + "@" + serviceName; + + // Create a group chat with valid information + return new MultiUserChat(SparkManager.getConnection(), room.toLowerCase()); + } + + + private void showError(String errorMessage) { + JOptionPane.showMessageDialog(this, errorMessage, Res.getString("title.error"), JOptionPane.ERROR_MESSAGE); + } + + public boolean isPrivate() { + return privateCheckbox.isSelected(); + } + + public boolean isPermanent() { + return permanentCheckBox.isSelected(); + } + + public boolean isPasswordProtected() { + String password = new String(passwordField.getPassword()); + if (password.length() > 0) { + return true; + } + + return false; + } + + public String getPassword() { + return new String(confirmPasswordField.getPassword()); + } + + /** + * Returns the Room name of the RoomCreationDialog + * + * @return The Room name + */ + public String getRoomName() { + return nameField.getText(); + } + /** + * Returns the Topic of the RoomCreationDialog + * @return The Rooms Topic + */ + public String getRoomTopic() { + return topicField.getText(); + } + } \ No newline at end of file