/**
* Setup bookmarks and custom streams.
*/
private void setupStreamsAndBookmarks()
{
final EventBus eventBus = EventBus.getInstance();
eventBus.addObserver(GotCurrentUserStreamBookmarks.class, new Observer<GotCurrentUserStreamBookmarks>()
{
private final AvatarUrlGenerator groupUrlGen = new AvatarUrlGenerator(EntityType.GROUP);
private final AvatarUrlGenerator personUrlGen = new AvatarUrlGenerator(EntityType.PERSON);
public void update(final GotCurrentUserStreamBookmarks event)
{
bookmarkList.clear();
bookmarksWidgetMap.clear();
List<StreamFilter> sortedStreamFilters = event.getResponse();
Collections.sort(sortedStreamFilters, new StreamFilterNameComparator());
for (final StreamFilter filter : sortedStreamFilters)
{
JSONObject req = StreamJsonRequestFactory.getJSONRequest(filter.getRequest());
String uniqueId = null;
String entityType = null;
String imgUrl = "";
if (req.containsKey("query"))
{
JSONObject query = req.get("query").isObject();
if (query.containsKey(StreamJsonRequestFactory.RECIPIENT_KEY))
{
JSONArray recipient = query.get(StreamJsonRequestFactory.RECIPIENT_KEY).isArray();
if (recipient.size() > 0)
{
JSONObject recipientObj = recipient.get(0).isObject();
uniqueId = recipientObj.get("name").isString().stringValue();
entityType = recipientObj.get("type").isString().stringValue().toLowerCase();
AvatarUrlGenerator urlGen = groupUrlGen;
if ("person".equals(entityType))
{
urlGen = personUrlGen;
}
imgUrl = urlGen.getSmallAvatarUrl(filter.getOwnerAvatarId());
}
}
}
if (uniqueId != null && entityType != null)
{
String bookmarkUrl = entityType + "/" + uniqueId;
StreamNamePanel bookmarkFilter = createPanel(filter.getName(), bookmarkUrl, imgUrl,
new ClickHandler()
{
public void onClick(final ClickEvent event)
{
if (new WidgetJSNIFacadeImpl()
.confirm("Are you sure you want to delete this bookmark?"))
{
StreamBookmarksModel.getInstance().delete(filter.getId());
}
event.stopPropagation();
}
}, style.deleteBookmark(), "", true);
bookmarkList.add(bookmarkFilter);
bookmarksWidgetMap.put(bookmarkUrl, bookmarkFilter);
}
}
if (sortedStreamFilters.size() == 0)
{
Label defaultLabel = new Label("Bookmarks allow you to quickly jump to any stream in Eureka.");
defaultLabel.addStyleName(style.noBookmarksMessage());
bookmarkList.add(defaultLabel);
}
bookmarksLoaded = true;
checkInit();
}
});
eventBus.addObserver(StreamActivitySubscriptionChangedEvent.class,
new Observer<StreamActivitySubscriptionChangedEvent>()
{
public void update(final StreamActivitySubscriptionChangedEvent ev)
{
boolean newStatus = ev.getResponse().getReceiveNewActivityNotifications();
setSubscribeStatus(newStatus);
String msg = newStatus ? "You will now receive emails for new activities to this stream"
: "You will no longer receive emails for new activities to this stream";
eventBus.notifyObservers(new ShowNotificationEvent(new Notification(msg)));
}
});
eventBus.addObserver(GotStreamActivitySubscriptionResponseEvent.class,
new Observer<GotStreamActivitySubscriptionResponseEvent>()
{
public void update(final GotStreamActivitySubscriptionResponseEvent result)
{
setSubscribeStatus(result.isSubscribed());
}
});
eventBus.addObserver(GotCurrentUserCustomStreamsResponseEvent.class,
new Observer<GotCurrentUserCustomStreamsResponseEvent>()
{
public void update(final GotCurrentUserCustomStreamsResponseEvent event)
{
filterList.clear();
customStreamWidgetMap.clear();
StreamNamePanel savedBy = createPanel("My Saved Items", "custom/0/"
+ "{\"query\":{\"savedBy\":\""
+ Session.getInstance().getCurrentPerson().getAccountId() + "\"}}",
"style/images/customStream.png", null, "", "", false);
filterList.add(savedBy);
customStreamWidgetMap.put(0L, savedBy);
StreamNamePanel likedBy = createPanel("My Liked Items", "custom/1/"
+ "{\"query\":{\"likedBy\":[{\"type\":\"PERSON\", \"name\":\""
+ Session.getInstance().getCurrentPerson().getAccountId() + "\"}]}}/My Liked Items",
"style/images/customStream.png", null, "", "", false);
filterList.add(likedBy);
customStreamWidgetMap.put(1L, likedBy);
for (final StreamFilter filter : event.getResponse().getStreamFilters())
{
StreamNamePanel filterPanel = createPanel(
filter.getName(),
"custom/"
+ filter.getId()
+ "/"
+ URL.encodeComponent(filter.getRequest().
replace("%%CURRENT_USER_ACCOUNT_ID%%",
Session.getInstance().getCurrentPerson().getAccountId())),
"style/images/customStream.png", new ClickHandler()
{
public void onClick(final ClickEvent event)
{
Dialog.showCentered(new CustomStreamDialogContent((Stream) filter));
event.stopPropagation();
}
}, style.editCustomStream(), "edit", false);
filterList.add(filterPanel);
customStreamWidgetMap.put(filter.getId(), filterPanel);
}
customStreamsLoaded = true;
checkInit();
}
});
eventBus.addObserver(StreamSearchBeginEvent.class, new Observer<StreamSearchBeginEvent>()
{
public void update(final StreamSearchBeginEvent event)
{
if (null == event.getSearchText())
{
eventBus.notifyObservers(new UpdateHistoryEvent(new CreateUrlRequest("search", "", false)));
searchBox.setText("");
}
}
});
}