Search in sources :

Example 1 with Form

use of org.jivesoftware.smackx.xdata.form.Form in project Spark by igniterealtime.

the class ChatSearch method search.

public void search(String query) {
    final List<ChatSearchResult> results = new ArrayList<>();
    AgentSession agentSession = FastpathPlugin.getAgentSession();
    try {
        Form form = agentSession.getTranscriptSearchForm();
        FillableForm filledForm = form.getFillableForm();
        filledForm.setAnswer("queryString", query);
        // Define Workgroups
        final List<Jid> workgroups = new ArrayList<>();
        workgroups.add(FastpathPlugin.getWorkgroup().getWorkgroupJID());
        List<String> workgroupStrings = JidUtil.toStringList(workgroups);
        filledForm.setAnswer("workgroups", workgroupStrings);
        ReportedData reportedData;
        try {
            reportedData = agentSession.searchTranscripts(filledForm);
            for (final ReportedData.Row row : reportedData.getRows()) {
                ChatSearchResult result = new ChatSearchResult(row, query);
                results.add(result);
            }
        } catch (XMPPException | SmackException | InterruptedException e) {
            Log.error(e);
        }
        results.sort(dateComparator);
        DefaultListModel<SearchItem> model = new DefaultListModel<>();
        final JList<SearchItem> list = new JList<>(model);
        list.setCellRenderer(new HistoryItemRenderer());
        for (ChatSearchResult result : results) {
            String person = result.getUsername();
            String question = result.getQuestion();
            String sessionID = result.getSessionID();
            Date date = result.getStartDate();
            final SearchItem item = new SearchItem(person, date, question);
            item.setSessionID(sessionID);
            model.addElement(item);
        }
        list.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    SearchItem item = list.getSelectedValue();
                    Transcript transcript = null;
                    try {
                        transcript = FastpathPlugin.getAgentSession().getTranscript(item.getSessionID());
                    } catch (XMPPException | SmackException | InterruptedException ee) {
                        Log.error("Error showing transcripts.", ee);
                    }
                    if (transcript == null) {
                        return;
                    }
                    ChatViewer chatViewer = new ChatViewer(transcript);
                    final JFrame frame = new JFrame(FpRes.getString("title.chat.transcript"));
                    frame.setIconImage(SparkManager.getMainWindow().getIconImage());
                    frame.getContentPane().setLayout(new BorderLayout());
                    frame.getContentPane().add(chatViewer, BorderLayout.CENTER);
                    frame.pack();
                    frame.setSize(600, 400);
                    frame.setLocationRelativeTo(SparkManager.getMainWindow());
                    frame.setVisible(true);
                }
            }
        });
        JScrollPane scrollPane = new JScrollPane(list);
        scrollPane.getViewport().setBackground(Color.white);
        JFrame frame = new JFrame(FpRes.getString("title.search.results"));
        frame.setIconImage(SparkManager.getMainWindow().getIconImage());
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        final BackgroundPane titlePane = new BackgroundPane() {

            private static final long serialVersionUID = -5603280927139789177L;

            public Dimension getPreferredSize() {
                final Dimension size = super.getPreferredSize();
                size.width = 0;
                return size;
            }
        };
        titlePane.setLayout(new GridBagLayout());
        titlePane.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY));
        JLabel userImage = new JLabel(FastpathRes.getImageIcon(FastpathRes.FASTPATH_IMAGE_24x24));
        userImage.setHorizontalAlignment(JLabel.LEFT);
        userImage.setText(FpRes.getString("title.chat.transcripts.search"));
        titlePane.add(userImage, new GridBagConstraints(0, 0, 4, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        userImage.setFont(new Font("Dialog", Font.BOLD, 12));
        JLabel itemsFound = new JLabel(Integer.toString(results.size()));
        titlePane.add(new JLabel(FpRes.getString("title.number.of.conversations.found")), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        titlePane.add(itemsFound, new GridBagConstraints(1, 1, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        JLabel searchTerm = new JLabel(FpRes.getString("query") + ": " + query);
        titlePane.add(searchTerm, new GridBagConstraints(0, 2, 4, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
        frame.getContentPane().add(titlePane, BorderLayout.NORTH);
        frame.pack();
        frame.setSize(400, 400);
        GraphicUtils.centerWindowOnScreen(frame);
        frame.setVisible(true);
    } catch (XMPPException | SmackException | InterruptedException e) {
        Log.error(e);
    }
}
Also used : GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) Form(org.jivesoftware.smackx.xdata.form.Form) FillableForm(org.jivesoftware.smackx.xdata.form.FillableForm) ArrayList(java.util.ArrayList) ChatViewer(org.jivesoftware.fastpath.workspace.panes.ChatViewer) FillableForm(org.jivesoftware.smackx.xdata.form.FillableForm) BackgroundPane(org.jivesoftware.fastpath.workspace.panes.BackgroundPane) Font(java.awt.Font) BorderLayout(java.awt.BorderLayout) AgentSession(org.jivesoftware.smackx.workgroup.agent.AgentSession) Transcript(org.jivesoftware.smackx.workgroup.packet.Transcript) MouseEvent(java.awt.event.MouseEvent) Jid(org.jxmpp.jid.Jid) SmackException(org.jivesoftware.smack.SmackException) MouseAdapter(java.awt.event.MouseAdapter) Dimension(java.awt.Dimension) Date(java.util.Date) HistoryItemRenderer(org.jivesoftware.fastpath.workspace.panes.HistoryItemRenderer) XMPPException(org.jivesoftware.smack.XMPPException) ReportedData(org.jivesoftware.smackx.search.ReportedData)

Example 2 with Form

use of org.jivesoftware.smackx.xdata.form.Form in project Spark by igniterealtime.

the class WorkgroupManager method showWorkgroup.

private void showWorkgroup(final ContactItem contactItem) throws Exception {
    VCard vcard = SparkManager.getVCardManager().getVCard();
    final Map<String, String> variables = new HashMap<>();
    String firstName = vcard.getFirstName();
    String lastName = vcard.getLastName();
    if (ModelUtil.hasLength(firstName) && ModelUtil.hasLength(lastName)) {
        variables.put("username", firstName + " " + lastName);
    } else if (ModelUtil.hasLength(firstName)) {
        variables.put("username", firstName);
    } else if (ModelUtil.hasLength(lastName)) {
        variables.put("username", lastName);
    }
    String email = vcard.getEmailHome();
    String emailWork = vcard.getEmailWork();
    if (ModelUtil.hasLength(emailWork)) {
        email = emailWork;
    }
    if (ModelUtil.hasLength(email)) {
        variables.put("email", email);
    }
    EntityBareJid workgroupJID = contactItem.getJid().asEntityBareJidOrThrow();
    Localpart nameOfWorkgroup = workgroupJID.getLocalpart();
    final JDialog workgroupDialog = new JDialog(SparkManager.getMainWindow(), "Contact " + nameOfWorkgroup + " Workgroup");
    Workgroup workgroup = new Workgroup(workgroupJID, SparkManager.getConnection());
    final Form workgroupForm = workgroup.getWorkgroupForm();
    String welcomeText = FormText.getWelcomeText(workgroupJID.toString());
    String startButton = FormText.getStartButton(workgroupJID.toString());
    final WorkgroupDataForm formUI = new WorkgroupDataForm(workgroupForm, variables);
    formUI.setBackground(Color.white);
    final JPanel titlePane = new LiveTitlePane("Contact Workgroup", FastpathRes.getImageIcon(FastpathRes.FASTPATH_IMAGE_24x24)) {

        private static final long serialVersionUID = -4484940286068835770L;

        public Dimension getPreferredSize() {
            final Dimension size = super.getPreferredSize();
            size.width = 400;
            return size;
        }
    };
    formUI.add(titlePane, new GridBagConstraints(0, 0, 3, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    final JButton submitButton = new JButton("Start Chat!");
    submitButton.addActionListener(e -> {
        FillableForm form = formUI.getFilledForm();
        if (validateForm(workgroupDialog, workgroupForm, form)) {
            enterQueue(contactItem.getJid().asEntityBareJidOrThrow(), form);
            workgroupDialog.dispose();
        }
    });
    formUI.setEnterListener(() -> {
        FillableForm form = formUI.getFilledForm();
        if (validateForm(workgroupDialog, workgroupForm, form)) {
            enterQueue(contactItem.getJid().asEntityBareJidOrThrow(), form);
            workgroupDialog.dispose();
        }
    });
    formUI.add(submitButton, new GridBagConstraints(1, 100, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
    workgroupDialog.getContentPane().setLayout(new BorderLayout());
    workgroupDialog.getContentPane().add(formUI, BorderLayout.CENTER);
    workgroupDialog.pack();
    GraphicUtils.centerWindowOnScreen(workgroupDialog);
    workgroupDialog.setVisible(true);
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) HashMap(java.util.HashMap) Form(org.jivesoftware.smackx.xdata.form.Form) FilledForm(org.jivesoftware.smackx.xdata.form.FilledForm) FillableForm(org.jivesoftware.smackx.xdata.form.FillableForm) JButton(javax.swing.JButton) FillableForm(org.jivesoftware.smackx.xdata.form.FillableForm) Dimension(java.awt.Dimension) Workgroup(org.jivesoftware.smackx.workgroup.user.Workgroup) BorderLayout(java.awt.BorderLayout) Localpart(org.jxmpp.jid.parts.Localpart) VCard(org.jivesoftware.smackx.vcardtemp.packet.VCard) EntityBareJid(org.jxmpp.jid.EntityBareJid) JDialog(javax.swing.JDialog)

Example 3 with Form

use of org.jivesoftware.smackx.xdata.form.Form in project Spark by igniterealtime.

the class Workpane method addRoomInfo.

private void addRoomInfo(final CharSequence sessionID, final RequestUtils utils, final ChatRoom room) {
    final JTabbedPane tabbedPane = new JTabbedPane();
    GroupChatParticipantList participantList = ((GroupChatRoom) room).getConferenceRoomInfo();
    room.getSplitPane().setRightComponent(tabbedPane);
    Form form;
    try {
        form = FastpathPlugin.getWorkgroup().getWorkgroupForm();
    } catch (XMPPException | SmackException | InterruptedException e) {
        Log.error(e);
        return;
    }
    final BackgroundPane transcriptAlert = new BackgroundPane() {

        public Dimension getPreferredSize() {
            final Dimension size = super.getPreferredSize();
            size.width = 0;
            return size;
        }
    };
    transcriptAlert.setLayout(new GridBagLayout());
    JLabel userImage = new JLabel(FastpathRes.getImageIcon(FastpathRes.FASTPATH_IMAGE_24x24));
    userImage.setHorizontalAlignment(JLabel.LEFT);
    userImage.setText(utils.getUsername());
    transcriptAlert.add(userImage, new GridBagConstraints(0, 0, 4, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    userImage.setFont(new Font("Dialog", Font.BOLD, 12));
    final RoomInformation roomInformation = new RoomInformation();
    roomInformation.showFormInformation(form, utils);
    final UserHistory userHistory = new UserHistory(utils.getUserID());
    final JScrollPane userScroll = new JScrollPane(roomInformation);
    final JScrollPane historyScroll = new JScrollPane(userHistory);
    historyScroll.getVerticalScrollBar().setBlockIncrement(50);
    historyScroll.getVerticalScrollBar().setUnitIncrement(20);
    tabbedPane.addTab(FpRes.getString("tab.user.info"), userScroll);
    tabbedPane.addTab(FpRes.getString("tab.participants"), participantList);
    tabbedPane.addTab(FpRes.getString("tab.user.history"), historyScroll);
    final Notes notes = new Notes(sessionID, room);
    tabbedPane.addTab(FpRes.getString("tab.notes"), notes);
    tabbedPane.addChangeListener(new ChangeListener() {

        boolean loaded;

        public void stateChanged(ChangeEvent e) {
            if (!loaded) {
                if (tabbedPane.getSelectedComponent() == historyScroll) {
                    userHistory.loadHistory();
                    loaded = true;
                }
            }
        }
    });
    final GroupChatRoom groupRoom = (GroupChatRoom) room;
    room.getTranscriptWindow().clear();
    final ChatFrame frame = SparkManager.getChatManager().getChatContainer().getChatFrame();
    if (frame != null) {
        int height = frame.getHeight();
        int width = frame.getWidth();
        if (height < 400) {
            height = 400;
        }
        if (width < 600) {
            width = 600;
        }
        frame.setSize(width, height);
        frame.validate();
        frame.repaint();
    }
    fireFastPathChatOpened(room, sessionID.toString(), utils, tabbedPane);
    DataManager.getInstance().setMetadataForRoom(room, utils.getMetadata());
}
Also used : GroupChatParticipantList(org.jivesoftware.spark.ui.conferences.GroupChatParticipantList) Form(org.jivesoftware.smackx.xdata.form.Form) SmackException(org.jivesoftware.smack.SmackException) GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) ChangeEvent(javax.swing.event.ChangeEvent) ChangeListener(javax.swing.event.ChangeListener) XMPPException(org.jivesoftware.smack.XMPPException)

Example 4 with Form

use of org.jivesoftware.smackx.xdata.form.Form in project Smack by igniterealtime.

the class FormTest method testFilloutForm.

/**
 * 1. Create a form to fill out and send it to the other user
 * 2. Retrieve the form to fill out, complete it and return it to the requestor
 * 3. Retrieve the completed form and check that everything is OK
 *
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws NotConnectedException if the XMPP connection is not connected.
 */
@SuppressWarnings("deprecation")
@SmackIntegrationTest
public void testFilloutForm() throws NotConnectedException, InterruptedException {
    DataForm.Builder formToSend = DataForm.builder(DataForm.Type.form);
    formToSend.setInstructions("Fill out this form to report your case.\nThe case will be created automatically.");
    formToSend.setTitle("Case configurations");
    formToSend.setFormType("https://igniterealtime.org/projects/smack/sinttest/form-test/1");
    // Add a hidden variable
    FormField field = FormField.hiddenBuilder("hidden_var").setValue("Some value for the hidden variable").build();
    formToSend.addField(field);
    // Add a fixed variable
    field = FormField.fixedBuilder().setValue("Section 1: Case description").build();
    formToSend.addField(field);
    // Add a text-single variable
    field = FormField.textSingleBuilder("name").setLabel("Enter a name for the case").build();
    formToSend.addField(field);
    // Add a text-multi variable
    field = FormField.textMultiBuilder("description").setLabel("Enter a description").build();
    formToSend.addField(field);
    // Add a boolean variable
    field = FormField.booleanBuilder("time").setLabel("Is this your first case?").build();
    formToSend.addField(field);
    // Add a text variable where an int value is expected
    field = FormField.textSingleBuilder("age").setLabel("How old are you?").build();
    formToSend.addField(field);
    // Create the chats between the two participants
    org.jivesoftware.smack.chat.Chat chat = org.jivesoftware.smack.chat.ChatManager.getInstanceFor(conOne).createChat(conTwo.getUser(), null);
    StanzaCollector collector = conOne.createStanzaCollector(new ThreadFilter(chat.getThreadID()));
    StanzaCollector collector2 = conTwo.createStanzaCollector(new ThreadFilter(chat.getThreadID()));
    Message msg = StanzaBuilder.buildMessage().setBody("To enter a case please fill out this form and send it back to me").addExtension(formToSend.build()).build();
    try {
        // Send the message with the form to fill out
        chat.sendMessage(msg);
        // Get the message with the form to fill out
        Message msg2 = collector2.nextResult();
        assertNotNull(msg2, "Message not found");
        // Retrieve the form to fill out
        Form formToRespond = Form.from(msg2);
        assertNotNull(formToRespond);
        assertNotNull(formToRespond.getField("name"));
        assertNotNull(formToRespond.getField("description"));
        // Obtain the form to send with the replies
        final FillableForm completedForm = formToRespond.getFillableForm();
        assertNotNull(completedForm.getField("hidden_var"));
        // Check that a field of type String does not accept booleans
        assertThrows(IllegalArgumentException.class, () -> completedForm.setAnswer("name", true));
        completedForm.setAnswer("name", "Credit card number invalid");
        completedForm.setAnswer("description", "The ATM says that my credit card number is invalid. What's going on?");
        completedForm.setAnswer("time", true);
        completedForm.setAnswer("age", 20);
        // Create a new message to send with the completed form
        msg2 = StanzaBuilder.buildMessage().to(conOne.getUser().asBareJid()).setThread(msg.getThread()).ofType(Message.Type.chat).setBody("To enter a case please fill out this form and send it back to me").addExtension(completedForm.getDataFormToSubmit()).build();
        // Send the message with the completed form
        conTwo.sendStanza(msg2);
        // Get the message with the completed form
        Message msg3 = collector.nextResult();
        assertNotNull(msg3, "Message not found");
        // Retrieve the completed form
        final DataForm completedForm2 = DataForm.from(msg3);
        assertNotNull(completedForm2);
        assertNotNull(completedForm2.getField("name"));
        assertNotNull(completedForm2.getField("description"));
        assertEquals(completedForm2.getField("name").getValues().get(0).toString(), "Credit card number invalid");
        assertNotNull(completedForm2.getField("time"));
        assertNotNull(completedForm2.getField("age"));
        assertEquals("20", completedForm2.getField("age").getValues().get(0).toString(), "The age is bad");
    } finally {
        collector.cancel();
        collector2.cancel();
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) Form(org.jivesoftware.smackx.xdata.form.Form) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) FillableForm(org.jivesoftware.smackx.xdata.form.FillableForm) ThreadFilter(org.jivesoftware.smack.filter.ThreadFilter) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) FillableForm(org.jivesoftware.smackx.xdata.form.FillableForm) StanzaCollector(org.jivesoftware.smack.StanzaCollector) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 5 with Form

use of org.jivesoftware.smackx.xdata.form.Form in project Smack by igniterealtime.

the class MultiUserChat method getConfigurationForm.

/**
 * Returns the room's configuration form that the room's owner can use.
 * The configuration form allows to set the room's language,
 * enable logging, specify room's type, etc..
 *
 * @return the Form that contains the fields to complete together with the instrucions or
 * <code>null</code> if no configuration is possible.
 * @throws XMPPErrorException if an error occurs asking the configuration form for the room.
 * @throws NoResponseException if there was no response from the server.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public Form getConfigurationForm() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    MUCOwner iq = new MUCOwner();
    iq.setTo(room);
    iq.setType(IQ.Type.get);
    IQ answer = connection.sendIqRequestAndWaitForResponse(iq);
    DataForm dataForm = DataForm.from(answer, MucConfigFormManager.FORM_TYPE);
    return new Form(dataForm);
}
Also used : Form(org.jivesoftware.smackx.xdata.form.Form) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) FillableForm(org.jivesoftware.smackx.xdata.form.FillableForm) IQ(org.jivesoftware.smack.packet.IQ) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) MUCOwner(org.jivesoftware.smackx.muc.packet.MUCOwner)

Aggregations

Form (org.jivesoftware.smackx.xdata.form.Form)7 FillableForm (org.jivesoftware.smackx.xdata.form.FillableForm)5 GridBagConstraints (java.awt.GridBagConstraints)3 Insets (java.awt.Insets)3 SmackException (org.jivesoftware.smack.SmackException)3 XMPPException (org.jivesoftware.smack.XMPPException)3 DataForm (org.jivesoftware.smackx.xdata.packet.DataForm)3 BorderLayout (java.awt.BorderLayout)2 Dimension (java.awt.Dimension)2 Font (java.awt.Font)2 MouseAdapter (java.awt.event.MouseAdapter)2 MouseEvent (java.awt.event.MouseEvent)2 Date (java.util.Date)2 JPanel (javax.swing.JPanel)2 IQ (org.jivesoftware.smack.packet.IQ)2 Color (java.awt.Color)1 FlowLayout (java.awt.FlowLayout)1 GridBagLayout (java.awt.GridBagLayout)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1