Search in sources :

Example 51 with SwingWorker

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();
}
Also used : ImageIcon(javax.swing.ImageIcon) SwingWorker(org.jivesoftware.spark.util.SwingWorker) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 52 with SwingWorker

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;
    }
}
Also used : Calendar(java.util.Calendar) Date(java.util.Date) IOException(java.io.IOException) SwingWorker(org.jivesoftware.spark.util.SwingWorker) LocalPreferences(org.jivesoftware.sparkimpl.settings.local.LocalPreferences) ConfirmListener(org.jivesoftware.spark.component.ConfirmDialog.ConfirmListener) File(java.io.File) ConfirmDialog(org.jivesoftware.spark.component.ConfirmDialog)

Example 53 with SwingWorker

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();
}
Also used : GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) SwingWorker(org.jivesoftware.spark.util.SwingWorker) EntityBareJid(org.jxmpp.jid.EntityBareJid) MultiUserChatManager(org.jivesoftware.smackx.muc.MultiUserChatManager) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 54 with SwingWorker

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);
    }
}
Also used : CheckUpdates(org.jivesoftware.sparkimpl.updater.CheckUpdates) SwingWorker(org.jivesoftware.spark.util.SwingWorker) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException)

Example 55 with SwingWorker

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();
}
Also used : SwingWorker(org.jivesoftware.spark.util.SwingWorker)

Aggregations

SwingWorker (org.jivesoftware.spark.util.SwingWorker)78 SmackException (org.jivesoftware.smack.SmackException)21 XMPPException (org.jivesoftware.smack.XMPPException)17 Presence (org.jivesoftware.smack.packet.Presence)12 MouseAdapter (java.awt.event.MouseAdapter)11 MouseEvent (java.awt.event.MouseEvent)11 GridBagConstraints (java.awt.GridBagConstraints)10 Insets (java.awt.Insets)10 JLabel (javax.swing.JLabel)10 ActionEvent (java.awt.event.ActionEvent)9 JPanel (javax.swing.JPanel)9 LocalPreferences (org.jivesoftware.sparkimpl.settings.local.LocalPreferences)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 EntityBareJid (org.jxmpp.jid.EntityBareJid)8 Collection (java.util.Collection)7 Action (javax.swing.Action)7 GridBagLayout (java.awt.GridBagLayout)6 AbstractAction (javax.swing.AbstractAction)6 JDialog (javax.swing.JDialog)6