
package org.openspml.browser;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;

import org.openspml.util.Util;
import org.openspml.message.*;

public class AttributesPanel extends JPanel {

    List _rows = new ArrayList();
    ActionListener _listener;
    boolean _modifications;

    public AttributesPanel() {
        setLayout(new LinearLayout(LinearLayout.VERTICAL));
    }

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

    public void setModifications(boolean b) {
        _modifications = b;
    }

    public void reset() {
        _rows = new ArrayList();
        removeAll();
    }

    public void reset(List attdefs) {

        reset();
        if (attdefs == null || attdefs.size() == 0)
            addRow();
        else {
            for (int i = 0 ; i < attdefs.size() ; i++) {
                AttributeDefinition def = 
                    (AttributeDefinition)attdefs.get(i);

		// filter out objectclass, we handle that
		// as an explicit widget in AddPanel, 
		// and don't allow it to be modified
		// in ModificationPanel
		if (!"objectclass".equals(def.getName()))
		    addRow(def.getName(), null);
            }
        }
    }

    public void addRow() {
        addRow(null, null);
    }

    public void addRow(String name, Object value) {
        Row row = new Row(_listener, name, value);
        _rows.add(row);
        add(row);
    }

    public List getAttributes(boolean filterNull) {

        List atts = null;
        if (_rows != null) {
            Iterator it = _rows.iterator();
            while (it.hasNext()) {
                Row r = (Row)it.next();
                String name = r.getName();
                if (name != null) {
                    Object value = r.getValue();
                    // on a create request we can also filter null values
                    if (!filterNull || value != null) {
                        Attribute att;
                        if (_modifications)
                            att = new Modification();
                        else
                            att = new Attribute();
                        att.setName(name);
                        att.setValue(value);
                        if (atts == null)
                            atts = new ArrayList();
                        atts.add(att);
                    }
                }
            }
        }
        return atts;
    }

    public class Row extends JPanel {

        JTextField _name;
        JTextField _value;
        boolean multi;

        public Row(ActionListener l, String name, Object value) {

            setLayout(new LinearLayout());
            
            _name = new JTextField(25);
            _name.addActionListener(l);
            add(_name);

            if (name != null) {
                _name.setText(name);
                _name.setEditable(false);
            }

            _value = new JTextField(25);
            _value.addActionListener(l);
            add(_value);

            // todo: support Lists better
            if (value instanceof List) {
                multi = true;
                _value.setText(Util.encodeCommaString((List)value));
            }
            else if (value != null) {
                _value.setText(value.toString());
            }
        }

        public String getName() {
            return SwingUtil.getText(_name);
        }

        public Object getValue() {

            Object value = null;
            String s = SwingUtil.getText(_value);
            if (!multi)
                value = s;
            else 
                value = Util.decodeCommaString(s);

            return value;
        }
    }

}
