001    /*
002     * Copyright (C) 2008 Adam Cornett This program is free software; you can
003     * redistribute it and/or modify it under the terms of the GNU General Public
004     * License as published by the Free Software Foundation; either version 3 of the
005     * License, or (at your option) any later version. This program is distributed
006     * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
007     * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
008     * See the GNU General Public License for more details. You should have received
009     * a copy of the GNU General Public License along with this program; if not, see
010     * <http://www.gnu.org/licenses>.
011     */
012    
013    package net.codescore.ui.landing;
014    
015    import java.security.Principal;
016    import java.util.List;
017    
018    import org.zkoss.zkex.zul.Borderlayout;
019    import org.zkoss.zkex.zul.Center;
020    import org.zkoss.zkex.zul.East;
021    import org.zkoss.zkex.zul.North;
022    import org.zkoss.zkex.zul.West;
023    import org.zkoss.zul.Button;
024    import org.zkoss.zul.Label;
025    import org.zkoss.zul.Vbox;
026    
027    import net.codescore.controllers.HomeController;
028    import net.codescore.dbo.Competition;
029    import net.codescore.dbo.Person;
030    import net.codescore.managers.CompetitionManager;
031    import net.codescore.managers.PersonManager;
032    import net.codescore.ui.components.windows.TopControlledWin;
033    import net.codescore.ui.landing.homeparts.CurrCompWin;
034    import net.codescore.ui.landing.homeparts.Teams;
035    
036    public class Home extends TopControlledWin<HomeController> {
037            private static final int STU_MODE = 1, COACH_MODE = 2, JUDGE_MODE = 3;
038            private CurrCompWin ccw;
039            private Center centerLayout;
040            private East eastLayout;
041            private Borderlayout mainLayout;
042            private int mode;
043            private North northLayout;
044            private Person per;
045            private West westLayout;
046    
047            public void onCreate() {
048                    HomeController hc = new HomeController(getDesktop(),this);
049                    this.setController(hc);
050                    super.onCreate();
051                    this.setWidth("800px");
052                    this.setHeight("500px");
053                    initLayout();
054                    initPerson();
055                    initCurrent();
056                    initStatic();
057                    switch (mode) {
058                            case STU_MODE:
059                                    initTeams();
060                                    break;
061                            case COACH_MODE:
062                                    break;
063                            case JUDGE_MODE:
064                                    break;
065                            default:
066                                    throw new RuntimeException("Unknown Mode");
067                    }
068            }
069    
070            public void updateCurComps() {
071                    ccw.update();
072            }
073    
074            private void initCurrent() {
075                    List<Competition> currComps =
076                            CompetitionManager.getCurrentComps(per.getObjectContext());
077                    ccw = new CurrCompWin(currComps);
078                    ccw.setParent(centerLayout);
079            }
080    
081            private void initLayout() {
082                    mainLayout = new Borderlayout();
083                    mainLayout.setParent(this);
084                    westLayout = new West();
085                    westLayout.setParent(mainLayout);
086                    westLayout.setSize("33%");
087                    eastLayout = new East();
088                    eastLayout.setParent(mainLayout);
089                    eastLayout.setSize("33%");
090                    centerLayout = new Center();
091                    centerLayout.setParent(mainLayout);
092                    centerLayout.setFlex(true);
093                    northLayout = new North();
094                    northLayout.setParent(mainLayout);
095            }
096    
097            private void initPerson() {
098                    // Get User Info
099                    Principal pri = getDesktop().getExecution().getUserPrincipal();
100                    if (pri == null)
101                            throw new RuntimeException("Unable to get User Principal!");
102                    String pName = pri.getName();
103                    per = PersonManager.getPersonByName(pName);
104                    getController().setPerson(per);
105                    mode = STU_MODE;
106                    if (getDesktop().getExecution().isUserInRole("Coach")) {
107                            mode = COACH_MODE;
108                    } else if (getDesktop().getExecution().isUserInRole("Judge")) {
109                            mode = JUDGE_MODE;
110                    }
111                    // Setup UI
112                    Vbox userInfoBox = new Vbox();
113                    Label uNameL = new Label(pName);
114                    uNameL.setParent(userInfoBox);
115                    uNameL.setStyle("font-weight:bold; font-size:16pt");
116                    userInfoBox.setParent(northLayout);
117    
118            }
119    
120            private void initTeams() {
121                    Teams t = new Teams(per);
122                    t.setParent(westLayout);
123            }
124            private void initStatic() {
125                    Vbox vbox = new Vbox();
126                    vbox.setParent(eastLayout);
127                    Button probRepo=new Button("Browse Problems");
128                    probRepo.setParent(vbox);
129                    
130                    
131            }
132    }