aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/accountinfo/AccountDetailsPanel.java
blob: 6365bfc465f737c2e996c19aef1448361eced148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license. See terms of license at gnu.org.
 */
package net.java.sip.communicator.plugin.accountinfo;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;

import javax.imageio.*;
import javax.swing.*;

import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.ServerStoredDetails.*;
import net.java.sip.communicator.util.*;
import net.java.sip.communicator.util.skin.*;
import net.java.sip.communicator.util.swing.*;

/**
 * The right side panel of AccountDetailsDialog. Shows one tab of a summary of
 * contact information for the selected subcontact, and has an extended tab
 * listing all of the details.
 *
 * @author Yana Stamcheva
 * @author Adam Netocny
 */
public class AccountDetailsPanel
    extends TransparentPanel
    implements Skinnable
{
    private static final long serialVersionUID = 5524135388175045624L;

    private Logger logger = Logger.getLogger(AccountDetailsPanel.class);

    /**
     * The operation set giving access to the server stored account details.
     */
    private OperationSetServerStoredAccountInfo accountInfoOpSet;

    private JTextField firstNameField = new JTextField();

    private JTextField middleNameField = new JTextField();

    private JTextField lastNameField = new JTextField();

    private JTextField genderField = new JTextField();

    private JTextField ageField = new JTextField();

    private JTextField birthdayField = new JTextField();

    private JTextField emailField = new JTextField();

    private JTextField phoneField = new JTextField();

    private JLabel avatarLabel = new JLabel();

    private JButton applyButton
        = new JButton(Resources.getString("service.gui.APPLY"));

    private JPanel buttonPanel =
        new TransparentPanel(new FlowLayout(FlowLayout.CENTER));

    private JScrollPane mainScrollPane = new JScrollPane();

    private boolean isDataLoaded = false;

    private FirstNameDetail firstNameDetail;

    private MiddleNameDetail middleNameDetail;

    private LastNameDetail lastNameDetail;

    private GenderDetail genderDetail;

    private BirthDateDetail birthDateDetail;

    private EmailAddressDetail emailDetail;

    private PhoneNumberDetail phoneDetail;

    private BinaryDetail avatarDetail;

    private byte[] newAvatarImage;

    private ImageIcon avatarImageIcon = null;

    /**
     * The last avatar file directory open.
     */
    private File lastAvatarDir;

    /**
     * Construct a panel containing all account details for the given protocol
     * provider.
     *
     * @param protocolProvider the protocol provider service
     */
    public AccountDetailsPanel(ProtocolProviderService protocolProvider)
    {
        super(new BorderLayout());

        this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
//        this.setPreferredSize(new Dimension(500, 400));

        accountInfoOpSet
            = protocolProvider
                .getOperationSet(OperationSetServerStoredAccountInfo.class);

        if (accountInfoOpSet == null)
        {
            initUnsupportedPanel();
        }
        else
        {
            this.initSummaryPanel();

            if (protocolProvider.isRegistered())
            {
                loadDetails();
            }
        }
    }

    private void initSummaryPanel()
    {
        JPanel summaryPanel = new TransparentPanel(new BorderLayout(10, 10));

        summaryPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        summaryPanel.setSize(this.getWidth(), this.getHeight());

        // Create the avatar panel.
        JPanel leftPanel = new TransparentPanel(new BorderLayout());
        JPanel avatarPanel = new TransparentPanel(new BorderLayout());
        JButton changeAvatarButton = new JButton(Resources.getString("plugin.accountinfo.CHANGE"));
        JPanel changeButtonPanel
            = new TransparentPanel(new FlowLayout(FlowLayout.CENTER));

        changeAvatarButton.addActionListener(new ChangeAvatarActionListener());

        changeButtonPanel.add(changeAvatarButton);

        avatarPanel.add(avatarLabel, BorderLayout.CENTER);
        avatarPanel.add(changeButtonPanel, BorderLayout.SOUTH);

        leftPanel.add(avatarPanel, BorderLayout.NORTH);

        summaryPanel.add(leftPanel, BorderLayout.WEST);

        // Create the summary details panel.
        JPanel detailsPanel = new TransparentPanel(new BorderLayout(10, 10));
        detailsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        summaryPanel.add(detailsPanel, BorderLayout.CENTER);

        // Labels panel.
        JPanel labelsPanel = new TransparentPanel(new GridLayout(0, 1, 5, 5));

        labelsPanel.add(new JLabel(
            Resources.getString("plugin.accountinfo.FIRST_NAME")));
//        labelsPanel.add(new JLabel(Resources.getString("plugin.accountinfo.MIDDLE_NAME")));
        labelsPanel.add(new JLabel(
            Resources.getString("plugin.accountinfo.LAST_NAME")));
        labelsPanel.add(new JLabel(
            Resources.getString("plugin.accountinfo.GENDER")));
        labelsPanel.add(new JLabel(
            Resources.getString("plugin.accountinfo.AGE")));
        labelsPanel.add(new JLabel(
            Resources.getString("plugin.accountinfo.BDAY")));
        labelsPanel.add(new JLabel(
            Resources.getString("plugin.accountinfo.EMAIL")));
        labelsPanel.add(new JLabel(
            Resources.getString("plugin.accountinfo.PHONE")));

        detailsPanel.add(labelsPanel, BorderLayout.WEST);

        // Values panel.
        JPanel valuesPanel = new TransparentPanel(new GridLayout(0, 1, 5, 5));

        valuesPanel.add(firstNameField);
//        valuesPanel.add(middleNameField);
        valuesPanel.add(lastNameField);
        valuesPanel.add(genderField);
        valuesPanel.add(ageField);
        valuesPanel.add(birthdayField);
        valuesPanel.add(emailField);
        valuesPanel.add(phoneField);

        detailsPanel.add(valuesPanel, BorderLayout.CENTER);

        this.mainScrollPane.getViewport().add(summaryPanel);

        this.add(mainScrollPane, BorderLayout.NORTH);

        this.applyButton.addActionListener(new SubmitActionListener());

        this.buttonPanel.add(applyButton);

        this.add(buttonPanel, BorderLayout.SOUTH);

        // All items are now instantiated and could safely load the skin.
        loadSkin();
    }

    /**
     * Loads details for
     */
    public void loadDetails()
    {
        this.loadSummaryDetails();
        this.isDataLoaded = true;
    }

    /**
     * Creates the panel that indicates to the user that the currently selected
     * contact does not support server stored contact info.
     */
    private void initUnsupportedPanel()
    {
        JTextArea unsupportedTextArea =
            new JTextArea(Resources.getString(
                "plugin.accountinfo.NOT_SUPPORTED"));

        unsupportedTextArea.setEditable(false);
        unsupportedTextArea.setLineWrap(true);

        JPanel unsupportedPanel
            = new TransparentPanel(new FlowLayout(FlowLayout.CENTER));

        unsupportedTextArea.setPreferredSize(new Dimension(200, 200));

        unsupportedPanel.setBorder(
            BorderFactory.createEmptyBorder(50, 20, 50, 20));

        unsupportedPanel.add(unsupportedTextArea);

        this.add(unsupportedPanel);
    }

    /**
     * Creates a panel that can be added as the summary tab that displays the
     * following details: -
     * <p>
     * Avatar(Contact image) - FirstNameDetail - MiddleNameDetail -
     * LastNameDetail - BirthdateDetail (and calculate age) - GenderDetail -
     * EmailAddressDetail - PhoneNumberDetail. All other details will be* added
     * to our list of extended details.
     */
    private void loadSummaryDetails()
    {
        Iterator<GenericDetail> contactDetails;

        // Avatar details.
        contactDetails
            = accountInfoOpSet.getDetails(BinaryDetail.class);

        byte[] avatarImage = null;
        if (contactDetails.hasNext())
        {
            avatarDetail = (BinaryDetail) contactDetails.next();

            avatarImage = avatarDetail.getBytes();
        }

        if (avatarImage != null && avatarImage.length > 0)
        {
            avatarImageIcon = new ImageIcon(
                getScaledImageInstance(avatarImage));
            avatarLabel.setIcon(avatarImageIcon);
        }

        // First name details.
        contactDetails =
            accountInfoOpSet.getDetails(FirstNameDetail.class);

        String firstNameDetailString = "";
        while (contactDetails.hasNext())
        {
            firstNameDetail = (FirstNameDetail) contactDetails.next();

            firstNameDetailString =
                firstNameDetailString + " " + firstNameDetail.getDetailValue();
        }

        firstNameField.setText(firstNameDetailString);

        // Middle name details.
        contactDetails =
            accountInfoOpSet.getDetails(MiddleNameDetail.class);

        String middleNameDetailString = "";
        while (contactDetails.hasNext())
        {
            middleNameDetail = (MiddleNameDetail) contactDetails.next();
            middleNameDetailString =
                middleNameDetailString + " " + middleNameDetail.getDetailValue();
        }

        middleNameField.setText(middleNameDetailString);

        // Last name details.
        contactDetails =
            accountInfoOpSet.getDetails(LastNameDetail.class);

        String lastNameDetailString = "";
        while (contactDetails.hasNext())
        {
            lastNameDetail = (LastNameDetail) contactDetails.next();

            lastNameDetailString =
                lastNameDetailString + " " + lastNameDetail.getDetailValue();
        }

        lastNameField.setText(lastNameDetailString);

        // Gender details.
        contactDetails =
            accountInfoOpSet.getDetails(GenderDetail.class);

        String genderDetailString = "";
        while (contactDetails.hasNext())
        {
            genderDetail = (GenderDetail) contactDetails.next();
            genderDetailString = genderDetailString + " "
                                    + genderDetail.getDetailValue();
        }

        genderField.setText(genderDetailString);

        // Birthday details.
        contactDetails =
            accountInfoOpSet.getDetails(BirthDateDetail.class);

        String birthDateDetailString = "";
        String ageDetail = "";
        if (contactDetails.hasNext())
        {
            birthDateDetail = (BirthDateDetail) contactDetails.next();

            Calendar calendarDetail =
                (Calendar) birthDateDetail.getDetailValue();

            Date birthDate = calendarDetail.getTime();
            DateFormat dateFormat = DateFormat.getDateInstance();

            birthDateDetailString = dateFormat.format(birthDate).trim();

            Calendar c = Calendar.getInstance();
            int age = c.get(Calendar.YEAR) - calendarDetail.get(Calendar.YEAR);

            if (c.get(Calendar.MONTH) < calendarDetail.get(Calendar.MONTH))
                age--;

            ageDetail = Integer.toString(age).trim();
        }

        birthdayField.setText(birthDateDetailString);
        ageField.setText(ageDetail);

        // Email details.
        contactDetails =
            accountInfoOpSet.getDetails(EmailAddressDetail.class);

        String emailDetailString = "";
        while (contactDetails.hasNext())
        {
            emailDetail = (EmailAddressDetail) contactDetails.next();
            emailDetailString = emailDetailString + " "
                + emailDetail.getDetailValue();
        }

        emailField.setText(emailDetailString);

        // Phone number details.
        contactDetails =
            accountInfoOpSet.getDetails(PhoneNumberDetail.class);

        String phoneNumberDetailString = "";
        while (contactDetails.hasNext())
        {
            phoneDetail = (PhoneNumberDetail) contactDetails.next();
            phoneNumberDetailString =
                phoneNumberDetailString + " " + phoneDetail.getDetailValue();
        }

        phoneField.setText(phoneNumberDetailString);
    }

    private class SubmitActionListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String firstName = firstNameField.getText();
//            String middleName = middleNameField.getText();
            String lastName = lastNameField.getText();
            String gender = genderField.getText();
            String email = emailField.getText();
            String phoneNumber = phoneField.getText();

            Calendar birthDateCalendar = Calendar.getInstance();

            if(birthdayField.getText() != null
                && birthdayField.getText().length() > 0)
            {
                try
                {
                    DateFormat dateFormat
                        = DateFormat.getDateInstance(DateFormat.DEFAULT);

                    Date birthDate = dateFormat.parse(birthdayField.getText());

                    birthDateCalendar.setTime(birthDate);
                }
                catch (ParseException e2)
                {
                    logger.error("Failed to parse birth date.", e2);
                }
            }

            try
            {
                FirstNameDetail newFirstNameDetail
                    = new ServerStoredDetails.FirstNameDetail(firstName);

                if (firstNameDetail == null)
                    accountInfoOpSet.addDetail(newFirstNameDetail);
                else
                    accountInfoOpSet.replaceDetail( firstNameDetail,
                                                    newFirstNameDetail);

//                MiddleNameDetail newMiddleNameDetail
//                    = new ServerStoredDetails.MiddleNameDetail(middleName);
//
//                if (middleNameDetail == null)
//                    accountInfoOpSet.addDetail(newMiddleNameDetail);
//                else
//                    accountInfoOpSet.replaceDetail( middleNameDetail,
//                                                    newMiddleNameDetail);

                LastNameDetail newLastNameDetail
                    = new ServerStoredDetails.LastNameDetail(lastName);

                if (lastNameDetail == null)
                    accountInfoOpSet.addDetail(newLastNameDetail);
                else
                    accountInfoOpSet.replaceDetail( lastNameDetail,
                        newLastNameDetail);

                GenderDetail newGenderDetail
                    = new ServerStoredDetails.GenderDetail(gender);

                if (genderDetail == null)
                    accountInfoOpSet.addDetail(newGenderDetail);
                else
                    accountInfoOpSet.replaceDetail( genderDetail,
                                                    newGenderDetail);

                BirthDateDetail newBirthDateDetail
                    = new ServerStoredDetails.BirthDateDetail(birthDateCalendar);

                if (birthDateDetail == null)
                    accountInfoOpSet.addDetail(newBirthDateDetail);
                else
                    accountInfoOpSet.replaceDetail( birthDateDetail,
                                                    newBirthDateDetail);

                EmailAddressDetail newEmailDetail
                    = new ServerStoredDetails.EmailAddressDetail(email);

                if (emailDetail == null)
                    accountInfoOpSet.addDetail(newEmailDetail);
                else
                    accountInfoOpSet.replaceDetail( emailDetail,
                                                    newEmailDetail);

                PhoneNumberDetail newPhoneDetail
                    = new ServerStoredDetails.PhoneNumberDetail(phoneNumber);

                if (phoneDetail == null)
                    accountInfoOpSet.addDetail(newPhoneDetail);
                else
                    accountInfoOpSet.replaceDetail( phoneDetail,
                                                    newPhoneDetail);

                BinaryDetail newAvatarDetail
                    = new ServerStoredDetails.BinaryDetail(
                        "Avatar",
                        newAvatarImage);

                if (avatarDetail == null)
                    accountInfoOpSet.addDetail(newAvatarDetail);
                else
                    accountInfoOpSet.replaceDetail( avatarDetail,
                                                    newAvatarDetail);
            }
            catch (ClassCastException e1)
            {
                logger.error("Failed to update account details.", e1);
            }
            catch (OperationFailedException e1)
            {
                logger.error("Failed to update account details.", e1);
            }
        }
    }

    private class ChangeAvatarActionListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            SipCommFileChooser chooser = GenericFileDialog.create(
                null, "Change avatar...",
                SipCommFileChooser.LOAD_FILE_OPERATION,
                lastAvatarDir.getAbsolutePath());
            chooser.addFilter(new ImageFilter());

            File file = chooser.getFileFromDialog();

            if (file != null)
            {
                try
                {
                    lastAvatarDir = file.getParentFile();

                    FileInputStream in = new FileInputStream(file);
                    byte buffer[] = new byte[in.available()];
                    in.read(buffer);

                    if (buffer == null || buffer.length <= 0)
                        return;

                    newAvatarImage = buffer;

                    avatarImageIcon = new ImageIcon(
                        getScaledImageInstance(newAvatarImage));
                    avatarLabel.setIcon(avatarImageIcon);
                }
                catch (IOException ex)
                {
                    logger.error("Failed to load image.", ex);
                }
            }
        }
    }

    /**
     * A custom filter that would accept only image files.
     */
    private static class ImageFilter extends SipCommFileFilter
    {
        /**
         * Accept all directories and all gif, jpg, tiff, or png files.
         * Method implemented from FileFilter abstract class.
         *
         * @param f a file to accept or not
         */
        @Override
        public boolean accept(File f)
        {
            if (f.isDirectory())
            {
                return true;
            }

            String extension = getExtension(f);
            if (extension != null)
            {
                if (extension.equals("tiff") ||
                    extension.equals("tif") ||
                    extension.equals("gif") ||
                    extension.equals("jpeg") ||
                    extension.equals("jpg") ||
                    extension.equals("png"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            return false;
        }

        /**
         * Get the extension of a file.
         *
         * @param f the file
         * @return the extension of the file
         */
        public String getExtension(File f)
        {
            String ext = null;
            String s = f.getName();
            int i = s.lastIndexOf('.');

            if (i > 0 &&  i < s.length() - 1) {
                ext = s.substring(i+1).toLowerCase();
            }
            return ext;
        }

        /**
         * The description of this filter.
         */
        @Override
        public String getDescription()
        {
            return Resources.getString("plugin.accountinfo.ONLY_MESSAGE");
        }

    }

    /**
     * Returns a scaled <tt>Image</tt> instance of the given byte image.
     *
     * @param image the image in bytes
     * @return a scaled <tt>Image</tt> instance of the given byte image.
     */
    private Image getScaledImageInstance(byte[] image)
    {
        Image resultImage = null;

        try
        {
            resultImage = ImageIO.read(
                    new ByteArrayInputStream(image));
        }
        catch (Exception e)
        {
            logger.error("Failed to convert bytes to image.", e);
        }

        if(resultImage == null)
            return null;

        return resultImage.getScaledInstance(
            avatarLabel.getWidth(),
            avatarLabel.getHeight(),
            Image.SCALE_SMOOTH);
    }

    /**
     * Returns <code>true</code> if the account details are loaded,
     * <code>false</code> - otherwise.
     *
     * @return <code>true</code> if the account details are loaded,
     * <code>false</code> - otherwise
     */
    public boolean isDataLoaded()
    {
        return isDataLoaded;
    }

    /**
     * Loads the avatar default icon.
     */
    public void loadSkin()
    {
        avatarLabel.setIcon(Resources.getImage("accountInfoDefaultPersonIcon"));
    }
}