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 window for the registration of a new user.
043     * 
044     * @author Tobias Kuhn
045     */
046    public class RegisterWindow 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 TextField emailField = new TextField(250, this, Font.ITALIC);
054            private PasswordField passwordField = new PasswordField(250, this);
055            private PasswordField retypePasswordField = new PasswordField(250, this);
056            private CheckBox stayLoggedInCheckBox = new CheckBox();
057            
058            /**
059             * Creates a new registration window.
060             * 
061             * @param username The default username (from the login window).
062             * @param password The default password (from the login window).
063             * @param stayLoggedIn The default value for staying logged in (from the login window).
064             * @param wiki The wiki instance.
065             */
066            public RegisterWindow(String username, String password, boolean stayLoggedIn, Wiki wiki) {
067                    this.wiki = wiki;
068                    
069                    usernameField.setText(username);
070                    passwordField.setText(password);
071                    stayLoggedInCheckBox.setSelected(stayLoggedIn);
072                    
073                    setTitle("User Registration");
074                    setTitleFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
075                    setModal(true);
076                    setWidth(new Extent(470));
077                    setHeight(new Extent(290));
078                    setResizable(false);
079                    setMovable(true);
080                    setClosable(!wiki.isLoginRequiredForViewing());
081                    setTitleBackground(Style.windowTitleBackground);
082                    setStyleName("Default");
083                    
084                    wiki.log("logi", "registration window");
085    
086                    Grid mainGrid = new Grid(1);
087                    mainGrid.setInsets(new Insets(10, 10, 10, 0));
088                    mainGrid.setColumnWidth(0, new Extent(450));
089                    mainGrid.setRowHeight(0, new Extent(200));
090                    
091                    Column messageColumn = new Column();
092                    GridLayoutData layout1 = new GridLayoutData();
093                    layout1.setAlignment(new Alignment(Alignment.LEFT, Alignment.TOP));
094                    messageColumn.setLayoutData(layout1);
095                    Label label = new Label("Enter your data for registration:");
096                    label.setFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
097                    messageColumn.add(label);
098                    messageColumn.add(new VSpace());
099                    
100                    Grid formGrid = new Grid(2);
101                    formGrid.setInsets(new Insets(10, 10, 10, 0));
102                    formGrid.add(new SolidLabel("username:", Font.ITALIC));
103                    formGrid.add(usernameField);
104                    formGrid.add(new SolidLabel("email:", Font.ITALIC));
105                    formGrid.add(emailField);
106                    formGrid.add(new SolidLabel("password:", Font.ITALIC));
107                    formGrid.add(passwordField);
108                    formGrid.add(new SolidLabel("retype password:", Font.ITALIC));
109                    formGrid.add(retypePasswordField);
110                    formGrid.add(new SolidLabel("stay logged in:", Font.ITALIC));
111                    formGrid.add(stayLoggedInCheckBox);
112                    messageColumn.add(formGrid);
113                    
114                    mainGrid.add(messageColumn);
115    
116                    Row buttonBar = new Row();
117                    buttonBar.setCellSpacing(new Extent(10));
118                    buttonBar.setInsets(new Insets(0, 0, 0, 10));
119                    buttonBar.add(new GeneralButton("Register", 80, this));
120                    buttonBar.add(new GeneralButton("Cancel", 80, this));
121                    GridLayoutData layout2 = new GridLayoutData();
122                    layout2.setAlignment(new Alignment(Alignment.CENTER, Alignment.BOTTOM));
123                    buttonBar.setLayoutData(layout2);
124                    mainGrid.add(buttonBar);
125                    
126                    add(mainGrid);
127                    
128                    wiki.getApplication().setFocusedComponent(usernameField);
129            }
130    
131            public void actionPerformed(ActionEvent e) {
132                    UserBase ub = wiki.getUserBase();
133                    String username = usernameField.getText();
134                    String password = passwordField.getText();
135                    String password2 = retypePasswordField.getText();
136                    String email = emailField.getText();
137                    boolean stayLoggedIn = stayLoggedInCheckBox.isSelected();
138                    if ("Cancel".equals(e.getActionCommand())) {
139                            wiki.log("logi", "registration canceled");
140                            setVisible(false);
141                            wiki.removeWindow(this);
142                            if (wiki.isLoginRequiredForViewing()) {
143                                    wiki.showLoginWindow();
144                            }
145                    } else {
146                            wiki.log("logi", "pressed: register");
147                            if (username.length() < 3 || username.length() > 20) {
148                                    wiki.log("logi", "invalid username");
149                                    wiki.showWindow(new MessageWindow(
150                                                    "Error",
151                                                    "Username needs between 3 and 20 characters.",
152                                                    "OK"
153                                            ));
154                            } else if (!username.matches("[a-zA-Z0-9'.][a-zA-Z0-9'._\\- ]*[a-zA-Z0-9'.]")) {
155                                    wiki.log("logi", "invalid username");
156                                    wiki.showWindow(new MessageWindow(
157                                                    "Error",
158                                                    "Username contains illegal characters.",
159                                                    "OK"
160                                            ));
161                            } else if (password.length() < 5) {
162                                    wiki.log("logi", "invalid password");
163                                    wiki.showWindow(new MessageWindow(
164                                                    "Error",
165                                                    "Password needs at least 5 characters.",
166                                                    "OK"
167                                            ));
168                            } else if (!password.equals(password2)) {
169                                    wiki.log("logi", "retype password does not match");
170                                    wiki.showWindow(new MessageWindow(
171                                                    "Error",
172                                                    "The two passwords do not match.",
173                                                    "OK"
174                                            ));
175                            } else if (email.indexOf("@") < 0) {
176                                    wiki.log("logi", "no email");
177                                    wiki.showWindow(new MessageWindow(
178                                                    "Error",
179                                                    "Please provide a valid email address.",
180                                                    "OK"
181                                            ));
182                            } else {
183                                    User user = ub.register(username, email, password);
184                                    if (user == null) {
185                                            wiki.log("logi", "username already taken: " + username);
186                                            wiki.showWindow(new MessageWindow(
187                                                            "Error",
188                                                            "Username is already taken.",
189                                                            "OK"
190                                                    ));
191                                    } else {
192                                            wiki.log("logi", "register successful for " + username);
193                                            wiki.login(user, stayLoggedIn);
194                                            wiki.removeWindow(this);
195                                    }
196                            }
197                    }
198            }
199            
200    }