Search in sources :

Example 1 with BookmarksListener

use of org.jivesoftware.spark.ui.conferences.BookmarksListener in project Spark by igniterealtime.

the class BookmarkPlugin method initialize.

@Override
public void initialize() {
    final SwingWorker bookmarkThreadWorker = new SwingWorker() {

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

        /**
         * Installing menu into spark menu and adding events to bookmarks
         */
        @Override
        public void finished() {
            try {
                initialize();
            } catch (Exception e) {
                Log.error(e);
            }
        }

        /**
         */
        public void initialize() {
            final JMenu bookmarkMenu = new JMenu(Res.getString("menuitem.bookmarks"));
            createMenu(bookmarkMenu);
            if (bookmarkMenu.getMenuComponentCount() > 0) {
                SparkManager.getMainWindow().getMenu().add(bookmarkMenu, 3);
            }
            BookmarksUI bookmarksUi = ConferenceServices.getBookmarkedConferences();
            bookmarksUi.addBookmarksListener(new BookmarksListener() {

                @Override
                public void bookmarkAdded(String roomJID) {
                    rescan(bookmarkMenu);
                }

                @Override
                public void bookmarkRemoved(String roomJID) {
                    rescan(bookmarkMenu);
                }
            });
        }

        /**
         * Rescaning our bookmarks and remaking menu items
         *
         * @param bookmarkMenu menu Jmenu
         */
        public void rescan(JMenu bookmarkMenu) {
            // removing old menus
            bookmarkMenu.removeAll();
            try {
                // making new
                setBookmarks(bookmarkMenu);
                int onPanel = SparkManager.getMainWindow().getMenu().getComponentIndex(bookmarkMenu);
                if (onPanel < 0) {
                    if (bookmarkMenu.getMenuComponentCount() > 0) {
                        int menuCount = SparkManager.getMainWindow().getMenu().getMenuCount();
                        SparkManager.getMainWindow().getMenu().add(bookmarkMenu, menuCount - 2);
                    }
                }
                if (onPanel >= 0) {
                    if (bookmarkMenu.getMenuComponentCount() <= 0) {
                        SparkManager.getMainWindow().getMenu().remove(bookmarkMenu);
                    }
                }
                SparkManager.getMainWindow().getMenu().invalidate();
                SparkManager.getMainWindow().getMenu().validate();
                SparkManager.getMainWindow().getMenu().repaint();
            } catch (XMPPException | SmackException ex) {
                Log.error(ex);
            }
        }

        /**
         * Updating statusbar and generating menu items
         *
         * @param bookmarkMenu menu Jmenu
         */
        public void createMenu(JMenu bookmarkMenu) {
            try {
                setBookmarks(bookmarkMenu);
            } catch (XMPPException | SmackException ex) {
                Log.error(ex);
            }
        }

        /**
         * loading menu items and setting bookmarks listeners
         *
         * @param bookmarkMenu menu Jmenu
         */
        public void setBookmarks(JMenu bookmarkMenu) throws XMPPException, SmackException {
            BookmarkManager manager = BookmarkManager.getBookmarkManager(SparkManager.getConnection());
            if (manager != null) {
                Collection<BookmarkedConference> bookmarkedConferences = manager.getBookmarkedConferences();
                final Collection<BookmarkedURL> bookmarkedLinks = manager.getBookmarkedURLs();
                for (Object bookmarkedLink : bookmarkedLinks) {
                    final BookmarkedURL link = (BookmarkedURL) bookmarkedLink;
                    Action urlAction = new AbstractAction() {

                        private static final long serialVersionUID = 4246574779205966917L;

                        @Override
                        public void actionPerformed(ActionEvent actionEvent) {
                            try {
                                BrowserLauncher.openURL(link.getURL());
                            } catch (Exception e) {
                                Log.error(e);
                            }
                        }
                    };
                    urlAction.putValue(Action.NAME, link.getName());
                    urlAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.LINK_16x16));
                    bookmarkMenu.add(urlAction);
                }
                for (Object bookmarkedConference : bookmarkedConferences) {
                    final BookmarkedConference conferences = (BookmarkedConference) bookmarkedConference;
                    Action conferenceAction = new AbstractAction() {

                        private static final long serialVersionUID = 5964584172262968704L;

                        @Override
                        public void actionPerformed(ActionEvent actionEvent) {
                            final TimerTask task = new SwingTimerTask() {

                                @Override
                                public void doRun() {
                                    ConferenceUtils.joinConferenceOnSeperateThread(conferences.getName(), conferences.getJid(), conferences.getPassword());
                                }
                            };
                            TaskEngine.getInstance().schedule(task, 10);
                        }
                    };
                    conferenceAction.putValue(Action.NAME, conferences.getName());
                    conferenceAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.CONFERENCE_IMAGE_16x16));
                    bookmarkMenu.add(conferenceAction);
                }
            }
        }
    };
    bookmarkThreadWorker.start();
}
Also used : BookmarkedURL(org.jivesoftware.smackx.bookmarks.BookmarkedURL) Action(javax.swing.Action) AbstractAction(javax.swing.AbstractAction) ActionEvent(java.awt.event.ActionEvent) SmackException(org.jivesoftware.smack.SmackException) BookmarksListener(org.jivesoftware.spark.ui.conferences.BookmarksListener) BookmarkedConference(org.jivesoftware.smackx.bookmarks.BookmarkedConference) SmackException(org.jivesoftware.smack.SmackException) XMPPException(org.jivesoftware.smack.XMPPException) BookmarksUI(org.jivesoftware.spark.ui.conferences.BookmarksUI) BookmarkManager(org.jivesoftware.smackx.bookmarks.BookmarkManager) SwingTimerTask(org.jivesoftware.spark.util.SwingTimerTask) SwingTimerTask(org.jivesoftware.spark.util.SwingTimerTask) TimerTask(java.util.TimerTask) SwingWorker(org.jivesoftware.spark.util.SwingWorker) XMPPException(org.jivesoftware.smack.XMPPException) AbstractAction(javax.swing.AbstractAction) JMenu(javax.swing.JMenu)

Aggregations

ActionEvent (java.awt.event.ActionEvent)1 TimerTask (java.util.TimerTask)1 AbstractAction (javax.swing.AbstractAction)1 Action (javax.swing.Action)1 JMenu (javax.swing.JMenu)1 SmackException (org.jivesoftware.smack.SmackException)1 XMPPException (org.jivesoftware.smack.XMPPException)1 BookmarkManager (org.jivesoftware.smackx.bookmarks.BookmarkManager)1 BookmarkedConference (org.jivesoftware.smackx.bookmarks.BookmarkedConference)1 BookmarkedURL (org.jivesoftware.smackx.bookmarks.BookmarkedURL)1 BookmarksListener (org.jivesoftware.spark.ui.conferences.BookmarksListener)1 BookmarksUI (org.jivesoftware.spark.ui.conferences.BookmarksUI)1 SwingTimerTask (org.jivesoftware.spark.util.SwingTimerTask)1 SwingWorker (org.jivesoftware.spark.util.SwingWorker)1