/v3.2/nimbits-tds/src/com/nimbits/client/panels/ConnectionPanel.java

http://nimbits-server.googlecode.com/ · Java · 318 lines · 203 code · 88 blank · 27 comment · 7 complexity · aa864ee01e8e0bcbffc73499efe02cbb MD5 · raw file

  1. /*
  2. * Copyright (c) 2010 Tonic Solutions LLC.
  3. *
  4. * http://www.nimbits.com
  5. *
  6. *
  7. * Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * http://www.gnu.org/licenses/gpl.html
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  12. */
  13. package com.nimbits.client.panels;
  14. import com.extjs.gxt.ui.client.event.BaseEvent;
  15. import com.extjs.gxt.ui.client.event.Events;
  16. import com.extjs.gxt.ui.client.event.Listener;
  17. import com.extjs.gxt.ui.client.event.MessageBoxEvent;
  18. import com.extjs.gxt.ui.client.widget.ContentPanel;
  19. import com.extjs.gxt.ui.client.widget.Info;
  20. import com.extjs.gxt.ui.client.widget.MessageBox;
  21. import com.extjs.gxt.ui.client.widget.button.Button;
  22. import com.extjs.gxt.ui.client.widget.button.ButtonGroup;
  23. import com.extjs.gxt.ui.client.widget.menu.Menu;
  24. import com.extjs.gxt.ui.client.widget.menu.MenuItem;
  25. import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
  26. import com.google.gwt.core.client.GWT;
  27. import com.google.gwt.user.client.rpc.AsyncCallback;
  28. import com.google.gwt.user.client.ui.AbstractImagePrototype;
  29. import com.nimbits.client.exception.NimbitsException;
  30. import com.nimbits.client.icons.Icons;
  31. import com.nimbits.client.model.category.Category;
  32. import com.nimbits.client.model.common.CommonFactoryLocator;
  33. import com.nimbits.client.model.connection.Connection;
  34. import com.nimbits.client.model.diagram.Diagram;
  35. import com.nimbits.client.model.email.EmailAddress;
  36. import com.nimbits.client.model.point.Point;
  37. import com.nimbits.client.service.user.UserService;
  38. import com.nimbits.client.service.user.UserServiceAsync;
  39. import java.util.List;
  40. class ConnectionPanel extends NavigationEventProvider {
  41. private final UserServiceAsync userService = GWT.create(UserService.class);
  42. private final EmailAddress email;
  43. private final ContentPanel mainPanel = new ContentPanel();
  44. private int connectionCount = 0;
  45. // @Override
  46. // protected void onRender(com.google.gwt.user.client.Element parent, int pos) {
  47. // super.onRender(parent, pos);
  48. // }
  49. private UserListPanel createUserList() {
  50. final UserListPanel userList = new UserListPanel(email);
  51. userList.addCategoryClickedListeners(new NavigationEventProvider.CategoryClickedListener() {
  52. @Override
  53. public void onCategoryClicked(final Category c, boolean readOnly) throws NimbitsException {
  54. notifyCategoryClickedListener(c, readOnly);
  55. }
  56. });
  57. userList.addPointClickedListeners(new NavigationEventProvider.PointClickedListener() {
  58. @Override
  59. public void onPointClicked(Point p) {
  60. notifyPointClickedListener(p);
  61. }
  62. });
  63. userList.addDiagramClickedListeners(new NavigationEventProvider.DiagramClickedListener() {
  64. @Override
  65. public void onDiagramClicked(final Diagram p) {
  66. notifyDiagramClickedListener(p);
  67. }
  68. });
  69. return userList;
  70. }
  71. public ConnectionPanel(final EmailAddress email) throws NimbitsException {
  72. this.email = email;
  73. mainPanel.setTopComponent(toolbar());
  74. mainPanel.setHeaderVisible(false);
  75. mainPanel.add(createUserList());
  76. mainPanel.setFrame(false);
  77. add(mainPanel);
  78. // Image i = new Image();
  79. // i.setWidth("170px");
  80. // i.setHeight("140px");
  81. // i.setUrl("resources/images/net.jpg");
  82. // i.setStyleName("CenterImage");
  83. // add(i);
  84. }
  85. private ToolBar toolbar() throws NimbitsException {
  86. final ToolBar t = new ToolBar();
  87. t.setBorders(false);
  88. ButtonGroup group = new ButtonGroup(1);
  89. group.setBodyBorder(false);
  90. group.setBorders(false);
  91. //group.setFrame(false);
  92. group.setWidth(getWidth());
  93. Button connectionRequest = pendingConnectionsButton();
  94. Button FriendButton = connectionButton();
  95. Button refreshButton = showConnectionsButton();
  96. group.add(FriendButton);
  97. group.add(connectionRequest);
  98. group.add(refreshButton);
  99. t.add(group);
  100. return t;
  101. }
  102. private Button showConnectionsButton() {
  103. final Button b = new Button("Show Connections");
  104. b.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.refresh()));
  105. b.addListener(Events.OnClick, new Listener<BaseEvent>() {
  106. @Override
  107. public void handleEvent(BaseEvent be) {
  108. reloadConnections();
  109. }
  110. });
  111. return b;
  112. }
  113. private void reloadConnections() {
  114. mainPanel.removeAll();
  115. mainPanel.add(createUserList());
  116. doLayout(true);
  117. }
  118. private Button connectionButton() {
  119. final Button b = new Button("New Connection");
  120. b.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.addFriend()));
  121. b.addListener(Events.OnClick, new Listener<BaseEvent>() {
  122. @Override
  123. public void handleEvent(BaseEvent be) {
  124. final MessageBox box = MessageBox.prompt("Connect to Friends",
  125. "Enter an email address to invite a friend to connect their Data Points to yours. After they approve your request " +
  126. "you'll be able to see each others data points and diagrams (based on permission levels).");
  127. box.addCallback(new Listener<MessageBoxEvent>() {
  128. @Override
  129. public void handleEvent(MessageBoxEvent be) {
  130. final String email;
  131. email = be.getValue();
  132. if (email != null) {
  133. if (email.length() > 0) {
  134. UserServiceAsync userService;
  135. userService = GWT.create(UserService.class);
  136. EmailAddress emailAddress = CommonFactoryLocator.getInstance().createEmailAddress(email);
  137. try {
  138. userService.sendConnectionRequest(emailAddress, new AsyncCallback<Void>() {
  139. @Override
  140. public void onFailure(Throwable caught) {
  141. }
  142. @Override
  143. public void onSuccess(Void result) {
  144. Info.display("Connection Request", "Connection Request Sent!");
  145. }
  146. });
  147. } catch (NimbitsException e) {
  148. // e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  149. }
  150. }
  151. }
  152. }
  153. });
  154. }
  155. });
  156. return b;
  157. }
  158. private Button pendingConnectionsButton() {
  159. final Button connectionRequest = new Button("Requests(" + connectionCount + ")");
  160. connectionRequest.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.add16()));
  161. userService.getPendingConnectionRequests(email, new AsyncCallback<List<Connection>>() {
  162. @Override
  163. public void onFailure(Throwable caught) {
  164. GWT.log(caught.getMessage(), caught);
  165. }
  166. @Override
  167. public void onSuccess(final List<Connection> result) {
  168. final Menu scrollMenu = new Menu();
  169. scrollMenu.setMaxHeight(200);
  170. for (final Connection r : result) {
  171. final MenuItem m = new MenuItem(r.getRequestorEmail().getValue());
  172. m.addListener(Events.Select, new Listener<BaseEvent>() {
  173. @Override
  174. public void handleEvent(final BaseEvent be) {
  175. // final Dialog simple = new Dialog();
  176. //simple.setHeading(");
  177. final MessageBox box = new MessageBox();
  178. box.setButtons(MessageBox.YESNOCANCEL);
  179. box.setIcon(MessageBox.QUESTION);
  180. box.setTitle("Connection request approval");
  181. box.addCallback(new Listener<MessageBoxEvent>() {
  182. @Override
  183. public void handleEvent(final MessageBoxEvent be) {
  184. final Button btn = be.getButtonClicked();
  185. try {
  186. if (btn.getText().equals("Yes")) {
  187. acceptConnection(r, true);
  188. scrollMenu.remove(m);
  189. } else if (btn.getText().equals("No")) {
  190. scrollMenu.remove(m);
  191. acceptConnection(r, false);
  192. }
  193. } catch (NimbitsException e) {
  194. GWT.log(e.getMessage(), e);
  195. }
  196. }
  197. private void acceptConnection(
  198. final Connection r,
  199. boolean accepted) throws NimbitsException {
  200. UserServiceAsync userService;
  201. userService = GWT.create(UserService.class);
  202. userService.connectionRequestReply(r.getTargetEmail(), r.getRequestorEmail(), r.getUUID(), accepted, new AsyncCallback<Void>() {
  203. @Override
  204. public void onFailure(Throwable e) {
  205. GWT.log(e.getMessage(), e);
  206. }
  207. @Override
  208. public void onSuccess(Void result) {
  209. reloadConnections();
  210. connectionCount += (-1);
  211. connectionRequest.setText("Requests(" + connectionCount + ")");
  212. }
  213. });
  214. }
  215. });
  216. box.setMessage("The owner of the email address: '" + r.getRequestorEmail().getValue() + "' would like to connect with you. You will have read only access to each others data points. Is that OK?");
  217. box.show();
  218. }
  219. });
  220. scrollMenu.add(m);
  221. }
  222. connectionRequest.setMenu(scrollMenu);
  223. connectionCount = result.size();
  224. connectionRequest.setText("Requests(" + connectionCount + ")");
  225. // Window.alert("" + result.size());
  226. }
  227. });
  228. return connectionRequest;
  229. }
  230. }