001    package users.stdforms;
002    
003    import users.*;
004    import users.swing.*;
005    
006    import sale.*;
007    
008    import util.swing.*;
009    
010    import javax.swing.*;
011    import javax.swing.event.*;
012    
013    import java.awt.*;
014    import java.awt.event.*;
015    
016    import java.util.*;
017    
018    /**
019     * FormSheet that can be used for log on procedures. The FormSheet will contain a
020     * {@link javax.swing.JComboBox} allowing to select from a range of user names and optionally a password
021     * input line.
022     *
023     * When the user clicks the "OK" button, the FormSheet checks if the password entered equals the
024     * password of the user selected. Prior to this, the password will be garbled using
025     * {@link User#garblePassWD the current global password garbler}. If the checking was successful,
026     * {@link #getResult} will return the selected user, otherwise it will return <code>null</code>.<p>
027     *
028     * <p>A typical use sequence would look something like that:</p>
029     *
030     * <pre>
031     *
032     * ...
033     * <b>LogOnForm lof = new LogOnForm ("Please log on",
034     *                                "Select your user name:",
035     *                                "Enter your password:",
036     *                                true,
037     *                                {@link UserManager#getGlobalUM UserManager.getGlobalUM()},
038     *                                null,
039     *                                null);</b>
040     *
041     * ...setFormSheet (lof);
042     *
043     * if (<b>lof.getResult() != null</b>) {
044     *   // do log on of user lof.getResult
045     * }
046     * ...
047     *
048     * </pre>
049     *
050     * @author Steffen Zschaler
051     * @version 2.0 28/07/1999
052     * @since v2.0
053     */
054    public class LogOnForm extends FormSheet {
055    
056        /**
057         * The current user
058         *
059         * @serial
060         */
061        private User m_uCurrent;
062    
063        /**
064         * The current password.
065         *
066         * @serial
067         */
068        private char[] m_sPassword;
069    
070        /**
071         * True, if we asked a password.
072         *
073         * @serial
074         */
075        private boolean m_fAskPassWd;
076    
077        /**
078         * Resulting user.
079         *
080         * @serial
081         */
082        private User m_uLogOnUser;
083        
084        /**
085         * Converst a String to char[]-Array
086         * @param strValue the String to be converted
087         * @return the converted value as char[]-Array
088         */
089        public static char[] getCharFromString(String strValue) {
090            char[] chReturn = new char[strValue.length()];
091            int i;
092            for(i=0;i<strValue.length();i++) {
093                chReturn[i] = strValue.charAt(i);
094            }
095            return chReturn;
096        }
097    
098        /**
099         * Create a new LogOnForm. Uses a {@link FormSheetContentCreator}.
100         *
101         * @param sCaption the caption of the {@link FormSheet}.
102         * @param sUserPrompt a string prompting the user to select his/her user name.
103         * @param sPassWdPrompt  a string prompting the user to enter his/her password. May be <code>null</code>
104         * only if <i>fAskPassWd</i> is false.
105         * @param fAskPassWd if false no password is needed and selection of the user name is sufficient.
106         * @param um the UserManager that manages the users to select from. Normally the
107         * {@link UserManager#getGlobalUM global UserManager}.
108         * @param cmp a comparator that defines the order in which the user names appear. If <code>null</code>,
109         * users will be ordered by their names.
110         * @param uf a filter that allows only a subset of the users to be selected from. If <code>null</code>,
111         * no filtering will occur.
112         */
113        public LogOnForm(String sCaption, final String sUserPrompt, final String sPassWdPrompt,
114                boolean fAskPassWd, final UserManager um, final Comparator cmp, final UserFilter uf) {
115            super(sCaption, (JComponent)null, true);
116    
117            m_fAskPassWd = fAskPassWd;
118    
119            addContentCreator(new FormSheetContentCreator() {
120                protected void createFormSheetContent(FormSheet fs) {
121                    //JPanel jpForm = new JPanel(new GridLayout(((m_fAskPassWd) ? (2) : (1)), 2));
122                    JPanel jpForm = new JPanel();
123                    JPanel jpCaption = new JPanel(new GridLayout(((m_fAskPassWd) ? (2) : (1)), 1, 5, 5));
124                    JPanel jpBoxes = new JPanel(new GridLayout(((m_fAskPassWd) ? (2) : (1)), 1, 5, 5));
125                    jpForm.add(jpCaption);
126                    jpForm.add(jpBoxes);
127                    jpBoxes.setPreferredSize(new Dimension(100,50));
128                    //jpForm.add(new JLabel(sUserPrompt));
129                    jpCaption.add(new JLabel(sUserPrompt));
130    
131                    UserComboBoxModel ucbmModel = new UserComboBoxModel(um, uf, cmp);
132    
133                    JComboBox jcb = new JComboBox(ucbmModel);
134                    jcb.setRenderer(new JUserList.UserListCellRenderer());
135                    jcb.addItemListener(new ItemListener() {
136                        public void itemStateChanged(ItemEvent e) {
137                            if (e.getStateChange() == ItemEvent.SELECTED) {
138                                m_uCurrent = (User)e.getItem();
139                            } else {
140                                m_uCurrent = null;
141                            }
142                        }
143                    });
144    
145                    jpBoxes.add(jcb);
146    
147                    if (m_fAskPassWd) {
148                        jpCaption.add(new JLabel(sPassWdPrompt));
149    
150                        JTextField jtf = new JPasswordField();
151                        jtf.getDocument().addDocumentListener(new DocumentListener() {
152                            public void changedUpdate(DocumentEvent e) {
153                                try {
154                                    m_sPassword = getCharFromString(e.getDocument().getText(0, e.getDocument().getLength()));
155                                }
156                                catch (javax.swing.text.BadLocationException ex) {}
157                            }
158    
159                            public void insertUpdate(DocumentEvent e) {
160                                try {
161                                    m_sPassword = getCharFromString(e.getDocument().getText(0, e.getDocument().getLength()));
162                                }
163                                catch (javax.swing.text.BadLocationException ex) {}
164                            }
165    
166                            public void removeUpdate(DocumentEvent e) {
167                                try {
168                                    m_sPassword = getCharFromString(e.getDocument().getText(0, e.getDocument().getLength()));
169                                }
170                                catch (javax.swing.text.BadLocationException ex) {}
171                            }
172                        });
173    
174                        jpBoxes.add(jtf);
175                    }
176    
177                    fs.setComponent(jpForm);
178                }
179            });
180        }
181    
182        /**
183         * Return the user that was selected if any. Return <code>null</code> if no user was selected, the password
184         * was wrong or the FormSheet was cancelled. The value is only valid after the FormSheet was closed!
185         *
186         * @override Never
187         */
188        public User getResult() {
189            return m_uLogOnUser;
190        }
191    
192        /**
193         * Overridden to check the password input and make sure the selected user can be returned by
194         * {@link #getResult}.
195         *
196         * @override Never
197         */
198        public void ok() {
199            if (m_uCurrent != null) {
200                // If no input occurred, the password string is null. We don't want to run into a
201                // NullPointerException, so we come up with a dummy password.
202                if (m_sPassword == null) {
203                    m_sPassword = new char[0];
204                }
205                if ((!m_fAskPassWd) || (m_uCurrent.isPassWd(User.garblePassWD(m_sPassword)))) {
206                    m_uLogOnUser = m_uCurrent;
207                }
208            }
209    
210            super.ok();
211        }
212    }