Search in sources :

Example 61 with SwingWorker

use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.

the class ChatQueue method offerRecieved.

public void offerRecieved(Offer offer) {
    this.offer = offer;
    // Retrieve workgroup form
    Form form;
    try {
        form = FastpathPlugin.getWorkgroup().getWorkgroupForm();
    } catch (XMPPException | SmackException e) {
        Log.error("Unable to retrieve Workgroup form.", e);
        return;
    }
    final RequestUtils utils = new RequestUtils(offer.getMetaData());
    nameLabel.setText(FpRes.getString("message.incoming.chat.request", utils.getUsername()));
    nameLabel.setIcon(FastpathRes.getImageIcon(FastpathRes.FASTPATH_IMAGE_16x16));
    Color linkColor = new Color(69, 92, 137);
    int count = 1;
    for (final FormField field : form.getFields()) {
        String variable = field.getVariable();
        String label = field.getLabel();
        if (label != null) {
            final JLabel nameLabel = new JLabel(label);
            nameLabel.setFont(new Font("Dialog", Font.BOLD, 11));
            String value = utils.getValue(variable);
            if (value == null) {
                value = "";
            }
            final WrappedLabel valueLabel = new WrappedLabel();
            valueLabel.setBackground(Color.white);
            valueLabel.setText(value);
            add(nameLabel, new GridBagConstraints(0, count, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
            add(valueLabel, new GridBagConstraints(1, count, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
            count++;
        }
    }
    add(progressBar, new GridBagConstraints(0, count, 3, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(15, 5, 5, 5), 0, 0));
    count++;
    viewLabel = new LinkLabel(FpRes.getString("message.view.more.information"), null, linkColor, Color.red);
    add(viewLabel, new GridBagConstraints(0, count, 1, 1, 0.0, 1.0, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    final JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    buttonPanel.setOpaque(false);
    buttonPanel.add(acceptButton);
    buttonPanel.add(declineButton);
    add(buttonPanel, new GridBagConstraints(1, count, 2, 1, 1.0, 1.0, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    viewLabel.addMouseListener(new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {
            showInformation();
        }
    });
    final Date endTime = offer.getExpiresDate();
    Date now = new Date();
    long mill = endTime.getTime() - now.getTime();
    int seconds = (int) (mill / 1000);
    progressBar.setMaximum(seconds);
    progressBar.setValue(seconds);
    SwingWorker worker = new SwingWorker() {

        public Object construct() {
            while (true) {
                Date now = new Date();
                if (now.getTime() >= endTime.getTime()) {
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Log.error(e);
                }
                progressBar.setValue(progressBar.getValue() - 1);
                progressBar.setStringPainted(true);
                int seconds = (int) (endTime.getTime() - now.getTime()) / 1000;
                if (seconds <= 60) {
                    String timeString = seconds + " " + FpRes.getString("seconds");
                    progressBar.setString(timeString);
                } else {
                    long difference = endTime.getTime() - now.getTime();
                    String timeString = ModelUtil.getTimeFromLong(difference);
                    progressBar.setString(timeString);
                }
            }
            return progressBar;
        }
    };
    worker.start();
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) FlowLayout(java.awt.FlowLayout) MouseEvent(java.awt.event.MouseEvent) Form(org.jivesoftware.smackx.xdata.Form) SmackException(org.jivesoftware.smack.SmackException) Color(java.awt.Color) MouseAdapter(java.awt.event.MouseAdapter) JLabel(javax.swing.JLabel) RequestUtils(org.jivesoftware.fastpath.workspace.util.RequestUtils) Font(java.awt.Font) Date(java.util.Date) LinkLabel(org.jivesoftware.spark.component.LinkLabel) SwingWorker(org.jivesoftware.spark.util.SwingWorker) WrappedLabel(org.jivesoftware.spark.component.WrappedLabel) XMPPException(org.jivesoftware.smack.XMPPException) FormField(org.jivesoftware.smackx.xdata.FormField)

Example 62 with SwingWorker

use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.

the class SoundPreference method load.

public void load() {
    if (soundPanel == null) {
        soundPanel = new SoundPanel();
    }
    SwingWorker worker = new SwingWorker() {

        public Object construct() {
            loadFromFile();
            return preferences;
        }

        public void finished() {
            // Set default settings
            soundPanel.setIncomingMessageSound(preferences.getIncomingSound());
            soundPanel.playIncomingSound(preferences.isPlayIncomingSound());
            soundPanel.setOutgoingMessageSound(preferences.getOutgoingSound());
            soundPanel.playOutgoingSound(preferences.isPlayOutgoingSound());
            soundPanel.setOfflineSound(preferences.getOfflineSound());
            soundPanel.playOfflineSound(preferences.isPlayOfflineSound());
            soundPanel.setInvitationSound(preferences.getIncomingInvitationSoundFile());
            soundPanel.setPlayInvitationSound(preferences.playIncomingInvitationSound());
        }
    };
    worker.start();
}
Also used : SwingWorker(org.jivesoftware.spark.util.SwingWorker)

Example 63 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() {

        public Object construct() {
            return resizeImage(selectedFile);
        }

        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 64 with SwingWorker

use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.

the class VCardManager method viewProfile.

/**
 * Displays <code>VCardViewer</code> for a particular JID.
 *
 * @param jid    the jid of the user to display.
 * @param parent the parent component to use for displaying dialog.
 */
public void viewProfile(final String jid, final JComponent parent) {
    final SwingWorker vcardThread = new SwingWorker() {

        VCard vcard = new VCard();

        public Object construct() {
            vcard = getVCard(jid);
            return vcard;
        }

        public void finished() {
            if (vcard == null) {
                // Show vcard not found
                UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
                JOptionPane.showMessageDialog(parent, Res.getString("message.unable.to.load.profile", jid), Res.getString("title.profile.not.found"), JOptionPane.ERROR_MESSAGE);
            } else {
                editor.displayProfile(jid, vcard, parent);
            }
        }
    };
    vcardThread.start();
}
Also used : SwingWorker(org.jivesoftware.spark.util.SwingWorker) VCard(org.jivesoftware.smackx.vcardtemp.packet.VCard)

Example 65 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.
 * @throws Exception if there is an error during check
 */
public void checkForUpdate(boolean explicit) throws Exception {
    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() {

            public void yesOption() {
                SwingWorker worker = new SwingWorker() {

                    public Object construct() {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            Log.error(e);
                        }
                        return "ok";
                    }

                    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();
            }

            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)

Aggregations

SwingWorker (org.jivesoftware.spark.util.SwingWorker)65 SmackException (org.jivesoftware.smack.SmackException)22 XMPPException (org.jivesoftware.smack.XMPPException)15 Presence (org.jivesoftware.smack.packet.Presence)11 MouseAdapter (java.awt.event.MouseAdapter)8 MouseEvent (java.awt.event.MouseEvent)8 LocalPreferences (org.jivesoftware.sparkimpl.settings.local.LocalPreferences)8 ActionEvent (java.awt.event.ActionEvent)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 GridBagConstraints (java.awt.GridBagConstraints)6 Insets (java.awt.Insets)6 Collection (java.util.Collection)6 JLabel (javax.swing.JLabel)6 JPanel (javax.swing.JPanel)5 ContactItem (org.jivesoftware.spark.ui.ContactItem)5 Action (javax.swing.Action)4 Cursor (java.awt.Cursor)3 ActionListener (java.awt.event.ActionListener)3 File (java.io.File)3