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