001    package market.stdform;
002    
003    import java.awt.Dimension;
004    import java.awt.GridBagConstraints;
005    import java.awt.GridBagLayout;
006    import java.awt.GridLayout;
007    
008    import javax.swing.BoxLayout;
009    import javax.swing.JComboBox;
010    import javax.swing.JLabel;
011    import javax.swing.JPanel;
012    import javax.swing.JPasswordField;
013    import javax.swing.JTextField;
014    
015    import market.Conversions;
016    import market.UCustomer;
017    import market.UPerson;
018    import market.UStaffer;
019    import market.VCDummy;
020    import market.VCPositiveDouble;
021    import market.VCPositiveInteger;
022    import market.swing.ComponentFactory;
023    import market.swing.JTFCheckable;
024    import sale.FormSheet;
025    import sale.FormSheetContentCreator;
026    
027    /**
028     * This FormSheet class is used in multiple cases, for both editing customers and employees.
029     * The look of the FormSheet varies slightly depending on the type of person to be edited.
030     * Customers have a field with their company and their discount, employees have a field with
031     * their special occupation and their salary.
032     * The types of usage for this FormSheet are:
033     * <ul>
034     * <li>For new customers to sign up: all fields but discount are editable.</li>
035     * <li>For customers to edit their data: all fields but login and discount are enabled</li>
036     * <li>For the manager to edit customers: only the password fields are enabled</li>
037     * <li>For the manager to employ new workers: all fields are enabled.</li>
038     * <li>For the manager to edit employees: all fields but login are enabled</li>
039     * </ul>
040     */
041    public class FSEditPersonData extends FormSheet{
042    
043        public static final int JTFC_FIRSTNAME = 0;
044        public static final int JTFC_NAME = 1;
045        public static final int JTFC_TELEPHONE = 2;
046        public static final int JTFC_STREET = 3;
047        public static final int JTFC_POSTCODE = 4;
048        public static final int JTFC_CITY = 5;
049        public static final int JTFC_LOGIN = 6;
050        public static final int JTFC_COMPANY = 7;
051        public static final int JTFC_SALARY = 8;
052        public static final int JTFC_SECTION = 9;
053        public static final int JTFC_DISCOUNT = 10;
054        private static final int WIDTH = 15;
055        private UPerson person;
056        private JComboBox jcb_salutation;
057        private JComboBox jcb_qualification;
058        private JTFCheckable jtfc_firstName = new JTFCheckable(JTFC_FIRSTNAME, new VCDummy("Vorname"), WIDTH);
059        private JTFCheckable jtfc_name = new JTFCheckable(JTFC_NAME, new VCDummy("Nachname"), WIDTH);
060        private JTFCheckable jtfc_telephone = new JTFCheckable(JTFC_TELEPHONE,
061                new VCDummy("Telefon", true), WIDTH);
062        private JTFCheckable jtfc_street = new JTFCheckable(JTFC_STREET, new VCDummy("Straße"), WIDTH);
063        private JTFCheckable jtfc_postcode = new JTFCheckable(JTFC_POSTCODE,
064                new VCPositiveInteger("Postleitzahl"), WIDTH);
065        private JTFCheckable jtfc_city = new JTFCheckable(JTFC_CITY, new VCDummy("Stadt"), WIDTH);
066        private JTFCheckable jtfc_login = new JTFCheckable(JTFC_LOGIN, new VCDummy("Login"), WIDTH);
067        private JTFCheckable jtfc_company = new JTFCheckable(JTFC_COMPANY, new VCDummy("Firma"), 10);
068        private JTFCheckable jtfc_discount = new JTFCheckable(JTFC_DISCOUNT, new VCDummy("Rabatt"), 10);
069        private JTFCheckable jtfc_salary = new JTFCheckable(JTFC_SALARY, new VCPositiveDouble("Gehalt"), WIDTH);
070        private JPasswordField jpf_password = new JPasswordField(WIDTH);
071        private JPasswordField jpf_confirm = new JPasswordField(WIDTH);
072        private JTextField jtf_Date = new JTextField(WIDTH);
073    
074        /**
075         * Creates a new FSEditPersonData FormSheet
076         *
077         * @param up the user to be edited, if <code>null</code> the fields stay empty.
078         * @param option an option to specify the special look of the FormSheet.
079         * <ul>
080         * <li>0 customer edits his profile</li>
081         * <li>1 manager edits customer profile</li>
082         * <li>2 manager edits worker profile</li>
083         * </ul>
084         */
085        public FSEditPersonData(UPerson up, final int option){
086            super("Personendaten", null);
087            this.person = up;
088            this.addContentCreator(new FormSheetContentCreator(){
089                protected void createFormSheetContent(FormSheet fs) {
090                    fs.removeAllButtons();
091                    JPanel jp_m = new JPanel();
092                    JPanel jp_main = new JPanel();
093                    JPanel jp_person = new JPanel();
094                    JPanel jp_address = new JPanel();
095                    JPanel jp_login = new JPanel();
096                    jcb_salutation = new JComboBox(new Object[] {UPerson.FRAU, UPerson.HERR});
097                    jcb_qualification = new JComboBox(new Object[]
098                            {UStaffer.SELLER, UStaffer.WAREHOUSE_WORKER, UStaffer.MANAGER});
099                    jcb_qualification.setPreferredSize(new Dimension(15,10));
100                    jcb_salutation.setPreferredSize(new Dimension(15,10));
101                    GridBagLayout gridbag = new GridBagLayout();
102                    GridBagConstraints c = new GridBagConstraints();
103                    jp_m.setLayout(gridbag);
104                        c.weighty = 1;
105                        c.anchor = GridBagConstraints.CENTER;
106                    gridbag.setConstraints(jp_main, c);
107                    jp_m.add(jp_main);
108                    jp_main.setLayout(new BoxLayout(jp_main, BoxLayout.Y_AXIS));
109                    jp_person.setLayout(new GridLayout(4, 2));
110                    jp_person.setBorder(ComponentFactory.createInsetBorder("Personendaten"));
111                    jp_address.setLayout(new GridLayout(3, 2));
112                    jp_address.setBorder(ComponentFactory.createInsetBorder("Adressdaten"));
113                    jp_login.setLayout(new GridLayout(3, 2));
114                    jp_login.setBorder(ComponentFactory.createInsetBorder("Login"));
115                    jp_person.add(new JLabel("Anrede:"));                jp_person.add(jcb_salutation);
116                    jp_person.add(new JLabel("Vorname:"));               jp_person.add(jtfc_firstName);
117                    jp_person.add(new JLabel("Nachname:"));              jp_person.add(jtfc_name);
118                    jp_person.add(new JLabel("Telefon:"));               jp_person.add(jtfc_telephone);
119                    jp_address.add(new JLabel("Straße:"));               jp_address.add(jtfc_street);
120                    jp_address.add(new JLabel("Postleitzahl:"));         jp_address.add(jtfc_postcode);
121                    jp_address.add(new JLabel("Stadt:"));                jp_address.add(jtfc_city);
122                    jp_login.add(new JLabel("Login:"));                  jp_login.add(jtfc_login);
123                    jp_login.add(new JLabel("Passwort:"));               jp_login.add(jpf_password);
124                    jp_login.add(new JLabel("Passwort-Wiederholung:"));  jp_login.add(jpf_confirm);
125                    jtfc_discount.setEditable(false);
126                    jp_main.add(jp_person);
127                    jp_main.add(jp_address);
128                    jp_main.add(jp_login);
129                    if (option == 0 || option == 1) {
130                        jp_main.add(getCompanyPanel());
131                    }
132                    if (option == 2) {
133                        jp_main.add(getQualificationPanel());
134                    }
135                    fs.addButton("OK", ButtonIDs.BTN_ACCEPT, null);
136                    fs.addButton("Zurück", ButtonIDs.BTN_BACK, null);
137                    fs.setComponent(jp_m);
138                }
139            });
140            // if a person was referred, the textfields are filled with the person's data
141            if (person != null) {
142                jcb_salutation.setSelectedItem(person.getSalutation());
143                jtfc_firstName.setText(person.getFirstName());
144                jtfc_name.setText(person.getSurname());
145                jtfc_telephone.setText(person.getTelephone());
146                jtfc_street.setText(person.getStreet());
147                jtfc_postcode.setText(String.valueOf(person.getPostcode()));
148                jtfc_city.setText(person.getCity());
149                jtfc_login.setText(person.getName());
150                jtfc_login.setEditable(false);      //login can never be changed
151                if (person instanceof UCustomer) {
152                    UCustomer p = (UCustomer)person;
153                    jtfc_company.setText(p.getCompany());
154                    jtfc_discount.setText(Conversions.fixedDecimal(100 * p.getDiscount(), 3) + " %");
155                }
156                if (person instanceof UStaffer) {
157                    UStaffer p = (UStaffer)person;
158                    jcb_qualification.setSelectedItem(p.getQualification());
159                    jtfc_salary.setText(Conversions.valueToCurrency(p.getSalary()));
160                }
161                jtf_Date.setText(person.getDayOfRegistration().toString());
162            }
163            if (option == 1) {              //if manager view customer's data...
164                jcb_salutation.setEnabled(false);           //disable all but password
165                jtfc_name.setEditable(false);
166                jtfc_firstName.setEditable(false);
167                jtfc_telephone.setEditable(false);
168                jtfc_street.setEditable(false);
169                jtfc_postcode.setEditable(false);
170                jtfc_city.setEditable(false);
171                jtfc_login.setEditable(false);
172                jtfc_company.setEditable(false);
173            }
174        }
175    
176        /**
177         * @return the panel with textfields which are used for customers only.
178         */
179        private JPanel getCompanyPanel(){
180            JPanel jp_company = new JPanel();
181            jp_company.setLayout(new GridLayout(3,2));
182            jp_company.setBorder(ComponentFactory.createInsetBorder("Sonstiges"));
183            jp_company.add(new JLabel("Firma: "));
184            jp_company.add(jtfc_company);
185            jp_company.add(new JLabel("Rabatt: "));
186            jtfc_discount.setText(Conversions.fixedDecimal(0, 3));
187            jp_company.add(jtfc_discount);
188            jp_company.add(new JLabel("Mitglied seit: "));
189            jp_company.add(jtf_Date);
190            jtf_Date.setEditable(false);
191            return jp_company;
192        }
193    
194        /**
195         * @return the panel with textfields which are used for employees only.
196         */
197        private JPanel getQualificationPanel(){
198            JPanel jp_qualification = new JPanel();
199            jp_qualification.setLayout(new GridLayout(3,2));
200            jp_qualification.setBorder(ComponentFactory.createInsetBorder("Beschäftigung"));
201            jp_qualification.add(new JLabel("Beschäftigung: "));
202            jp_qualification.add(jcb_qualification);
203            jp_qualification.add(new JLabel("Gehalt: "));
204            jp_qualification.add(jtfc_salary);
205            jp_qualification.add(new JLabel("Angestellt seit: "));
206            jp_qualification.add(jtf_Date);
207            jtf_Date.setEditable(false);
208            return jp_qualification;
209        }
210    
211        /**
212         * Checks if password and confirmation match.
213         * @return <code>true</code> if password and confirmation match, otherwise <code>false</code>.
214         */
215        public boolean passwordsEqual(){
216            String s1 = String.valueOf(jpf_password.getPassword());
217            String s2 = String.valueOf(jpf_confirm.getPassword());
218            return s1.compareTo(s2) == 0;
219        }
220    
221        /**
222         * @return the password typed into the password field.
223         */
224        public char[] getPassword(){
225            if(passwordsEqual() && (jpf_password.getPassword().length > 0)) {
226                return jpf_password.getPassword();
227            }
228            else return null;
229        }
230    
231        /**
232         * Checks if a password has been set.
233         * @return <code>true</code> if a password has been set, otherwise <code>false</code>.
234         */
235        public boolean isPasswordSet() {
236            return jpf_password.getPassword().length > 0 && jpf_confirm.getPassword().length > 0;
237        }
238    
239        /**
240         * @return value of the salutation ComboBox.
241         */
242        public String getSalutation(){
243            return (String)jcb_salutation.getSelectedItem();
244        }
245    
246        /**
247         * @return the value of the occupation ComboBox.
248         */
249        public String getQualification(){
250            return (String)jcb_qualification.getSelectedItem();
251        }
252    
253        /**
254         * Creation method for this FormSheet if it is to be used by customers.
255         * @param customer the customer to be displayed with the FormSheet.
256         * @return the FormSheet as {@link FSCheckable}.
257         */
258        public static FSCheckable getCustomerProfileForCustomer(UCustomer customer) {
259            FormSheet fs = new FSEditPersonData(customer, 0);
260            return new FSCheckable(fs);
261        }
262    
263        /**
264         * Creation method for this FormSheet if it is to be used by the manger to view customers' data.
265         * @param customer the customer to be displayed with the FormSheet.
266         * @return the FormSheet as {@link FSCheckable}.
267         */
268        public static FSCheckable getCustomerProfileForManager(UCustomer customer){
269            FormSheet fs = new FSEditPersonData(customer, 1);
270            return new FSCheckable(fs);
271        }
272    
273        /**
274         * Creation method for this FormSheet if it is to be used by the manger to view employees' data.
275         * @param staffer the employee to be displayed with the FormSheet.
276         * @return the FormSheet as {@link FSCheckable}.
277         */
278        public static FSCheckable getStafferProfile(UStaffer staffer){
279            FormSheet fs = new FSEditPersonData(staffer, 2);
280            return new FSCheckable(fs);
281        }
282    }