Search in sources :

Example 1 with ContextMenuListener

use of org.jivesoftware.spark.plugin.ContextMenuListener in project Spark by igniterealtime.

the class ConferenceServices method addPopupListeners.

private void addPopupListeners() {
    final ContactList contactList = SparkManager.getWorkspace().getContactList();
    // Add ContactList items.
    final Action inviteAllAction = new AbstractAction() {

        private static final long serialVersionUID = -7486282521151183678L;

        public void actionPerformed(ActionEvent actionEvent) {
            Collection<ContactItem> contacts = contactList.getActiveGroup().getContactItems();
            startConference(contacts);
        }
    };
    inviteAllAction.putValue(Action.NAME, Res.getString("menuitem.invite.group.to.conference"));
    inviteAllAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.CONFERENCE_IMAGE_16x16));
    final Action conferenceAction = new AbstractAction() {

        private static final long serialVersionUID = 4724119680969496581L;

        public void actionPerformed(ActionEvent actionEvent) {
            Collection<ContactItem> contacts = contactList.getSelectedUsers();
            startConference(contacts);
        }
    };
    conferenceAction.putValue(Action.NAME, Res.getString("menuitem.start.a.conference"));
    conferenceAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_WORKGROUP_QUEUE_IMAGE));
    contactList.addContextMenuListener(new ContextMenuListener() {

        public void poppingUp(Object component, JPopupMenu popup) {
            Collection<ContactItem> col = contactList.getSelectedUsers();
            if (component instanceof ContactGroup) {
                popup.add(inviteAllAction);
            } else if (component instanceof Collection<?> && col.size() > 0) {
                popup.add(conferenceAction);
            }
        }

        public void poppingDown(JPopupMenu popup) {
        }

        public boolean handleDefaultAction(MouseEvent e) {
            return false;
        }
    });
    // Add to Actions Menu
    final JMenu actionsMenu = SparkManager.getMainWindow().getMenuByName(Res.getString("menuitem.actions"));
    actionsMenu.add(conferenceAction);
}
Also used : MouseEvent(java.awt.event.MouseEvent) ActionEvent(java.awt.event.ActionEvent) ContextMenuListener(org.jivesoftware.spark.plugin.ContextMenuListener) Collection(java.util.Collection)

Example 2 with ContextMenuListener

use of org.jivesoftware.spark.plugin.ContextMenuListener in project Spark by igniterealtime.

the class ContactListAssistantPlugin method initialize.

@Override
public void initialize() {
    moveToMenu = new JMenu(Res.getString("menuitem.move.to"));
    copyToMenu = new JMenu(Res.getString("menuitem.copy.to"));
    localPreferences = new LocalPreferences();
    final ContactList contactList = SparkManager.getContactList();
    contactList.addContextMenuListener(new ContextMenuListener() {

        @Override
        public void poppingUp(Object object, final JPopupMenu popup) {
            final Collection<ContactItem> contactItems = Collections.unmodifiableCollection(contactList.getSelectedUsers());
            if (!contactItems.isEmpty()) {
                final List<ContactGroup> contactGroups = contactList.getContactGroups();
                Collections.sort(contactGroups, ContactList.GROUP_COMPARATOR);
                for (final ContactGroup group : contactGroups) {
                    if (group.isUnfiledGroup() || group.isOfflineGroup()) {
                        continue;
                    }
                    if (isContactItemInGroup(contactItems, group)) {
                        continue;
                    }
                    final Action moveAction = new AbstractAction() {

                        private static final long serialVersionUID = 6542011870221162331L;

                        @Override
                        public void actionPerformed(ActionEvent actionEvent) {
                            moveItems(contactItems, group.getGroupName());
                        }
                    };
                    final Action copyAction = new AbstractAction() {

                        private static final long serialVersionUID = 2232885525630977329L;

                        @Override
                        public void actionPerformed(ActionEvent actionEvent) {
                            copyItems(contactItems, group.getGroupName());
                        }
                    };
                    moveAction.putValue(Action.NAME, group.getGroupName());
                    moveToMenu.add(moveAction);
                    copyAction.putValue(Action.NAME, group.getGroupName());
                    copyToMenu.add(copyAction);
                }
                popup.addPopupMenuListener(new PopupMenuListener() {

                    @Override
                    public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {
                    }

                    @Override
                    public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) {
                        moveToMenu.removeAll();
                        copyToMenu.removeAll();
                        popup.removePopupMenuListener(this);
                    }

                    @Override
                    public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) {
                        moveToMenu.removeAll();
                        copyToMenu.removeAll();
                        popup.removePopupMenuListener(this);
                    }
                });
                int index = -1;
                if (!Default.getBoolean("DISABLE_RENAMES") && Enterprise.containsFeature(Enterprise.RENAMES_FEATURE)) {
                    for (int i = 0; i < popup.getComponentCount(); i++) {
                        Object o = popup.getComponent(i);
                        if (o instanceof JMenuItem && ((JMenuItem) o).getText().equals(Res.getString("menuitem.rename"))) {
                            index = i;
                            break;
                        }
                    }
                } else
                    index = 3;
                if (contactItems.size() == 1) {
                    // Add MOVE/COPY options right after the RENAME option or in it's place if it doesn't exist.
                    if (index != -1) {
                        // See if we should disable the "Move to" and "Copy to" menu options
                        if (!Default.getBoolean("DISABLE_MOVE_AND_COPY") && Enterprise.containsFeature(Enterprise.MOVE_COPY_FEATURE)) {
                            popup.add(moveToMenu, index + 1);
                            popup.add(copyToMenu, index + 2);
                        }
                    }
                } else if (contactItems.size() > 1) {
                    // See if we should disable the "Move to" and "Copy to" menu options
                    if (!Default.getBoolean("DISABLE_MOVE_AND_COPY") && Enterprise.containsFeature(Enterprise.MOVE_COPY_FEATURE)) {
                        popup.addSeparator();
                        popup.add(moveToMenu);
                        popup.add(copyToMenu);
                    }
                    // Clean up the extra separator if "Broadcast" menu items are disabled
                    if (!Default.getBoolean("DISABLE_BROADCAST_MENU_ITEM") && Enterprise.containsFeature(Enterprise.BROADCAST_FEATURE))
                        popup.addSeparator();
                }
            }
        }

        @Override
        public void poppingDown(JPopupMenu popup) {
        }

        @Override
        public boolean handleDefaultAction(MouseEvent e) {
            return false;
        }
    });
    updateAvatarsInContactList();
    SettingsManager.addPreferenceListener(preference -> updateAvatarsInContactList());
}
Also used : Action(javax.swing.Action) AbstractAction(javax.swing.AbstractAction) MouseEvent(java.awt.event.MouseEvent) ActionEvent(java.awt.event.ActionEvent) PopupMenuListener(javax.swing.event.PopupMenuListener) ContextMenuListener(org.jivesoftware.spark.plugin.ContextMenuListener) ContactList(org.jivesoftware.spark.ui.ContactList) PopupMenuEvent(javax.swing.event.PopupMenuEvent) JPopupMenu(javax.swing.JPopupMenu) Collection(java.util.Collection) List(java.util.List) ContactList(org.jivesoftware.spark.ui.ContactList) LocalPreferences(org.jivesoftware.sparkimpl.settings.local.LocalPreferences) ContactGroup(org.jivesoftware.spark.ui.ContactGroup) JMenuItem(javax.swing.JMenuItem) AbstractAction(javax.swing.AbstractAction) JMenu(javax.swing.JMenu)

Example 3 with ContextMenuListener

use of org.jivesoftware.spark.plugin.ContextMenuListener in project Spark by igniterealtime.

the class PresenceChangePlugin method initialize.

public void initialize() {
    // Listen for right-clicks on ContactItem
    final ContactList contactList = SparkManager.getWorkspace().getContactList();
    final Action listenAction = new AbstractAction() {

        private static final long serialVersionUID = 7705539667621148816L;

        public void actionPerformed(ActionEvent e) {
            for (ContactItem item : contactList.getSelectedUsers()) {
                String bareAddress = XmppStringUtils.parseBareJid(item.getJID());
                sparkContacts.add(bareAddress);
            }
        }
    };
    listenAction.putValue(Action.NAME, Res.getString("menuitem.alert.when.online"));
    listenAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_ALARM_CLOCK));
    final Action removeAction = new AbstractAction() {

        private static final long serialVersionUID = -8726129089417116105L;

        public void actionPerformed(ActionEvent e) {
            for (ContactItem item : contactList.getSelectedUsers()) {
                String bareAddress = XmppStringUtils.parseBareJid(item.getJID());
                sparkContacts.remove(bareAddress);
            }
        }
    };
    removeAction.putValue(Action.NAME, Res.getString("menuitem.remove.alert.when.online"));
    removeAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_DELETE));
    contactList.addContextMenuListener(new ContextMenuListener() {

        public void poppingUp(Object object, JPopupMenu popup) {
            if (object instanceof ContactItem) {
                ContactItem item = (ContactItem) object;
                String bareAddress = XmppStringUtils.parseBareJid(item.getJID());
                if (!item.getPresence().isAvailable() || item.getPresence().isAway()) {
                    if (sparkContacts.contains(bareAddress)) {
                        popup.add(removeAction);
                    } else {
                        popup.add(listenAction);
                    }
                }
            }
        }

        public void poppingDown(JPopupMenu popup) {
        }

        public boolean handleDefaultAction(MouseEvent e) {
            return false;
        }
    });
    // Check presence changes
    SparkManager.getConnection().addAsyncStanzaListener(stanza -> {
        try {
            Presence presence = (Presence) stanza;
            if (!presence.isAvailable() || presence.isAway()) {
                return;
            }
            String from = presence.getFrom();
            ArrayList<String> removelater = new ArrayList<>();
            for (final String jid : sparkContacts) {
                if (jid.equals(XmppStringUtils.parseBareJid(from))) {
                    removelater.add(jid);
                    // sparkContacts.remove(jid);
                    String nickname = SparkManager.getUserManager().getUserNicknameFromJID(jid);
                    String time = SparkManager.DATE_SECOND_FORMATTER.format(new Date());
                    String infoText = Res.getString("message.user.now.available.to.chat", nickname, time);
                    if (localPref.getShowToasterPopup()) {
                        EventQueue.invokeLater(() -> {
                            SparkToaster toaster = new SparkToaster();
                            toaster.setDisplayTime(5000);
                            toaster.setBorder(BorderFactory.createBevelBorder(0));
                            toaster.setToasterHeight(150);
                            toaster.setToasterWidth(200);
                            toaster.setTitle(nickname);
                            toaster.showToaster(null, infoText);
                            toaster.setCustomAction(new AbstractAction() {

                                private static final long serialVersionUID = 4827542713848133369L;

                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    SparkManager.getChatManager().getChatRoom(jid);
                                }
                            });
                        });
                    }
                    ChatRoom room = SparkManager.getChatManager().getChatRoom(jid);
                    if (localPref.getWindowTakesFocus()) {
                        EventQueue.invokeLater(() -> SparkManager.getChatManager().activateChat(jid, nickname));
                    }
                    EventQueue.invokeLater(() -> room.getTranscriptWindow().insertNotificationMessage(infoText, ChatManager.NOTIFICATION_COLOR));
                }
            }
            for (String s : removelater) {
                sparkContacts.remove(s);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }, new StanzaTypeFilter(Presence.class));
}
Also used : Action(javax.swing.Action) AbstractAction(javax.swing.AbstractAction) MouseEvent(java.awt.event.MouseEvent) SparkToaster(org.jivesoftware.sparkimpl.plugin.alerts.SparkToaster) ActionEvent(java.awt.event.ActionEvent) ContactItem(org.jivesoftware.spark.ui.ContactItem) ContextMenuListener(org.jivesoftware.spark.plugin.ContextMenuListener) ArrayList(java.util.ArrayList) ContactList(org.jivesoftware.spark.ui.ContactList) JPopupMenu(javax.swing.JPopupMenu) Date(java.util.Date) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) Presence(org.jivesoftware.smack.packet.Presence) AbstractAction(javax.swing.AbstractAction)

Example 4 with ContextMenuListener

use of org.jivesoftware.spark.plugin.ContextMenuListener in project Spark by igniterealtime.

the class BroadcastPlugin method initialize.

public void initialize() {
    // See if we should disable all "Broadcast" menu items
    if (Default.getBoolean("DISABLE_BROADCAST_MENU_ITEM") || !Enterprise.containsFeature(Enterprise.BROADCAST_FEATURE))
        return;
    // Add as ContainerDecoratr
    SparkManager.getChatManager().addSparkTabHandler(this);
    StanzaFilter serverFilter = new StanzaTypeFilter(Message.class);
    SparkManager.getConnection().addAsyncStanzaListener(this, serverFilter);
    // Register with action menu
    final JMenu actionsMenu = SparkManager.getMainWindow().getMenuByName(Res.getString("menuitem.actions"));
    JMenuItem broadcastHistoryMenu = new JMenuItem(Res.getString("title.broadcast.history"), SparkRes.getImageIcon(SparkRes.HISTORY_16x16));
    JMenuItem broadcastMenu = new JMenuItem(Res.getString("title.broadcast.message"), SparkRes.getImageIcon(SparkRes.MEGAPHONE_16x16));
    ResourceUtils.resButton(broadcastMenu, Res.getString("title.broadcast.message"));
    actionsMenu.add(broadcastHistoryMenu);
    actionsMenu.add(broadcastMenu);
    broadcastMenu.addActionListener(e -> broadcastToRoster());
    broadcastHistoryMenu.addActionListener(e -> {
        new BroadcastHistoryFrame().run();
    });
    // Register with action menu
    JMenuItem startConversationtMenu = new JMenuItem("", SparkRes.getImageIcon(SparkRes.SMALL_MESSAGE_IMAGE));
    ResourceUtils.resButton(startConversationtMenu, Res.getString("menuitem.start.a.chat"));
    if (!Default.getBoolean("HIDE_START_A_CHAT") && Enterprise.containsFeature(Enterprise.START_A_CHAT_FEATURE)) {
        actionsMenu.add(startConversationtMenu, 0);
    }
    startConversationtMenu.addActionListener(e -> {
        ContactList contactList = SparkManager.getWorkspace().getContactList();
        Collection<ContactItem> selectedUsers = contactList.getSelectedUsers();
        String selectedUser = "";
        Iterator<ContactItem> selectedUsersIterator = selectedUsers.iterator();
        if (selectedUsersIterator.hasNext()) {
            ContactItem contactItem = selectedUsersIterator.next();
            selectedUser = contactItem.getJID();
        }
        UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
        UIManager.put("OptionPane.cancelButtonText", Res.getString("cancel"));
        String jid = (String) JOptionPane.showInputDialog(SparkManager.getMainWindow(), Res.getString("label.enter.address"), Res.getString("title.start.chat"), JOptionPane.QUESTION_MESSAGE, null, null, selectedUser);
        if (ModelUtil.hasLength(jid) && ModelUtil.hasLength(XmppStringUtils.parseDomain(jid))) {
            if (ModelUtil.hasLength(jid) && jid.indexOf('@') == -1) {
                // Append server address
                jid = jid + "@" + SparkManager.getConnection().getServiceName();
            }
            String nickname = SparkManager.getUserManager().getUserNicknameFromJID(jid);
            jid = UserManager.escapeJID(jid);
            ChatRoom chatRoom = SparkManager.getChatManager().createChatRoom(jid, nickname, nickname);
            SparkManager.getChatManager().getChatContainer().activateChatRoom(chatRoom);
        }
    });
    // Add send to selected users.
    final ContactList contactList = SparkManager.getWorkspace().getContactList();
    contactList.addContextMenuListener(new ContextMenuListener() {

        public void poppingUp(Object component, JPopupMenu popup) {
            if (component instanceof ContactGroup) {
                final ContactGroup group = (ContactGroup) component;
                Action broadcastMessageAction = new AbstractAction() {

                    private static final long serialVersionUID = -6411248110270296726L;

                    public void actionPerformed(ActionEvent e) {
                        broadcastToGroup(group);
                    }
                };
                broadcastMessageAction.putValue(Action.NAME, Res.getString("menuitem.broadcast.to.group"));
                broadcastMessageAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.MEGAPHONE_16x16));
                popup.add(broadcastMessageAction);
            }
        }

        public void poppingDown(JPopupMenu popup) {
        }

        public boolean handleDefaultAction(MouseEvent e) {
            return false;
        }
    });
    // Add Broadcast to roster
    StatusBar statusBar = SparkManager.getWorkspace().getStatusBar();
    RolloverButton broadcastToRosterButton = new RolloverButton(SparkRes.getImageIcon(SparkRes.MEGAPHONE_16x16));
    broadcastToRosterButton.setToolTipText(Res.getString("message.send.a.broadcast"));
    statusBar.invalidate();
    statusBar.validate();
    statusBar.repaint();
    broadcastToRosterButton.addActionListener(e -> broadcastToRoster());
}
Also used : AbstractAction(javax.swing.AbstractAction) Action(javax.swing.Action) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) MouseEvent(java.awt.event.MouseEvent) ActionEvent(java.awt.event.ActionEvent) ContactItem(org.jivesoftware.spark.ui.ContactItem) ContextMenuListener(org.jivesoftware.spark.plugin.ContextMenuListener) ContactList(org.jivesoftware.spark.ui.ContactList) StatusBar(org.jivesoftware.spark.ui.status.StatusBar) RolloverButton(org.jivesoftware.spark.component.RolloverButton) JPopupMenu(javax.swing.JPopupMenu) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) JMenuItem(javax.swing.JMenuItem) BroadcastHistoryFrame(org.jivesoftware.spark.ui.BroadcastHistoryFrame) ContactGroup(org.jivesoftware.spark.ui.ContactGroup) AbstractAction(javax.swing.AbstractAction) JMenu(javax.swing.JMenu)

Example 5 with ContextMenuListener

use of org.jivesoftware.spark.plugin.ContextMenuListener in project Spark by igniterealtime.

the class JabberVersion method initialize.

public void initialize() {
    // Create IQ Filter
    StanzaFilter packetFilter = new StanzaTypeFilter(IQ.class);
    SparkManager.getConnection().addAsyncStanzaListener(stanza -> {
        IQ iq = (IQ) stanza;
        try {
            // Handle Version Request
            if (iq instanceof Version && iq.getType() == IQ.Type.get) {
                // Send Version
                Version version = new Version(JiveInfo.getName(), JiveInfo.getVersion(), JiveInfo.getOS());
                // Send back as a reply
                version.setStanzaId(iq.getStanzaId());
                version.setType(IQ.Type.result);
                version.setTo(iq.getFrom());
                version.setFrom(iq.getTo());
                SparkManager.getConnection().sendStanza(version);
            } else // Send time
            if (iq instanceof Time && iq.getType() == IQ.Type.get) {
                Time time = new Time();
                time.setStanzaId(iq.getStanzaId());
                time.setFrom(iq.getTo());
                time.setTo(iq.getFrom());
                time.setTime(new Date());
                time.setType(IQ.Type.result);
                // Send Time
                SparkManager.getConnection().sendStanza(time);
            }
        } catch (SmackException.NotConnectedException e) {
            Log.warning("Unable to answer request: " + stanza, e);
        }
    }, packetFilter);
    final ContactList contactList = SparkManager.getWorkspace().getContactList();
    contactList.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("control F11"), "viewClient");
    contactList.addContextMenuListener(new ContextMenuListener() {

        public void poppingUp(final Object component, JPopupMenu popup) {
            if (!(component instanceof ContactItem)) {
                return;
            }
            ContactItem contactItem = (ContactItem) component;
            if (contactItem.getPresence() == null) {
                return;
            }
            Action versionRequest = new AbstractAction() {

                private static final long serialVersionUID = -5619737417315441711L;

                public void actionPerformed(ActionEvent e) {
                    viewClient();
                }
            };
            versionRequest.putValue(Action.NAME, Res.getString("menuitem.view.client.version"));
            popup.add(versionRequest);
        }

        public void poppingDown(JPopupMenu popup) {
        }

        public boolean handleDefaultAction(MouseEvent e) {
            return false;
        }
    });
    contactList.getActionMap().put("viewClient", new AbstractAction("viewClient") {

        private static final long serialVersionUID = 8282301357403753561L;

        public void actionPerformed(ActionEvent evt) {
            viewClient();
        }
    });
}
Also used : Action(javax.swing.Action) AbstractAction(javax.swing.AbstractAction) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) MouseEvent(java.awt.event.MouseEvent) ActionEvent(java.awt.event.ActionEvent) SmackException(org.jivesoftware.smack.SmackException) ContactItem(org.jivesoftware.spark.ui.ContactItem) IQ(org.jivesoftware.smack.packet.IQ) ContextMenuListener(org.jivesoftware.spark.plugin.ContextMenuListener) Time(org.jivesoftware.smackx.time.packet.Time) ContactList(org.jivesoftware.spark.ui.ContactList) Date(java.util.Date) JPopupMenu(javax.swing.JPopupMenu) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) Version(org.jivesoftware.smackx.iqversion.packet.Version) AbstractAction(javax.swing.AbstractAction)

Aggregations

ContextMenuListener (org.jivesoftware.spark.plugin.ContextMenuListener)6 ActionEvent (java.awt.event.ActionEvent)5 MouseEvent (java.awt.event.MouseEvent)5 AbstractAction (javax.swing.AbstractAction)4 Action (javax.swing.Action)4 JPopupMenu (javax.swing.JPopupMenu)4 ContactList (org.jivesoftware.spark.ui.ContactList)4 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)3 ContactItem (org.jivesoftware.spark.ui.ContactItem)3 Collection (java.util.Collection)2 Date (java.util.Date)2 JMenu (javax.swing.JMenu)2 JMenuItem (javax.swing.JMenuItem)2 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)2 Presence (org.jivesoftware.smack.packet.Presence)2 ChatRoom (org.jivesoftware.spark.ui.ChatRoom)2 ContactGroup (org.jivesoftware.spark.ui.ContactGroup)2 SparkToaster (org.jivesoftware.sparkimpl.plugin.alerts.SparkToaster)2 java.awt.event (java.awt.event)1 ArrayList (java.util.ArrayList)1