001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008, 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            
048            private TextField usernameField = new TextField();
049            private PasswordField passwordField = new PasswordField();
050            
051            /**
052             * Creates a new login screen.
053             * 
054             * @param wiki The wiki instance.
055             * @param window The window in which the login screen is shown.
056             */
057            public LoginScreen(Wiki wiki, Window window) {
058                    this.wiki = wiki;
059                    this.window = window;
060                    
061                    wiki.log("logi", "login page");
062                    
063                    add(new VSpace(20));
064                    add(new Title("AceWiki Login", true));
065                    add(new VSpace(20));
066                    
067                    Grid grid = new Grid(2);
068                    grid.setInsets(new Insets(10, 5, 0, 0));
069                    grid.add(new SolidLabel("username:", Font.ITALIC));
070                    usernameField.setWidth(new Extent(100));
071                    grid.add(usernameField);
072                    grid.add(new SolidLabel("password:", Font.ITALIC));
073                    passwordField.setWidth(new Extent(100));
074                    grid.add(passwordField);
075                    add(grid);
076                    
077                    add(new VSpace(20));
078                    
079                    Row r3 = new Row();
080                    r3.setInsets(new Insets(10, 0));
081                    r3.setCellSpacing(new Extent(5));
082                    r3.add(new GeneralButton("Login", this));
083                    add(r3);
084            }
085    
086            public void actionPerformed(ActionEvent e) {
087                    wiki.log("logi", "pressed: login");
088                    String username = usernameField.getText();
089                    String password = passwordField.getText();
090                    String correctPassword = getPassword(username);
091                    if (password.equals(correctPassword)) {
092                            wiki.log("logi", "correct password for " + username);
093                            wiki.setUsername(username);
094                            wiki.log("syst", "login");
095                            window.setContent(wiki.getContentPane());
096                    } else {
097                            wiki.log("logi", "incorrect password: " + username);
098                            window.getContent().add(new MessageWindow("Error", "Invalid username or password!", "OK"));
099                    }
100            }
101            
102            private String getPassword(String username) {
103                    // Put your code here that returns the password for the given username!
104                    return "password";
105            }
106            
107    }