/**
 * Copyright (C) 2004-2011 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.spark.component;

import org.jivesoftware.resource.SparkRes;

import javax.swing.*;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.plaf.basic.BasicTextFieldUI;

/**
 * Creates a Firefox Search type box that allows for icons inside of a
 * textfield. This could be used to build out your own search objects.
 */
public class IconTextField extends JPanel {
    private final JTextField textField;
    private final JLabel imageComponent;
    private final JLabel downOption;

    /**
     * Creates a new IconTextField with Icon.
     */
    public IconTextField(Icon icon) {
        setLayout(new GridBagLayout());
        setBackground((Color) UIManager.get("TextField.background"));

        textField = new JTextField();
        textField.setUI(new BasicTextFieldUI());
        textField.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
        //setBorder(new JTextField().getBorder());
        this.setBorder(BorderFactory.createLineBorder(Color.lightGray));
        imageComponent = new JLabel(icon);
        downOption = new JLabel(SparkRes.getImageIcon(SparkRes.Icon.DOWN_OPTION_IMAGE));

        add(downOption, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
        add(imageComponent, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
        add(textField, new GridBagConstraints(2, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 5, 0, 0), 0, 0));

        downOption.setVisible(false);
    }

    public void enableDropdown(boolean enable) {
        downOption.setVisible(enable);
    }

    /**
     * Sets the text of the textfield.
     */
    public void setText(String text) {
        textField.setText(text);
    }

    /**
     * Returns the text inside of the textfield.
     */
    public String getText() {
        return textField.getText();
    }

    /**
     * Sets the icon to use inside of the textfield.
     */
    public void setIcon(Icon icon) {
        imageComponent.setIcon(icon);
    }

    /**
     * Returns the current icon used in the textfield.
     */
    public Icon getIcon() {
        return imageComponent.getIcon();
    }

    /**
     * Returns the component that holds the icon.
     */
    public JComponent getImageComponent() {
        return imageComponent;
    }

    /**
     * Returns the text component used.
     */
    public JTextField getTextComponent() {
        return textField;
    }

    public JLabel getDownOption() {
        return downOption;
    }
}
