Package simtools.ui

Source Code of simtools.ui.DynamicColorPanel$ColorMapperListModel

package simtools.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

import simtools.data.DataSource;

/**
* This panel enable the setting of a dynamic color.
* @author zxpletran007
*
*/
public class DynamicColorPanel extends JPropertiesPanel{

    public static MenuResourceBundle resources = ResourceFinder.getMenu(DynamicColorChooser.class);

    public final static String DYNAMIC_COLOR_MAPPER = "DYNAMIC_COLOR_MAPPER";
    public final static String DYNAMIC_COLOR_SOURCE = "DYNAMIC_COLOR_SOURCE";

    protected GridBagPanel mPanel;
    protected JList cmlist;
    protected JButton cmNew, cmDelete, cmDuplicate;
    protected FilteredSourceTree dstree;
    protected ActionCheckBox cbDyna;

    protected boolean isOptional;
   

    public DynamicColorPanel(JDialog owner,  String message) {
        this(owner, message, true);
    }
    public DynamicColorPanel(JDialog owner,  String message, boolean isOptional) {
        super(owner, null);
        this.isOptional = isOptional;
       
        cbDyna = new ActionCheckBox(message, false) {
            public void actionPerformed(ActionEvent e) {
                mPanel.setEnabled(isSelected());
                dstree.setEnabled(isSelected());
                cmlist.setEnabled(isSelected());
                cmNew.setEnabled(isSelected());
                cmDelete.setEnabled(isSelected() && (cmlist.getSelectedValue() != null));
                dstree.setEnabled(isSelected());
                cmDuplicate.setEnabled(isSelected() && (cmlist.getSelectedValue() != null));
               
                updateWarnings();
            }
        };

        // mapper panel
        mPanel = new GridBagPanel(resources.getString("ChooseAColorMapper"));

        final ColorMapperListModel listModel = new ColorMapperListModel();
        // Create the list and put it in a scroll pane.
        cmlist = new JList(listModel);
        cmlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        cmlist.setVisibleRowCount(5);
        JScrollPane cmlistScrollPane = new JScrollPane(cmlist);

        cmNew = new JButton(resources.getString("New"));
        cmDelete = new JButton(resources.getString("Delete"));
        cmDuplicate = new JButton(resources.getString("Duplicate"));

        mPanel.addOnCurrentRow(cmlistScrollPane, 3, true, true, true);

        mPanel.addOnCurrentRow(cmNew);
        mPanel.addOnCurrentRow(cmDelete);
        mPanel.addOnCurrentRow(cmDuplicate);
        mPanel.carriageReturn();


        // data source panel
        dstree = FilteredSourceTree.getFromPool(message);
        dstree.setBorder(BorderFactory.createTitledBorder(resources.getString("ChooseADataSource")));
        dstree.getSourceTree().addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                updateWarnings();
            }
        });

        cbDyna.apply();


        // create contents
        if (isOptional){
            addOnCurrentRow(cbDyna, 2, false, false, true);
       
        } else {
            addOnCurrentRow(new JLabel(message), 2, false, false, true);
        }
      


        addOnCurrentRow(mPanel, 1, false, true, false);
        addOnCurrentRow(dstree, 1, true, true, true);

        // specify listeners
        cmNew.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ColorMapper cm = ColorMapper.createColorMapperDialog(DynamicColorPanel.this._owner);
                if (cm == null) {
                    return;
                }
                if (ColorMapper.colorMappers == null) {
                    ColorMapper.colorMappers = new Vector();
                }
                if (ColorMapper.colorMappers.contains(cm)) {
                    String[] message = new String[] { "Replace existing Color Mapper named ", cm.toString() };
                    int answer = JOptionPane.showConfirmDialog(DynamicColorPanel.this._owner, message,
                            "Color mapper already exists!", JOptionPane.YES_NO_OPTION);
                    if (answer == JOptionPane.YES_OPTION) {
                        // Object.equals is magic...
                        int index = ColorMapper.colorMappers.indexOf(cm);
                        ColorMapper.colorMappers.set(index, cm);
                        listModel.update();
                        cmlist.setSelectedValue(cm, true);
                    }
                    return;
                }
                ColorMapper.colorMappers.add(cm);
                listModel.update();
                cmlist.setSelectedValue(cm, true);
            }
        });
        cmDelete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object o = cmlist.getSelectedValue();
                if (o == null) {
                    return;
                }
                if (ColorMapper.colorMappers != null) {
                    ColorMapper.colorMappers.remove(o);
                }
                listModel.update();
               
                updateWarnings();
            }
        });
        cmDelete.setEnabled(false);

        cmDuplicate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object o = (ColorMapper)cmlist.getSelectedValue();
                if ((o == null) || !(o instanceof ColorMapper)) {
                    return;
                }
                try{
                    ColorMapper cm =  (ColorMapper)((ColorMapper)o).clone();
                    if (ColorMapper.colorMappers != null) {
                        ColorMapper.colorMappers.add(cm);
                    }
                    listModel.update();
                }catch (CloneNotSupportedException cnse){
                }
            }
        });
        cmDuplicate.setEnabled(false);


        cmlist.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                cmDelete.setEnabled(cmlist.getSelectedValue() != null);
                cmDuplicate.setEnabled(cmlist.getSelectedValue() != null);
                updateWarnings();
            }
        });
        cmlist.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int index = cmlist.locationToIndex(e.getPoint());
                    ((ColorMapper) ColorMapper.colorMappers.get(index)).editDialog(DynamicColorPanel.this._owner);
                }
            }
        });

    }
  
   
    /* (non-Javadoc)
     * @see javax.swing.JComponent#setEnabled(boolean)
     */
    public void setEnabled(boolean enabled){
        super.setEnabled(enabled);
       
        dstree.setEnabled(enabled);
        cbDyna.setEnabled(enabled);
        cmNew.setEnabled(enabled);
        cmDelete.setEnabled(enabled);
        cmDuplicate.setEnabled(enabled);
        cmlist.setEnabled(enabled);
        mPanel.setEnabled(enabled);
    }

    public String[] getPropertyNames() {
        String[] properties = {DYNAMIC_COLOR_MAPPER, DYNAMIC_COLOR_SOURCE};

        return properties;
    }

    public boolean  updateWarnings(){
        boolean res;
        if ((cbDyna.isSelected()) && (cmlist.getSelectedValue() == null)) {
            displayWarning(resources.getString("PleaseChooseAColorMapper"));
            res = true;

        } else if ((cbDyna.isSelected()) && (!(dstree.getSelectedSourceOrCollection() instanceof DataSource))) {
            displayWarning(resources.getString("PleaseChooseADataSource"));
            res = true;

        } else {
            res = super.updateWarnings();
        }
        return res;
    }
   

    public Object getPropertyValue(String name) {
        Object res = null;

       if (name.equalsIgnoreCase(DYNAMIC_COLOR_MAPPER)) {
            if (cbDyna.isSelected()){  

                res= cmlist.getSelectedValue();
                if (res == null) {
                    if (cmlist.getModel().getSize() == 1) {
                        cmlist.setSelectedIndex(0);
                        res = cmlist.getSelectedValue();
                    }
                }
            }

        } else if (name.equalsIgnoreCase(DYNAMIC_COLOR_SOURCE)) {
            if (cbDyna.isSelected()){
                Object o = dstree.getSelectedSourceOrCollection();
                if (o instanceof DataSource){
                    res = o;
                }
            }
        }
        return res;
    }


    public void setPropertyValue(String name, Object value) {

        if (name.equalsIgnoreCase(DYNAMIC_COLOR_MAPPER)) {
            if (value instanceof ColorMapper){
                cmlist.setSelectedValue(value, true);
            } else {
                cmlist.clearSelection();
            }

        } else if (name.equalsIgnoreCase(DYNAMIC_COLOR_SOURCE)) {
            if (value instanceof DataSource){
                dstree.setSelectedValue(value);
            } else {
                dstree.setSelectedValue(null);
            }
        }

        if (isOptional){
            cbDyna.setSelected( cmlist.getSelectedValue() instanceof ColorMapper || dstree.getSelectedSourceOrCollection() instanceof DataSource);
        } else {
            cbDyna.setSelected(true);
        }
        cbDyna.apply();
        updateWarnings();
    }
   

    protected static class ColorMapperListModel extends DefaultListModel {
        ColorMapperListModel() {
            fillFromMainList();
        }

        public void update() {
            this.clear();
            fillFromMainList();
        }

        protected void fillFromMainList() {
            if (ColorMapper.colorMappers != null) {
                for (Iterator it = ColorMapper.colorMappers.iterator(); it.hasNext();) {
                    addElement(it.next());
                }
            }
        }
    }
}
TOP

Related Classes of simtools.ui.DynamicColorPanel$ColorMapperListModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.