PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 14package com.nimbits.client.controls;
 15
 16import com.extjs.gxt.ui.client.event.BaseEvent;
 17import com.extjs.gxt.ui.client.event.Events;
 18import com.extjs.gxt.ui.client.event.Listener;
 19import com.extjs.gxt.ui.client.event.MessageBoxEvent;
 20import com.extjs.gxt.ui.client.widget.ContentPanel;
 21import com.extjs.gxt.ui.client.widget.LayoutContainer;
 22import com.extjs.gxt.ui.client.widget.MessageBox;
 23import com.extjs.gxt.ui.client.widget.button.Button;
 24import com.extjs.gxt.ui.client.widget.menu.SeparatorMenuItem;
 25import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
 26import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
 27import com.google.gwt.core.client.GWT;
 28import com.google.gwt.user.client.Window;
 29import com.google.gwt.user.client.rpc.AsyncCallback;
 30import com.google.gwt.user.client.ui.AbstractImagePrototype;
 31import com.nimbits.client.exception.NimbitsException;
 32import com.nimbits.client.icons.Icons;
 33import com.nimbits.client.model.Const;
 34import com.nimbits.client.model.LoginInfo;
 35import com.nimbits.client.model.email.EmailAddress;
 36import com.nimbits.client.service.instantmessage.IMService;
 37import com.nimbits.client.service.instantmessage.IMServiceAsync;
 38import com.nimbits.client.service.twitter.TwitterService;
 39import com.nimbits.client.service.twitter.TwitterServiceAsync;
 40import com.nimbits.client.service.user.UserService;
 41import com.nimbits.client.service.user.UserServiceAsync;
 42
 43import java.util.Map;
 44
 45import static com.google.gwt.user.client.Window.alert;
 46
 47
 48public class MainMenuToolBar extends LayoutContainer {
 49    //    private static final Icons.INSTANCE. Icons.INSTANCE. = GWT.create(Icons.INSTANCE.class);
 50    private final UserServiceAsync us = GWT.create(UserService.class);
 51
 52
 53    public MainMenuToolBar(final String logoutURL, final LoginInfo loginInfo, final Map<String, String> settings) {
 54        // setLayout(new FlowLayout(0));
 55
 56        final ToolBar toolBar = new ToolBar();
 57
 58        toolBar.setBorders(true);
 59
 60        toolBar.add(new SeparatorToolItem());
 61
 62        final Button logoutButton = new Button("Logout");
 63
 64        final Button IMButton = instantMessageButton();
 65
 66        final Button TwitterButton = twitterButton(loginInfo.getEmailAddress());
 67
 68        final Button FBButton = facebookButton();
 69
 70        final Listener<MessageBoxEvent> l = newKeyListener();
 71
 72        final Button SecretButton = newKeyButton(l);
 73
 74        logoutButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.Restart()));
 75        logoutButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
 76
 77            @Override
 78            public void handleEvent(final BaseEvent be) {
 79                Window.Location.replace(logoutURL);
 80
 81            }
 82        });
 83
 84
 85        final Button homeButton = new Button("nimbits.com");
 86        homeButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.Home()));
 87        homeButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
 88
 89            @Override
 90            public void handleEvent(BaseEvent be) {
 91                Window.open("http://www.nimbits.com", "", "");
 92
 93            }
 94        });
 95
 96        final Button helpButton = new Button("Help");
 97        helpButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.Help()));
 98        helpButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
 99
100            @Override
101            public void handleEvent(BaseEvent be) {
102                Window.open("http://groups.google.com/group/nimbits", "", "");
103            }
104        });
105
106        final Button adminButton = new Button("Admin");
107        adminButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.Key()));
108        adminButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
109
110            @Override
111            public void handleEvent(BaseEvent be) {
112                alert("This is where the admin feature is going");
113                // Window.open("http://groups.google.com/group/nimbits", "", "");
114
115            }
116        });
117
118        toolBar.add(helpButton);
119        toolBar.add(homeButton);
120
121        toolBar.add(IMButton);
122
123
124        if (settings.containsKey(Const.PARAM_TWITTER_CLIENT_ID) && loginInfo != null) {
125            toolBar.add(TwitterButton);
126        }
127        if (settings.containsKey(Const.PARAM_FACEBOOK_CLIENT_ID)) {
128            toolBar.add(FBButton);
129        }
130
131        toolBar.add(SecretButton);
132
133        toolBar.add(logoutButton);
134        toolBar.add(new SeparatorMenuItem());
135
136        if (loginInfo.isUserAdmin()) {
137            toolBar.add(adminButton);
138        }
139
140        ContentPanel panel = new ContentPanel();
141        panel.setCollapsible(false);
142
143        panel.setHeaderVisible(false);
144        panel.setFrame(false);
145
146        panel.setTopComponent(toolBar);
147
148        LayoutContainer c = new LayoutContainer();
149        c.setStyleAttribute("backgroundColor", "white");
150        c.setBorders(true);
151        panel.add(c);
152
153        add(toolBar);
154    }
155
156
157//	final private Listener<MessageBoxEvent> connectionConfirmListener = new Listener<MessageBoxEvent>() {
158//		public void handleEvent(MessageBoxEvent ce) {
159//
160//
161//	};
162
163    private Button newKeyButton(final Listener<MessageBoxEvent> l) {
164        Button SecretButton = new Button("Secret Key");
165        SecretButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.Key()));
166        SecretButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
167
168            public void handleEvent(BaseEvent be) {
169                MessageBox.confirm("New Secret Key",
170                        "Your Nimbits Account is based on your email address and you can securely log in with any address registered with Google. " +
171                                "We also provide a secret key to program against Nimbits. " +
172                                "Press YES to generate a new secret key and to have it emailed to the account you are currently logged in with. " +
173                                "Your old key will no longer be valid.",
174                        l);
175
176            }
177        });
178        return SecretButton;
179    }
180
181    private Listener<MessageBoxEvent> newKeyListener() {
182        return new Listener<MessageBoxEvent>() {
183            @Override
184            public void handleEvent(MessageBoxEvent ce) {
185                Button btn = ce.getButtonClicked();
186                if (btn.getText().toLowerCase().equals("yes")) {
187                    try {
188                        us.updateSecret(new AsyncCallback<Void>() {
189
190                            @Override
191                            public void onFailure(Throwable caught) {
192
193
194                            }
195
196                            @Override
197                            public void onSuccess(Void result) {
198                                Window.alert("Your new secret has been emailed to you. Your old secret key is no longer valid.");
199
200                            }
201
202                        });
203                    } catch (NimbitsException e) {
204                        alert(e.getMessage());
205                    }
206
207                }
208
209            }
210        };
211    }
212
213    private Button facebookButton() {
214        Button FBButton = new Button("Enable facebook");
215        FBButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.text()));
216        FBButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
217
218            @Override
219            public void handleEvent(BaseEvent be) {
220                Window.Location.replace("http://apps.facebook.com/Nimbits");
221
222            }
223        });
224        return FBButton;
225    }
226
227    private Button twitterButton(final EmailAddress email) {
228        Button TwitterButton = new Button("Enable Twitter");
229        TwitterButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.text()));
230        TwitterButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
231
232            @Override
233            public void handleEvent(BaseEvent be) {
234
235
236                TwitterServiceAsync twitterService = GWT.create(TwitterService.class);
237                try {
238                    twitterService.twitterAuthorise(email, new AsyncCallback<String>() {
239
240                        @Override
241                        public void onFailure(Throwable caught) {
242                            GWT.log(caught.getMessage(), caught);
243                        }
244
245                        @Override
246                        public void onSuccess(String result) {
247                            //	Window.alert(result);
248                            Window.Location.replace(result);
249
250                        }
251
252                    });
253                } catch (NimbitsException e) {
254                    Window.alert("This server may not be configured for twitter");
255
256                }
257
258
259                //    Window.open("http://www.nimbits.com?TW=1&email=" + email, "", "");
260
261
262            }
263        });
264        return TwitterButton;
265    }
266
267    private Button instantMessageButton() {
268        Button IMButton = new Button("Enable Instant Messaging");
269        IMButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.list_items()));
270
271        IMButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
272
273            @Override
274            public void handleEvent(BaseEvent be) {
275
276                IMServiceAsync IMService = GWT.create(IMService.class);
277                try {
278                    IMService.sendInvite(new AsyncCallback<Void>() {
279
280                        @Override
281                        public void onFailure(Throwable caught) {
282
283
284                        }
285
286                        @Override
287                        public void onSuccess(Void result) {
288                            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.");
289
290                        }
291
292                    });
293                } catch (NimbitsException ignored) {
294
295                }
296
297            }
298        });
299        return IMButton;
300    }
301
302}