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 * @param title The title to be displayed. 058 * @param passwordRequired true if a password is required (not fully implemented). 059 */ 060 public LoginScreen(Wiki wiki, Window window, String title, boolean passwordRequired) { 061 this.wiki = wiki; 062 this.window = window; 063 this.passwordRequired = passwordRequired; 064 065 wiki.log("logi", "login page"); 066 067 add(new VSpace(20)); 068 add(new Title(title + " - Login", true)); 069 add(new VSpace(20)); 070 071 Grid grid = new Grid(2); 072 grid.setInsets(new Insets(10, 5, 0, 0)); 073 grid.add(new SolidLabel("username:", Font.ITALIC)); 074 usernameField.setWidth(new Extent(200)); 075 usernameField.addActionListener(this); 076 grid.add(usernameField); 077 if (passwordRequired) { 078 grid.add(new SolidLabel("password:", Font.ITALIC)); 079 passwordField.setWidth(new Extent(200)); 080 passwordField.addActionListener(this); 081 grid.add(passwordField); 082 } 083 add(grid); 084 085 add(new VSpace(30)); 086 087 Row r3 = new Row(); 088 r3.setInsets(new Insets(10, 0)); 089 r3.setCellSpacing(new Extent(5)); 090 r3.add(new GeneralButton("Login", this)); 091 add(r3); 092 } 093 094 public void actionPerformed(ActionEvent e) { 095 wiki.log("logi", "pressed: login"); 096 String username = usernameField.getText(); 097 if (passwordRequired) { 098 String password = passwordField.getText(); 099 String correctPassword = getPassword(username); 100 if (password.equals(correctPassword)) { 101 wiki.log("logi", "correct password for " + username); 102 wiki.setUsername(username); 103 wiki.log("syst", "login"); 104 window.setContent(wiki.getContentPane()); 105 } else { 106 wiki.log("logi", "incorrect password: " + username); 107 window.getContent().add(new MessageWindow("Error", "Invalid username or password!", "OK")); 108 } 109 } else { 110 if (username.equals("")) { 111 wiki.log("logi", "no username entered"); 112 window.getContent().add(new MessageWindow("Error", "Please enter your name!", "OK")); 113 } else { 114 wiki.log("logi", "username: " + username); 115 wiki.setUsername(username); 116 wiki.log("syst", "login"); 117 window.setContent(wiki.getContentPane()); 118 } 119 } 120 } 121 122 private String getPassword(String username) { 123 // Put your code here that returns the password for the given username! 124 return "password"; 125 } 126 127 }