/*
 * Copyright (c) 2006-2011 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.dialogs.channellist;

import com.dmdirc.lists.GroupListManager;

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

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

/**
 * Gathers input for a group list search and begins the search.
 */
public class SearchTermsPanel extends JPanel implements ActionListener {

    /** Serial version UID. */
    private static final long serialVersionUID = 1L;
    /** Group list manager to perform searches on. */
    private GroupListManager manager;
    /** Search terms input field. */
    private JTextField searchTerms;

    /**
     * Creates a new panel to gather input for a group list search.
     *
     * @param manager Group list manager to perform search with
     */
    public SearchTermsPanel(final GroupListManager manager) {
        this.manager = manager;
        searchTerms = new JTextField();
        layoutComponents();
    }

    /** Lays out the components in the panel. */
    private void layoutComponents() {
        final JButton search = new JButton("Search");
        search.addActionListener(this);
        setLayout(new MigLayout("fill, hidemode 3, ins 0"));
        add(new JLabel("Search terms: "), "align label");
        add(searchTerms, "growx, pushx");
        add(search, "");
    }

    /**
     * {@inheritDoc}
     *
     * @param e Action event
     */
    @Override
    public void actionPerformed(final ActionEvent e) {
        manager.startSearch(searchTerms.getText());
    }
}

/*
 * Copyright (c) 2006-2011 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.dialogs.channellist;

import com.dmdirc.lists.GroupListManager;
import com.dmdirc.lists.GroupListObserver;

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

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

/** Gathers input for a group list search and begins the search. */
public class SearchTermsPanel extends JPanel implements ActionListener, GroupListObserver {

    /** Serial version UID. */
    private static final long serialVersionUID = 1L;
    /** Group list manager to perform searches on. */
    private GroupListManager manager;
    /** Search terms input field. */
    private JTextField searchTerms;
    /** Search button. */
    private JButton search;

    /**
     * Creates a new panel to gather input for a group list search.
     *
     * @param manager Group list manager to perform search with
     */
    public SearchTermsPanel(final GroupListManager manager) {
        this.manager = manager;
        searchTerms = new JTextField();
        layoutComponents();
        manager.addGroupListObserver(this);
    }

    /** Lays out the components in the panel. */
    private void layoutComponents() {
        search = new JButton("Search");
        search.addActionListener(this);
        setLayout(new MigLayout("fill, hidemode 3, ins 0"));
        add(new JLabel("Search terms: "), "align label");
        add(searchTerms, "growx, pushx");
        add(search, "");
    }

    /**
     * {@inheritDoc}
     *
     * @param e Action event
     */
    @Override
    public void actionPerformed(final ActionEvent e) {
        manager.startSearch(searchTerms.getText());
        search.setEnabled(false);
    }

    /** {@inheritDoc} */
    @Override
    public void onGroupListStarted() {
        //Ignore
    }

    /** {@inheritDoc} */
    @Override
    public void onGroupListFinished() {
        if (search != null) {
            search.setEnabled(true);
        }
    }
}

Disable search button until the list has finished.