PageRenderTime 31ms CodeModel.GetById 15ms app.highlight 13ms RepoModel.GetById 0ms app.codeStats 1ms

/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
 14package com.nimbits.client.panels;
 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.Info;
 22import com.extjs.gxt.ui.client.widget.MessageBox;
 23import com.extjs.gxt.ui.client.widget.button.Button;
 24import com.extjs.gxt.ui.client.widget.button.ButtonGroup;
 25import com.extjs.gxt.ui.client.widget.menu.Menu;
 26import com.extjs.gxt.ui.client.widget.menu.MenuItem;
 27import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
 28import com.google.gwt.core.client.GWT;
 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.category.Category;
 34import com.nimbits.client.model.common.CommonFactoryLocator;
 35import com.nimbits.client.model.connection.Connection;
 36import com.nimbits.client.model.diagram.Diagram;
 37import com.nimbits.client.model.email.EmailAddress;
 38import com.nimbits.client.model.point.Point;
 39import com.nimbits.client.service.user.UserService;
 40import com.nimbits.client.service.user.UserServiceAsync;
 41
 42import java.util.List;
 43
 44class ConnectionPanel extends NavigationEventProvider {
 45
 46
 47    private final UserServiceAsync userService = GWT.create(UserService.class);
 48    private final EmailAddress email;
 49    private final ContentPanel mainPanel = new ContentPanel();
 50    private int connectionCount = 0;
 51
 52//    @Override
 53//    protected void onRender(com.google.gwt.user.client.Element parent, int pos) {
 54//        super.onRender(parent, pos);
 55//    }
 56
 57    private UserListPanel createUserList() {
 58        final UserListPanel userList = new UserListPanel(email);
 59        userList.addCategoryClickedListeners(new NavigationEventProvider.CategoryClickedListener() {
 60
 61            @Override
 62            public void onCategoryClicked(final Category c, boolean readOnly) throws NimbitsException {
 63                notifyCategoryClickedListener(c, readOnly);
 64            }
 65        });
 66
 67        userList.addPointClickedListeners(new NavigationEventProvider.PointClickedListener() {
 68            @Override
 69            public void onPointClicked(Point p) {
 70                notifyPointClickedListener(p);
 71            }
 72        });
 73
 74        userList.addDiagramClickedListeners(new NavigationEventProvider.DiagramClickedListener() {
 75
 76            @Override
 77            public void onDiagramClicked(final Diagram p) {
 78
 79                notifyDiagramClickedListener(p);
 80            }
 81
 82        });
 83
 84        return userList;
 85
 86    }
 87
 88    public ConnectionPanel(final EmailAddress email) throws NimbitsException {
 89        this.email = email;
 90
 91        mainPanel.setTopComponent(toolbar());
 92        mainPanel.setHeaderVisible(false);
 93        mainPanel.add(createUserList());
 94        mainPanel.setFrame(false);
 95
 96
 97        add(mainPanel);
 98
 99//			Image i = new Image();
100//			i.setWidth("170px");
101//			i.setHeight("140px");
102//			i.setUrl("resources/images/net.jpg");
103//			i.setStyleName("CenterImage");
104        //	 add(i);
105
106    }
107
108    private ToolBar toolbar() throws NimbitsException {
109        final ToolBar t = new ToolBar();
110        t.setBorders(false);
111        ButtonGroup group = new ButtonGroup(1);
112        group.setBodyBorder(false);
113        group.setBorders(false);
114        //group.setFrame(false);
115        group.setWidth(getWidth());
116
117        Button connectionRequest = pendingConnectionsButton();
118        Button FriendButton = connectionButton();
119        Button refreshButton = showConnectionsButton();
120        group.add(FriendButton);
121        group.add(connectionRequest);
122        group.add(refreshButton);
123        t.add(group);
124
125
126        return t;
127
128    }
129
130    private Button showConnectionsButton() {
131        final Button b = new Button("Show Connections");
132        b.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.refresh()));
133
134        b.addListener(Events.OnClick, new Listener<BaseEvent>() {
135
136            @Override
137            public void handleEvent(BaseEvent be) {
138                reloadConnections();
139
140
141            }
142
143
144        });
145        return b;
146
147    }
148
149    private void reloadConnections() {
150        mainPanel.removeAll();
151        mainPanel.add(createUserList());
152        doLayout(true);
153    }
154
155    private Button connectionButton() {
156        final Button b = new Button("New Connection");
157        b.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.addFriend()));
158
159        b.addListener(Events.OnClick, new Listener<BaseEvent>() {
160
161            @Override
162            public void handleEvent(BaseEvent be) {
163
164
165                final MessageBox box = MessageBox.prompt("Connect to Friends",
166                        "Enter an email address to invite a friend to connect their Data Points to yours. After they approve your request " +
167                                "you'll be able to see each others data points and diagrams (based on permission levels).");
168                box.addCallback(new Listener<MessageBoxEvent>() {
169                    @Override
170                    public void handleEvent(MessageBoxEvent be) {
171                        final String email;
172                        email = be.getValue();
173                        if (email != null) {
174                            if (email.length() > 0) {
175                                UserServiceAsync userService;
176                                userService = GWT.create(UserService.class);
177                                EmailAddress emailAddress = CommonFactoryLocator.getInstance().createEmailAddress(email);
178                                try {
179                                    userService.sendConnectionRequest(emailAddress, new AsyncCallback<Void>() {
180
181                                        @Override
182                                        public void onFailure(Throwable caught) {
183
184
185                                        }
186
187                                        @Override
188                                        public void onSuccess(Void result) {
189
190                                            Info.display("Connection Request", "Connection Request Sent!");
191
192                                        }
193
194                                    });
195                                } catch (NimbitsException e) {
196                                    // e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
197                                }
198
199
200                            }
201                        }
202                    }
203
204                });
205
206            }
207        });
208        return b;
209    }
210
211    private Button pendingConnectionsButton() {
212        final Button connectionRequest = new Button("Requests(" + connectionCount + ")");
213
214        connectionRequest.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.add16()));
215        userService.getPendingConnectionRequests(email, new AsyncCallback<List<Connection>>() {
216
217            @Override
218            public void onFailure(Throwable caught) {
219                GWT.log(caught.getMessage(), caught);
220
221            }
222
223            @Override
224            public void onSuccess(final List<Connection> result) {
225
226
227                final Menu scrollMenu = new Menu();
228                scrollMenu.setMaxHeight(200);
229                for (final Connection r : result) {
230                    final MenuItem m = new MenuItem(r.getRequestorEmail().getValue());
231                    m.addListener(Events.Select, new Listener<BaseEvent>() {
232
233                        @Override
234                        public void handleEvent(final BaseEvent be) {
235                            //	final Dialog simple = new Dialog();
236                            //simple.setHeading(");
237                            final MessageBox box = new MessageBox();
238                            box.setButtons(MessageBox.YESNOCANCEL);
239                            box.setIcon(MessageBox.QUESTION);
240                            box.setTitle("Connection request approval");
241                            box.addCallback(new Listener<MessageBoxEvent>() {
242
243                                @Override
244                                public void handleEvent(final MessageBoxEvent be) {
245
246                                    final Button btn = be.getButtonClicked();
247                                    try {
248                                        if (btn.getText().equals("Yes")) {
249
250                                            acceptConnection(r, true);
251
252                                            scrollMenu.remove(m);
253                                        } else if (btn.getText().equals("No")) {
254                                            scrollMenu.remove(m);
255
256                                            acceptConnection(r, false);
257
258                                        }
259                                    } catch (NimbitsException e) {
260                                        GWT.log(e.getMessage(), e);
261                                    }
262
263                                }
264
265                                private void acceptConnection(
266                                        final Connection r,
267                                        boolean accepted) throws NimbitsException {
268                                    UserServiceAsync userService;
269                                    userService = GWT.create(UserService.class);
270                                    userService.connectionRequestReply(r.getTargetEmail(), r.getRequestorEmail(), r.getUUID(), accepted, new AsyncCallback<Void>() {
271
272                                        @Override
273                                        public void onFailure(Throwable e) {
274                                            GWT.log(e.getMessage(), e);
275
276                                        }
277
278                                        @Override
279                                        public void onSuccess(Void result) {
280                                            reloadConnections();
281                                            connectionCount += (-1);
282
283                                            connectionRequest.setText("Requests(" + connectionCount + ")");
284
285                                        }
286
287                                    });
288                                }
289
290
291                            });
292
293                            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?");
294                            box.show();
295
296
297                        }
298
299                    });
300                    scrollMenu.add(m);
301                }
302
303
304                connectionRequest.setMenu(scrollMenu);
305                connectionCount = result.size();
306
307                connectionRequest.setText("Requests(" + connectionCount + ")");
308
309                //	Window.alert("" + result.size());
310
311
312            }
313
314
315        });
316        return connectionRequest;
317    }
318}