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()));
}
});
}
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);
}
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();
}
});
}
}
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);
}
Aggregations