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

import java.util.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import net.java.sip.communicator.impl.gui.customcontrols.*;
import net.java.sip.communicator.impl.gui.lookandfeel.*;
import net.java.sip.communicator.impl.gui.utils.*;

/**
 * The <tt>SmiliesSelectorBox</tt> is the component where user could choose
 * a smiley icon to send.
 * 
 * @author Yana Stamcheva
 */
public class SmiliesSelectorBox extends JMenuBar
    implements ActionListener {

    private ChatWindow chatWindow;

    private ArrayList imageList;

    private int gridRowCount = 0;

    private int gridColCount = 0;
    
    private JMenu selectorBox = new JMenu();
    
    /**
     * Creates an instance of this <tt>SmiliesSelectorBox</tt> and initializes
     * the panel with the smiley icons given by the incoming imageList.
     * 
     * @param imageList The pack of smiley icons.
     */
    public SmiliesSelectorBox(ArrayList imageList, ChatWindow chatWindow) {

        this.imageList = imageList;

        this.chatWindow = chatWindow;
        
        this.selectorBox.setUI(new SIPCommChatSelectorMenuUI());
        
        this.selectorBox.setPreferredSize(new Dimension(24, 24));
        
        //Should explicetly remove any border in order to align correctly the
        //icon.
        this.selectorBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        
        this.selectorBox.setIcon(new ImageIcon(ImageLoader
            .getImage(ImageLoader.SMILIES_ICON)));
        
        this.calculateGridDimensions(imageList.size());

        this.selectorBox.getPopupMenu().setLayout(new GridLayout(
                this.gridRowCount, this.gridColCount, 5, 5));
                
        for (int i = 0; i < imageList.size(); i++) {

            Smiley smiley = (Smiley) this.imageList.get(i);

            ChatToolbarButton imageButton = new ChatToolbarButton(
                    ImageLoader.getImage(smiley.getImageID()));

            imageButton.setToolTipText(smiley.getSmileyStrings()[0]);

            imageButton.addActionListener(this);

            this.selectorBox.add(imageButton);
        }

        this.add(selectorBox);
    }
    
    /**
     * In order to have a popup which is at the form closest to sqware.
     * @param itemsCount the count of items that will be laied out.
     */
    private void calculateGridDimensions(int itemsCount) {

        this.gridRowCount = (int) Math.round(Math.sqrt(itemsCount));

        this.gridColCount = (int) Math.round(itemsCount / gridRowCount);
    }

    /**
     * Writes the symbol corresponding to a choosen smiley icon to the write
     * message area at the end of the current text.
     */
    public void actionPerformed(ActionEvent e) {

        JButton imageButton = (JButton) e.getSource();
        String buttonText = imageButton.getToolTipText();

        for (int i = 0; i < this.imageList.size(); i++) {

            Smiley smiley = (Smiley) this.imageList.get(i);

            if (buttonText.equals(smiley.getSmileyStrings()[0])) {

                ChatPanel chatPanel = this.chatWindow
                        .getCurrentChatPanel();

                chatPanel.addTextInWriteArea(
                        smiley.getSmileyStrings()[0] + " ");

                chatPanel.requestFocusInWriteArea();
            }
        }
    }

    /**
     * Opens the smilies selector box.
     */
    public void open()
    {
        this.selectorBox.doClick();
    }

    /**
     * Returns TRUE if the selector box is opened, otherwise returns FALSE.
     * @return TRUE if the selector box is opened, otherwise returns FALSE
     */
    public boolean isMenuSelected()
    {
        if(selectorBox.isPopupMenuVisible())
            return true;
        
        return false;
    }
}

@@ -272,24 +272,25 @@
 wt.event.*;%0A
+%0A
 import javax
@@ -2056,61 +2056,45 @@
     
-ChatToolbarButton imageButton = new ChatToolbarButton
+JMenuItem smileyItem = new JMenuItem 
 (%0A  
@@ -2111,16 +2111,30 @@
         
+new ImageIcon(
 ImageLoa
@@ -2167,18 +2167,31 @@
 geID()))
+)
 ;%0A
+            
 %0A       
@@ -2195,27 +2195,162 @@
         
-imageButton
+smileyItem.setPreferredSize(new Dimension(28, 28));%0A            smileyItem.setMargin(new Insets(2, -5, 2, 0));%0A            %0A            smileyItem
 .setTool
@@ -2401,27 +2401,26 @@
         
-imageButton
+smileyItem
 .addActi
@@ -2471,27 +2471,26 @@
 Box.add(
-imageButton
+smileyItem
 );%0A     
@@ -2895,32 +2895,476 @@
 wCount);%0A    %7D%0A%0A
+    /**%0A     * Opens the smilies selector box.%0A     */%0A    public void open()%0A    %7B%0A        this.selectorBox.doClick();%0A    %7D%0A%0A    /**%0A     * Returns TRUE if the selector box is opened, otherwise returns FALSE.%0A     * @return TRUE if the selector box is opened, otherwise returns FALSE%0A     */%0A    public boolean isMenuSelected()%0A    %7B%0A        if(selectorBox.isPopupMenuVisible())%0A            return true;%0A        %0A        return false;%0A    %7D%0A%0A
     /**%0A     * W
@@ -3538,20 +3538,23 @@
 Event e)
+%0A   
  %7B%0A
-%0A
         
@@ -3558,37 +3558,40 @@
    J
-Button imageButton = (JButton
+MenuItem smileyItem = (JMenuItem
 ) e.
@@ -3635,19 +3635,18 @@
 t = 
-imageButton
+smileyItem
 .get
@@ -4146,451 +4146,7 @@
    %7D
-%0A%0A    /**%0A     * Opens the smilies selector box.%0A     */%0A    public void open()%0A    %7B%0A        this.selectorBox.doClick();%0A    %7D%0A%0A    /**%0A     * Returns TRUE if the selector box is opened, otherwise returns FALSE.%0A     * @return TRUE if the selector box is opened, otherwise returns FALSE%0A     */%0A    public boolean isMenuSelected()%0A    %7B%0A        if(selectorBox.isPopupMenuVisible())%0A            return true;%0A        %0A        return false;%0A    %7D
 %0A%7D%0A

fix the smilies problem on mac