
package org.openspml.browser;

import java.util.ArrayList;

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

import org.openspml.message.Identifier;

/**
 * A panel combining a request id text box with an async mode checkbox.
 * Needed by most request submission panels.
 */
public class IdentifierPanel extends JPanel implements ActionListener {

    JTextField _id;
    JComboBox _type;
    ActionListener _listener;
    
    public static final String[] TYPES = {
        Identifier.TYPE_GUID,
        Identifier.TYPE_GenericString,
        Identifier.TYPE_EMailAddress,
        Identifier.TYPE_DN,
        Identifier.TYPE_UserIDAndOrDomainName,
        Identifier.TYPE_LibertyUniqueID,
        Identifier.TYPE_PassportUniqueID,
        Identifier.TYPE_URN,
        Identifier.TYPE_SAMLSubject,
        Identifier.TYPE_OID
    };

    public static final String[] NAMES = {
        "GUID",
        "GenericString",
        "Email Address",
        "DN",
        "UserIDAndOrDomainName",
        "LibertyUniqueID",
        "PassportUniqueID",
        "URN",
        "SAMLSubject",
        "OID"
    };

    public IdentifierPanel() {

	setLayout(new LinearLayout(0));
	
        _id = new JTextField(25);
        _id.addActionListener(this);
        add(_id);

        _type = new JComboBox(NAMES);
        _type.addActionListener(this);
        add(_type);
    }

    public void addActionListener(ActionListener l) {
	_listener = l;
    }

    public void actionPerformed(ActionEvent e) {
        // mutate it into an event on ourslves
        // actually it doesn't really matter at the moment,
        // any event will cause the parent panels to regen the XML
        if (_listener != null) {
            e = new ActionEvent(this, 0, null);
            _listener.actionPerformed(e);
        }
    }

    public String getId() {
        return SwingUtil.getText(_id);
    }

    public String getType() {

	String type = null;
	int i = _type.getSelectedIndex();
	if (i >= 0)
	    type = TYPES[i];

	return type;
    }

    public void setText(String s) {
	_id.setText(s);
    }

    public Identifier getIdentifier() {

	// avoid creating one if we only have a type
	Identifier id = null;
	String s = getId();
	if (s != null) {
	    id = new Identifier();
	    id.setId(s);
	    id.setType(getType());
	}
	return id;
    }

}
