001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008-2009, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch).
003    //
004    // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the
005    // terms of the GNU Lesser General Public License as published by the Free Software Foundation,
006    // either version 3 of the License, or (at your option) any later version.
007    //
008    // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY
009    // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
010    // PURPOSE. See the GNU Lesser General Public License for more details.
011    //
012    // You should have received a copy of the GNU Lesser General Public License along with the Attempto
013    // Java Packages. If not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki.gui.page;
016    
017    import nextapp.echo2.app.Column;
018    import nextapp.echo2.app.Extent;
019    import nextapp.echo2.app.Font;
020    import nextapp.echo2.app.Grid;
021    import nextapp.echo2.app.Insets;
022    import nextapp.echo2.app.Row;
023    import nextapp.echo2.app.Window;
024    import nextapp.echo2.app.event.ActionEvent;
025    import nextapp.echo2.app.event.ActionListener;
026    import ch.uzh.ifi.attempto.acewiki.Wiki;
027    import ch.uzh.ifi.attempto.acewiki.gui.Title;
028    import ch.uzh.ifi.attempto.echocomp.GeneralButton;
029    import ch.uzh.ifi.attempto.echocomp.MessageWindow;
030    import ch.uzh.ifi.attempto.echocomp.PasswordField;
031    import ch.uzh.ifi.attempto.echocomp.SolidLabel;
032    import ch.uzh.ifi.attempto.echocomp.TextField;
033    import ch.uzh.ifi.attempto.echocomp.VSpace;
034    
035    /**
036     * This class represents a page where the users can login. Note that the login feature
037     * is implemented only very rudimentary at the moment. The password is always "password".
038     * 
039     * @author Tobias Kuhn
040     */
041    public class LoginScreen extends Column implements ActionListener {
042            
043            private static final long serialVersionUID = -6704597832001286479L;
044            
045            private Wiki wiki;
046            private Window window;
047            private boolean passwordRequired;
048            
049            private TextField usernameField = new TextField();
050            private PasswordField passwordField = new PasswordField();
051            
052            /**
053             * Creates a new login screen.
054             * 
055             * @param wiki The wiki instance.
056             * @param window The window in which the login screen is shown.
057             */
058            public LoginScreen(Wiki wiki, Window window, String title, boolean passwordRequired) {
059                    this.wiki = wiki;
060                    this.window = window;
061                    this.passwordRequired = passwordRequired;
062                    
063                    wiki.log("logi", "login page");
064                    
065                    add(new VSpace(20));
066                    add(new Title(title + " - Login", true));
067                    add(new VSpace(20));
068                    
069                    Grid grid = new Grid(2);
070                    grid.setInsets(new Insets(10, 5, 0, 0));
071                    grid.add(new SolidLabel("username:", Font.ITALIC));
072                    usernameField.setWidth(new Extent(200));
073                    usernameField.addActionListener(this);
074                    grid.add(usernameField);
075                    if (passwordRequired) {
076                            grid.add(new SolidLabel("password:", Font.ITALIC));
077                            passwordField.setWidth(new Extent(200));
078                            passwordField.addActionListener(this);
079                            grid.add(passwordField);
080                    }
081                    add(grid);
082                    
083                    add(new VSpace(30));
084                    
085                    Row r3 = new Row();
086                    r3.setInsets(new Insets(10, 0));
087                    r3.setCellSpacing(new Extent(5));
088                    r3.add(new GeneralButton("Login", this));
089                    add(r3);
090            }
091    
092            public void actionPerformed(ActionEvent e) {
093                    wiki.log("logi", "pressed: login");
094                    String username = usernameField.getText();
095                    if (passwordRequired) {
096                            String password = passwordField.getText();
097                            String correctPassword = getPassword(username);
098                            if (password.equals(correctPassword)) {
099                                    wiki.log("logi", "correct password for " + username);
100                                    wiki.setUsername(username);
101                                    wiki.log("syst", "login");
102                                    window.setContent(wiki.getContentPane());
103                            } else {
104                                    wiki.log("logi", "incorrect password: " + username);
105                                    window.getContent().add(new MessageWindow("Error", "Invalid username or password!", "OK"));
106                            }
107                    } else {
108                            if (username.equals("")) {
109                                    wiki.log("logi", "no username entered");
110                                    window.getContent().add(new MessageWindow("Error", "Please enter your name!", "OK"));
111                            } else {
112                                    wiki.log("logi", "username: " + username);
113                                    wiki.setUsername(username);
114                                    wiki.log("syst", "login");
115                                    window.setContent(wiki.getContentPane());
116                            }
117                    }
118            }
119            
120            private String getPassword(String username) {
121                    // Put your code here that returns the password for the given username!
122                    return "password";
123            }
124            
125    }