001 // This file is part of AceWiki.
002 // Copyright 2008-2012, AceWiki developers.
003 //
004 // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005 // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006 // the License, or (at your option) any later version.
007 //
008 // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010 // Lesser General Public License for more details.
011 //
012 // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013 // not, see http://www.gnu.org/licenses/.
014
015 package ch.uzh.ifi.attempto.acewiki.gui;
016
017 import nextapp.echo.app.Alignment;
018 import nextapp.echo.app.Column;
019 import nextapp.echo.app.Extent;
020 import nextapp.echo.app.Font;
021 import nextapp.echo.app.Grid;
022 import nextapp.echo.app.Insets;
023 import nextapp.echo.app.Row;
024 import nextapp.echo.app.WindowPane;
025 import nextapp.echo.app.event.ActionEvent;
026 import nextapp.echo.app.event.ActionListener;
027 import nextapp.echo.app.layout.GridLayoutData;
028 import ch.uzh.ifi.attempto.acewiki.Wiki;
029 import ch.uzh.ifi.attempto.acewiki.core.User;
030 import ch.uzh.ifi.attempto.acewiki.core.UserBase;
031 import ch.uzh.ifi.attempto.echocomp.CheckBox;
032 import ch.uzh.ifi.attempto.echocomp.GeneralButton;
033 import ch.uzh.ifi.attempto.echocomp.Label;
034 import ch.uzh.ifi.attempto.echocomp.MessageWindow;
035 import ch.uzh.ifi.attempto.echocomp.PasswordField;
036 import ch.uzh.ifi.attempto.echocomp.SolidLabel;
037 import ch.uzh.ifi.attempto.echocomp.Style;
038 import ch.uzh.ifi.attempto.echocomp.TextField;
039 import ch.uzh.ifi.attempto.echocomp.VSpace;
040
041 /**
042 * This class represents a login window.
043 *
044 * @author Tobias Kuhn
045 */
046 public class LoginWindow extends WindowPane implements ActionListener {
047
048 private static final long serialVersionUID = -6704597832001286479L;
049
050 private Wiki wiki;
051
052 private TextField usernameField = new TextField(250, this, Font.ITALIC);
053 private PasswordField passwordField = new PasswordField(250, this);
054 private CheckBox stayLoggedInCheckBox = new CheckBox();
055
056 /**
057 * Creates a new login window.
058 *
059 * @param wiki The wiki instance.
060 */
061 public LoginWindow(Wiki wiki) {
062 this.wiki = wiki;
063
064 setTitle("Login");
065 setTitleFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
066 setModal(true);
067 setWidth(new Extent(470));
068 setHeight(new Extent(240));
069 setResizable(false);
070 setMovable(true);
071 setClosable(!wiki.isLoginRequiredForViewing());
072 setTitleBackground(Style.windowTitleBackground);
073 setStyleName("Default");
074
075 wiki.log("logi", "login window");
076
077 Grid mainGrid = new Grid(1);
078 mainGrid.setInsets(new Insets(10, 10, 10, 0));
079 mainGrid.setColumnWidth(0, new Extent(450));
080 mainGrid.setRowHeight(0, new Extent(150));
081
082 Column messageColumn = new Column();
083 GridLayoutData layout1 = new GridLayoutData();
084 layout1.setAlignment(new Alignment(Alignment.LEFT, Alignment.TOP));
085 messageColumn.setLayoutData(layout1);
086 Label label = new Label("Enter your username and password:");
087 label.setFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
088 messageColumn.add(label);
089 messageColumn.add(new VSpace());
090
091 Grid formGrid = new Grid(2);
092 formGrid.setInsets(new Insets(10, 10, 10, 0));
093 formGrid.add(new SolidLabel("username:", Font.ITALIC));
094 formGrid.add(usernameField);
095 usernameField.setText(wiki.getCookie("lastusername"));
096 formGrid.add(new SolidLabel("password:", Font.ITALIC));
097 formGrid.add(passwordField);
098 formGrid.add(new SolidLabel("stay logged in:", Font.ITALIC));
099 stayLoggedInCheckBox.setSelected(!wiki.getCookie("stayloggedin").equals("false"));
100 formGrid.add(stayLoggedInCheckBox);
101 messageColumn.add(formGrid);
102
103 mainGrid.add(messageColumn);
104
105 Row buttonBar = new Row();
106 buttonBar.setCellSpacing(new Extent(10));
107 buttonBar.setInsets(new Insets(0, 0, 0, 10));
108 buttonBar.add(new GeneralButton("Login", 80, this));
109 if (wiki.isUserRegistrationOpen()) {
110 buttonBar.add(new GeneralButton("Register...", 80, this));
111 }
112 if (!wiki.isLoginRequiredForViewing()) {
113 buttonBar.add(new GeneralButton("Cancel", 80, this));
114 }
115 GridLayoutData layout2 = new GridLayoutData();
116 layout2.setAlignment(new Alignment(Alignment.CENTER, Alignment.BOTTOM));
117 buttonBar.setLayoutData(layout2);
118 mainGrid.add(buttonBar);
119
120 add(mainGrid);
121
122 if (usernameField.getText().length() == 0) {
123 wiki.getApplication().setFocusedComponent(usernameField);
124 } else {
125 wiki.getApplication().setFocusedComponent(passwordField);
126 }
127 }
128
129 public void actionPerformed(ActionEvent e) {
130 UserBase ub = wiki.getUserBase();
131 String username = usernameField.getText();
132 String password = passwordField.getText();
133 boolean stayLoggedIn = stayLoggedInCheckBox.isSelected();
134 if ("Cancel".equals(e.getActionCommand())) {
135 wiki.log("logi", "login canceled");
136 setVisible(false);
137 wiki.removeWindow(this);
138 } else if ("Register...".equals(e.getActionCommand())) {
139 wiki.log("logi", "pressed: register...");
140 wiki.showWindow(new RegisterWindow(username, password, stayLoggedIn, wiki));
141 wiki.removeWindow(this);
142 } else {
143 wiki.log("logi", "pressed: login");
144 User user = ub.login(username, password);
145 if (user != null) {
146 wiki.log("logi", "correct password for " + username);
147 wiki.login(user, stayLoggedInCheckBox.isSelected());
148 wiki.removeWindow(this);
149 } else {
150 wiki.log("logi", "incorrect username or password for " + username);
151 wiki.showWindow(new MessageWindow(
152 "Error",
153 "Invalid username or password!",
154 "OK"
155 ));
156 }
157 }
158 }
159
160 }