Search in sources :

Example 11 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class ReversiPlugin method showInvitationAlert.

/**
 * Display an alert that allows the user to accept or reject a game
 * invitation.
 *
 * @param invitation
 *            the game invitation.
 */
private void showInvitationAlert(final GameOffer invitation) {
    // Got an offer to start a new game. So, make sure that a chat is
    // started with the other
    // user and show an invite panel.
    final ChatRoom room = SparkManager.getChatManager().getChatRoom(XmppStringUtils.parseBareJid(invitation.getFrom()));
    inviteAlert = new JPanel();
    inviteAlert.setLayout(new BorderLayout());
    JPanel invitePanel = new JPanel() {

        private static final long serialVersionUID = 5942001917654498678L;

        protected void paintComponent(Graphics g) {
            ImageIcon imageIcon = ReversiRes.getImageIcon(ReversiRes.REVERSI_ICON);
            Image image = imageIcon.getImage();
            g.drawImage(image, 0, 0, null);
        }
    };
    invitePanel.setPreferredSize(new Dimension(24, 24));
    inviteAlert.add(invitePanel, BorderLayout.WEST);
    JPanel content = new JPanel(new BorderLayout());
    // TODO: convert to more
    String opponentName = invitation.getFrom();
    // readable name.
    content.add(new JLabel(opponentName + " is requesting a Reversi game ..."), BorderLayout.CENTER);
    JPanel buttonPanel = new JPanel();
    // The accept button. When clicked, accept the game offer.
    final JButton acceptButton = new JButton("Accept");
    final JButton declineButton = new JButton("Decline");
    acceptButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // Accept the game offer by sending a positive reply packet.
            GameOffer reply = new GameOffer();
            reply.setTo(invitation.getFrom());
            reply.setStanzaId(invitation.getStanzaId());
            reply.setType(IQ.Type.result);
            try {
                SparkManager.getConnection().sendStanza(reply);
            } catch (SmackException.NotConnectedException e1) {
                Log.warning("Unable to accept game offer from " + invitation.getFrom(), e1);
            }
            // Hide the response panel. TODO: make this work.
            room.getTranscriptWindow().remove(inviteAlert);
            inviteAlert.remove(1);
            inviteAlert.add(new JLabel("Starting game..."), BorderLayout.CENTER);
            declineButton.setEnabled(false);
            acceptButton.setEnabled(false);
            // Remove the invitation from the map.
            gameInvitations.remove(invitation.getFrom());
            // Show the game board.
            showReversiBoard(invitation.getGameID(), room, !invitation.isStartingPlayer(), invitation.getFrom());
        }
    });
    buttonPanel.add(acceptButton);
    // The decline button. When clicked, reject the game offer.
    declineButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // Reject the game offer by sending an error packet.
            GameOffer reply = new GameOffer();
            reply.setTo(invitation.getFrom());
            reply.setStanzaId(invitation.getPacketID());
            reply.setType(IQ.Type.error);
            try {
                SparkManager.getConnection().sendStanza(reply);
            } catch (SmackException.NotConnectedException e1) {
                Log.warning("Unable to decline game offer from " + invitation.getFrom(), e1);
            }
            // Hide the response panel. TODO: make this work.
            room.getTranscriptWindow().remove(inviteAlert);
            declineButton.setVisible(false);
            acceptButton.setVisible(false);
            // Remove the invitation from the map.
            gameInvitations.remove(invitation.getFrom());
        }
    });
    buttonPanel.add(declineButton);
    content.add(buttonPanel, BorderLayout.SOUTH);
    inviteAlert.add(content, BorderLayout.CENTER);
    // Add the invitation to the Map of invites. If there's a pending
    // invite, remove it
    // before adding the new one (possible if the opponent sends two invites
    // in a row).
    Object oldInvitation = gameInvitations.put(invitation.getFrom(), inviteAlert);
    if (oldInvitation != null) {
    // TODO: clean it up by removing it from the transcript window.
    }
    // Add the response panel to the transcript window.
    room.getTranscriptWindow().addComponent(inviteAlert);
}
Also used : JPanel(javax.swing.JPanel) ImageIcon(javax.swing.ImageIcon) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) Image(java.awt.Image) Graphics(java.awt.Graphics) BorderLayout(java.awt.BorderLayout) ActionListener(java.awt.event.ActionListener) ChatRoom(org.jivesoftware.spark.ui.ChatRoom)

Example 12 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class TicTacToePlugin method showInvitationAlert.

/**
 * insert the Invitation Dialog into the Chat
 *
 * @param invitation
 */
private void showInvitationAlert(final GameOfferPacket invitation) {
    invitation.setType(IQ.Type.result);
    invitation.setTo(invitation.getFrom());
    final ChatRoom room = SparkManager.getChatManager().getChatRoom(XmppStringUtils.parseBareJid(invitation.getFrom()));
    String name = XmppStringUtils.parseLocalpart(invitation.getFrom());
    final JPanel panel = new JPanel();
    JLabel text = new JLabel(TTTRes.getString("ttt.game.request", name));
    JLabel game = new JLabel(TTTRes.getString("ttt.game.name"));
    game.setFont(new Font("Dialog", Font.BOLD, 24));
    game.setForeground(Color.RED);
    JButton accept = new JButton(Res.getString("button.accept").replace("&", ""));
    JButton decline = new JButton(Res.getString("button.decline").replace("&", ""));
    panel.add(text);
    panel.add(game);
    panel.add(accept);
    panel.add(decline);
    room.getTranscriptWindow().addComponent(panel);
    accept.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                SparkManager.getConnection().sendStanza(invitation);
            } catch (SmackException.NotConnectedException e1) {
                Log.warning("Unable to send invitation accept to " + invitation.getTo(), e1);
            }
            invitation.setStartingPlayer(!invitation.isStartingPlayer());
            createTTTWindow(invitation, invitation.getFrom());
            panel.remove(3);
            panel.remove(2);
            panel.repaint();
            panel.revalidate();
        }
    });
    decline.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            invitation.setType(IQ.Type.error);
            try {
                SparkManager.getConnection().sendStanza(invitation);
            } catch (SmackException.NotConnectedException e1) {
                Log.warning("Unable to send invitation decline to " + invitation.getTo(), e1);
            }
            panel.remove(3);
            panel.remove(2);
            panel.repaint();
            panel.revalidate();
        }
    });
}
Also used : JPanel(javax.swing.JPanel) ActionListener(java.awt.event.ActionListener) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) Font(java.awt.Font)

Example 13 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class UserSearchResults method openChatRoom.

private void openChatRoom(int row) {
    String jid = (String) resultsTable.getValueAt(row, 0);
    String nickname = XmppStringUtils.parseLocalpart(jid);
    TableColumn column;
    try {
        column = resultsTable.getColumn("nick");
        int col = column.getModelIndex();
        nickname = (String) resultsTable.getValueAt(row, col);
        if (!ModelUtil.hasLength(nickname)) {
            nickname = XmppStringUtils.parseLocalpart(jid);
        }
    } catch (Exception e1) {
    // Ignore e1
    }
    ChatManager chatManager = SparkManager.getChatManager();
    ChatRoom chatRoom = chatManager.createChatRoom(jid, nickname, nickname);
    ChatContainer chatRooms = chatManager.getChatContainer();
    chatRooms.activateChatRoom(chatRoom);
}
Also used : ChatContainer(org.jivesoftware.spark.ui.ChatContainer) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) TableColumn(javax.swing.table.TableColumn) ChatManager(org.jivesoftware.spark.ChatManager)

Example 14 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class ConversationHistoryPlugin method initialize.

public void initialize() {
    transcriptDir = new File(SparkManager.getUserDirectory(), "transcripts");
    conFile = new File(transcriptDir, "conversations.xml");
    contacts = new JList(model);
    contacts.setCellRenderer(new InternalRenderer());
    window = new Window(SparkManager.getMainWindow());
    final JPanel mainPanel = new JPanel(new BorderLayout());
    final JLabel titleLabel = new JLabel(Res.getString("label.recent.conversation"));
    titleLabel.setFont(new Font("Dialog", Font.BOLD, 11));
    titleLabel.setHorizontalAlignment(JLabel.CENTER);
    mainPanel.add(titleLabel, BorderLayout.NORTH);
    mainPanel.add(contacts, BorderLayout.CENTER);
    mainPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
    window.add(mainPanel);
    // Add Listeners
    contacts.addMouseListener(new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {
            if (SwingUtilities.isRightMouseButton(e)) {
                contacts.setSelectedIndex(contacts.locationToIndex(e.getPoint()));
                String user = jidMap.get(contacts.getSelectedValue());
                ContactItem contact = SparkManager.getContactList().getContactItemByJID(user);
                SparkManager.getContactList().setSelectedUser(contact.getJID());
                SparkManager.getContactList().showPopup(contacts, e, contact);
            }
            if (e.getClickCount() == 2) {
                final JLabel label = (JLabel) contacts.getSelectedValue();
                String user = jidMap.get(label);
                if (user != null) {
                    final String contactUsername = SparkManager.getUserManager().getUserNicknameFromJID(user);
                    SparkManager.getChatManager().activateChat(user, contactUsername);
                    window.dispose();
                }
            }
        }
    });
    contacts.addKeyListener(new KeyAdapter() {

        public void keyReleased(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_ENTER) {
                final JLabel label = (JLabel) contacts.getSelectedValue();
                String user = jidMap.get(label);
                if (user != null) {
                    final String contactUsername = SparkManager.getUserManager().getUserNicknameFromJID(user);
                    SparkManager.getChatManager().activateChat(user, contactUsername);
                    window.dispose();
                }
            } else if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
                window.dispose();
            }
        }
    });
    contacts.addFocusListener(new FocusListener() {

        public void focusGained(FocusEvent e) {
        }

        public void focusLost(FocusEvent e) {
            window.dispose();
        }
    });
    // Load Previous History
    loadPreviousHistory();
    // Add Keymapping to ContactList
    SparkManager.getMainWindow().getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_E, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "historyPeople");
    SparkManager.getMainWindow().getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_E, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "historyPeople");
    SparkManager.getMainWindow().getRootPane().getActionMap().put("historyPeople", new AbstractAction("historyPeople") {

        private static final long serialVersionUID = 2465628887318732082L;

        public void actionPerformed(ActionEvent e) {
            // Show History Popup
            showHistoryPopup();
        }
    });
    // Persist order of conversations.
    SparkManager.getChatManager().addMessageFilter(new MessageFilter() {

        public void filterOutgoing(ChatRoom room, Message message) {
            addUserToHistory(room);
        }

        public void filterIncoming(ChatRoom room, Message message) {
            addUserToHistory(room);
        }
    });
}
Also used : Message(org.jivesoftware.smack.packet.Message) ContactItem(org.jivesoftware.spark.ui.ContactItem) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) MessageFilter(org.jivesoftware.spark.ui.MessageFilter)

Example 15 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class JinglePlugin method placeCall.

public void placeCall(String jid) throws SmackException {
    // cancel call request if no Media Locator available
    if (PhoneManager.isUseStaticLocator() && PhoneManager.isUsingMediaLocator()) {
        return;
    }
    PhoneManager.setUsingMediaLocator(true);
    jid = SparkManager.getUserManager().getFullJID(jid);
    ChatRoom room = SparkManager.getChatManager().getChatRoom(XmppStringUtils.parseBareJid(jid));
    if (JingleStateManager.getInstance().getJingleRoomState(room) != null) {
        return;
    }
    SparkManager.getChatManager().getChatContainer().activateChatRoom(room);
    // Create a new Jingle Call with a full JID
    JingleSession session = null;
    try {
        session = jingleManager.createOutgoingJingleSession(jid);
    } catch (XMPPException e) {
        Log.error(e);
    }
    TranscriptWindow transcriptWindow = room.getTranscriptWindow();
    StyledDocument doc = (StyledDocument) transcriptWindow.getDocument();
    Style style = doc.addStyle("StyleName", null);
    OutgoingCall outgoingCall = new OutgoingCall();
    outgoingCall.handleOutgoingCall(session, room, jid);
    StyleConstants.setComponent(style, outgoingCall);
    // Insert the image at the end of the text
    try {
        doc.insertString(doc.getLength(), "ignored text", style);
        doc.insertString(doc.getLength(), "\n", null);
    } catch (BadLocationException e) {
        Log.error(e);
    }
    room.scrollToBottom();
}
Also used : ChatRoom(org.jivesoftware.spark.ui.ChatRoom) TranscriptWindow(org.jivesoftware.spark.ui.TranscriptWindow) StyledDocument(javax.swing.text.StyledDocument) JingleSession(org.jivesoftware.smackx.jingleold.JingleSession) Style(javax.swing.text.Style) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

ChatRoom (org.jivesoftware.spark.ui.ChatRoom)45 ChatManager (org.jivesoftware.spark.ChatManager)10 ChatRoomImpl (org.jivesoftware.spark.ui.rooms.ChatRoomImpl)10 ActionEvent (java.awt.event.ActionEvent)8 Message (org.jivesoftware.smack.packet.Message)8 ChatRoomNotFoundException (org.jivesoftware.spark.ui.ChatRoomNotFoundException)8 ContactItem (org.jivesoftware.spark.ui.ContactItem)7 Presence (org.jivesoftware.smack.packet.Presence)6 ChatRoomListenerAdapter (org.jivesoftware.spark.ui.ChatRoomListenerAdapter)6 ActionListener (java.awt.event.ActionListener)5 MouseEvent (java.awt.event.MouseEvent)5 Date (java.util.Date)5 JButton (javax.swing.JButton)5 JLabel (javax.swing.JLabel)5 JPanel (javax.swing.JPanel)5 SmackException (org.jivesoftware.smack.SmackException)5 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)5 SimpleDateFormat (java.text.SimpleDateFormat)4 JPopupMenu (javax.swing.JPopupMenu)4 SparkManager (org.jivesoftware.spark.SparkManager)4