Search in sources :

Example 56 with SwingWorker

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

the class SipCodecsPreference method load.

@Override
public void load() {
    SwingWorker thread = new SwingWorker() {

        LocalPreferences localPreferences;

        public Object construct() {
            localPreferences = SettingsManager.getLocalPreferences();
            return localPreferences;
        }

        public void finished() {
            String sel = localPreferences.getSelectedCodecs();
            String avail = localPreferences.getAvailableCodecs();
            panel.setAvailable(avail);
            panel.setSelected(sel);
        }
    };
    thread.start();
}
Also used : SwingWorker(org.jivesoftware.spark.util.SwingWorker) LocalPreferences(org.jivesoftware.sparkimpl.settings.local.LocalPreferences)

Example 57 with SwingWorker

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

the class SipPreference method load.

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

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

        public void finished() {
            panel.setUserName(preferences.getUserName() != null ? preferences.getUserName() : "");
            panel.setAuthUserName(preferences.getAuthUserName() != null ? preferences.getAuthUserName() : "");
            panel.setServer(preferences.getServer() != null ? preferences.getServer() : "");
            panel.setPassword(preferences.getPassword() != null ? preferences.getPassword() : "");
            panel.setRegister(preferences.isRegisterAtStart());
            panel.setStunServer(preferences.getStunServer());
            panel.setStunPort(preferences.getStunPort());
            panel.setUseStun(preferences.isUseStun());
        }
    };
    worker.start();
}
Also used : SwingWorker(org.jivesoftware.spark.util.SwingWorker)

Example 58 with SwingWorker

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

the class ChatRoomTransferDecorator method showFilePicker.

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

        @Override
        public Object construct() {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e1) {
                Log.error(e1);
            }
            return true;
        }

        @Override
        public void finished() {
            FileDialog fileChooser = SparkManager.getTransferManager().getFileChooser(SparkManager.getChatManager().getChatContainer().getChatFrame(), Res.getString("title.select.file.to.send"));
            if (SparkManager.getTransferManager().getDefaultDirectory() != null) {
                fileChooser.setDirectory(SparkManager.getTransferManager().getDefaultDirectory().getAbsolutePath());
            }
            fileChooser.setVisible(true);
            final File[] files = fileChooser.getFiles();
            if (files.length == 0) {
                // no selection
                return;
            }
            // Single-file selection is used. Using the first array item is safe.
            File file = files[0];
            if (file.exists()) {
                SparkManager.getTransferManager().setDefaultDirectory(file.getParentFile());
                SparkManager.getTransferManager().sendFile(file, ((ChatRoomImpl) chatRoom).getParticipantJID());
            }
        }
    };
    worker.start();
}
Also used : SwingWorker(org.jivesoftware.spark.util.SwingWorker) FileDialog(java.awt.FileDialog) File(java.io.File)

Example 59 with SwingWorker

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

the class SparkTransferManager method sendScreenshot.

public void sendScreenshot(final ChatRoomButton button, final ChatRoom room) {
    button.setEnabled(false);
    final MainWindow mainWindow = SparkManager.getMainWindow();
    final ChatFrame chatFrame = SparkManager.getChatManager().getChatContainer().getChatFrame();
    final boolean mainWindowVisible = mainWindow.isVisible();
    final boolean chatFrameVisible = chatFrame.isVisible();
    if (mainWindowVisible) {
        mainWindow.setVisible(false);
    }
    if (chatFrameVisible) {
        chatFrame.setVisible(false);
    }
    final SwingWorker worker = new SwingWorker() {

        @Override
        public Object construct() {
            try {
                Thread.sleep(1000);
                Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
                return robot.createScreenCapture(area);
            } catch (Throwable e) {
                Log.error(e);
                if (mainWindowVisible) {
                    mainWindow.setVisible(true);
                }
                if (chatFrameVisible) {
                    chatFrame.setVisible(true);
                }
            }
            return null;
        }

        @Override
        public void finished() {
            bufferedImage = (BufferedImage) get();
            if (bufferedImage == null) {
                UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
                JOptionPane.showMessageDialog(null, Res.getString("title.error"), "Unable to process screenshot.", JOptionPane.ERROR_MESSAGE);
                return;
            }
            final Frame frame = new Frame();
            frame.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
            selectionPanel.setImage(bufferedImage);
            selectionPanel.validate();
            selectionPanel.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseReleased(MouseEvent e) {
                    Rectangle clip = selectionPanel.getClip();
                    BufferedImage newImage = null;
                    try {
                        newImage = bufferedImage.getSubimage((int) clip.getX(), (int) clip.getY(), (int) clip.getWidth(), (int) clip.getHeight());
                    } catch (Exception e1) {
                    // Nothing to do
                    }
                    if (newImage != null) {
                        sendImage(newImage, room);
                        bufferedImage = null;
                        selectionPanel.clear();
                    }
                    frame.dispose();
                    frame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    if (mainWindowVisible) {
                        mainWindow.setVisible(true);
                    }
                    if (chatFrameVisible) {
                        chatFrame.setVisible(true);
                    }
                    selectionPanel.removeMouseListener(this);
                }
            });
            frame.addKeyListener(new KeyAdapter() {

                @Override
                public void keyReleased(KeyEvent e) {
                    if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
                        frame.dispose();
                        frame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                        if (mainWindowVisible) {
                            mainWindow.setVisible(true);
                        }
                        if (chatFrameVisible) {
                            chatFrame.setVisible(true);
                        }
                    }
                }
            });
            frame.setSize(bufferedImage.getWidth(null), bufferedImage.getHeight());
            frame.add(selectionPanel);
            frame.setUndecorated(true);
            // Determine if full-screen mode is supported directly
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            if (gs.isFullScreenSupported()) {
                gs.setFullScreenWindow(frame);
            } else {
                // Full-screen mode will be simulated
                frame.setVisible(true);
            }
            button.setEnabled(true);
        }
    };
    worker.start();
}
Also used : Frame(java.awt.Frame) ChatFrame(org.jivesoftware.spark.ui.ChatFrame) MouseEvent(java.awt.event.MouseEvent) KeyAdapter(java.awt.event.KeyAdapter) Rectangle(java.awt.Rectangle) MouseAdapter(java.awt.event.MouseAdapter) Cursor(java.awt.Cursor) GraphicsEnvironment(java.awt.GraphicsEnvironment) BufferedImage(java.awt.image.BufferedImage) URISyntaxException(java.net.URISyntaxException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) AWTException(java.awt.AWTException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) KeyEvent(java.awt.event.KeyEvent) GraphicsDevice(java.awt.GraphicsDevice) ChatFrame(org.jivesoftware.spark.ui.ChatFrame) MainWindow(org.jivesoftware.MainWindow) SwingWorker(org.jivesoftware.spark.util.SwingWorker)

Example 60 with SwingWorker

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

the class ChatContainer method addKeyNavigation.

/**
 * Adds navigation capability to chat rooms. Users can navigate using the
 * alt-left or right arrow keys.
 */
private void addKeyNavigation() {
    KeyStroke leftStroke = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
    String leftStrokeString = org.jivesoftware.spark.util.StringUtils.keyStroke2String(leftStroke);
    // Handle Left Arrow
    this.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("alt " + leftStrokeString + ""), "navigateLeft");
    this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("alt " + leftStrokeString + ""), "navigateLeft");
    this.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("alt " + leftStrokeString + ""), "navigateLeft");
    this.getActionMap().put("navigateLeft", new AbstractAction("navigateLeft") {

        private static final long serialVersionUID = -8677467560602512074L;

        @Override
        public void actionPerformed(ActionEvent evt) {
            navigateLeft();
        }
    });
    KeyStroke rightStroke = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);
    String rightStrokeString = org.jivesoftware.spark.util.StringUtils.keyStroke2String(rightStroke);
    // Handle Right Arrow
    this.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("alt " + rightStrokeString + ""), "navigateRight");
    this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("alt " + rightStrokeString + ""), "navigateRight");
    this.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("alt " + rightStrokeString + ""), "navigateRight");
    this.getActionMap().put("navigateRight", new AbstractAction("navigateRight") {

        private static final long serialVersionUID = -7676330627598261416L;

        @Override
        public void actionPerformed(ActionEvent evt) {
            navigateRight();
        }
    });
    this.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ESCAPE"), "escape");
    this.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ESCAPE"), "escape");
    this.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("Ctrl W"), "escape");
    this.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "escape");
    this.getActionMap().put("escape", new AbstractAction("escape") {

        private static final long serialVersionUID = 5165074248488666495L;

        @Override
        public void actionPerformed(ActionEvent evt) {
            closeActiveRoom();
        }
    });
    this.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() | InputEvent.SHIFT_MASK), "shiftCmdW");
    this.getActionMap().put("shiftCmdW", new AbstractAction("shiftCmdW") {

        private static final long serialVersionUID = -1179625099164632251L;

        @Override
        public void actionPerformed(ActionEvent evt) {
            closeAllChatRooms();
        }
    });
    // Add KeyMappings
    getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "searchContacts");
    getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control F"), "searchContacts");
    getActionMap().put("searchContacts", new AbstractAction("searchContacts") {

        private static final long serialVersionUID = -6904085783599775675L;

        @Override
        public void actionPerformed(ActionEvent evt) {
            SwingWorker worker = new SwingWorker() {

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

                @Override
                public void finished() {
                    SparkManager.getUserManager().searchContacts("", SparkManager.getChatManager().getChatContainer().getChatFrame());
                }
            };
            worker.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