/**
* Copyright (C) Gadglet .
*
* This file is part of Gadglet
*
* Gadglet is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Gadglet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
*/
package com.gadglet.client.gwt.core.ui;
import com.gadglet.client.gwt.core.GadgletQuery;
import com.gadglet.client.gwt.core.GadgletRequest;
import com.gadglet.client.gwt.core.GadgletResponse;
import com.gadglet.client.gwt.ui.DebugDialogBox;
import com.gadglet.client.gwt.ui.UIUtils;
import com.gadglet.params.ProfileFields;
import com.gadglet.params.ReqActionTypes;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.impl.PreferencesUtil;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
* Create the user friends list. Used only in Bizlets (Google Apps)
*
*/
public class DomainFriendsPanel {
DebugDialogBox debug = DebugDialogBox.getErrorNotifier();
protected final PreferencesUtil prefs = PreferencesUtil.nativeInitPrefs();
private final VerticalPanel domainFrindsContainer = new VerticalPanel();
private final VerticalPanel domainFrindsList = new VerticalPanel();
final GadgletQuery viewFriendsQry = new GadgletQuery();
final ViewFriendsRequest viewFriendsRequest = new ViewFriendsRequest(
viewFriendsQry);
final GadgletQuery removeFriendQry = new GadgletQuery();
final RemoveFriendRequest removeFriendRequest = new RemoveFriendRequest(
removeFriendQry);
private static DomainFriendsPanel domainFriendsPanel = null;
public VerticalPanel getDomainFrindsContainer(){
return this.domainFrindsContainer;
}
public static DomainFriendsPanel getDomainFriendsPanel() {
if (domainFriendsPanel == null) {
domainFriendsPanel = new DomainFriendsPanel();
}
return domainFriendsPanel;
}
DomainFriendsPanel(){
removeFriendQry.setRequestAction(ReqActionTypes.REMOVE_FRIEND);
domainFrindsContainer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
Label remoteFrindsLabel = new Label();
remoteFrindsLabel.setWidth("250px");
remoteFrindsLabel.setStylePrimaryName("friendsLabel");
remoteFrindsLabel.setText(prefs.getMsg("gadgetLabelGadgetFriends") );
domainFrindsContainer.add(remoteFrindsLabel);
domainFrindsContainer.add(domainFrindsList);
}
public void viewDomainFriends(){
domainFrindsList.clear();
viewFriendsQry.setRequestAction(ReqActionTypes.GET_FRIENDS);
viewFriendsRequest.makeRequest();
}
/**
* Request class used to handle the results of the user domain frinds list
*
*/
public class ViewFriendsRequest extends GadgletRequest {
ViewFriendsRequest(GadgletQuery req) {
super(req);
}
@Override
protected void processResults(GadgletResponse jResponse) {
if (jResponse.isSuccessful()) {
int inviteNum = jResponse.getRootItemsNum();
for (int index = 0; index < inviteNum; index++) {
JSONObject item = jResponse.getItem(index);
String photo = null;
String nickName = null;
String title= null;
String userId = null;
if(item.get(ProfileFields.PROFILE_THUMBNAILURL.getParamName())!=null)
photo = item.get(ProfileFields.PROFILE_THUMBNAILURL.getParamName()).isString().stringValue();
if(item.get(ProfileFields.PROFILE_NICKNAME.getParamName())!=null)
nickName = item.get(ProfileFields.PROFILE_NICKNAME.getParamName()).isString().stringValue();
if(item.get(ProfileFields.PROFILE_TITLE.getParamName())!=null)
title = item.get(ProfileFields.PROFILE_TITLE.getParamName()).isString().stringValue();
if(item.get(ProfileFields.PROFILE_FRIEND_ID.getParamName())!=null)
userId = item.get(ProfileFields.PROFILE_FRIEND_ID.getParamName()).isString().stringValue();
try {
DomainUser domainUserFriend = new DomainUser(nickName,photo,title,userId);
domainFrindsList
.add(domainUserFriend.getRemoteFriendItem());
} catch (Exception e) {
debug.showError(111, e.getMessage());
}
}
} else
debug.showError(101, jResponse.getError());
}
}
/**
* Class represents a single Domain user which is a friend of the current user.
*
*/
private class DomainUser {
private String invitedByNickName;
private String friendPhoto;
private String title;
private final String userId;
public DomainUser(
String invitedByNickName, String photo, String title, String userId) {
this.friendPhoto = photo;
this.title = title;
this.invitedByNickName = invitedByNickName;
this.userId = userId;
}
HorizontalPanel getRemoteFriendItem() {
HorizontalPanel item = new HorizontalPanel();
VerticalPanel name = new VerticalPanel();
name.add(new HTML(invitedByNickName));
name.add(new HTML(title));
item.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
item.add(name);
item.add(UIUtils.getSpace());
if(this.friendPhoto!=null && this.friendPhoto.length()>0){
Image img = new Image(this.friendPhoto);
img.setPixelSize(30, 30);
item.add(img);
item.add(UIUtils.getSpace());
}
if(userId!=null && !userId.isEmpty()){
final Button removeButton = new Button(prefs.getMsg("gadgetLabelRemove"));
removeButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
removeFriendQry.clearParamList();
removeFriendQry.addParam(ProfileFields.PROFILE_FRIEND_ID.getParamName(), userId);
removeFriendRequest.makeRequest();
}
});
item.add(removeButton);
}
return item;
}
}
/**
* A Request class use to remove a friend from the current user friends list
*
*/
public class RemoveFriendRequest extends GadgletRequest {
RemoveFriendRequest(GadgletQuery req) {
super(req);
}
@Override
protected void processResults(GadgletResponse data) {
GadgletResponse jResponse = data;
if (jResponse.isSuccessful()) {
// refresh
viewDomainFriends();
} else {
debug.showError(101, jResponse.getError());
}
}
}
}