55import com .vaadin .flow .component .Text ;
66import com .vaadin .flow .component .applayout .AppLayout ;
77import com .vaadin .flow .component .applayout .DrawerToggle ;
8+ import com .vaadin .flow .component .button .Button ;
89import com .vaadin .flow .component .dependency .CssImport ;
9- import com .vaadin .flow .component .html .Anchor ;
10- import com .vaadin .flow .component .html .H1 ;
11- import com .vaadin .flow .component .html .Image ;
12- import com .vaadin .flow .component .html .Span ;
10+ import com .vaadin .flow .component .dialog .Dialog ;
11+ import com .vaadin .flow .component .html .*;
1312import com .vaadin .flow .component .orderedlayout .FlexComponent ;
1413import com .vaadin .flow .component .orderedlayout .HorizontalLayout ;
1514import com .vaadin .flow .component .orderedlayout .VerticalLayout ;
1615import com .vaadin .flow .component .tabs .Tab ;
1716import com .vaadin .flow .component .tabs .TabVariant ;
1817import com .vaadin .flow .component .tabs .Tabs ;
18+ import com .vaadin .flow .component .textfield .TextField ;
1919import com .vaadin .flow .router .BeforeEnterEvent ;
2020import com .vaadin .flow .router .BeforeEnterObserver ;
2121import com .vaadin .flow .router .Route ;
3535public class MainPage extends AppLayout implements BeforeEnterObserver {
3636
3737 private HashMap <Tab , Component > tabMap = new HashMap <>();
38- private Image background = new Image ("https://i.imgur.com/4AhkxVz.jpg" , "Background" );
38+
39+ MongoDBOperations database ;
40+ private Image background = new Image ("https://i.imgur.com/rrlI3vE.jpg" , "Background" );
41+
3942
4043 public MainPage () {
44+ database = BeanUtil .getBean (MongoDBOperations .class );
4145 this .setContent (background );
4246 setPrimarySection (AppLayout .Section .DRAWER );
4347 }
@@ -52,14 +56,20 @@ public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
5256 tabs = new Tabs (
5357 createHomeTab (),
5458 createTab ("Add Exercise" , AddExercisePage .class ),
55- createTab ("Create/Join Group" , JoinCreateGroupPage .class ),
59+ createJoinGroupTab (),
60+ createCreateGroupTab (),
5661 createChartsTab (),
5762 createMyGroupsTab ()
5863 );
5964 tabs .addSelectedChangeListener (event -> {
6065 final Tab selectedTab = event .getSelectedTab ();
6166 if (tabMap .containsKey (selectedTab )) {
62- setContent (tabMap .get (selectedTab ));
67+ if (!selectedTab .getLabel ().equals ("Join Group" )&&!selectedTab .getLabel ().equals ("Create Group" ))
68+ setContent (tabMap .get (selectedTab ));
69+ else
70+ {
71+ ((Dialog ) tabMap .get (selectedTab )).open ();
72+ }
6373 }
6474 });
6575
@@ -84,6 +94,23 @@ public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
8494 addToDrawer (tabs );
8595 }
8696
97+ private Tab createJoinGroupTab ()
98+ {
99+ Tab joinGroup = new Tab ("Join Group" );
100+ joinGroup .addThemeVariants (TabVariant .LUMO_ICON_ON_TOP );
101+ tabMap .put (joinGroup ,createJoinGroupDialog ());
102+ return joinGroup ;
103+ }
104+
105+ private Tab createCreateGroupTab ()
106+ {
107+ Tab createGroup = new Tab ("Create Group" );
108+ createGroup .addThemeVariants (TabVariant .LUMO_ICON_ON_TOP );
109+ tabMap .put (createGroup ,createCreateGroupDialog ());
110+ return createGroup ;
111+ }
112+
113+
87114 private Tab createHomeTab ()
88115 {
89116 Tab home = new Tab ("Home" );
@@ -149,4 +176,78 @@ private void createHeaderWithoutLogout() {
149176
150177 addToNavbar (header );
151178 }
179+
180+ private Dialog createJoinGroupDialog ()
181+ {
182+ Dialog dialog = new Dialog ();
183+ TextField joinGroupNameField = new TextField ();
184+ TextField joinGroupPassField = new TextField ();
185+ joinGroupNameField .getStyle ().set ("width" , "15em" );
186+ joinGroupNameField .setMaxLength (13 );
187+ joinGroupNameField .setLabel ("Group Name: " );
188+ joinGroupPassField .getStyle ().set ("width" , "15em" );
189+ joinGroupPassField .setMaxLength (13 );
190+ joinGroupPassField .setLabel ("Group Password: " );
191+ Label joinErrorLabel = new Label ();
192+ Button confirmJoinGroup = new Button ("Confirm" );
193+ VerticalLayout vL = new VerticalLayout (joinGroupNameField , joinGroupPassField , confirmJoinGroup , joinErrorLabel );
194+
195+ confirmJoinGroup .addClickListener ( event -> {
196+ // User is attempting to join a group
197+ if (!joinGroupNameField .getValue ().equals ("" ) && !joinGroupPassField .getValue ().equals ("" )) {
198+ if (database .groupExists (joinGroupNameField .getValue (), joinGroupPassField .getValue ())) {
199+ database .insertGroup (joinGroupNameField .getValue (), joinGroupPassField .getValue (),
200+ SecurityUtils .getUsername ());
201+ dialog .close ();
202+ System .out .printf ("[DEBUG] Group Joined: Name - %s | Password - %s%n" , joinGroupNameField .getValue (), joinGroupPassField .getValue ());
203+ } else {
204+ System .out .printf ("[DEBUG] Someone tried to join a group that does not exist. Password Incorrect.%n" );
205+ }
206+ }
207+ dialog .close ();
208+ });
209+
210+ dialog .add (vL );
211+
212+ return dialog ;
213+ }
214+
215+ private Dialog createCreateGroupDialog ()
216+ {
217+ Dialog dialog = new Dialog ();
218+ TextField newGroupNameField = new TextField ();
219+ TextField newGroupPassField = new TextField ();
220+ newGroupNameField .getStyle ().set ("width" , "15em" );
221+ newGroupNameField .setMaxLength (13 );
222+ newGroupNameField .setLabel ("New Group Name: " );
223+ newGroupPassField .getStyle ().set ("width" , "15em" );
224+ newGroupPassField .setMaxLength (13 );
225+ newGroupPassField .setLabel ("New Group Password: " );
226+ Label createErrorLabel = new Label ();
227+ Button confirmCreateGroup = new Button ("Confirm" );
228+ VerticalLayout vL = new VerticalLayout (newGroupNameField , newGroupPassField , confirmCreateGroup , createErrorLabel );
229+
230+ confirmCreateGroup .addClickListener ( event -> {
231+ // User is creating a new group
232+ if (!newGroupNameField .getValue ().equals ("" ) && !newGroupPassField .getValue ().equals ("" )) {
233+ if (!database .groupExists (newGroupNameField .getValue ())) {
234+ database .insertGroup (newGroupNameField .getValue (), newGroupPassField .getValue (),
235+ SecurityUtils .getUsername ());
236+ database .makeAdmin (SecurityUtils .getUsername (), newGroupNameField .getValue ());
237+ dialog .close ();
238+ System .out .printf ("[DEBUG] Group Created: Name - %s | Password - %s%n" , newGroupNameField .getValue (), newGroupPassField .getValue ());
239+ } else {
240+ vL .remove (createErrorLabel );
241+ createErrorLabel .setText ("Group Name already exists" );
242+ vL .add (createErrorLabel );
243+ System .out .printf ("[DEBUG] Someone tried to create a group when the name already exists%n" );
244+ }
245+ }
246+ dialog .close ();
247+ });
248+
249+ dialog .add (vL );
250+
251+ return dialog ;
252+ }
152253}
0 commit comments