/*
 * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
 * 
 * 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.ui.swing.dialogs.actioneditor;

import com.dmdirc.ui.IconManager;
import com.dmdirc.ui.swing.components.ImageButton;
import com.dmdirc.ui.swing.components.ImageToggleButton;
import com.dmdirc.ui.swing.components.TextLabel;
import com.dmdirc.util.ListenerList;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JToggleButton;

import net.miginfocom.swing.MigLayout;

/**
 * Action condition display panel.
 */
public class ActionConditionDisplayPanel extends JPanel implements ActionListener {

    /**
     * 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 = 1;
    /** Info label. */
    private TextLabel label;
    /** Edit button. */
    private JToggleButton editButton;
    /** Delete button. */
    private ImageButton deleteButton;
    /** Edit panel. */
    private ActionConditionEditorPanel editPanel;
    /** Listeners. */
    private ListenerList listeners;

    /** Instantiates the panel. */
    public ActionConditionDisplayPanel() {
        super();

        initComponents();
        addListeners();
        layoutComponents();
    }

    /** Initialises the components. */
    private void initComponents() {
        label = new TextLabel("");
        editButton = new ImageToggleButton("edit", IconManager.getIconManager().
                getIcon("edit-inactive"),
                IconManager.getIconManager().getIcon("edit-active"));
        deleteButton = new ImageButton("delete", IconManager.getIconManager().
                getIcon("close-inactive"),
                IconManager.getIconManager().getIcon("close-active"));

        editPanel = new ActionConditionEditorPanel();
        listeners = new ListenerList();
    }

    /** Adds the listeners. */
    private void addListeners() {
    }

    /** Lays out the components. */
    private void layoutComponents() {
        setLayout(new MigLayout("fill, pack, hidemode 3"));
        add(label, "growx");
        add(editButton, "right");
        add(deleteButton, "right, wrap");
        add(editPanel, "grow");
    }

    /** 
     * {@inheritDoc}
     * 
     * @param e Action event
     */
    @Override
    public void actionPerformed(final ActionEvent e) {
        if (e.getSource().equals(deleteButton)) {
            fireConditionRemoved(this);
        } else if (e.getSource().equals(editButton)) {
            editPanel.setVisible(editButton.getModel().isSelected());
        }
    }

    /**
     * Adds an ActionConditionRemovalListener to the listener list.
     *
     * @param listener Listener to add
     */
    public void addConditionListener(final ActionConditionRemovalListener listener) {
        if (listener == null) {
            return;
        }

        listeners.add(ActionConditionRemovalListener.class, listener);
    }

    /**
     * Removes an ActionConditionRemovalListener from the listener list.
     *
     * @param listener Listener to remove
     */
    public void removeConditionListener(final ActionConditionRemovalListener listener) {
        listeners.remove(ActionConditionRemovalListener.class, listener);
    }

    /**
     * Fired when the an action condition is removed.
     *
     * @param condition Removed condition
     */
    protected void fireConditionRemoved(final ActionConditionDisplayPanel condition) {
        for (ActionConditionRemovalListener listener : listeners.get(ActionConditionRemovalListener.class)) {
            listener.conditionRemoved(condition);
        }
    }

    /** {@inheritDoc} */
    @Override
    public void setEnabled(final boolean enabled) {
        editPanel.setEnabled(enabled);
        editButton.setEnabled(enabled);
        deleteButton.setEnabled(enabled);
    }
}

@@ -1199,24 +1199,116 @@
 ioneditor;%0A%0A
+import com.dmdirc.actions.ActionCondition;%0Aimport com.dmdirc.actions.interfaces.ActionType;%0A
 import com.d
@@ -2420,83 +2420,378 @@
 rs;%0A
-%0A    /** Instantiates the panel. */%0A    public ActionConditionDisplayPanel(
+    /** Action condition. */%0A    private ActionCondition condition;%0A    /** Action trigger. */%0A    private ActionType trigger;%0A%0A    /** %0A     * Instantiates the panel.%0A     * %0A     * @param condition Action condition%0A     * @param trigger Action trigger%0A     */%0A    public ActionConditionDisplayPanel(final ActionCondition condition,%0A            final ActionType trigger
 ) %7B%0A
@@ -2830,24 +2830,66 @@
 mponents();%0A
+        setCondition(trigger, condition);%0A
         addL
@@ -3044,10 +3044,8 @@
 bel(
-%22%22
 );%0A 
@@ -5522,11 +5522,789 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Sets the new action condition.%0A     * %0A     * @param trigger Action trigger%0A     * @param condition Action condition%0A     */%0A    public void setCondition(final ActionType trigger,%0A            final ActionCondition condition) %7B%0A        this.trigger = trigger;%0A        this.condition = condition;%0A%0A        label.setText(%22The %22 +%0A                trigger.getType().getArgNames()%5Bcondition.getArg()%5D + %22's %22 + condition.getComponent().%0A                getName() + %22 %22 + condition.getComparison().getName() + %22 '%22 +%0A                condition.getTarget() + %22'%22);%0A    %7D%0A    %0A    /**%0A     * Returns the action condition represented by this panel.%0A     * %0A     * @return Action condition%0A     */%0A    public ActionCondition getCondition() %7B%0A        return condition;%0A    %7D
 %0A%7D%0A

work on issue 1027: New Actions Editor UI