From 636c93a72a01720985d697b31102383b7ce74ea8 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Sun, 13 Apr 2014 10:07:10 +0000 Subject: [PATCH] added avatar replacement image if user has no avatar in comments too git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/user/news-feed@94799 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../newsfeed/client/panels/NewsFeedPanel.java | 12 ++-- .../newsfeed/client/ui/AvatarReplacement.java | 17 +++-- .../client/ui/AvatarReplacement4Comments.java | 68 +++++++++++++++++++ .../ui/AvatarReplacement4Comments.ui.xml | 23 +++++++ .../newsfeed/client/ui/SingleComment.java | 21 +++++- .../newsfeed/client/ui/SingleComment.ui.xml | 5 +- .../newsfeed/client/ui/TweetTemplate.java | 9 ++- 7 files changed, 136 insertions(+), 19 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/user/newsfeed/client/ui/AvatarReplacement4Comments.java create mode 100644 src/main/java/org/gcube/portlets/user/newsfeed/client/ui/AvatarReplacement4Comments.ui.xml diff --git a/src/main/java/org/gcube/portlets/user/newsfeed/client/panels/NewsFeedPanel.java b/src/main/java/org/gcube/portlets/user/newsfeed/client/panels/NewsFeedPanel.java index 72f7bac..7058c13 100644 --- a/src/main/java/org/gcube/portlets/user/newsfeed/client/panels/NewsFeedPanel.java +++ b/src/main/java/org/gcube/portlets/user/newsfeed/client/panels/NewsFeedPanel.java @@ -102,11 +102,6 @@ public class NewsFeedPanel extends Composite { private static final String warning = GWT.getModuleBaseURL() + "../images/warning_blue.png"; private static final String spacer = GWT.getModuleBaseURL() + "../images/feeds-spacer.gif"; public static final String loading = GWT.getModuleBaseURL() + "../images/feeds-loader.gif"; - /** - * since we want to maintain the color assigned to an avatar replacement through all the session - * this hashmap maintain the assigned users color, chosen reandomly at the beginning - */ - public static final HashMap avatarReplacementAssignedColors = new HashMap(); public static final String GET_OID_PARAMETER = "oid"; @@ -837,9 +832,10 @@ public class NewsFeedPanel extends Composite { @Override public void onSuccess(Comment result) { if (result != null) { - owner.addComment(new SingleComment(result, owner, (result.getUserid().equals(myUserInfo.getUsername())) )); + owner.addComment(new SingleComment(result, owner, (result.getUserid().equals(myUserInfo.getUsername()))), false); owner.setCommentingDisabled(false); owner.updateCommentsNumberCount(); + owner.showAddCommentForm(false); } else { Window.alert("Could not deliver this comment. Please try again in a short while."); @@ -858,7 +854,7 @@ public class NewsFeedPanel extends Composite { @Override public void onSuccess(Comment result) { if (result != null) { - owner.addComment(new SingleComment(result, owner, (result.getUserid().equals(myUserInfo.getUsername())) )); + owner.addComment(new SingleComment(result, owner, (result.getUserid().equals(myUserInfo.getUsername()))), true); owner.setCommentingDisabled(false); } else { @@ -883,7 +879,7 @@ public class NewsFeedPanel extends Composite { public void onSuccess(ArrayList comments) { owner.clearComments(); for (Comment comment :comments) - owner.addComment(new SingleComment(comment, owner,(comment.getUserid().equals(myUserInfo.getUsername())) )); + owner.addComment(new SingleComment(comment, owner,(comment.getUserid().equals(myUserInfo.getUsername())) ), false); owner.setCommentsFetched(true); if (commentForm2Add) owner.showAddCommentForm(false); diff --git a/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/AvatarReplacement.java b/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/AvatarReplacement.java index 94db3cd..02e11b1 100644 --- a/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/AvatarReplacement.java +++ b/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/AvatarReplacement.java @@ -1,5 +1,7 @@ package org.gcube.portlets.user.newsfeed.client.ui; +import java.util.HashMap; + import org.gcube.portlets.user.newsfeed.client.panels.NewsFeedPanel; import com.google.gwt.core.client.GWT; @@ -12,8 +14,13 @@ import com.google.gwt.user.client.ui.Widget; public class AvatarReplacement extends Composite { - private static NoAvatarUiBinder uiBinder = GWT - .create(NoAvatarUiBinder.class); + private static NoAvatarUiBinder uiBinder = GWT.create(NoAvatarUiBinder.class); + + /** + * to maintain the color assigned to an avatar replacement, for each page load + * this hashmap maintain the assigned users color, chosen reandomly at the beginning + */ + public static final HashMap avatarReplacementAssignedColors = new HashMap(); interface NoAvatarUiBinder extends UiBinder { } @@ -55,12 +62,12 @@ public class AvatarReplacement extends Composite { * @param username */ private void pickRandomColor(String username) { - if (! NewsFeedPanel.avatarReplacementAssignedColors.containsKey(username)) { + if (! avatarReplacementAssignedColors.containsKey(username)) { String randomColor = randomColors[Random.nextInt(randomColors.length)]; avatarBox.getElement().getStyle().setBackgroundColor(randomColor); - NewsFeedPanel.avatarReplacementAssignedColors.put(username, randomColor); + avatarReplacementAssignedColors.put(username, randomColor); } else - avatarBox.getElement().getStyle().setBackgroundColor(NewsFeedPanel.avatarReplacementAssignedColors.get(username)); + avatarBox.getElement().getStyle().setBackgroundColor(avatarReplacementAssignedColors.get(username)); } diff --git a/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/AvatarReplacement4Comments.java b/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/AvatarReplacement4Comments.java new file mode 100644 index 0000000..404d0a6 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/AvatarReplacement4Comments.java @@ -0,0 +1,68 @@ +package org.gcube.portlets.user.newsfeed.client.ui; + +import java.util.HashMap; + +import org.gcube.portlets.user.newsfeed.client.panels.NewsFeedPanel; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.uibinder.client.UiBinder; +import com.google.gwt.uibinder.client.UiField; +import com.google.gwt.user.client.Random; +import com.google.gwt.user.client.ui.Composite; +import com.google.gwt.user.client.ui.HTML; +import com.google.gwt.user.client.ui.Widget; + +public class AvatarReplacement4Comments extends Composite { + + private static NoAvatarUiBinder uiBinder = GWT.create(NoAvatarUiBinder.class); + + interface NoAvatarUiBinder extends UiBinder { + } + + @UiField HTML avatarBox; + /** + * the random colors + */ + private String[] randomColors = { + "#8e8e93", + "#ff2d55", + "#ff3b30", + "#ff9500", + "#ffcc00", + "#4cd964", + "#5ac8fa", + "#34aadc", + "#007aff", + "#5856d6" + }; + + + public AvatarReplacement4Comments() { + initWidget(uiBinder.createAndBindUi(this)); + } + + public void setInitials(String username, String firstName, String lastName) { + pickRandomColor(username); + String first = "A"; + if (firstName != null && firstName.trim().length() > 0) + first = firstName.trim().substring(0, 1); + String second = "Z"; + if (lastName != null && lastName.trim().length() > 0) + second = lastName.trim().substring(0, 1); + avatarBox.setText(first+second); + } + /** + * randomize of does not find it, else color is maintained through all the session + * @param username + */ + private void pickRandomColor(String username) { + if (! AvatarReplacement.avatarReplacementAssignedColors.containsKey(username)) { + String randomColor = randomColors[Random.nextInt(randomColors.length)]; + avatarBox.getElement().getStyle().setBackgroundColor(randomColor); + AvatarReplacement.avatarReplacementAssignedColors.put(username, randomColor); + } else + avatarBox.getElement().getStyle().setBackgroundColor(AvatarReplacement.avatarReplacementAssignedColors.get(username)); + } + + +} diff --git a/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/AvatarReplacement4Comments.ui.xml b/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/AvatarReplacement4Comments.ui.xml new file mode 100644 index 0000000..f469201 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/AvatarReplacement4Comments.ui.xml @@ -0,0 +1,23 @@ + + + + .avatar-frame { + border: 1px solid #E6E6E6; + padding: 2px; + } + .avatar-replacement { + display: table-cell; + text-align: center; + vertical-align: middle; + font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; + font-weight: 300; + font-size: 12px; + padding: 0; + color: #FFF; + } + + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/SingleComment.java b/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/SingleComment.java index ac9cf00..49f9725 100644 --- a/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/SingleComment.java +++ b/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/SingleComment.java @@ -4,7 +4,6 @@ import org.gcube.portal.databook.client.GCubeSocialNetworking; import org.gcube.portal.databook.client.util.Encoder; import org.gcube.portal.databook.shared.Comment; import org.gcube.portlets.user.newsfeed.client.event.DeleteCommentEvent; -import org.gcube.portlets.user.newsfeed.client.panels.NewsFeedPanel; import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ClickEvent; @@ -37,6 +36,7 @@ public class SingleComment extends Composite { @UiField HTMLPanel mainPanel; @UiField Image avatarImage; + @UiField AvatarReplacement4Comments avatarReplacement; @UiField HTML commentText; @UiField HTML timeArea; @UiField HTML closeImage; @@ -53,6 +53,25 @@ public class SingleComment extends Composite { avatarImage.setPixelSize(30, 30); avatarImage.setUrl(toShow.getThumbnailURL()); + //check if the user has his own avatar + if (toShow.getThumbnailURL().endsWith("img_id=0") || !toShow.getThumbnailURL().contains("?")) { //it means no avatar is set + avatarImage.setVisible(false); + String f = "A"; + String s = "Z"; + if (toShow.getFullName() != null) { + String[] parts = toShow.getFullName().split("\\s"); + if (parts.length > 0) { + f = parts[0].toUpperCase(); + s = parts[parts.length-1].toUpperCase(); + } else { + f = toShow.getFullName().substring(0,1); + s = toShow.getFullName().substring(1,2); + } + } + avatarReplacement.setInitials(toShow.getUserid(), f, s); + avatarReplacement.setVisible(true); + } + String commentToShow = toShow.getText(); //replace the < & and > diff --git a/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/SingleComment.ui.xml b/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/SingleComment.ui.xml index e416cd1..8c15aec 100644 --- a/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/SingleComment.ui.xml +++ b/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/SingleComment.ui.xml @@ -1,6 +1,6 @@ + xmlns:g="urn:import:com.google.gwt.user.client.ui" xmlns:m="urn:import:org.gcube.portlets.user.newsfeed.client.ui"> .important { font-weight: bold; @@ -12,7 +12,8 @@ - +
diff --git a/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/TweetTemplate.java b/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/TweetTemplate.java index f920ede..1cf6ecf 100644 --- a/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/TweetTemplate.java +++ b/src/main/java/org/gcube/portlets/user/newsfeed/client/ui/TweetTemplate.java @@ -239,7 +239,7 @@ public class TweetTemplate extends Composite { commentsNo.setTitle("Show all Comments"); } for (Comment comment : myFeed.getComments()) { - addComment(new SingleComment(comment, this, (comment.getUserid().equals(myUserInfo.getUsername())))); + addComment(new SingleComment(comment, this, (comment.getUserid().equals(myUserInfo.getUsername()))), false); } showAddCommentForm(false); } @@ -442,9 +442,12 @@ public class TweetTemplate extends Composite { mainHTML.remove(w); } - public void addComment(SingleComment comment) { + public void addComment(SingleComment comment, boolean isEdit) { + if (isEdit) + commentsPanel.insert(comment, commentsPanel.getWidgetCount()-1); + else + commentsPanel.add(comment); myComments.add(comment); - commentsPanel.add(comment); } public void clearComments() {