Search in sources :

Example 1 with ChatRoomButton

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

the class ChatRoomOpeningListener method chatRoomOpened.

@Override
public void chatRoomOpened(final ChatRoom room) {
    if (// Check for 1on1 Chat
    !(room instanceof ChatRoomImpl)) {
        return;
    }
    final ChatRoomButton sendGameButton = new ChatRoomButton("BS");
    room.getToolBar().addChatRoomButton(sendGameButton);
    final String opponentJID = ((ChatRoomImpl) room).getJID();
    sendGameButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final GameOfferPacket offer = new GameOfferPacket();
            offer.setTo(opponentJID);
            offer.setType(IQ.Type.get);
            room.getTranscriptWindow().insertCustomText(BsRes.getString("request"), false, false, Color.BLUE);
            try {
                SparkManager.getConnection().sendStanza(offer);
            } catch (SmackException.NotConnectedException e1) {
                Log.warning("Unable to send offer to " + opponentJID, e1);
            }
            SparkManager.getConnection().addAsyncStanzaListener(new StanzaListener() {

                @Override
                public void processPacket(Stanza stanza) {
                    GameOfferPacket answer = (GameOfferPacket) stanza;
                    answer.setStartingPlayer(offer.isStartingPlayer());
                    answer.setGameID(offer.getGameID());
                    String name = XmppStringUtils.parseLocalpart(opponentJID);
                    if (answer.getType() == IQ.Type.result) {
                        // ACCEPT
                        room.getTranscriptWindow().insertCustomText(BsRes.getString("accepted", name), false, false, Color.BLUE);
                        createWindow(answer, opponentJID);
                    } else {
                        // DECLINE
                        room.getTranscriptWindow().insertCustomText(BsRes.getString("declined", name), false, false, Color.RED);
                    }
                }
            }, new PacketIDFilter(offer.getPacketID()));
        }
    });
}
Also used : ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) GameOfferPacket(battleship.packets.GameOfferPacket) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter) ChatRoomButton(org.jivesoftware.spark.ui.ChatRoomButton)

Example 2 with ChatRoomButton

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

the class ReversiPlugin method addToolbarButton.

/**
 * Adds the Reversi toolbar button.
 */
private void addToolbarButton() {
    ChatManager manager = SparkManager.getChatManager();
    chatRoomListener = new ChatRoomListenerAdapter() {

        ImageIcon icon = ReversiRes.getImageIcon(ReversiRes.REVERSI_ICON);

        public void chatRoomOpened(final ChatRoom room) {
            if (!(room instanceof ChatRoomImpl)) {
                // Don't do anything if this is not a 1on1-Chat
                return;
            }
            ChatRoomButton button = new ChatRoomButton(icon);
            button.setToolTipText("Reversi");
            room.getToolBar().addChatRoomButton(button);
            // Add a button listener that sends out a game invite on a user
            // click.
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    // Show "requesting a game panel"
                    final JPanel request = new JPanel();
                    request.setLayout(new BorderLayout());
                    JPanel requestPanel = new JPanel() {

                        private static final long serialVersionUID = 4490592207923738251L;

                        protected void paintComponent(Graphics g) {
                            g.drawImage(icon.getImage(), 0, 0, null);
                        }
                    };
                    requestPanel.setPreferredSize(new Dimension(24, 24));
                    request.add(requestPanel, BorderLayout.WEST);
                    String opponentJID = ((ChatRoomImpl) room).getJID();
                    // TODO:
                    String opponentName = "[" + opponentJID + "]";
                    // convert
                    // to
                    // more
                    // readable
                    // name.
                    final JPanel content = new JPanel(new BorderLayout());
                    final JLabel label = new JLabel("Requesting a Reversi game with " + opponentName + ", please wait...");
                    content.add(label, BorderLayout.CENTER);
                    JPanel buttonPanel = new JPanel();
                    final JButton cancelButton = new JButton("Cancel");
                    cancelButton.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            GameOffer reply = new GameOffer();
                            reply.setTo(((ChatRoomImpl) room).getJID());
                            reply.setType(IQ.Type.error);
                            try {
                                SparkManager.getConnection().sendStanza(reply);
                            } catch (SmackException.NotConnectedException e1) {
                                Log.warning("Unable to send invitation cancellation to " + reply.getTo(), e1);
                            }
                            cancelButton.setText("Canceled");
                            cancelButton.setEnabled(false);
                        }
                    });
                    buttonPanel.add(cancelButton, BorderLayout.SOUTH);
                    content.add(buttonPanel, BorderLayout.SOUTH);
                    request.add(content, BorderLayout.CENTER);
                    room.getTranscriptWindow().addComponent(request);
                    final GameOffer offer = new GameOffer();
                    offer.setTo(opponentJID);
                    // Add a listener for a reply to our offer.
                    SparkManager.getConnection().addAsyncStanzaListener(new StanzaListener() {

                        public void processPacket(Stanza stanza) {
                            GameOffer offerReply = ((GameOffer) stanza);
                            if (offerReply.getType() == IQ.Type.result) {
                                // Remove the offer panel
                                room.getTranscriptWindow().remove(request);
                                content.remove(1);
                                label.setText("Starting game...");
                                // Show game board (using original offer!).
                                showReversiBoard(offer.getGameID(), room, offer.isStartingPlayer(), offerReply.getFrom());
                            } else if (offerReply.getType() == IQ.Type.error) {
                                cancelButton.setVisible(false);
                                JPanel userDeclinedPanel = new JPanel(new BorderLayout());
                                JLabel userDeclined = new JLabel("User declined...");
                                userDeclinedPanel.add(userDeclined, BorderLayout.SOUTH);
                                request.add(userDeclinedPanel, BorderLayout.SOUTH);
                            }
                        // TODO: Handle error case
                        }
                    }, new StanzaIdFilter(offer.getStanzaId()));
                    try {
                        SparkManager.getConnection().sendStanza(offer);
                    } catch (SmackException.NotConnectedException e1) {
                        Log.warning("Unable to send invitation to " + offer.getTo(), e1);
                    }
                }
            });
        }

        public void chatRoomClosed(ChatRoom room) {
            super.chatRoomClosed(room);
        // TODO: if game is in progress, close it down. What we need is
        // an API that lets us see
        // TODO: if there's a transcript alert currently there.
        }
    };
    manager.addChatRoomListener(chatRoomListener);
}
Also used : ImageIcon(javax.swing.ImageIcon) JPanel(javax.swing.JPanel) ActionEvent(java.awt.event.ActionEvent) Stanza(org.jivesoftware.smack.packet.Stanza) SmackException(org.jivesoftware.smack.SmackException) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) StanzaListener(org.jivesoftware.smack.StanzaListener) Dimension(java.awt.Dimension) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) Graphics(java.awt.Graphics) ChatRoomListenerAdapter(org.jivesoftware.spark.ui.ChatRoomListenerAdapter) ActionListener(java.awt.event.ActionListener) BorderLayout(java.awt.BorderLayout) StanzaIdFilter(org.jivesoftware.smack.filter.StanzaIdFilter) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) ChatManager(org.jivesoftware.spark.ChatManager) ChatRoomButton(org.jivesoftware.spark.ui.ChatRoomButton)

Example 3 with ChatRoomButton

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

the class PhoneManager method chatRoomOpened.

public void chatRoomOpened(final ChatRoom room) {
    if (!phones.isEmpty() && room instanceof ChatRoomImpl) {
        final ChatRoomImpl chatRoomImpl = (ChatRoomImpl) room;
        final ChatRoomButton dialButton = new ChatRoomButton(SparkRes.getImageIcon(SparkRes.DIAL_PHONE_IMAGE_24x24));
        dialButton.setToolTipText(Res.getString("tooltip.place.voice.call"));
        final List<Action> actions = new ArrayList<>();
        SwingWorker actionWorker = new SwingWorker() {

            public Object construct() {
                for (Phone phone : phones) {
                    final Collection<Action> phoneActions = phone.getPhoneActions(chatRoomImpl.getParticipantJID());
                    if (phoneActions != null) {
                        for (Action action : phoneActions) {
                            actions.add(action);
                        }
                    }
                }
                return actions;
            }

            public void finished() {
                if (!actions.isEmpty()) {
                    room.addChatRoomButton(dialButton, true);
                }
            }
        };
        actionWorker.start();
        dialButton.addMouseListener(new MouseAdapter() {

            public void mousePressed(final MouseEvent e) {
                SwingWorker worker = new SwingWorker() {

                    public Object construct() {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e1) {
                            e1.printStackTrace();
                        }
                        return true;
                    }

                    public void finished() {
                        // Handle actions.
                        if (actions.size() > 0) {
                            // Display PopupMenu
                            final JPopupMenu menu = new JPopupMenu();
                            for (Action action : actions) {
                                menu.add(action);
                            }
                            menu.show(dialButton, e.getX(), e.getY());
                        }
                    }
                };
                worker.start();
            }
        });
    }
}
Also used : Action(javax.swing.Action) MouseEvent(java.awt.event.MouseEvent) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MouseAdapter(java.awt.event.MouseAdapter) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) JPopupMenu(javax.swing.JPopupMenu) SwingWorker(org.jivesoftware.spark.util.SwingWorker) ChatRoomButton(org.jivesoftware.spark.ui.ChatRoomButton)

Example 4 with ChatRoomButton

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

the class TicTacToePlugin method addButtonToToolBar.

/**
 * Add the TTT-Button to every opening Chatroom
 * and create Listeners for it
 */
private void addButtonToToolBar() {
    _chatRoomListener = new ChatRoomListenerAdapter() {

        @Override
        public void chatRoomOpened(final ChatRoom room) {
            if (!(room instanceof ChatRoomImpl)) {
                // Don't do anything if this is not a 1on1-Chat
                return;
            }
            final ChatRoomButton sendGameButton = new ChatRoomButton(buttonimg);
            room.getToolBar().addChatRoomButton(sendGameButton);
            final String opponentJID = ((ChatRoomImpl) room).getJID();
            sendGameButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    if (_currentInvitations.contains(XmppStringUtils.parseBareJid(opponentJID))) {
                        return;
                    }
                    final GameOfferPacket offer = new GameOfferPacket();
                    offer.setTo(opponentJID);
                    offer.setType(IQ.Type.get);
                    _currentInvitations.add(XmppStringUtils.parseBareJid(opponentJID));
                    room.getTranscriptWindow().insertCustomText(TTTRes.getString("ttt.request.sent"), false, false, Color.BLUE);
                    try {
                        SparkManager.getConnection().sendStanza(offer);
                    } catch (SmackException.NotConnectedException e1) {
                        Log.warning("Unable to send offer to " + opponentJID, e1);
                    }
                    SparkManager.getConnection().addAsyncStanzaListener(new StanzaListener() {

                        @Override
                        public void processPacket(Stanza stanza) {
                            GameOfferPacket answer = (GameOfferPacket) stanza;
                            answer.setStartingPlayer(offer.isStartingPlayer());
                            answer.setGameID(offer.getGameID());
                            if (answer.getType() == IQ.Type.result) {
                                // ACCEPT
                                _currentInvitations.remove(XmppStringUtils.parseBareJid(opponentJID));
                                room.getTranscriptWindow().insertCustomText(TTTRes.getString("ttt.request.accept"), false, false, Color.BLUE);
                                createTTTWindow(answer, opponentJID);
                            } else {
                                // DECLINE
                                room.getTranscriptWindow().insertCustomText(TTTRes.getString("ttt.request.decline"), false, false, Color.RED);
                                _currentInvitations.remove(XmppStringUtils.parseBareJid(opponentJID));
                            }
                        }
                    }, new PacketIDFilter(offer.getPacketID()));
                }
            });
        }

        @Override
        public void chatRoomClosed(ChatRoom room) {
            if (room instanceof ChatRoomImpl) {
                ChatRoomImpl cri = (ChatRoomImpl) room;
                _currentInvitations.remove(cri.getParticipantJID());
            }
        }
    };
    SparkManager.getChatManager().addChatRoomListener(_chatRoomListener);
}
Also used : ActionEvent(java.awt.event.ActionEvent) SmackException(org.jivesoftware.smack.SmackException) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) ChatRoomListenerAdapter(org.jivesoftware.spark.ui.ChatRoomListenerAdapter) ActionListener(java.awt.event.ActionListener) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) GameOfferPacket(tic.tac.toe.packet.GameOfferPacket) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter) ChatRoomButton(org.jivesoftware.spark.ui.ChatRoomButton)

Aggregations

ChatRoomButton (org.jivesoftware.spark.ui.ChatRoomButton)4 ChatRoomImpl (org.jivesoftware.spark.ui.rooms.ChatRoomImpl)4 ActionEvent (java.awt.event.ActionEvent)3 ActionListener (java.awt.event.ActionListener)3 StanzaListener (org.jivesoftware.smack.StanzaListener)3 Stanza (org.jivesoftware.smack.packet.Stanza)3 SmackException (org.jivesoftware.smack.SmackException)2 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)2 ChatRoom (org.jivesoftware.spark.ui.ChatRoom)2 ChatRoomListenerAdapter (org.jivesoftware.spark.ui.ChatRoomListenerAdapter)2 GameOfferPacket (battleship.packets.GameOfferPacket)1 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 Graphics (java.awt.Graphics)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Action (javax.swing.Action)1 ImageIcon (javax.swing.ImageIcon)1