use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.
the class AvatarPanel method changeAvatar.
private void changeAvatar(final File selectedFile, final Component parent) {
SwingWorker worker = new SwingWorker() {
@Override
public Object construct() {
return resizeImage(selectedFile);
}
@Override
public void finished() {
BufferedImage avatarImage = (BufferedImage) get();
String message = "";
int finalImageWidth = avatarImage.getWidth();
int finalImageHeight = avatarImage.getHeight();
boolean showWarning = false;
if (finalImageWidth != finalImageHeight) {
message += "\u2022 " + Res.getString("message.image.not.square") + "\n";
showWarning = true;
}
if (finalImageWidth < 32 && finalImageHeight < 32) {
message += "\u2022 " + Res.getString("message.image.small.resolution") + "\n";
showWarning = true;
}
if (showWarning) {
message += Res.getString("message.image.suggestion");
JOptionPane.showMessageDialog(parent, message, Res.getString("title.warning"), JOptionPane.WARNING_MESSAGE);
}
// convert BufferedImage to bytes
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ImageIO.write(avatarImage, "png", baos);
setAvatar(new ImageIcon(avatarImage));
setAvatarBytes(baos.toByteArray());
} catch (IOException ex) {
Log.error(ex);
}
}
};
worker.start();
}
use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.
the class CheckUpdates method checkForUpdate.
/**
* Checks Spark Manager and/or Jive Software for the latest version of Spark.
*
* @param explicit true if the user explicitly asks for the latest version.
*/
public void checkForUpdate(boolean explicit) {
if (UPDATING) {
return;
}
UPDATING = true;
if (isLocalBuildAvailable()) {
return;
}
LocalPreferences localPreferences = SettingsManager.getLocalPreferences();
// defaults to 7, 0=disabled
int CheckForUpdates = localPreferences.getCheckForUpdates();
if (CheckForUpdates == 0) {
return;
}
Date lastChecked = localPreferences.getLastCheckForUpdates();
if (lastChecked == null) {
lastChecked = new Date();
// This is the first invocation of Communicator
localPreferences.setLastCheckForUpdates(lastChecked);
SettingsManager.saveSettings();
}
// Check to see if it has been a CheckForUpdates (default 7) days
Calendar calendar = Calendar.getInstance();
calendar.setTime(lastChecked);
calendar.add(Calendar.DATE, CheckForUpdates);
final Date lastCheckedPlusAPeriod = calendar.getTime();
boolean periodOrLonger = new Date().getTime() >= lastCheckedPlusAPeriod.getTime();
if (periodOrLonger || explicit || sparkPluginInstalled) {
if (!explicit && !localPreferences.isBetaCheckingEnabled()) {
return;
}
// Check version on server.
lastChecked = new Date();
localPreferences.setLastCheckForUpdates(lastChecked);
SettingsManager.saveSettings();
final SparkVersion serverVersion = newBuildAvailable();
if (serverVersion == null) {
UPDATING = false;
UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
if (explicit) {
UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Res.getString("message.no.updates"), Res.getString("title.no.updates"), JOptionPane.INFORMATION_MESSAGE);
}
return;
}
// Otherwise updates are available
String downloadURL = serverVersion.getDownloadURL();
String filename = downloadURL.substring(downloadURL.lastIndexOf("/") + 1);
if (filename.indexOf('=') != -1) {
filename = filename.substring(filename.indexOf('=') + 1);
}
// Set Download Directory
final File downloadDir = new File(Spark.getSparkUserHome(), "updates");
downloadDir.mkdirs();
// Set file to download.
final File fileToDownload = new File(downloadDir, filename);
if (fileToDownload.exists()) {
fileToDownload.delete();
}
ConfirmDialog confirm = new ConfirmDialog();
confirm.showConfirmDialog(SparkManager.getMainWindow(), Res.getString("title.new.version.available"), Res.getString("message.new.spark.available", filename), Res.getString("yes"), Res.getString("no"), null);
confirm.setDialogSize(400, 300);
confirm.setConfirmListener(new ConfirmListener() {
@Override
public void yesOption() {
SwingWorker worker = new SwingWorker() {
@Override
public Object construct() {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Log.error(e);
}
return "ok";
}
@Override
public void finished() {
if (Spark.isWindows()) {
downloadUpdate(fileToDownload, serverVersion);
} else {
// Launch browser to download page.
try {
if (sparkPluginInstalled) {
BrowserLauncher.openURL(serverVersion.getDownloadURL());
} else {
BrowserLauncher.openURL("http://www.igniterealtime.org/downloads/index.jsp#spark");
}
} catch (Exception e) {
Log.error(e);
}
UPDATING = false;
}
}
};
worker.start();
}
@Override
public void noOption() {
UPDATING = false;
}
});
} else {
UPDATING = false;
}
}
use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.
the class ChatManager method activateChat.
/**
* Activate a chat room with the selected user.
*
* @param jidCs the jid of the user to chat with.
* @param nicknameString the nickname of the user.
*/
public void activateChat(final CharSequence jidCs, final String nicknameString) {
final Resourcepart nickname = Resourcepart.fromOrThrowUnchecked(nicknameString);
final EntityBareJid jid = JidCreate.entityBareFromUnescapedOrThrowUnchecked(jidCs);
SwingWorker worker = new SwingWorker() {
final ChatManager chatManager = SparkManager.getChatManager();
ChatRoom chatRoom;
@Override
public Object construct() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
Log.error("Error in activate chat.", e);
}
ChatContainer chatRooms = chatManager.getChatContainer();
try {
chatRoom = chatRooms.getChatRoom(jid);
} catch (ChatRoomNotFoundException e) {
// Do nothing
}
return chatRoom;
}
@Override
public void finished() {
if (chatRoom == null) {
chatRoom = UIComponentRegistry.createChatRoom(jid, nickname, nickname);
chatManager.getChatContainer().addChatRoom(chatRoom);
}
chatManager.getChatContainer().activateChatRoom(chatRoom);
}
};
worker.start();
}
use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.
the class MainWindow method checkForUpdates.
/**
* Checks for the latest update on the server.
*
* @param forced true if you want to bypass the normal checking security.
*/
private void checkForUpdates(final boolean forced) {
final CheckUpdates updater = new CheckUpdates();
try {
final SwingWorker updateThread = new SwingWorker() {
@Override
public Object construct() {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Log.error(e);
}
return "ok";
}
@Override
public void finished() {
try {
updater.checkForUpdate(forced);
} catch (Exception e) {
Log.error("There was an error while checking for a new update.", e);
}
}
};
updateThread.start();
} catch (Exception e) {
Log.warning("Error updating.", e);
}
}
use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.
the class LoginUIPanel method validateLogin.
/**
* Validates the users login information.
*/
private void validateLogin() {
final SwingWorker loginValidationThread = new SwingWorker() {
@Override
public Object construct() {
setLoginUsername(getUsername());
setLoginPassword(getPassword());
setLoginServer(getServerName());
boolean loginSuccessfull = beforeLoginValidations() && login();
if (loginSuccessfull) {
afterLogin();
lblProgress.setText(Res.getString("message.connecting.please.wait"));
// Startup Spark
startSpark();
// dispose login dialog
loginDialog.dispose();
// Show ChangeLog if we need to.
// new ChangeLogDialog().showDialog();
} else {
EventQueue.invokeLater(() -> {
setComponentsAvailable(true);
setProgressBarVisible(false);
});
}
return loginSuccessfull;
}
};
// Start the login process in separate thread.
// Disable text fields
setComponentsAvailable(false);
// Show progressbar
setProgressBarVisible(true);
loginValidationThread.start();
}
Aggregations