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.echocomp.GeneralButton;
031    import ch.uzh.ifi.attempto.echocomp.Label;
032    import ch.uzh.ifi.attempto.echocomp.MessageWindow;
033    import ch.uzh.ifi.attempto.echocomp.PasswordField;
034    import ch.uzh.ifi.attempto.echocomp.SolidLabel;
035    import ch.uzh.ifi.attempto.echocomp.Style;
036    import ch.uzh.ifi.attempto.echocomp.TextField;
037    import ch.uzh.ifi.attempto.echocomp.VSpace;
038    
039    /**
040     * This class represents a window with information about the current user.
041     * 
042     * @author Tobias Kuhn
043     */
044    public class UserWindow extends WindowPane implements ActionListener {
045            
046            private static final long serialVersionUID = -6704597832001286479L;
047            
048            private Wiki wiki;
049            private User user;
050            
051            private TextField emailField = new TextField(220, this, Font.ITALIC);
052            private PasswordField passwordField = new PasswordField(220, this);
053            private PasswordField newPasswordField = new PasswordField(220, this);
054            private PasswordField retypePasswordField = new PasswordField(220, this);
055            
056            private Row buttonBar;
057            
058            /**
059             * Creates a new user window.
060             * 
061             * @param wiki The wiki instance.
062             */
063            public UserWindow(Wiki wiki) {
064                    this.wiki = wiki;
065                    this.user = wiki.getUser();
066                    
067                    setTitle("User: " + user.getName());
068                    setTitleFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
069                    setModal(true);
070                    setWidth(new Extent(470));
071                    setHeight(new Extent(360));
072                    setResizable(false);
073                    setMovable(true);
074                    setTitleBackground(Style.windowTitleBackground);
075                    setStyleName("Default");
076                    
077                    wiki.log("logi", "registration window");
078    
079                    Grid mainGrid = new Grid(1);
080                    mainGrid.setInsets(new Insets(10, 10, 10, 0));
081                    mainGrid.setColumnWidth(0, new Extent(450));
082                    mainGrid.setRowHeight(0, new Extent(270));
083                    
084                    Column messageColumn = new Column();
085                    GridLayoutData layout1 = new GridLayoutData();
086                    layout1.setAlignment(new Alignment(Alignment.LEFT, Alignment.TOP));
087                    messageColumn.setLayoutData(layout1);
088                    Label label = new Label("Your personal information:");
089                    label.setFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
090                    messageColumn.add(label);
091                    messageColumn.add(new VSpace());
092                    
093                    Grid formGrid = new Grid(2);
094                    formGrid.setInsets(new Insets(10, 10, 10, 0));
095                    formGrid.add(new SolidLabel("username:", Font.ITALIC));
096                    formGrid.add(new SolidLabel(user.getName(), Font.ITALIC));
097                    formGrid.add(new SolidLabel("registration:", Font.ITALIC));
098                    formGrid.add(new SolidLabel(user.getUserData("registerdate"), Font.ITALIC));
099                    formGrid.add(new SolidLabel("number of sessions:", Font.ITALIC));
100                    formGrid.add(new SolidLabel(user.getUserData("logincount"), Font.ITALIC));
101                    formGrid.add(new SolidLabel("email:", Font.ITALIC));
102                    emailField.setText(user.getUserData("email"));
103                    emailField.setEnabled(false);
104                    formGrid.add(emailField);
105                    formGrid.add(new SolidLabel("current password:", Font.ITALIC));
106                    passwordField.setText("***************");
107                    passwordField.setEnabled(false);
108                    formGrid.add(passwordField);
109                    formGrid.add(new SolidLabel("new password:", Font.ITALIC));
110                    newPasswordField.setEnabled(false);
111                    formGrid.add(newPasswordField);
112                    formGrid.add(new SolidLabel("retype new password:", Font.ITALIC));
113                    retypePasswordField.setEnabled(false);
114                    formGrid.add(retypePasswordField);
115                    messageColumn.add(formGrid);
116                    
117                    mainGrid.add(messageColumn);
118    
119                    buttonBar = new Row();
120                    buttonBar.setCellSpacing(new Extent(10));
121                    buttonBar.setInsets(new Insets(0, 0, 0, 10));
122                    buttonBar.add(new GeneralButton("Unlock", 80, this));
123                    buttonBar.add(new GeneralButton("Close", 80, this));
124                    GridLayoutData layout2 = new GridLayoutData();
125                    layout2.setAlignment(new Alignment(Alignment.CENTER, Alignment.BOTTOM));
126                    buttonBar.setLayoutData(layout2);
127                    mainGrid.add(buttonBar);
128                    
129                    add(mainGrid);
130            }
131    
132            public void actionPerformed(ActionEvent e) {
133                    String password = passwordField.getText();
134                    String newPassword = newPasswordField.getText();
135                    String newPassword2 = retypePasswordField.getText();
136                    String email = emailField.getText();
137                    if ("Cancel".equals(e.getActionCommand()) || "Close".equals(e.getActionCommand())) {
138                            wiki.log("logi", "registration canceled");
139                            setVisible(false);
140                            wiki.removeWindow(this);
141                    } else if ("Unlock".equals(e.getActionCommand())) {
142                            emailField.setEnabled(true);
143                            passwordField.setEnabled(true);
144                            passwordField.setText("");
145                            newPasswordField.setEnabled(true);
146                            retypePasswordField.setEnabled(true);
147                            buttonBar.removeAll();
148                            buttonBar.add(new GeneralButton("Change", 80, this));
149                            buttonBar.add(new GeneralButton("Cancel", 80, this));
150                            wiki.getApplication().setFocusedComponent(emailField);
151                    } else {
152                            wiki.log("logi", "pressed: change user data");
153                            if (!user.isCorrectPassword(password)) {
154                                    wiki.log("logi", "invalid password");
155                                    if (password.length() == 0) {
156                                            wiki.showWindow(new MessageWindow(
157                                                            "Error",
158                                                            "You have to enter your current password to apply the changes.",
159                                                            "OK"
160                                                    ));
161                                    } else {
162                                            wiki.showWindow(new MessageWindow(
163                                                            "Error",
164                                                            "The password is not correct.",
165                                                            "OK"
166                                                    ));
167                                    }
168                            } else if (newPassword.length() > 0 && newPassword.length() < 5) {
169                                    wiki.log("logi", "invalid new password");
170                                    wiki.showWindow(new MessageWindow(
171                                                    "Error",
172                                                    "Password needs at least 5 characters.",
173                                                    "OK"
174                                            ));
175                            } else if (!newPassword.equals(newPassword2)) {
176                                    wiki.log("logi", "retype password does not match");
177                                    wiki.showWindow(new MessageWindow(
178                                                    "Error",
179                                                    "The two passwords do not match.",
180                                                    "OK"
181                                            ));
182                            } else if (email.indexOf("@") < 0) {
183                                    wiki.log("logi", "no email");
184                                    wiki.showWindow(new MessageWindow(
185                                                    "Error",
186                                                    "Please provide a valid email address.",
187                                                    "OK"
188                                            ));
189                            } else {
190                                    user.setUserData("email", email);
191                                    if (newPassword.length() > 0) {
192                                            user.changePassword(password, newPassword);
193                                    }
194                                    wiki.removeWindow(this);
195                            }
196                    }
197            }
198            
199    }