Search in sources :

Example 31 with SwingWorker

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

the class SparkTransferManager method sendImage.

/**
 * Send an image to a user.
 *
 * @param image the image to send.
 * @param room  the ChatRoom of the user you wish to send the image to.
 */
public void sendImage(final BufferedImage image, final ChatRoom room) {
    File tmpDirectory = new File(Spark.getSparkUserHome(), "/tempImages");
    tmpDirectory.mkdirs();
    String imageName = "image_" + StringUtils.randomString(2) + ".png";
    final File imageFile = new File(tmpDirectory, imageName);
    // Write image to system.
    room.setCursor(new Cursor(Cursor.WAIT_CURSOR));
    SwingWorker writeImageThread = new SwingWorker() {

        public Object construct() {
            try {
                // Write out file in separate thread.
                ImageIO.write(image, "png", imageFile);
            } catch (IOException e) {
                Log.error(e);
            }
            return true;
        }

        public void finished() {
            ChatRoomImpl roomImpl = (ChatRoomImpl) room;
            sendFile(imageFile, roomImpl.getParticipantJID());
            SparkManager.getChatManager().getChatContainer().activateChatRoom(room);
            room.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
    };
    writeImageThread.start();
}
Also used : SwingWorker(org.jivesoftware.spark.util.SwingWorker) IOException(java.io.IOException) Cursor(java.awt.Cursor) File(java.io.File) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl)

Example 32 with SwingWorker

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

the class BookmarksUI method addRegisteredServices.

private void addRegisteredServices() {
    SwingWorker worker = new SwingWorker() {

        @Override
        public Object construct() {
            try {
                if (SparkManager.getConnection().isConnected()) {
                    mucServices = MultiUserChatManager.getInstanceFor(SparkManager.getConnection()).getServiceNames();
                }
            } catch (XMPPException | SmackException e) {
                Log.error("Unable to load MUC Service Names.", e);
            }
            return mucServices;
        }

        @Override
        public void finished() {
            if (mucServices == null) {
                return;
            }
            for (Object mucService : mucServices) {
                String service = (String) mucService;
                if (!hasService(service)) {
                    addServiceToList(service);
                }
            }
        }
    };
    worker.start();
}
Also used : SmackException(org.jivesoftware.smack.SmackException) SwingWorker(org.jivesoftware.spark.util.SwingWorker) XMPPException(org.jivesoftware.smack.XMPPException)

Example 33 with SwingWorker

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

the class ConferenceRoomBrowser method startLoadingImg.

private void startLoadingImg() {
    SwingWorker startLoading = new SwingWorker() {

        @Override
        public Object construct() {
            return null;
        }

        @Override
        public void finished() {
            refreshButton.setIcon(SparkRes.getImageIcon(SparkRes.BUSY_IMAGE));
            refreshButton.validate();
            refreshButton.repaint();
            refreshItem.setIcon(SparkRes.getImageIcon(SparkRes.BUSY_IMAGE));
            refreshItem.validate();
            refreshItem.repaint();
        }
    };
    startLoading.start();
}
Also used : SwingWorker(org.jivesoftware.spark.util.SwingWorker)

Example 34 with SwingWorker

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

the class ConferenceRoomBrowser method addRoomToTable.

/**
 * Adds a room to the room table.
 *
 * @param jid
 *            the jid of the conference room.
 * @param roomName
 *            the name of the conference room.
 * @param numberOfOccupants
 *            the number of occupants in the conference room. If -1 is
 *            specified, the the occupant count will show as n/a.
 */
private void addRoomToTable(final String jid, final String roomName, final int numberOfOccupants) {
    SwingWorker addRoomThread = new SwingWorker() {

        @Override
        public Object construct() {
            JLabel iconLabel = new JLabel();
            iconLabel.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
            boolean isbookmark = false;
            boolean ispassword = false;
            ImageIcon bookmarkicon = SparkRes.getImageIcon(SparkRes.BOOKMARK_ICON);
            ImageIcon passwordicon = SparkRes.getImageIcon(SparkRes.LOCK_16x16);
            if (isBookmarked(jid)) {
                isbookmark = true;
                iconLabel.setIcon(SparkRes.getImageIcon(SparkRes.BOOKMARK_ICON));
            }
            if (isPasswordProtected(jid)) {
                ispassword = true;
            }
            if (isbookmark && ispassword) {
                Image img = ImageCombiner.combine(bookmarkicon, passwordicon);
                iconLabel.setIcon(new ImageIcon(img));
            } else if (isbookmark) {
                iconLabel.setIcon(bookmarkicon);
            } else if (ispassword) {
                Image img = ImageCombiner.returnTransparentImage(passwordicon.getIconWidth(), passwordicon.getIconHeight());
                Image combined = ImageCombiner.combine(new ImageIcon(img), passwordicon);
                iconLabel.setIcon(new ImageIcon(combined));
            }
            String occupants = Integer.toString(numberOfOccupants);
            if (numberOfOccupants == -1) {
                occupants = "n/a";
            }
            final Object[] insertRoom = new Object[] { iconLabel, roomName, XmppStringUtils.parseLocalpart(jid), occupants };
            return insertRoom;
        }

        @Override
        public void finished() {
            Object[] insertRoom = (Object[]) get();
            roomsTable.getTableModel().addRow(insertRoom);
        }
    };
    addRoomThread.start();
}
Also used : ImageIcon(javax.swing.ImageIcon) SwingWorker(org.jivesoftware.spark.util.SwingWorker) JLabel(javax.swing.JLabel)

Example 35 with SwingWorker

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

the class SendFileTransfer method sendFile.

public void sendFile(final OutgoingFileTransfer transfer, FileTransferManager transferManager, final String jid, final String nickname) {
    this.transferManager = transferManager;
    // SPARK-1869
    FileTransferNegotiator.getInstanceFor(SparkManager.getConnection());
    FileTransferNegotiator.IBB_ONLY = SettingsManager.getLocalPreferences().isFileTransferIbbOnly();
    cancelButton.setVisible(true);
    retryButton.setVisible(false);
    this.fullJID = jid;
    this.nickname = nickname;
    this.transfer = transfer;
    String fileName = transfer.getFileName();
    long fileSize = transfer.getFileSize();
    ByteFormat format = new ByteFormat();
    String text = format.format(fileSize);
    fileToSend = new File(transfer.getFilePath());
    imageLabel.setFile(fileToSend);
    fileLabel.setText(fileName + " (" + text + ")");
    ContactList contactList = SparkManager.getWorkspace().getContactList();
    ContactItem contactItem = contactList.getContactItemByJID(jid);
    titleLabel.setText(Res.getString("message.transfer.waiting.on.user", contactItem.getDisplayName()));
    if (isImage(fileName)) {
        try {
            URL imageURL = new File(transfer.getFilePath()).toURI().toURL();
            ImageIcon image = new ImageIcon(imageURL);
            image = GraphicUtils.scaleImageIcon(image, 64, 64);
            imageLabel.setIcon(image);
        } catch (MalformedURLException e) {
            Log.error("Could not locate image.", e);
            imageLabel.setIcon(SparkRes.getImageIcon(SparkRes.DOCUMENT_INFO_32x32));
        }
    } else {
        File file = new File(transfer.getFilePath());
        Icon icon = GraphicUtils.getIcon(file);
        imageLabel.setIcon(icon);
    }
    cancelButton.addMouseListener(new MouseAdapter() {

        public void mouseClicked(MouseEvent mouseEvent) {
            transfer.cancel();
        }
    });
    cancelButton.addMouseListener(new MouseAdapter() {

        public void mouseEntered(MouseEvent e) {
            cancelButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }

        public void mouseExited(MouseEvent e) {
            cancelButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
    });
    progressBar.setMaximum(100);
    progressBar.setVisible(false);
    progressBar.setStringPainted(true);
    add(progressBar, new GridBagConstraints(1, 2, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 150, 0));
    add(progressLabel, new GridBagConstraints(1, 3, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 150, 0));
    SwingWorker worker = new SwingWorker() {

        public Object construct() {
            while (true) {
                try {
                    if (transfer.getBytesSent() > 0 && _starttime == 0) {
                        _starttime = System.currentTimeMillis();
                    }
                    long starttime = System.currentTimeMillis();
                    long startbyte = transfer.getBytesSent();
                    Thread.sleep(500);
                    FileTransfer.Status status = transfer.getStatus();
                    if (status == Status.error || status == Status.complete || status == Status.cancelled || status == Status.refused) {
                        break;
                    }
                    long endtime = System.currentTimeMillis();
                    long endbyte = transfer.getBytesSent();
                    long timediff = endtime - starttime;
                    long bytediff = endbyte - startbyte;
                    updateBar(transfer, nickname, TransferUtils.calculateSpeed(bytediff, timediff));
                } catch (InterruptedException e) {
                    Log.error("Unable to sleep thread.", e);
                }
            }
            return "";
        }

        public void finished() {
            updateBar(transfer, nickname, "??MB/s");
        }
    };
    worker.start();
    makeClickable(imageLabel);
    makeClickable(titleLabel);
}
Also used : ImageIcon(javax.swing.ImageIcon) MalformedURLException(java.net.MalformedURLException) GridBagConstraints(java.awt.GridBagConstraints) MouseEvent(java.awt.event.MouseEvent) Insets(java.awt.Insets) ContactItem(org.jivesoftware.spark.ui.ContactItem) MouseAdapter(java.awt.event.MouseAdapter) OutgoingFileTransfer(org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer) FileTransfer(org.jivesoftware.smackx.filetransfer.FileTransfer) ContactList(org.jivesoftware.spark.ui.ContactList) Cursor(java.awt.Cursor) URL(java.net.URL) Status(org.jivesoftware.smackx.filetransfer.FileTransfer.Status) ByteFormat(org.jivesoftware.spark.util.ByteFormat) SwingWorker(org.jivesoftware.spark.util.SwingWorker) ImageIcon(javax.swing.ImageIcon) Icon(javax.swing.Icon) File(java.io.File)

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