/**
* 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.GadgetNativeUtils;
import com.gadglet.client.gwt.GadgetUtils;
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.ProfPrivacyStatus;
import com.gadglet.params.ProfileFields;
import com.gadglet.params.ReqActionTypes;
import com.gadglet.params.ReqParamNames;
import com.gadglet.params.ResParamNames;
import com.gadglet.params.SharingType;
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.DialogBox;
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.RadioButton;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
* UI class which enables the user to share content items with his friends
*
*/
public class ShareWithDialogPanel {
private static ShareWithDialogPanel shareWithDialogPanel;
final DebugDialogBox debug = DebugDialogBox.getErrorNotifier();
private static DialogBox dialogBox = new DialogBox();
protected final PreferencesUtil prefs = PreferencesUtil.nativeInitPrefs();
private final VerticalPanel remoteFrindsList = new VerticalPanel();
private final VerticalPanel mainPanel = new VerticalPanel();
private final ScrollPanel scrollPanel = new ScrollPanel();
final GadgletQuery viewFriendsQry = new GadgletQuery();
final GadgletQuery updateShareWithQry = new GadgletQuery();
final UpdateShareWithRequest updateShareWithRequest = new UpdateShareWithRequest(
updateShareWithQry);
final ViewFriendsRequest viewFriendsRequest = new ViewFriendsRequest(
viewFriendsQry);
private String itemId;
public String getItemId(){
return this.itemId;
}
private ShareWithDialogPanel() {
updateShareWithQry.setRequestAction(ReqActionTypes.SHARE);
final Button closeButton = new Button(prefs.getMsg("gadgetLabelClose"));
closeButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
dialogBox.hide();
}
});
int width = GadgetNativeUtils.getGadgetWidth() - 100;
int height = GadgetNativeUtils.getGadgetHeight() - 100;
if (GadgetUtils.isCanvasView())
height = height / 2;
scrollPanel.setPixelSize(width + 10, height + 10);
mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
scrollPanel.add(remoteFrindsList);
mainPanel.add(scrollPanel);
mainPanel.add(closeButton);
dialogBox.setWidget(mainPanel);
}
public static ShareWithDialogPanel getShareWithDialogPanel() {
if (shareWithDialogPanel == null) {
shareWithDialogPanel = new ShareWithDialogPanel();
}
return shareWithDialogPanel;
}
public void showShareWithDialogPanel(String itemId,String title) {
this.itemId = itemId;
dialogBox.setText(prefs.getMsg("gadgetLabelSharingItem")+" : "+title);
remoteFrindsList.clear();
viewFriendsQry.setRequestAction(ReqActionTypes.SHARED_LIST);
viewFriendsQry.setRequestItemkey(itemId);
viewFriendsRequest.makeRequest();
if (GadgetUtils.isCanvasView())
GadgetUtils.dialogBoxTop(dialogBox);
else
dialogBox.center();
}
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 nickName = null;
String photo = null;
String shareType = null;
String frindId = null;
try {
if(item.get(ProfileFields.PROFILE_NICKNAME.getParamName())==null)
continue;
if(item.get(ResParamNames.RES_SHARE_WITH.getParamName())==null)
continue;
if (item.get(ProfileFields.PROFILE_THUMBNAILURL
.getParamName()) != null)
photo = item
.get(ProfileFields.PROFILE_THUMBNAILURL
.getParamName()).isString()
.stringValue();
if (item.get(ResParamNames.RES_SHARE_TYPE
.getParamName()) != null)
shareType = item
.get(ResParamNames.RES_SHARE_TYPE
.getParamName()).isString()
.stringValue();
frindId = item
.get(ResParamNames.RES_SHARE_WITH
.getParamName()).isString()
.stringValue();
if (item.get(
ProfileFields.PROFILE_NICKNAME.getParamName())
.isString()
.stringValue()
.equals(ProfPrivacyStatus.PRIVATE
.getPrivacyStatus()))
nickName = prefs
.getMsg("gadgetLabelPrivateProfile");
else
nickName = item
.get(ProfileFields.PROFILE_NICKNAME
.getParamName()).isString()
.stringValue();
SharedItemFriend remoteFriend = new SharedItemFriend(
nickName, photo, shareType, frindId);
remoteFrindsList
.add(remoteFriend.getRemoteFriendItem());
} catch (Exception e) {
debug.showError(111, e.getMessage());
}
}
} else
debug.showError(101, jResponse.getError());
}
}
private class SharedItemFriend {
private String invitedByNickName;
private String friendPhoto;
private String friendId;
private RadioButton noShare;
private RadioButton viewShare;
private RadioButton editShare;
final HorizontalPanel shareTypePanel = new HorizontalPanel();
public SharedItemFriend(String invitedByNickName, String photo,
String shareType, String friendId) {
this.friendPhoto = photo;
this.invitedByNickName = invitedByNickName;
this.friendId = friendId;
noShare = new RadioButton(friendId,
prefs.getMsg("gadgetLabelNoShare"));
viewShare = new RadioButton(friendId,
prefs.getMsg("gadgetLabelViewShare"));
editShare = new RadioButton(friendId,
prefs.getMsg("gadgetLabelEditShare"));
noShare.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
updateShareWithQry.clearParamList();
updateShareWithQry.setRequestItemkey(getItemId());
updateShareWithQry.addParam(ReqParamNames.SHARE_WITH,
getFriendId());
updateShareWithRequest.makeRequest();
}
});
viewShare.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
updateShareWithQry.clearParamList();
updateShareWithQry.setRequestItemkey(getItemId());
updateShareWithQry.addParam(ReqParamNames.SHARE_WITH,
getFriendId());
updateShareWithQry.addParam(ReqParamNames.SHARE_TYPE,
SharingType.READ_ONLY.getSharingType());
updateShareWithRequest.makeRequest();
}
});
editShare.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
updateShareWithQry.clearParamList();
updateShareWithQry.setRequestItemkey(getItemId());
updateShareWithQry.addParam(ReqParamNames.SHARE_WITH,
getFriendId());
updateShareWithQry.addParam(ReqParamNames.SHARE_TYPE,
SharingType.EDIT.getSharingType());
updateShareWithRequest.makeRequest();
}
});
if (shareType == null)
noShare.setValue(true);
else if (shareType.equals(SharingType.READ_ONLY
.getSharingType()))
viewShare.setValue(true);
else if (shareType.equals(SharingType.EDIT.getSharingType()))
editShare.setValue(true);
else
noShare.setValue(true);
shareTypePanel.add(noShare);
shareTypePanel.add(viewShare);
shareTypePanel.add(editShare);
}
SharedItemFriend getSharedItemFriend(){
return this;
}
String getFriendId(){
return this.friendId;
}
HorizontalPanel getRemoteFriendItem() {
HorizontalPanel item = new HorizontalPanel();
item.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
item.setHeight("40px");
item.add(new HTML(invitedByNickName));
item.add(UIUtils.getSpace());
if (this.friendPhoto != null) {
Image img = new Image(this.friendPhoto);
img.setPixelSize(30, 30);
item.add(img);
}
item.add(UIUtils.getSpace());
item.add(shareTypePanel);
return item;
}
}
public class UpdateShareWithRequest extends GadgletRequest {
public UpdateShareWithRequest(GadgletQuery req) {
super(req);
}
@Override
protected void processResults(GadgletResponse data) {
// do nothing
}
}
}