/*
 * 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 java.util.*;

import javax.swing.*;
import javax.swing.Timer;

import net.java.sip.communicator.impl.gui.*;
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
{   
    private JPanel contactPanel = new JPanel(new BorderLayout());
    
    private JPanel namePanel = new JPanel(new GridLayout(0, 1));
    
    private JLabel nameLabel = new JLabel("", JLabel.CENTER);
     
    private JLabel timeLabel = new JLabel("00:00:00", JLabel.CENTER);
    
    private JLabel photoLabel = new JLabel(new ImageIcon(
            ImageLoader.getImage(ImageLoader.DEFAULT_USER_PHOTO)));
    
    private Date initDate;
    private Timer timer;
    
    private 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);
        
        initDate = new Date(System.currentTimeMillis());
        timer = new Timer(1000, new CallTimerListener());
        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
    {   
        public void actionPerformed(ActionEvent e)
        {
            Date time = GuiUtils.substractDates(
                    new Date(System.currentTimeMillis()),
                    initDate);
            
            timeLabel.setText(GuiUtils.formatTime(time));
        }
    }    
}

@@ -325,53 +325,8 @@
 r;%0A%0A
-import net.java.sip.communicator.impl.gui.*;%0A
 impo
@@ -706,19 +706,16 @@
 JPanel%0A%7B
-   
 %0A    pri
@@ -1143,17 +1143,54 @@
 ate 
-initDate;
+startDate;%0A    %0A    private Date endDate;%0A    
 %0A   
@@ -1248,24 +1248,58 @@
 Label;%0A    %0A
+    private String callType;%0A    %0A
     /**%0A    
@@ -1420,32 +1420,101 @@
 all participant%0A
+     * @param callType the call type. INCOMING_CALL or OUTGOING_CALL%0A
      */%0A    publ
@@ -1624,16 +1624,21 @@
         
+this.
 stateLab
@@ -1739,27 +1739,33 @@
    %0A        
-ini
+this.star
 tDate = new 
@@ -1802,24 +1802,29 @@
 ));%0A        
+this.
 timer = new 
@@ -1865,24 +1865,29 @@
 ));%0A        
+this.
 timer.setRep
@@ -3332,19 +3332,20 @@
         
-ini
+star
 tDate);%0A
@@ -3349,32 +3349,60 @@
 );%0A            %0A
+            endDate = time;%0A
             time
@@ -3458,15 +3458,885 @@
  %7D%0A    %7D
-    
+%0A%0A    /**%0A     * Returns the start time of the contained participant call.%0A     * %0A     * @return the start time of the contained participant call%0A     */%0A    public Date getStartTime()%0A    %7B        %0A        return startDate;%0A    %7D%0A    %0A    /**%0A     * Returns the start time of the contained participant call.%0A     * %0A     * @return the start time of the contained participant call%0A     */%0A    public Date getEndTime()%0A    %7B%0A        if(endDate == null)%0A            endDate = startDate;%0A        %0A        return endDate;%0A    %7D%0A    %0A    /**%0A     * Returns this call type : INCOMING_CALL or OUTGOING_CALL%0A     * @return Returns this call type : INCOMING_CALL or OUTGOING_CALL%0A     */%0A    public String getCallType()%0A    %7B%0A        return GuiCallParticipantRecord.INCOMING_CALL;%0A    %7D%0A    %0A    public void setCallType(String callType)%0A    %7B%0A        this.callType = callType;%0A    %7D
 %0A%7D%0A

save the start time and end time of the call