/*
 * Copyright (c) 2006-2013 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.ui_swing.components.statusbar;

import com.dmdirc.addons.ui_swing.SwingController;
import com.dmdirc.ui.StatusMessage;

import java.awt.Color;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;

/**
 * Previous status bar messages popup.
 */
class MessagePopup extends StatusbarTogglePanel<JLabel> {

    /**
     * A version number for this class. It should be changed whenever the class
     * structure is changed (or anything else that would prevent serialized
     * objects being unserialized with the new class).
     */
    private static final long serialVersionUID = 2;
    /** Parent window. */
    private final Window parentWindow;
    /** List of historical messages. */
    private final List<StatusMessage> messages;
    /** Parent panel. */
    private final JPanel parent;
    /** Swing controller. */
    private final SwingController controller;

    /**
     * Creates a new message history popup.
     *
     * @param parent Parent to size against
     * @param parentWindow Parent window
     * @param controller Swing controller
     */
    public MessagePopup(final JPanel parent, final Window parentWindow,
            final SwingController controller) {
        super(new JLabel("^"),
                new SidelessEtchedBorder(SidelessEtchedBorder.Side.LEFT),
                new SidelessEtchedBorder(SidelessEtchedBorder.Side.TOP));
        this.parentWindow = parentWindow;
        this.parent = parent;
        this.controller = controller;
        messages = new ArrayList<StatusMessage>();
    }

    /* {@inheritDoc} */
    @Override
    protected StatusbarPopupWindow getWindow() {
        return new MessageHistoryPanel(controller, this);
    }

    /* {@inheritDoc} */
    @Override
    public void mouseEntered(final MouseEvent e) {
        super.mouseEntered(e);
        if (!isDialogOpen()) {
            setBorder(nonSelectedBorder);
            setBackground(UIManager.getColor("ToolTip.background"));
            setForeground(UIManager.getColor("ToolTip.foreground"));
        }
    }

    /* {@inheritDoc} */
    @Override
    public void mouseExited(final MouseEvent e) {
        super.mouseExited(e);
        if (!isDialogOpen()) {
            setBorder(new SidelessEtchedBorder(SidelessEtchedBorder.Side.LEFT));
            setBackground(null);
            setForeground(null);
        }
    }

    /**
     * Adds a message to this history window.
     *
     * @param message to add
     */
    public void addMessage(final StatusMessage message) {
        synchronized(message) {
            messages.add(message);
        }
    }

    /* {@inheritDoc} */
    @Override
    protected Color getPopupBackground() {
        return parent.getBackground();
    }

    /* {@inheritDoc} */
    @Override
    protected Color getPopupForeground() {
        return super.getForeground();
    }

    /** Message history status bar popup window. */
    private class MessageHistoryPanel extends StatusbarPopupWindow {

        /**
         * A version number for this class. It should be changed whenever the
         * class structure is changed (or anything else that would prevent
         * serialized objects being unserialized with the new class).
         */
        private static final long serialVersionUID = 2;

        /**
         * Creates a new message history window.
         *
         * @param window Parent window
         */
        public MessageHistoryPanel(final SwingController controller,
                final JPanel parent) {
            super(controller, parent, parentWindow);
        }

        /* {@inheritDoc} */
        @Override
        protected void initPanel(final JPanel panel) {
            panel.setLayout(new MigLayout("ins 0 0 0 rel, fill, wmin "
                    + (parent.getWidth() - 5) + ", wmax "
                    + (parent.getWidth() - 5)));
            panel.setBackground(parent.getBackground());
            panel.setForeground(parent.getForeground());
            panel.setBorder(new GappedEtchedBorder(this));
        }

        /* {@inheritDoc} */
        @Override
        protected void initContent(final JPanel panel) {
            if (messages.isEmpty()) {
                panel.add(new JLabel("No previous messages."), "grow, push");
                return;
            }

            for (final StatusMessage message : messages) {
                panel.add(new JLabel(message.getMessage(), message.getIconType()
                        == null ? null : controller.getIconManager()
                        .getIcon(message.getIconType()), SwingConstants.LEFT),
                        "grow, push, wrap");
            }
        }

        /* {@inheritDoc} */
        @Override
        protected Point getPopupLocation() {
            final Point point = parent.getLocationOnScreen();
            point.y = point.y - getHeight() + 2;
            return point;
        }
    }
}

@@ -1265,16 +1265,64 @@
 Message;
+%0Aimport com.dmdirc.util.collections.RollingList;
 %0A%0Aimport
@@ -1422,59 +1422,8 @@
 ent;
-%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Aim
@@ -2088,16 +2088,23 @@
 e final 
+Rolling
 List%3CSta
@@ -2889,13 +2889,15 @@
 new 
-Array
+Rolling
 List
@@ -2912,16 +2912,17 @@
 essage%3E(
+5
 );%0A    %7D
@@ -3912,16 +3912,17 @@
 hronized
+ 
 (message
@@ -5664,32 +5664,32 @@
 %0A            %7D%0A%0A
-
             for 
@@ -5727,16 +5727,26 @@
 messages
+.getList()
 ) %7B%0A    

Add limit to number of status bar messages.