/* Copyright (c) 2005 Stanford University and Christopher Bruns
 * 
 * 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.
 */

/*
 * Created on Jan 19, 2006
 * Original author: Christopher Bruns
 */
package org.simtk.isimsu;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import org.simtk.gui.*;

class OldIonSelectionDialog extends UndoableDialog implements Observer {
    JComboBox saltMenu = new JComboBox();
    Collection ionConditions;
    APBSParameters apbsParameters;

    OldIonSelectionDialog(Collection conditions, APBSParameters parameters, Frame parentFrame) {
        super(parentFrame);

        setTitle("ISIM: Select Ion Conditions");
        
        // add scrollbar when more than 10 items are in JComboBox
        saltMenu.setMaximumRowCount(10);
        
        ionConditions = conditions;
        this.apbsParameters = parameters;
        
        Container panel = getContentPane();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        JLabel dialogHeading = new JLabel("Click the pulldown arrow to select salt conditions:");
        dialogHeading.setAlignmentX(0.0f);
        panel.add(dialogHeading);

        // Use HTML styles in list of items
        saltMenu.setRenderer(new IonTableListRenderer());
        
        refreshListOfConditions();
        
        saltMenu.addActionListener(this);
        
        setLocationRelativeTo(parentFrame);
        
        panel.add(saltMenu);
        pack();
        
        addParametersListener(this);
    }
    
    private void refreshListOfConditions() {
        SaltCondition defaultConditions = null;
        
        // Populate list from global list of ion conditions
        for (Iterator conditionsIterator = ionConditions.iterator(); conditionsIterator.hasNext();) {
            SaltCondition conditions = (SaltCondition) conditionsIterator.next();
            if (defaultConditions == null) defaultConditions = conditions;
            if (conditions != null) addNewParameters(conditions);
        }
        setNewParameters(defaultConditions);
    }
    
    // Catch model changes from parent class
    public void update(Observable observable, Object parameters) {
        if (parameters instanceof SaltCondition) {
            apbsParameters.setIons((SaltCondition) parameters);
            saltMenu.setSelectedItem(parameters);
        }
    }
    
    public void actionPerformed(ActionEvent event) {
        super.actionPerformed(event);
        if (event.getSource() == saltMenu) {
            SaltCondition selectedSaltConditions = (SaltCondition) saltMenu.getSelectedItem();
            setNewParameters(selectedSaltConditions);
        }
    }
    
    void addNewParameters(SaltCondition parameters) {
        saltMenu.addItem(parameters);
    }

    // Create a class so HTML styles can be included in JComboBox list
    // This one is a simple HTML label in each item in the JComboBox
    class HTMLListRenderer extends JEditorPane implements ListCellRenderer {
        public HTMLListRenderer() {
            setOpaque(true);
            setContentType("text/html");
            setEditable(false);
        }
        public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus)
        {
            if (value == null) 
                setText("null");
            else 
                setText(value.toString());

            // Hope this invokes current look and feel color...
            setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
            setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());

            return this;
        }
        static final long serialVersionUID = 01L;
    }

    // List Cell style with fixed spaced table rows for each list cell
    class IonTableListRenderer extends JPanel implements ListCellRenderer {
        RowCellPane ionCell = new RowCellPane();
        RowCellPane ionListCell = new RowCellPane();
        RowCellPane temperatureCell = new RowCellPane();
        RowCellPane permittivityCell = new RowCellPane();
        
        public IonTableListRenderer() {
            setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); // A row is horizontal
            add(ionCell);
            add(ionListCell);
            add(temperatureCell);
            add(permittivityCell);
        }
        
        public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus)
        {
            if ( (value != null) && (value instanceof SaltCondition) ) {
                SaltCondition saltCondition = (SaltCondition) value;
                
                // Create a pretty row showing all of the information for a particular ion condition                

                ionCell.setText(saltCondition.getName());

                // List each ion individually
                Iterator ionIterator = saltCondition.iterator();
                String ionListString = "";
                boolean isFirstItemInList = true;
                while (ionIterator.hasNext()) {
                    IonConcentration ion = (IonConcentration) ionIterator.next();
                    if (! isFirstItemInList)
                        ionListString += "<br>\n"; // line breaks between ion entries in pull down menu
                    ionListString += ion.getConcentration() + "mM " + ion.getIonId();
                    isFirstItemInList = false;
                }
                ionListCell.setText(ionListString);

                temperatureCell.setText("" + saltCondition.getTemperature() + " &deg;K");
                permittivityCell.setText( " &#949;<sub>s</sub> = " + saltCondition.getSolventPermittivity() );
            }
            else {
                String nullString = "(null)";
                ionCell.setText(nullString);
                ionListCell.setText(nullString);
                temperatureCell.setText(nullString);
                permittivityCell.setText(nullString);
            }

            // Hope this invokes current look and feel color...
            setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
            setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());

            return this;
        }

        class RowCellPane extends JEditorPane {
            RowCellPane() {
                setOpaque(false);
                setContentType("text/html");
                setEditable(false);  
                setText("");
            }
            static final long serialVersionUID = 01L;
        }

        static final long serialVersionUID = 01L;
    }

    static final long serialVersionUID = 01L;
}

