Search in sources :

Example 11 with TitlePanel

use of org.jivesoftware.spark.component.TitlePanel in project Spark by igniterealtime.

the class PreferencesPanel method selectionChanged.

private synchronized void selectionChanged() {
    PreferenceUI o = list.getSelectedValue();
    Preference pref = o.getPreference();
    pref.load();
    JComponent comp = pref.getGUI();
    flowPanel.removeAll();
    // Create the title panel for this dialog
    TitlePanel titlePanel = new TitlePanel(pref.getTitle(), pref.getTooltip(), pref.getIcon(), false);
    flowPanel.add(comp, BorderLayout.CENTER);
    flowPanel.add(titlePanel, BorderLayout.NORTH);
    flowPanel.invalidate();
    flowPanel.validate();
    flowPanel.repaint();
    currentPreference = pref;
}
Also used : Preference(org.jivesoftware.spark.preference.Preference) JComponent(javax.swing.JComponent) TitlePanel(org.jivesoftware.spark.component.TitlePanel)

Example 12 with TitlePanel

use of org.jivesoftware.spark.component.TitlePanel in project Spark by igniterealtime.

the class UserSearchForm method addSearchServices.

private void addSearchServices() {
    // Populate with Search Services
    servicesBox = new JComboBox<>();
    for (CharSequence searchService : searchServices) {
        String service = searchService.toString();
        servicesBox.addItem(service);
    }
    // Load the property file and add the search services that are read
    final Properties props = new Properties();
    String nextprop;
    boolean numbprop_bool;
    int numbprop;
    if (pluginsettings.exists()) {
        // Log.warning("Search-service Properties-file does exist= " + pluginsettings.getPath());
        try {
            numbprop = 0;
            props.load(new FileInputStream(pluginsettings));
            String testsearch;
            numbprop_bool = true;
            while (numbprop_bool) {
                nextprop = "search" + numbprop;
                testsearch = props.getProperty(nextprop);
                if (null != testsearch) {
                    Log.warning("Search-Info: SearchService-" + numbprop + " from properties-file is " + nextprop + " : " + testsearch);
                    servicesBox.addItem(testsearch);
                    numbprop++;
                } else
                    numbprop_bool = false;
            }
        } catch (IOException ioe) {
            Log.error(ioe);
        }
    }
    if (servicesBox.getItemCount() > 0) {
        servicesBox.setSelectedIndex(0);
    }
    titlePanel = new TitlePanel("", "", SparkRes.getImageIcon(SparkRes.BLANK_IMAGE), true);
    add(titlePanel, new GridBagConstraints(0, 0, 3, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    // Add Search Service ComboBox
    final JLabel serviceLabel = new JLabel("");
    ResourceUtils.resLabel(serviceLabel, servicesBox, Res.getString("label.search.service") + ":");
    add(serviceLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
    add(servicesBox, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 150, 0));
    final JButton addService = new JButton();
    ResourceUtils.resButton(addService, Res.getString("button.add.service"));
    add(addService, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
    addService.addActionListener(actionEvent -> {
        final String serviceName = JOptionPane.showInputDialog(getRootPane(), Res.getString("message.name.of.search.service.question"), Res.getString("title.add.search.service"), JOptionPane.QUESTION_MESSAGE);
        if (ModelUtil.hasLength(serviceName)) {
            SwingWorker findServiceThread = new SwingWorker() {

                DataForm newForm;

                @Override
                public Object construct() {
                    try {
                        DomainBareJid serviceJid = JidCreate.domainBareFrom(serviceName);
                        newForm = searchManager.getSearchForm(serviceJid);
                    } catch (XMPPException | SmackException | XmppStringprepException | InterruptedException e) {
                    // Nothing to do
                    }
                    return newForm;
                }

                @Override
                public void finished() {
                    if (newForm == null) {
                        UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
                        JOptionPane.showMessageDialog(getGUI(), Res.getString("message.search.service.not.available"), Res.getString("title.notification"), JOptionPane.ERROR_MESSAGE);
                    } else {
                        servicesBox.addItem(serviceName);
                        servicesBox.setSelectedItem(serviceName);
                        int numbprop1 = 0;
                        boolean numbprop_bool1 = true;
                        String nextprop1, testsearch;
                        while (numbprop_bool1) {
                            nextprop1 = "search" + numbprop1;
                            testsearch = props.getProperty(nextprop1);
                            if (testsearch != null)
                                numbprop1++;
                            else {
                                Log.warning("Search-Service: " + nextprop1 + " : " + serviceName + " added");
                                props.setProperty(nextprop1, serviceName);
                                numbprop_bool1 = false;
                            }
                        }
                        try {
                            props.store(new FileOutputStream(pluginsettings), null);
                        } catch (IOException e) {
                            Log.error(e);
                        }
                    }
                }
            };
            findServiceThread.start();
        }
    });
    servicesBox.addActionListener(actionEvent -> {
        SwingWorker worker = new SwingWorker() {

            @Override
            public Object construct() {
                try {
                    Thread.sleep(50);
                } catch (Exception e) {
                    Log.error("Problem sleeping thread.", e);
                }
                return "ok";
            }

            @Override
            public void finished() {
                showService(getSearchService());
            }
        };
        worker.start();
    });
    add(cardPanel, new GridBagConstraints(0, 3, 3, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
}
Also used : GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) SmackException(org.jivesoftware.smack.SmackException) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) IOException(java.io.IOException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) TitlePanel(org.jivesoftware.spark.component.TitlePanel) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) XMPPException(org.jivesoftware.smack.XMPPException) FileOutputStream(java.io.FileOutputStream) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) SwingWorker(org.jivesoftware.spark.util.SwingWorker) XMPPException(org.jivesoftware.smack.XMPPException) DomainBareJid(org.jxmpp.jid.DomainBareJid)

Example 13 with TitlePanel

use of org.jivesoftware.spark.component.TitlePanel in project Spark by igniterealtime.

the class WorkgroupInvitationDialog method hasSelectedAgent.

/**
 * Returns the agent selected.
 *
 * @param chatRoom the room this roster will be used with.
 * @param transfer true if this will be a transfer.
 * @return true if a valid agent has been selected.
 */
public boolean hasSelectedAgent(ChatRoom chatRoom, boolean transfer) {
    final JiveTreeNode workgroupsNode = new JiveTreeNode("Workgroups", true);
    final JiveTreeNode queueNode = new JiveTreeNode("Queues", true);
    final DomainBareJid workgroupService = JidCreate.domainBareFromOrThrowUnchecked("workgroup." + SparkManager.getSessionManager().getServerAddress());
    final EntityFullJid jid = SparkManager.getSessionManager().getJID();
    EntityBareJid room = chatRoom.getBareJid();
    Collection<EntityBareJid> agents = null;
    try {
        agents = getAvailableAgents(FastpathPlugin.getAgentSession().getAgentRoster(), room);
    } catch (SmackException.NotConnectedException | InterruptedException e) {
        Log.warning("Unable to get agent roster.", e);
    }
    // Clear jid field
    jidField.setText("");
    String title = FpRes.getString("invite.agent");
    String acceptButton = FpRes.getString("invite");
    if (transfer) {
        title = FpRes.getString("transfer.to.agent");
        acceptButton = FpRes.getString("transfer");
    }
    String stockMessage = FpRes.getString("message.transfering.to.user");
    if (!transfer) {
        stockMessage = FpRes.getString("message.join.me.in.chat");
    }
    rootNode = new JiveTreeNode("Ask For Assistance", true);
    tree = new Tree(rootNode);
    tree.setCellRenderer(new JiveTreeCellRenderer());
    // This will remove lines from the tree and setup initial l&f
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setRowHeight(16);
    tree.setExpandsSelectedPaths(true);
    tree.addTreeSelectionListener(e -> {
        TreePath path = e.getNewLeadSelectionPath();
        Object o = path.getLastPathComponent();
        if (o instanceof JiveTreeNode) {
            JiveTreeNode node = (JiveTreeNode) o;
            JiveTreeNode parentNode = (JiveTreeNode) node.getParent();
            if (parentNode == workgroupsNode) {
                jidField.setText(node.getUserObject().toString() + "@" + workgroupService);
            } else if (parentNode == queueNode) {
                jidField.setText(FastpathPlugin.getWorkgroup().getWorkgroupJID() + "/" + node.getUserObject().toString());
            } else {
                EntityBareJid agent = getAgent();
                if (agent != null) {
                    jidField.setText(agent.toString());
                }
            }
        }
    });
    // Create message field
    messageField = new JTextArea();
    messageField.setWrapStyleWord(true);
    inviteLabel = new JLabel();
    if (transfer) {
        inviteLabel.setText(FpRes.getString("transfer.to") + ":");
    } else {
        inviteLabel.setText(FpRes.getString("invite") + ":");
    }
    if (transfer) {
        // Create the title panel for this dialog
        titlePanel = new TitlePanel(FpRes.getString("title.transfer"), FpRes.getString("message.transfer.to.another.agent"), FastpathRes.getImageIcon(FastpathRes.CHATTING_AGENT_IMAGE), true);
    } else {
        // Create the title panel for this dialog
        titlePanel = new TitlePanel(FpRes.getString("title.invitation"), FpRes.getString("message.invite.another.agent"), FastpathRes.getImageIcon(FastpathRes.CHATTING_AGENT_IMAGE), true);
    }
    // Build Tree
    Localpart joinedWorkgroupName = FastpathPlugin.getWorkgroup().getWorkgroupJID().getLocalpartOrThrow();
    final JiveTreeNode workgroupNode = new JiveTreeNode(joinedWorkgroupName, true);
    final Iterator<EntityBareJid> agentIter = agents.iterator();
    while (agentIter.hasNext()) {
        while (agentIter.hasNext()) {
            String agentName = UserManager.unescapeJID(agentIter.next().toString());
            final JiveTreeNode agentNode = new JiveTreeNode(agentName, false, FastpathRes.getImageIcon(FastpathRes.GREEN_BALL));
            workgroupNode.add(agentNode);
        }
        if (workgroupNode.getChildCount() > 0) {
            rootNode.add(workgroupNode);
        }
    }
    Collection<String> workgroupAgents;
    try {
        workgroupAgents = Agent.getWorkgroups(workgroupService, jid, SparkManager.getConnection());
    } catch (XMPPException | SmackException | InterruptedException e) {
        Log.error(e);
        workgroupAgents = Collections.EMPTY_LIST;
    }
    if (workgroupAgents.size() > 0) {
        // Add workgroups to combobox
        for (String workgroup : workgroupAgents) {
            String workgroupName = XmppStringUtils.parseLocalpart(workgroup);
            final JiveTreeNode wgNode = new JiveTreeNode(workgroupName, false, FastpathRes.getImageIcon(FastpathRes.FASTPATH_IMAGE_16x16));
            workgroupsNode.add(wgNode);
        }
        rootNode.add(workgroupsNode);
    }
    Iterator<WorkgroupQueue> iter = FastpathPlugin.getAgentSession().getQueues();
    while (iter.hasNext()) {
        final WorkgroupQueue queue = iter.next();
        if (queue.getStatus() == WorkgroupQueue.Status.OPEN) {
            final JiveTreeNode qNode = new JiveTreeNode(queue.getName().toString(), false, FastpathRes.getImageIcon(FastpathRes.FASTPATH_IMAGE_16x16));
            queueNode.add(qNode);
        }
    }
    rootNode.add(queueNode);
    // New Roster tree. Do not show agents at all.
    final UserManager userManager = SparkManager.getUserManager();
    Collection<?> jids = userManager.getUserJidsInRoom(room, false);
    roster = new WorkgroupRosterTree((Collection<? extends BareJid>) jids, false, workgroups);
    try {
        rosterTree = roster.getRosterTree();
        JiveTreeNode node = (JiveTreeNode) rosterTree.getModel().getRoot();
        rootNode.add(node);
    } catch (Exception e) {
        Log.error("Error checking for selected agent", e);
    }
    // Construct main panel w/ layout.
    final JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(titlePanel, BorderLayout.NORTH);
    // The user should only be able to close this dialog.
    Object[] options = { acceptButton, FpRes.getString("cancel") };
    // Create objects to add to mainPanel.
    final JPanel parentPanel = new JPanel();
    final JLabel messageLabel = new JLabel();
    final JLabel agentLabel = new JLabel();
    parentPanel.setLayout(new GridBagLayout());
    // Update Labels using ResourceUtils
    ResourceUtils.resLabel(messageLabel, messageField, FpRes.getString("label.message.to.agent"));
    ResourceUtils.resLabel(agentLabel, tree, FpRes.getString("label.select.agent"));
    // Add Components to Parent Panel
    parentPanel.add(agentLabel, new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
    parentPanel.add(new JScrollPane(tree), new GridBagConstraints(0, 1, 3, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 100));
    parentPanel.add(messageLabel, new GridBagConstraints(0, 3, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
    parentPanel.add(new JScrollPane(messageField), new GridBagConstraints(0, 4, 3, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 25));
    parentPanel.add(inviteLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
    parentPanel.add(jidField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    // Use line wrap.
    messageField.setLineWrap(true);
    // Update MessageField with stock message
    messageField.setText(stockMessage);
    // Add Parent Object to master OptionPane
    pane = new JOptionPane(parentPanel, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);
    mainPanel.add(pane, BorderLayout.CENTER);
    dlg = new JDialog(SparkManager.getChatManager().getChatContainer().getChatFrame(), title, true);
    dlg.pack();
    dlg.setSize(500, 500);
    dlg.setContentPane(mainPanel);
    dlg.setLocationRelativeTo(chatRoom);
    pane.addPropertyChangeListener(this);
    // expand the tree for displaying
    for (int i = 0; i <= tree.getRowCount(); i++) {
        tree.expandPath(tree.getPathForRow(i));
    }
    dlg.setVisible(true);
    dlg.toFront();
    dlg.requestFocus();
    return isValid;
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) JTextArea(javax.swing.JTextArea) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) DomainBareJid(org.jxmpp.jid.DomainBareJid) EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid) WorkgroupQueue(org.jivesoftware.smackx.workgroup.agent.WorkgroupQueue) JiveTreeCellRenderer(org.jivesoftware.spark.component.JiveTreeCellRenderer) BorderLayout(java.awt.BorderLayout) JiveTreeNode(org.jivesoftware.spark.component.JiveTreeNode) Localpart(org.jxmpp.jid.parts.Localpart) Tree(org.jivesoftware.spark.component.Tree) DomainBareJid(org.jxmpp.jid.DomainBareJid) EntityBareJid(org.jxmpp.jid.EntityBareJid) JScrollPane(javax.swing.JScrollPane) EntityFullJid(org.jxmpp.jid.EntityFullJid) SmackException(org.jivesoftware.smack.SmackException) JLabel(javax.swing.JLabel) JOptionPane(javax.swing.JOptionPane) TitlePanel(org.jivesoftware.spark.component.TitlePanel) SmackException(org.jivesoftware.smack.SmackException) XMPPException(org.jivesoftware.smack.XMPPException) TreePath(javax.swing.tree.TreePath) UserManager(org.jivesoftware.spark.UserManager) Collection(java.util.Collection) XMPPException(org.jivesoftware.smack.XMPPException) JDialog(javax.swing.JDialog)

Example 14 with TitlePanel

use of org.jivesoftware.spark.component.TitlePanel in project Spark by igniterealtime.

the class CheckUpdates method downloadUpdate.

public void downloadUpdate(final File downloadedFile, final SparkVersion version) {
    final java.util.Timer timer = new java.util.Timer();
    // Prepare HTTP post
    final GetMethod post = new GetMethod(version.getDownloadURL());
    // Get HTTP client
    Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443));
    final HttpClient httpclient = new HttpClient();
    String proxyHost = System.getProperty("http.proxyHost");
    String proxyPort = System.getProperty("http.proxyPort");
    if (ModelUtil.hasLength(proxyHost) && ModelUtil.hasLength(proxyPort)) {
        try {
            httpclient.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort));
        } catch (NumberFormatException e) {
            Log.error(e);
        }
    }
    try {
        int result = httpclient.executeMethod(post);
        if (result != 200) {
            return;
        }
        long length = post.getResponseContentLength();
        int contentLength = (int) length;
        bar = new JProgressBar(0, contentLength);
    } catch (IOException e) {
        Log.error(e);
    }
    final JFrame frame = new JFrame(Res.getString("title.downloading.im.client"));
    frame.setIconImage(SparkRes.getImageIcon(SparkRes.SMALL_MESSAGE_IMAGE).getImage());
    titlePanel = new TitlePanel(Res.getString("title.upgrading.client"), Res.getString("message.version", version.getVersion()), SparkRes.getImageIcon(SparkRes.SEND_FILE_24x24), true);
    final Thread thread = new Thread(() -> {
        try {
            InputStream stream = post.getResponseBodyAsStream();
            long size = post.getResponseContentLength();
            ByteFormat formater = new ByteFormat();
            sizeText = formater.format(size);
            titlePanel.setDescription(Res.getString("message.version", version.getVersion()) + " \n" + Res.getString("message.file.size", sizeText));
            downloadedFile.getParentFile().mkdirs();
            FileOutputStream out = new FileOutputStream(downloadedFile);
            copy(stream, out);
            out.close();
            if (!cancel) {
                downloadComplete = true;
                promptForInstallation(downloadedFile, Res.getString("title.download.complete"), Res.getString("message.restart.spark"));
            } else {
                out.close();
                downloadedFile.delete();
            }
            UPDATING = false;
            frame.dispose();
        } catch (Exception ex) {
        // Nothing to do
        } finally {
            timer.cancel();
            // Release current connection to the connection pool once you are done
            post.releaseConnection();
        }
    });
    frame.getContentPane().setLayout(new GridBagLayout());
    frame.getContentPane().add(titlePanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    frame.getContentPane().add(bar, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    JEditorPane pane = new JEditorPane();
    boolean displayContentPane = version.getChangeLogURL() != null || version.getDisplayMessage() != null;
    try {
        pane.setEditable(false);
        if (version.getChangeLogURL() != null) {
            pane.setEditorKit(new HTMLEditorKit());
            pane.setPage(version.getChangeLogURL());
        } else if (version.getDisplayMessage() != null) {
            pane.setText(version.getDisplayMessage());
        }
        if (displayContentPane) {
            frame.getContentPane().add(new JScrollPane(pane), new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
        }
    } catch (IOException e) {
        Log.error(e);
    }
    frame.getContentPane().setBackground(Color.WHITE);
    frame.pack();
    if (displayContentPane) {
        frame.setSize(600, 400);
    } else {
        frame.setSize(400, 100);
    }
    frame.setLocationRelativeTo(SparkManager.getMainWindow());
    GraphicUtils.centerWindowOnScreen(frame);
    frame.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent windowEvent) {
            thread.interrupt();
            cancel = true;
            UPDATING = false;
            if (!downloadComplete) {
                UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
                JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Res.getString("message.updating.cancelled"), Res.getString("title.cancelled"), JOptionPane.ERROR_MESSAGE);
            }
        }
    });
    frame.setVisible(true);
    thread.start();
    timer.scheduleAtFixedRate(new TimerTask() {

        int seconds = 1;

        @Override
        public void run() {
            ByteFormat formatter = new ByteFormat();
            long value = bar.getValue();
            long average = value / seconds;
            String text = formatter.format(average) + "/Sec";
            String total = formatter.format(value);
            titlePanel.setDescription(Res.getString("message.version", version.getVersion()) + " \n" + Res.getString("message.file.size", sizeText) + "\n" + Res.getString("message.transfer.rate") + ": " + text + "\n" + Res.getString("message.total.downloaded") + ": " + total);
            seconds++;
        }
    }, 1000, 1000);
}
Also used : GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) JProgressBar(javax.swing.JProgressBar) WindowAdapter(java.awt.event.WindowAdapter) TimerTask(java.util.TimerTask) JFrame(javax.swing.JFrame) ByteFormat(org.jivesoftware.spark.util.ByteFormat) Protocol(org.apache.commons.httpclient.protocol.Protocol) JScrollPane(javax.swing.JScrollPane) InputStream(java.io.InputStream) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) IOException(java.io.IOException) TitlePanel(org.jivesoftware.spark.component.TitlePanel) IOException(java.io.IOException) HttpClient(org.apache.commons.httpclient.HttpClient) FileOutputStream(java.io.FileOutputStream) WindowEvent(java.awt.event.WindowEvent) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JEditorPane(javax.swing.JEditorPane)

Example 15 with TitlePanel

use of org.jivesoftware.spark.component.TitlePanel in project Spark by igniterealtime.

the class InvitationDialog method inviteUsersToRoom.

public void inviteUsersToRoom(final String serviceName, Collection<BookmarkedConference> rooms, String adHocRoomName, Collection<String> jids) {
    fillRoomsUI(rooms, adHocRoomName);
    JFrame parent = SparkManager.getChatManager().getChatContainer().getChatFrame();
    if (parent == null || !parent.isVisible()) {
        parent = SparkManager.getMainWindow();
    }
    // Add jids to user list
    if (jids != null) {
        for (Object jid : jids) {
            invitedUsers.addElement(jid);
        }
    }
    final JOptionPane pane;
    TitlePanel titlePanel;
    // Create the title panel for this dialog
    titlePanel = new TitlePanel(Res.getString("title.invite.to.conference"), Res.getString("message.invite.users.to.conference"), SparkRes.getImageIcon(SparkRes.BLANK_IMAGE), true);
    // Construct main panel w/ layout.
    final JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(titlePanel, BorderLayout.NORTH);
    // The user should only be able to close this dialog.
    Object[] options = { Res.getString("invite"), Res.getString("cancel") };
    pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);
    mainPanel.add(pane, BorderLayout.CENTER);
    final JOptionPane p = new JOptionPane();
    dlg = p.createDialog(parent, Res.getString("title.conference.rooms"));
    dlg.setModal(false);
    dlg.pack();
    dlg.setSize(500, 450);
    dlg.setResizable(true);
    dlg.setContentPane(mainPanel);
    dlg.setLocationRelativeTo(parent);
    PropertyChangeListener changeListener = e -> {
        String value = (String) pane.getValue();
        if (Res.getString("cancel").equals(value)) {
            pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
            dlg.dispose();
        } else if (Res.getString("invite").equals(value)) {
            final String roomTitle = getSelectedRoomName();
            final BookmarkedConference selectedBookmarkedConf = getSelectedBookmarkedConference();
            int size = invitedUserList.getModel().getSize();
            UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
            if (size == 0) {
                JOptionPane.showMessageDialog(dlg, Res.getString("message.specify.users.to.join.conference"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                return;
            }
            if (!ModelUtil.hasLength(roomTitle)) {
                JOptionPane.showMessageDialog(dlg, Res.getString("message.no.room.to.join.error"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                return;
            }
            String roomName = "";
            // Add all rooms the user is in to list.
            ChatManager chatManager = SparkManager.getChatManager();
            for (ChatRoom chatRoom : chatManager.getChatContainer().getChatRooms()) {
                if (chatRoom instanceof GroupChatRoom) {
                    GroupChatRoom groupRoom = (GroupChatRoom) chatRoom;
                    if (groupRoom.getRoomname().equals(roomTitle)) {
                        roomName = groupRoom.getMultiUserChat().getRoom();
                        break;
                    }
                }
            }
            String message = messageField.getText();
            final String messageText = message != null ? message : Res.getString("message.please.join.in.conference");
            if (invitedUsers.getSize() > 0) {
                invitedUserList.setSelectionInterval(0, invitedUsers.getSize() - 1);
            }
            GroupChatRoom chatRoom;
            try {
                chatRoom = SparkManager.getChatManager().getGroupChat(roomName);
            } catch (ChatNotFoundException e1) {
                dlg.setVisible(false);
                final List<String> jidList = new ArrayList<>();
                Object[] jids1 = invitedUserList.getSelectedValues();
                final int no = jids1 != null ? jids1.length : 0;
                for (int i = 0; i < no; i++) {
                    try {
                        jidList.add((String) jids1[i]);
                    } catch (NullPointerException ee) {
                        Log.error(ee);
                    }
                }
                SwingWorker worker = new SwingWorker() {

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

                    public void finished() {
                        try {
                            if (selectedBookmarkedConf == null) {
                                ConferenceUtils.createPrivateConference(serviceName, messageText, roomTitle, jidList);
                            } else {
                                ConferenceUtils.joinConferenceOnSeperateThread(selectedBookmarkedConf.getName(), selectedBookmarkedConf.getJid(), selectedBookmarkedConf.getPassword(), messageText, jidList);
                            }
                        } catch (SmackException ex) {
                            UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
                            JOptionPane.showMessageDialog(pane, "An error occurred.", Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                        }
                    }
                };
                worker.start();
                pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                return;
            }
            pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
            dlg.dispose();
            Object[] values = invitedUserList.getSelectedValues();
            final int no = values != null ? values.length : 0;
            for (int i = 0; i < no; i++) {
                String jid = (String) values[i];
                try {
                    chatRoom.getMultiUserChat().invite(jid, message != null ? message : Res.getString("message.please.join.in.conference"));
                } catch (SmackException.NotConnectedException e1) {
                    Log.warning("Unable to send stanza to " + jid, e1);
                }
                String nickname = SparkManager.getUserManager().getUserNicknameFromJID(jid);
                chatRoom.getTranscriptWindow().insertNotificationMessage("Invited " + nickname, ChatManager.NOTIFICATION_COLOR);
            }
        }
    };
    pane.addPropertyChangeListener(changeListener);
    dlg.setVisible(true);
    dlg.toFront();
    dlg.requestFocus();
}
Also used : SmackException(org.jivesoftware.smack.SmackException) UIManager(javax.swing.UIManager) Insets(java.awt.Insets) JDialog(javax.swing.JDialog) JTextField(javax.swing.JTextField) SettingsManager(org.jivesoftware.sparkimpl.settings.local.SettingsManager) Res(org.jivesoftware.resource.Res) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) Log(org.jivesoftware.spark.util.log.Log) Action(javax.swing.Action) ResourceUtils(org.jivesoftware.spark.util.ResourceUtils) ArrayList(java.util.ArrayList) MouseAdapter(java.awt.event.MouseAdapter) JComboBox(javax.swing.JComboBox) JFrame(javax.swing.JFrame) BorderLayout(java.awt.BorderLayout) JComponent(javax.swing.JComponent) ChatManager(org.jivesoftware.spark.ChatManager) ModelUtil(org.jivesoftware.spark.util.ModelUtil) JButton(javax.swing.JButton) SparkRes(org.jivesoftware.resource.SparkRes) JPopupMenu(javax.swing.JPopupMenu) Collection(java.util.Collection) JList(javax.swing.JList) SwingWorker(org.jivesoftware.spark.util.SwingWorker) JOptionPane(javax.swing.JOptionPane) GridBagConstraints(java.awt.GridBagConstraints) TitlePanel(org.jivesoftware.spark.component.TitlePanel) GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) ActionEvent(java.awt.event.ActionEvent) XmppStringUtils(org.jxmpp.util.XmppStringUtils) MouseEvent(java.awt.event.MouseEvent) JScrollPane(javax.swing.JScrollPane) ChatNotFoundException(org.jivesoftware.spark.ChatNotFoundException) List(java.util.List) DefaultListModel(javax.swing.DefaultListModel) AbstractAction(javax.swing.AbstractAction) PropertyChangeListener(java.beans.PropertyChangeListener) BookmarkedConference(org.jivesoftware.smackx.bookmarks.BookmarkedConference) SparkManager(org.jivesoftware.spark.SparkManager) JLabel(javax.swing.JLabel) RosterPickList(org.jivesoftware.spark.ui.RosterPickList) GridBagLayout(java.awt.GridBagLayout) JPanel(javax.swing.JPanel) JPanel(javax.swing.JPanel) PropertyChangeListener(java.beans.PropertyChangeListener) SmackException(org.jivesoftware.smack.SmackException) BookmarkedConference(org.jivesoftware.smackx.bookmarks.BookmarkedConference) JOptionPane(javax.swing.JOptionPane) TitlePanel(org.jivesoftware.spark.component.TitlePanel) GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) ChatNotFoundException(org.jivesoftware.spark.ChatNotFoundException) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) SwingWorker(org.jivesoftware.spark.util.SwingWorker) ArrayList(java.util.ArrayList) JList(javax.swing.JList) List(java.util.List) RosterPickList(org.jivesoftware.spark.ui.RosterPickList) ChatManager(org.jivesoftware.spark.ChatManager)

Aggregations

TitlePanel (org.jivesoftware.spark.component.TitlePanel)25 JPanel (javax.swing.JPanel)19 JOptionPane (javax.swing.JOptionPane)17 JDialog (javax.swing.JDialog)15 PropertyChangeListener (java.beans.PropertyChangeListener)14 BorderLayout (java.awt.BorderLayout)13 GridBagConstraints (java.awt.GridBagConstraints)13 Insets (java.awt.Insets)13 GridBagLayout (java.awt.GridBagLayout)12 JLabel (javax.swing.JLabel)11 SmackException (org.jivesoftware.smack.SmackException)11 Res (org.jivesoftware.resource.Res)10 SparkManager (org.jivesoftware.spark.SparkManager)10 SparkRes (org.jivesoftware.resource.SparkRes)8 XMPPException (org.jivesoftware.smack.XMPPException)8 Log (org.jivesoftware.spark.util.log.Log)8 JScrollPane (javax.swing.JScrollPane)7 JTextField (javax.swing.JTextField)7 ResourceUtils (org.jivesoftware.spark.util.ResourceUtils)7 SwingWorker (org.jivesoftware.spark.util.SwingWorker)7