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

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

import javax.swing.*;

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.util.*;
import net.java.sip.communicator.util.swing.*;

/**
 * The <tt>ChatSendPanel</tt> is the panel in the bottom of the chat. It
 * contains the send button, the status panel, where typing notifications are
 * shown and the selector box, where the protocol specific contact is chosen.
 *
 * @author Yana Stamcheva
 */
public class ChatSendPanel
    extends TransparentPanel
    implements ActionListener
{
    private final JButton sendButton;

    private final StatusPanel statusPanel = new StatusPanel();

    private final TransparentPanel sendPanel
        = new TransparentPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));

    private final JLabel statusLabel = new JLabel();

    private final ChatPanel chatPanel;

    /**
     * Creates an instance of <tt>ChatSendPanel</tt>.
     *
     * @param chatPanel The parent <tt>ChatPanel</tt>.
     */
    public ChatSendPanel(ChatPanel chatPanel)
    {
        super(new BorderLayout(5, 0));

        this.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));

        sendButton = new JButton(
            GuiActivator.getResources().getI18NString("service.gui.SEND"));

        this.chatPanel = chatPanel;

        this.statusPanel.add(statusLabel);

        this.sendPanel.add(sendButton);

        this.add(statusPanel, BorderLayout.CENTER);
        this.add(sendPanel, BorderLayout.EAST);

        this.sendButton.addActionListener(this);
        this.sendButton.setMnemonic(
            GuiActivator.getResources().getI18nMnemonic("service.gui.SEND"));
    }

    /**
     * Defines actions when send button is pressed.
     *
     * @param evt The <tt>ActionEvent</tt> object.
     */
    public void actionPerformed(ActionEvent evt)
    {   
        if (!chatPanel.isWriteAreaEmpty())
        {
            new Thread()
            {
                public void run()
                {
                    chatPanel.sendMessage();
                }
            }.start();
        }
    }

    /**
     * Sets the message text to the status panel in the bottom of the chat
     * window. Used to show typing notification messages, links' hrefs, etc.
     *
     * @param statusMessage The message text to be displayed.
     */
    public void setStatusMessage(String statusMessage)
    {
        int stringWidth = GuiUtils.getStringWidth(statusLabel, statusMessage);

        final int dot3 = GuiUtils.getStringWidth(statusLabel, "... ");

        // first, we avoid to loop if it is useless.
        final int statusPanelWidth = statusPanel.getWidth();
        if (dot3 >= statusPanelWidth)
        {
            if (stringWidth > dot3)
                statusMessage = "...";
        }
        else
        {
            while ((stringWidth > (statusPanelWidth - dot3))
                    && !statusMessage.equals("..."))
            {
                if (statusMessage.endsWith("..."))
                {
                    statusMessage = statusMessage.substring(0,
                        statusMessage.indexOf("...") - 1).concat("...");
                }
                else
                {
                    statusMessage = statusMessage.substring(0,
                        statusMessage.length() - 3).concat("...");
                }
                stringWidth = GuiUtils.getStringWidth(statusLabel, statusMessage);
            }
        }
        statusLabel.setText(statusMessage);
    }

    private class StatusPanel extends TransparentPanel
    {
        public StatusPanel()
        {
            super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        }

        /**
         * Overrides the <code>javax.swing.JComponent.paint()</code> to provide a
         * new round border for the status panel.
         *
         * @param g The Graphics object.
         */
        public void paint(Graphics g)
        {
            super.paint(g);

            g = g.create();
            try
            {
                AntialiasingManager.activateAntialiasing(g);

                Graphics2D g2 = (Graphics2D) g;

                g2.setColor(Constants.BORDER_COLOR);
                g2.setStroke(new BasicStroke(1f));

                g2.drawRoundRect(0, 0, this.getWidth() - 1,
                    this.getHeight() - 1, 8, 8);
            }
            finally
            {
                g.dispose();
            }
        }
    }

    /**
     * Returns the parent <tt>ChatPanel</tt>.
     * @return the parent <tt>ChatPanel</tt>
     */
    public ChatPanel getChatPanel()
    {
        return chatPanel;
    }

    /**
     * Returns the main container. Used by the single user chat panel to add
     * here the "send via" selector box.
     * @return the main container
     */
    public JPanel getSendPanel()
    {
        return sendPanel;
    }

    /**
     * Returns the status panel contained in this panel.
     * @return the status panel contained in this panel
     */
    public JPanel getStatusPanel()
    {
        return statusPanel;
    }

    /**
     * Returns the send button.
     *
     * @return The send button.
     */
    public JButton getSendButton()
    {
        return sendButton;
    }
}

@@ -316,24 +316,81 @@
 impl.gui.*;%0A
+import net.java.sip.communicator.impl.gui.lookandfeel.*;%0A
 import net.j
@@ -1668,16 +1668,35 @@
 tusLabel
+, BorderLayout.WEST
 );%0A%0A    
@@ -3977,813 +3977,200 @@
 new 
-Flow
+Border
 Layout(
-FlowLayout.LEFT, 0, 0));%0A        %7D%0A%0A        /**%0A         * Overrides the %3Ccode%3Ejavax.swing.JComponent.paint()%3C/code%3E to provide a%0A         * new round border for the status panel.%0A         *%0A         * @param g The Graphics object.%0A         */%0A        public void paint(Graphics g)%0A        %7B%0A            super.paint(g);%0A%0A            g = g.create();%0A            try%0A            %7B%0A                AntialiasingManager.activateAntialiasing(g);%0A%0A                Graphics2D g2 = (Graphics2D) g;%0A%0A                g2.setColor(Constants.BORDER_COLOR);%0A                g2.setStroke(new BasicStroke(1f));%0A%0A                g2.drawRoundRect(0, 0, this.getWidth() - 1,%0A                    this.getHeight() - 1, 8, 8);%0A            %7D%0A            finally%0A            %7B%0A                g.dispose();%0A            %7D
+));%0A%0A            this.setBorder(BorderFactory.createCompoundBorder(%0A                SIPCommBorders.getRoundBorder(),%0A                BorderFactory.createEmptyBorder(3, 3, 3, 3)));
 %0A   

Fix typing notification text position and layout.