Search in sources :

Example 1 with BookmarkedURL

use of org.jivesoftware.smackx.bookmarks.BookmarkedURL 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)

Example 2 with BookmarkedURL

use of org.jivesoftware.smackx.bookmarks.BookmarkedURL in project xabber-android by redsolution.

the class BookmarksManager method getUrlFromBookmarks.

public List<BookmarkedURL> getUrlFromBookmarks(AccountJid accountJid) {
    AccountItem accountItem = AccountManager.getInstance().getAccount(accountJid);
    List<BookmarkedURL> urls = Collections.emptyList();
    if (accountItem != null) {
        BookmarkManager bookmarkManager = BookmarkManager.getBookmarkManager(accountItem.getConnection());
        try {
            urls = bookmarkManager.getBookmarkedURLs();
        } catch (SmackException.NoResponseException | InterruptedException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) {
            LogManager.exception(this, e);
        }
    }
    return urls;
}
Also used : BookmarkedURL(org.jivesoftware.smackx.bookmarks.BookmarkedURL) AccountItem(com.xabber.android.data.account.AccountItem) BookmarkManager(org.jivesoftware.smackx.bookmarks.BookmarkManager)

Example 3 with BookmarkedURL

use of org.jivesoftware.smackx.bookmarks.BookmarkedURL in project xabber-android by redsolution.

the class BookmarksActivity method getBookmarks.

private List<BookmarkVO> getBookmarks(boolean cleanCache) {
    final List<BookmarkVO> bookmarksList = new ArrayList<>();
    if (cleanCache)
        BookmarksManager.getInstance().cleanCache(accountItem.getAccount());
    // urls
    List<BookmarkedURL> bookmarkedURLs = BookmarksManager.getInstance().getUrlFromBookmarks(accountItem.getAccount());
    for (int i = 0; i < bookmarkedURLs.size(); i++) {
        BookmarkedURL url = bookmarkedURLs.get(i);
        bookmarksList.add(new BookmarkVO(url.getName(), url.getURL()));
    }
    List<BookmarkedConference> bookmarkedConferences;
    try {
        bookmarkedConferences = BookmarksManager.getInstance().getConferencesFromBookmarks(accountItem.getAccount());
    } catch (SmackException.NoResponseException | InterruptedException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) {
        LogManager.exception(this, e);
        bookmarkedConferences = Collections.emptyList();
    }
    for (int i = 0; i < bookmarkedConferences.size(); i++) {
        BookmarkedConference conference = bookmarkedConferences.get(i);
        bookmarksList.add(new BookmarkVO(conference.getName() != null ? conference.getName() : "", conference.getJid() != null ? conference.getJid().toString() : "", conference.getNickname() != null ? conference.getNickname().toString() : "", conference.getPassword() != null ? conference.getPassword() : ""));
    }
    return bookmarksList;
}
Also used : BookmarkedURL(org.jivesoftware.smackx.bookmarks.BookmarkedURL) ArrayList(java.util.ArrayList) BookmarkVO(com.xabber.android.data.extension.bookmarks.BookmarkVO) BookmarkedConference(org.jivesoftware.smackx.bookmarks.BookmarkedConference)

Aggregations

BookmarkedURL (org.jivesoftware.smackx.bookmarks.BookmarkedURL)3 BookmarkManager (org.jivesoftware.smackx.bookmarks.BookmarkManager)2 BookmarkedConference (org.jivesoftware.smackx.bookmarks.BookmarkedConference)2 AccountItem (com.xabber.android.data.account.AccountItem)1 BookmarkVO (com.xabber.android.data.extension.bookmarks.BookmarkVO)1 ActionEvent (java.awt.event.ActionEvent)1 ArrayList (java.util.ArrayList)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 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