/v3.2/nimbits-tds/src/com/nimbits/client/controls/MainMenuToolBar.java

http://nimbits-server.googlecode.com/ · Java · 302 lines · 196 code · 84 blank · 22 comment · 6 complexity · c19a1fed4ace4e5241dc385f8a86d234 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.controls;
  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.LayoutContainer;
  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.menu.SeparatorMenuItem;
  23. import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
  24. import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
  25. import com.google.gwt.core.client.GWT;
  26. import com.google.gwt.user.client.Window;
  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.Const;
  32. import com.nimbits.client.model.LoginInfo;
  33. import com.nimbits.client.model.email.EmailAddress;
  34. import com.nimbits.client.service.instantmessage.IMService;
  35. import com.nimbits.client.service.instantmessage.IMServiceAsync;
  36. import com.nimbits.client.service.twitter.TwitterService;
  37. import com.nimbits.client.service.twitter.TwitterServiceAsync;
  38. import com.nimbits.client.service.user.UserService;
  39. import com.nimbits.client.service.user.UserServiceAsync;
  40. import java.util.Map;
  41. import static com.google.gwt.user.client.Window.alert;
  42. public class MainMenuToolBar extends LayoutContainer {
  43. // private static final Icons.INSTANCE. Icons.INSTANCE. = GWT.create(Icons.INSTANCE.class);
  44. private final UserServiceAsync us = GWT.create(UserService.class);
  45. public MainMenuToolBar(final String logoutURL, final LoginInfo loginInfo, final Map<String, String> settings) {
  46. // setLayout(new FlowLayout(0));
  47. final ToolBar toolBar = new ToolBar();
  48. toolBar.setBorders(true);
  49. toolBar.add(new SeparatorToolItem());
  50. final Button logoutButton = new Button("Logout");
  51. final Button IMButton = instantMessageButton();
  52. final Button TwitterButton = twitterButton(loginInfo.getEmailAddress());
  53. final Button FBButton = facebookButton();
  54. final Listener<MessageBoxEvent> l = newKeyListener();
  55. final Button SecretButton = newKeyButton(l);
  56. logoutButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.Restart()));
  57. logoutButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
  58. @Override
  59. public void handleEvent(final BaseEvent be) {
  60. Window.Location.replace(logoutURL);
  61. }
  62. });
  63. final Button homeButton = new Button("nimbits.com");
  64. homeButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.Home()));
  65. homeButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
  66. @Override
  67. public void handleEvent(BaseEvent be) {
  68. Window.open("http://www.nimbits.com", "", "");
  69. }
  70. });
  71. final Button helpButton = new Button("Help");
  72. helpButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.Help()));
  73. helpButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
  74. @Override
  75. public void handleEvent(BaseEvent be) {
  76. Window.open("http://groups.google.com/group/nimbits", "", "");
  77. }
  78. });
  79. final Button adminButton = new Button("Admin");
  80. adminButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.Key()));
  81. adminButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
  82. @Override
  83. public void handleEvent(BaseEvent be) {
  84. alert("This is where the admin feature is going");
  85. // Window.open("http://groups.google.com/group/nimbits", "", "");
  86. }
  87. });
  88. toolBar.add(helpButton);
  89. toolBar.add(homeButton);
  90. toolBar.add(IMButton);
  91. if (settings.containsKey(Const.PARAM_TWITTER_CLIENT_ID) && loginInfo != null) {
  92. toolBar.add(TwitterButton);
  93. }
  94. if (settings.containsKey(Const.PARAM_FACEBOOK_CLIENT_ID)) {
  95. toolBar.add(FBButton);
  96. }
  97. toolBar.add(SecretButton);
  98. toolBar.add(logoutButton);
  99. toolBar.add(new SeparatorMenuItem());
  100. if (loginInfo.isUserAdmin()) {
  101. toolBar.add(adminButton);
  102. }
  103. ContentPanel panel = new ContentPanel();
  104. panel.setCollapsible(false);
  105. panel.setHeaderVisible(false);
  106. panel.setFrame(false);
  107. panel.setTopComponent(toolBar);
  108. LayoutContainer c = new LayoutContainer();
  109. c.setStyleAttribute("backgroundColor", "white");
  110. c.setBorders(true);
  111. panel.add(c);
  112. add(toolBar);
  113. }
  114. // final private Listener<MessageBoxEvent> connectionConfirmListener = new Listener<MessageBoxEvent>() {
  115. // public void handleEvent(MessageBoxEvent ce) {
  116. //
  117. //
  118. // };
  119. private Button newKeyButton(final Listener<MessageBoxEvent> l) {
  120. Button SecretButton = new Button("Secret Key");
  121. SecretButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.Key()));
  122. SecretButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
  123. public void handleEvent(BaseEvent be) {
  124. MessageBox.confirm("New Secret Key",
  125. "Your Nimbits Account is based on your email address and you can securely log in with any address registered with Google. " +
  126. "We also provide a secret key to program against Nimbits. " +
  127. "Press YES to generate a new secret key and to have it emailed to the account you are currently logged in with. " +
  128. "Your old key will no longer be valid.",
  129. l);
  130. }
  131. });
  132. return SecretButton;
  133. }
  134. private Listener<MessageBoxEvent> newKeyListener() {
  135. return new Listener<MessageBoxEvent>() {
  136. @Override
  137. public void handleEvent(MessageBoxEvent ce) {
  138. Button btn = ce.getButtonClicked();
  139. if (btn.getText().toLowerCase().equals("yes")) {
  140. try {
  141. us.updateSecret(new AsyncCallback<Void>() {
  142. @Override
  143. public void onFailure(Throwable caught) {
  144. }
  145. @Override
  146. public void onSuccess(Void result) {
  147. Window.alert("Your new secret has been emailed to you. Your old secret key is no longer valid.");
  148. }
  149. });
  150. } catch (NimbitsException e) {
  151. alert(e.getMessage());
  152. }
  153. }
  154. }
  155. };
  156. }
  157. private Button facebookButton() {
  158. Button FBButton = new Button("Enable facebook");
  159. FBButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.text()));
  160. FBButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
  161. @Override
  162. public void handleEvent(BaseEvent be) {
  163. Window.Location.replace("http://apps.facebook.com/Nimbits");
  164. }
  165. });
  166. return FBButton;
  167. }
  168. private Button twitterButton(final EmailAddress email) {
  169. Button TwitterButton = new Button("Enable Twitter");
  170. TwitterButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.text()));
  171. TwitterButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
  172. @Override
  173. public void handleEvent(BaseEvent be) {
  174. TwitterServiceAsync twitterService = GWT.create(TwitterService.class);
  175. try {
  176. twitterService.twitterAuthorise(email, new AsyncCallback<String>() {
  177. @Override
  178. public void onFailure(Throwable caught) {
  179. GWT.log(caught.getMessage(), caught);
  180. }
  181. @Override
  182. public void onSuccess(String result) {
  183. // Window.alert(result);
  184. Window.Location.replace(result);
  185. }
  186. });
  187. } catch (NimbitsException e) {
  188. Window.alert("This server may not be configured for twitter");
  189. }
  190. // Window.open("http://www.nimbits.com?TW=1&email=" + email, "", "");
  191. }
  192. });
  193. return TwitterButton;
  194. }
  195. private Button instantMessageButton() {
  196. Button IMButton = new Button("Enable Instant Messaging");
  197. IMButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.list_items()));
  198. IMButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
  199. @Override
  200. public void handleEvent(BaseEvent be) {
  201. IMServiceAsync IMService = GWT.create(IMService.class);
  202. try {
  203. IMService.sendInvite(new AsyncCallback<Void>() {
  204. @Override
  205. public void onFailure(Throwable caught) {
  206. }
  207. @Override
  208. public void onSuccess(Void result) {
  209. Window.alert("Please check your instant messaging client for an invite to chat from nimbits1.appspot.com. You must accept the invitation in order for Nimbits to IM you.");
  210. }
  211. });
  212. } catch (NimbitsException ignored) {
  213. }
  214. }
  215. });
  216. return IMButton;
  217. }
  218. }