Search in sources :

Example 76 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 | InterruptedException 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.getDataForm().getFields()) {
        String variable = field.getFieldName();
        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.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 77 with SwingWorker

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

the class JinglePlugin method initialize.

public void initialize() {
    // Add Jingle to discovered items list.
    SparkManager.addFeature(JINGLE_NAMESPACE);
    final LocalPreferences localPref = SettingsManager.getLocalPreferences();
    // If there is a server entered in spark.properties use it as fallback
    if (!localPref.getStunFallbackHost().equals("")) {
        fallbackStunEnabled = true;
    }
    // Get the default port
    stunPort = localPref.getStunFallbackPort();
    // Set Jingle Enabled
    JingleManager.setJingleServiceEnabled();
    JingleManager.setServiceEnabled(SparkManager.getConnection(), true);
    // Add to PhoneManager
    PhoneManager.getInstance().addPhone(this);
    // Adds a tab handler.
    SparkManager.getChatManager().addSparkTabHandler(new JingleTabHandler());
    final SwingWorker jingleLoadingThread = new SwingWorker() {

        public Object construct() {
            if (fallbackStunEnabled) {
                stunServer = localPref.getStunFallbackHost();
                readyToConnect = true;
            }
            try {
                if (STUN.serviceAvailable(SparkManager.getConnection())) {
                    STUN stun = STUN.getSTUNServer(SparkManager.getConnection());
                    if (stun != null) {
                        List<STUN.StunServerAddress> servers = stun.getServers();
                        if (servers.size() > 0) {
                            stunServer = servers.get(0).getServer();
                            stunPort = Integer.parseInt(servers.get(0).getPort());
                            readyToConnect = true;
                        }
                    }
                }
                if (readyToConnect) {
                    JingleTransportManager transportManager = new ICETransportManager(SparkManager.getConnection(), stunServer, stunPort);
                    List<JingleMediaManager> mediaManagers = new ArrayList<>();
                    // Get the Locator from the Settings
                    String locator = SettingsManager.getLocalPreferences().getAudioDevice();
                    mediaManagers.add(new JmfMediaManager(locator, transportManager));
                    mediaManagers.add(new SpeexMediaManager(transportManager));
                    // mediaManagers.add(new ScreenShareMediaManager(transportManager));
                    jingleManager = new JingleManager(SparkManager.getConnection(), mediaManagers);
                    if (transportManager instanceof BridgedTransportManager) {
                        jingleManager.addCreationListener((BridgedTransportManager) transportManager);
                    } else if (transportManager instanceof ICETransportManager) {
                        jingleManager.addCreationListener((ICETransportManager) transportManager);
                    }
                }
            } catch (XMPPException | SmackException e) {
                Log.error("Unable to initialize", e);
            }
            return true;
        }

        public void finished() {
            addListeners();
        }
    };
    jingleLoadingThread.start();
    // Add Presence listener for better service discovery.
    addPresenceListener();
    SparkManager.getConnection().addConnectionListener(this);
}
Also used : JmfMediaManager(org.jivesoftware.smackx.jingleold.mediaimpl.jmf.JmfMediaManager) ICETransportManager(org.jivesoftware.smackx.jingleold.nat.ICETransportManager) JingleMediaManager(org.jivesoftware.smackx.jingleold.media.JingleMediaManager) SwingWorker(org.jivesoftware.spark.util.SwingWorker) JingleManager(org.jivesoftware.smackx.jingleold.JingleManager) SpeexMediaManager(org.jivesoftware.smackx.jingleold.mediaimpl.jspeex.SpeexMediaManager) LocalPreferences(org.jivesoftware.sparkimpl.settings.local.LocalPreferences) STUN(org.jivesoftware.smackx.jingleold.nat.STUN) JingleTransportManager(org.jivesoftware.smackx.jingleold.nat.JingleTransportManager) BridgedTransportManager(org.jivesoftware.smackx.jingleold.nat.BridgedTransportManager)

Example 78 with SwingWorker

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

the class Notes method saveNotes.

private void saveNotes() {
    String note = textPane.getText();
    // Check for empty note.
    if (!ModelUtil.hasLength(note)) {
        return;
    }
    // Save note.
    AgentSession agentSession = FastpathPlugin.getAgentSession();
    try {
        agentSession.setNote(sessionID, note);
        saveButton.setEnabled(false);
        statusLabel.setText(" " + FpRes.getString("message.notes.updated"));
        SwingWorker worker = new SwingWorker() {

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

            public void finished() {
                statusLabel.setText("");
            }
        };
        worker.start();
    } catch (XMPPException | SmackException | InterruptedException e1) {
        showError(FpRes.getString("message.unable.to.update.notes"));
        Log.error("Could not commit note.", e1);
    }
}
Also used : AgentSession(org.jivesoftware.smackx.workgroup.agent.AgentSession) SmackException(org.jivesoftware.smack.SmackException) SwingWorker(org.jivesoftware.spark.util.SwingWorker) XMPPException(org.jivesoftware.smack.XMPPException)

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