/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.gui.main.call;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.service.protocol.*;

/**
 * The <tt>CallParticipantPanel</tt> is the panel containing data for a call
 * participant in a given call. It contains information like call participant
 * name, photo, call duration, etc. 
 * 
 * @author Yana Stamcheva
 */
public class CallParticipantPanel extends JPanel
{
    JPanel contactPanel = new JPanel(new BorderLayout());
    
    JPanel namePanel = new JPanel(new GridLayout(0, 1));
    
    JLabel nameLabel = new JLabel("", JLabel.CENTER);
    
    JLabel timeLabel = new JLabel("00:00:00", JLabel.CENTER);
    
    JLabel photoLabel = new JLabel(new ImageIcon(
            ImageLoader.getImage(ImageLoader.DEFAULT_USER_PHOTO)));
    
    Timer timer = new Timer(1000, new CallTimerListener());
    
    JLabel stateLabel;
    
    /**
     * Creates a <tt>CallParticipantPanel</tt> for the given call participant.
     * 
     * @param participant the call participant
     */
    public CallParticipantPanel(CallParticipant participant)
    {
        super(new BorderLayout());
        
        stateLabel = new JLabel(participant.getState().getStateString(),
                JLabel.CENTER);
        
        timer.setRepeats(true);
        
        if(participant.getDisplayName() != null)
            nameLabel.setText(participant.getDisplayName());
        else
            nameLabel.setText(participant.getAddress());
        
        namePanel.add(nameLabel);
        namePanel.add(stateLabel);
        namePanel.add(timeLabel);
        
        contactPanel.add(photoLabel, BorderLayout.CENTER);
        contactPanel.add(namePanel, BorderLayout.SOUTH);
        
        this.add(contactPanel, BorderLayout.NORTH);
        this.setPreferredSize(new Dimension(100, 200));
    }

    /**
     * Sets the state of the contained call participant.
     * @param state the state of the contained call participant
     */
    public void setState(String state)
    {
        this.stateLabel.setText(state);
    }
    
    /**
     * Starts the timer that counts call duration.
     */
    public void startCallTimer()
    {
        timer.start();
    }
    
    /**
     * Stops the timer that counts call duration.
     */
    public void stopCallTimer()
    {
        timer.stop();
    }

    /**
     * Each second refreshes the time label to show to the user the exact
     * duration of the call.
     */
    private class CallTimerListener implements ActionListener
    {
        private int timer = 0;
        
        public void actionPerformed(ActionEvent e)
        {
            timer ++;
            int s = timer%60;
            int m = (timer - s)/60;
            int h = m/60;
            
            timeLabel.setText(processTime(h)
                    + ":" + processTime(m)
                    + ":" + processTime(s));
        }
    }

    /**
     * Adds a 0 in the beginning of one digit numbers.
     *
     * @param time The time parameter could be hours, minutes or seconds.
     * @return The formatted minutes string.
     */
    private String processTime(int time)
    {
        String timeString = new Integer(time).toString();

        String resultString = "";
        if (timeString.length() < 2)
            resultString = resultString.concat("0").concat(timeString);
        else
            resultString = timeString;

        return resultString;
    }
    
    
}

@@ -250,16 +250,36 @@
 event.*;
+%0Aimport java.util.*;
 %0A%0Aimport
@@ -290,24 +290,95 @@
 ax.swing.*;%0A
+import javax.swing.Timer;%0A%0Aimport net.java.sip.communicator.impl.gui.*;
 %0Aimport net.
@@ -751,21 +751,32 @@
 JPanel%0A%7B
+   
 %0A    
+private 
 JPanel c
@@ -830,16 +830,24 @@
    %0A    
+private 
 JPanel n
@@ -896,24 +896,32 @@
 );%0A    %0A    
+private 
 JLabel nameL
@@ -958,29 +958,38 @@
 ENTER);%0A    
+ 
 %0A    
+private 
 JLabel timeL
@@ -1039,24 +1039,32 @@
 );%0A    %0A    
+private 
 JLabel photo
@@ -1178,62 +1178,54 @@
     
-Timer timer = new Timer(1000, new CallTimerListener())
+private Date initDate;%0A    private Timer timer
 ;%0A  
@@ -1230,16 +1230,24 @@
     %0A   
+ 
+private
  JLabel 
@@ -1630,32 +1630,147 @@
 NTER);%0A        %0A
+        initDate = new Date(System.currentTimeMillis());%0A        timer = new Timer(1000, new CallTimerListener());%0A
         timer.se
@@ -3031,45 +3031,8 @@
    %7B
-%0A        private int timer = 0;%0A     
    %0A
@@ -3108,812 +3108,220 @@
     
-timer ++;%0A            int s = timer%2560;%0A            int m = (timer - s)/60;%0A            int h = m/60;%0A            %0A            timeLabel.setText(processTime(h)%0A                    + %22:%22 + processTime(m)%0A                    + %22:%22 + processTime(s));%0A        %7D%0A    %7D%0A%0A    /**%0A     * Adds a 0 in the beginning of one digit numbers.%0A     *%0A     * @param time The time parameter could be hours, minutes or seconds.%0A     * @return The formatted minutes string.
+Date time = GuiUtils.substractDates(%0A                    new Date(System.currentTimeMillis()),
 %0A     
-*/%0A
     
-private String processTime(int time)%0A    %7B%0A        String timeString = new Integer(time).toString();%0A%0A        String resultString = %22%22;%0A        if (timeString.length() %3C 2)%0A            resultString = resultString.concat(%220%22).concat(timeString);%0A        else%0A            resultString = timeString;%0A%0A        return resultString;%0A    %7D%0A    %0A
+           initDate);%0A            %0A            timeLabel.setText(GuiUtils.formatTime(time));%0A        %7D%0A    %7D
     

fix the call duration