@Override
public void handle(final MessageEvent e) {
Platform.runLater(new Runnable() {
@Override
public void run() {
Jid chatPartner = chatSession.getChatPartner().asBareJid();
ChatWindow chatWindow = windows.get(chatPartner);
if (chatWindow == null) {
chatWindow = new ChatWindow(chatPartner, xmppSession);
windows.put(chatPartner, chatWindow);
}
chatWindow.chatSession = chatSession;
chatWindow.show();
chatWindow.appendMessage(e.getMessage());
}
});
}
});
}
});
xmppSession.getRosterManager().addRosterListener(new RosterListener() {
@Override
public void rosterChanged(final RosterEvent e) {
Platform.runLater(new Runnable() {
@Override
public void run() {
for (Contact contact : e.getAddedContacts()) {
ContactItem contactItem1 = new ContactItem(contact);
contactItems.add(contactItem1);
contactMap.put(contact, contactItem1);
}
for (Contact contact : e.getUpdatedContacts()) {
ContactItem contactItem1 = contactMap.get(contact);
contactItem1.contact.set(contact);
}
for (Contact contact : e.getRemovedContacts()) {
contactItems.remove(contactMap.remove(contact));
}
}
});
}
});
xmppSession.addPresenceListener(new PresenceListener() {
@Override
public void handle(final PresenceEvent e) {
if (e.isIncoming()) {
if (e.getPresence().isAvailable()) {
Platform.runLater(new Runnable() {
@Override
public void run() {
Presence presence = e.getPresence();
Contact contact = xmppSession.getRosterManager().getContact(presence.getFrom());
if (contact != null) {
ContactItem contactItem1 = contactMap.get(contact);
contactItem1.presence.set(presence);
FXCollections.sort(contactItems);
}
}
});
} else if (e.getPresence().getType() == Presence.Type.SUBSCRIBE) {
xmppSession.getPresenceManager().denySubscription(e.getPresence().getFrom());
}
}
}
});
RpcManager rpcManager = xmppSession.getExtensionManager(RpcManager.class);
rpcManager.setRpcHandler(new RpcHandler() {
@Override
public Value process(Jid requester, String methodName, List<Value> parameters) throws RpcException {
if (methodName.equals("examples.getStateName")) {
if (!parameters.isEmpty()) {
if (parameters.get(0).getAsInteger() == 6) {
return new Value("Colorado");
}
}
}
throw new RpcException(123, "Invalid method name or parameter.");
}
});
AvatarManager avatarManager = xmppSession.getExtensionManager(AvatarManager.class);
avatarManager.addAvatarChangeListener(new AvatarChangeListener() {
@Override
public void avatarChanged(final AvatarChangeEvent e) {
Platform.runLater(new Runnable() {
@Override
public void run() {
Contact contact = xmppSession.getRosterManager().getContact(e.getContact());
if (contact != null) {
ContactItem contactItem = contactMap.get(contact);
if (contactItem != null) {
contactItem.avatar.set(e.getAvatar());
}
}
}
});
}
});
MessageDeliveryReceiptsManager messageDeliveryReceiptsManager = xmppSession.getExtensionManager(MessageDeliveryReceiptsManager.class);
messageDeliveryReceiptsManager.addMessageDeliveredListener(new MessageDeliveredListener() {
@Override
public void messageDelivered(MessageDeliveredEvent e) {
System.out.println("Message delivered: " + e.getMessageId());
}
});
xmppSession.getExtensionManager(EntityCapabilitiesManager.class).setEnabled(true);
GeoLocationManager geoLocationManager = xmppSession.getExtensionManager(GeoLocationManager.class);
geoLocationManager.addGeoLocationListener(new GeoLocationListener() {
@Override
public void geoLocationUpdated(GeoLocationEvent e) {
int i = 0;
}
});
SoftwareVersionManager softwareVersionManager = xmppSession.getExtensionManager(SoftwareVersionManager.class);
softwareVersionManager.setSoftwareVersion(new SoftwareVersion("Babbler", "0.1"));
final FileTransferManager fileTransferManager = xmppSession.getExtensionManager(FileTransferManager.class);
fileTransferManager.addFileTransferOfferListener(new FileTransferOfferListener() {
@Override
public void fileTransferOffered(FileTransferOfferEvent e) {
try {
OutputStream outputStream = new FileOutputStream("test222.png");
//e.reject();
final FileTransfer fileTransfer = e.accept(outputStream);
fileTransfer.transfer();
AnimationTimer animationTimer = new AnimationTimer() {
@Override
public void handle(long now) {
System.out.println(fileTransfer.getProgress());
if (fileTransfer.isDone()) {
stop();
}
}
};
animationTimer.start();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
try {
xmppSession.connect();
xmppSession.login(txtUser.getText(), txtPassword.getText(), "test");
//xmppSession.loginAnonymously();
Presence presence = new Presence();
xmppSession.send(presence);
//xmppSession.getRosterManager().requestRoster();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
});
ListView<ContactItem> listView = new ListView<>();
listView.setCellFactory(new Callback<ListView<ContactItem>, ListCell<ContactItem>>() {
@Override
public ListCell<ContactItem> call(ListView<ContactItem> contactListView) {
final ListCell<ContactItem> listCell = new ListCell<ContactItem>() {
@Override
protected void updateItem(final ContactItem item, boolean empty) {
super.updateItem(item, empty);
setGraphic(null);
setContextMenu(null);
if (item != null) {
final Jid user = item.contact.get().getJid();
if (item.avatar.get() == null) {
final Task<byte[]> task = new Task<byte[]>() {
@Override
protected byte[] call() throws Exception {
AvatarManager avatarManager = xmppSession.getExtensionManager(AvatarManager.class);
return avatarManager.getAvatar(user);
}
};
task.stateProperty().addListener(new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State state, Worker.State state2) {
switch (state2) {
case SUCCEEDED:
byte[] avatar = task.getValue();
if (avatar != null) {
item.avatar.set(avatar);
}
}
}
});
new Thread(task).start();
}
setGraphic(new ContactItemView(item));
ContextMenu contextMenu = new ContextMenu();
MenuItem lastActivityMenuItem = new MenuItem("Get last activity");
lastActivityMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
LastActivityManager lastActivityManager = xmppSession.getExtensionManager(LastActivityManager.class);
try {
lastActivityManager.getLastActivity(item.contact.get().getJid().withResource("test"));
} catch (XmppException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
});
MenuItem pingMenuItem = new MenuItem("Ping");
pingMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
PingManager pingManager = xmppSession.getExtensionManager(PingManager.class);
try {
pingManager.pingServer();
} catch (XmppException e) {
e.printStackTrace();
}
}
});
MenuItem searchMenuItem = new MenuItem("Search");
searchMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
SearchManager searchManager = xmppSession.getExtensionManager(SearchManager.class);
try {
Search search = new Search("22*", null, null, null);
searchManager.discoverSearchFields(new Jid("search.dev"));
Search result = searchManager.search(search, new Jid("search.dev"));
for (Search.Item item : result.getItems()) {
System.out.println(item.getJid());
}
} catch (XmppException e) {
e.printStackTrace();
}
}
});
MenuItem softwareVersionItem = new MenuItem("Get Software Version");
softwareVersionItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
SoftwareVersionManager softwareVersionManager = xmppSession.getExtensionManager(SoftwareVersionManager.class);
try {
SoftwareVersion softwareVersion = softwareVersionManager.getSoftwareVersion(item.contact.get().getJid());
if (softwareVersion != null)
System.out.println(softwareVersion.getName());
} catch (XmppException e) {
e.printStackTrace();
}
}
});
MenuItem serviceDiscoveryMenuItem = new MenuItem("Discover Info");
serviceDiscoveryMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
ServiceDiscoveryManager serviceDiscoveryManager = xmppSession.getExtensionManager(ServiceDiscoveryManager.class);
try {
Jid jid = new Jid(item.contact.get().getJid().getLocal(), item.contact.get().getJid().getDomain());
InfoNode infoNode = serviceDiscoveryManager.discoverInformation(null);
int i = 0;
} catch (XmppException e) {
e.printStackTrace();
}
}
});
MenuItem vCardItem = new MenuItem("Get VCard");
vCardItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
VCardManager vCardManager = xmppSession.getExtensionManager(VCardManager.class);
try {
Jid jid = new Jid(item.contact.get().getJid().getLocal(), item.contact.get().getJid().getDomain());
VCard vCard = vCardManager.getVCard(jid);
int i = 0;
} catch (XmppException e) {
e.printStackTrace();
}
}
});
MenuItem storeAnnotationsItems = new MenuItem("Store annotations");
storeAnnotationsItems.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
PrivateDataManager privateDataManager = xmppSession.getExtensionManager(PrivateDataManager.class);
try {
List<Annotation.Note> notes = new ArrayList<>();
notes.add(new Annotation.Note("Hallo", item.contact.get().getJid()));
privateDataManager.storeData(new Annotation(notes));
} catch (XmppException e) {
e.printStackTrace();
}
}
});
MenuItem getAnnotationsItems = new MenuItem("Get annotations");
getAnnotationsItems.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
PrivateDataManager privateDataManager = xmppSession.getExtensionManager(PrivateDataManager.class);
try {
Annotation annotations = privateDataManager.getData(Annotation.class);
int i = 0;
} catch (XmppException e) {
e.printStackTrace();
}
}
});
MenuItem pubSubItem = new MenuItem("PubSub");
pubSubItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
PubSubManager pubSubManager = xmppSession.getExtensionManager(PubSubManager.class);
ServiceDiscoveryManager serviceDiscoveryManager = xmppSession.getExtensionManager(ServiceDiscoveryManager.class);
try {
ItemNode infoNode = serviceDiscoveryManager.discoverItems(null);
int i = 0;
} catch (XmppException e) {
e.printStackTrace();
}
}
});
MenuItem pepItem = new MenuItem("PEP");
pepItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
try {
GeoLocationManager geoLocationManager = xmppSession.getExtensionManager(GeoLocationManager.class);
geoLocationManager.publish(new GeoLocation(45.44, 12.33));
} catch (XmppException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
});
MenuItem sendFile = new MenuItem("Send file");
sendFile.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
FileTransferManager fileTransferManager = xmppSession.getExtensionManager(FileTransferManager.class);
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(stage);
try {
fileTransferManager.offerFile(file, "", xmppSession.getPresenceManager().getPresence(item.contact.get().getJid()).getFrom(), 10000);
} catch (XmppException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
MenuItem timeItem = new MenuItem("Get time");
timeItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
EntityTimeManager entityTimeManager = xmppSession.getExtensionManager(EntityTimeManager.class);
try {
EntityTime entityTime = entityTimeManager.getEntityTime(Jid.valueOf("juliet@example.net/balcony"));
} catch (XmppException e) {
if (e instanceof NoResponseException) {
// The entity did not respond
} else if (e instanceof StanzaException) {
StanzaError stanzaError = ((StanzaException) e).getStanza().getError();
if (stanzaError.getCondition() instanceof ServiceUnavailable) {
// The entity returned a <service-unavailable/> stanza error.
}
}
}
}
});
contextMenu.getItems().addAll(lastActivityMenuItem, pingMenuItem, searchMenuItem, softwareVersionItem, serviceDiscoveryMenuItem, vCardItem, storeAnnotationsItems, getAnnotationsItems, pubSubItem, pepItem, sendFile, timeItem);
setContextMenu(contextMenu);
}
}
};
listCell.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getClickCount() == 2 && listCell.getItem() != null) {
Jid chatPartner = listCell.getItem().contact.get().getJid().asBareJid();
ChatWindow chatWindow = windows.get(chatPartner);
if (chatWindow == null) {
chatWindow = new ChatWindow(chatPartner, xmppSession);
windows.put(chatPartner, chatWindow);
}