use of org.jivesoftware.spark.ui.conferences.BookmarksUI 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();
}
Aggregations