use of org.jivesoftware.smack.packet.Stanza 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.smack.packet.Stanza 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);
}
use of org.jivesoftware.smack.packet.Stanza in project Spark by igniterealtime.
the class RawPacketSender method actionPerformed.
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(_sendButton)) {
Stanza stanza = new Stanza() {
@Override
public String toXML() {
return _inputarea.getText();
}
};
try {
SparkManager.getConnection().sendStanza(stanza);
_textarea.append("\n" + _inputarea.getText());
} catch (Exception exc) {
}
} else if (e.getSource().equals(_clear)) {
_textarea.setText("");
}
}
use of org.jivesoftware.smack.packet.Stanza in project Spark by igniterealtime.
the class BattleshipPlugin method initialize.
@Override
public void initialize() {
ProviderManager.addIQProvider(GameOfferPacket.ELEMENT_NAME, GameOfferPacket.NAMESPACE, GameOfferPacket.class);
ProviderManager.addExtensionProvider(MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE, MovePacket.class);
ProviderManager.addExtensionProvider(MoveAnswerPacket.ELEMENT_NAME, MoveAnswerPacket.NAMESPACE, MoveAnswerPacket.class);
_gameofferListener = new StanzaListener() {
@Override
public void processPacket(Stanza stanza) {
GameOfferPacket invitation = (GameOfferPacket) stanza;
if (invitation.getType() == IQ.Type.get) {
showInvitationInChat(invitation);
}
}
};
SparkManager.getConnection().addAsyncStanzaListener(_gameofferListener, new StanzaTypeFilter(GameOfferPacket.class));
_chatRoomListener = new ChatRoomOpeningListener();
SparkManager.getChatManager().addChatRoomListener(_chatRoomListener);
}
use of org.jivesoftware.smack.packet.Stanza in project spring-integration by spring-projects.
the class UriVariableTests method testInt2720XmppUriVariables.
@Test
public void testInt2720XmppUriVariables() throws Exception {
willThrow(new WebServiceIOException("intentional")).given(this.xmppConnection).sendStanza(Mockito.any(Stanza.class));
Message<?> message = MessageBuilder.withPayload("<spring/>").setHeader("to", "user").build();
try {
this.inputXmpp.send(message);
} catch (MessageHandlingException e) {
// expected
Class<?> causeType = e.getCause().getClass();
// offline
assertTrue(WebServiceIOException.class.equals(causeType));
}
ArgumentCaptor<Stanza> argument = ArgumentCaptor.forClass(Stanza.class);
Mockito.verify(this.xmppConnection).sendStanza(argument.capture());
assertEquals("user@jabber.org", argument.getValue().getTo().toString());
assertEquals("xmpp:user@jabber.org", this.interceptor.getLastUri().toString());
}
Aggregations