});
comboBox.setButtonCell(comboBox.getCellFactory().call(null));
comboBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Presence.Show>() {
@Override
public void changed(ObservableValue<? extends Presence.Show> observableValue, Presence.Show show, Presence.Show show2) {
xmppSession.send(new Presence(show2));
}
});
final Map<Contact, ContactItem> contactMap = new HashMap<>();
final ObservableList<ContactItem> contactItems = FXCollections.observableArrayList();
LogManager.getLogManager().reset();
Logger globalLogger = Logger.getLogger("");
Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.FINE);
consoleHandler.setFormatter(new LogFormatter());
globalLogger.addHandler(consoleHandler);
final Logger logger = Logger.getLogger("org.xmpp");
//logger.addHandler(consoleHandler);
logger.setLevel(Level.FINE);
//LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME).setLevel(Level.OFF);
//AbstractMessage message = new AbstractMessage(Jid.valueOf("juliet@example.net"), AbstractMessage.Type.CHAT);
//message.getExtensions().add(new Composing());
Button btnConnect = new Button("Login");
btnConnect.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
new Thread() {
@Override
public void run() {
// if (!useBosh.isSelected()) {
// xmppSession = new XmppSession(txtDomain.getText(), new TcpConnection(txtServer.getText(), Integer.parseInt(txtPort.getText()), Proxy.NO_PROXY));
// } else {
// xmppSession = new XmppSession(txtDomain.getText(), new BoshConnection(txtServer.getText(), Integer.parseInt(txtPort.getText())));
// }
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
}, new SecureRandom());
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
xmppSession.getChatManager().addChatSessionListener(new ChatSessionListener() {
@Override
public void chatSessionCreated(final ChatSessionEvent chatSessionEvent) {
final ChatSession chatSession = chatSessionEvent.getChatSession();
chatSession.addMessageListener(new MessageListener() {
@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) {