Package jsynoptic.builtin.ui

Source Code of jsynoptic.builtin.ui.ImagePropertiesPanel$ImageMapperListModel

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2005, by :
*     Corporate:
*         EADS Astrium SAS
*         EADS CRC
*     Individual:
*         Claude Cazenave
*
* $Id: ImagePropertiesPanel.java,v 1.21 2008/09/29 08:44:53 ogor Exp $
*
* Changes
* -------
* 20 jan 2005 : Initial public release (JB);
*
*/
package jsynoptic.builtin.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.ResourceBundle;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

import jsynoptic.builtin.ImageShape;
import jsynoptic.builtin.ImageShape.ImageShapePropertiesNames;
import simtools.data.DataInfo;
import simtools.data.DataSource;
import simtools.ui.ActionCheckBox;
import simtools.ui.BasicMessageWriter;
import simtools.ui.FilteredSourceTree;
import simtools.ui.ImageMapper;
import simtools.ui.ImagePreview;
import simtools.ui.GridBagPanel;
import simtools.ui.ResourceFinder;
import simtools.util.CurrentPathProvider;

/**
* @author jb
*/
public class ImagePropertiesPanel extends PropertiesPanel1D {
    public static JFileChooser fileChooser;

    protected JList cmlist;

    protected JButton bnew, bChoose, bdelete, bduplicate;

    protected JLabel lcolorMapper, lstatic, lchooseDs, lds;

    protected FilteredSourceTree dstree;

    protected ActionCheckBox cbDynamic;

    protected JRadioButton rbFit, rbResize;

    protected File currentFile;

    protected GridBagPanel dynamicImagePanel, staticImagePanel;

    /**
     * An optional default directory for Image shapes
     */
    public static File defaultDirectory = null;

    public static ResourceBundle resources = ResourceFinder.get(ImageShape.class);

    public static BasicMessageWriter messageWriter = ResourceFinder.getMessages(ImageShape.class);

    protected ImageMapper mapper;

    protected DataSource source;

    public ImagePropertiesPanel(String shapeName) {
        super(true, true, true, true, shapeName);
        addOnCurrentRow(createContent(), 2, true, true, true);
    }

    protected void setDynamicImage() {
    }

    public JComponent createContent() {
        currentFile = null;
        lstatic = new JLabel();
        if (currentFile != null) {
            lstatic.setText(currentFile.getName());
        }
        bChoose = new JButton(resources.getString("Choose"));
        rbFit = new JRadioButton(resources.getString("ScaleImageToFitObjectSize"), false);
        rbResize = new JRadioButton(resources.getString("ChangeObjectSizeToFitImage"), false);
        ButtonGroup bg = new ButtonGroup();
        bg.add(rbFit);
        bg.add(rbResize);
        bChoose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Set up the file chooser.
                if (fileChooser == null) {
                    fileChooser = new JFileChooser();
                    // Add a custom file filter and disable the default
                    // (Accept All) file filter.
                    fileChooser.addChoosableFileFilter(new ImageMapper.ImageFileFilter());
                    fileChooser.setAcceptAllFileFilterUsed(false);
                    // Add the preview pane.
                    fileChooser.setAccessory(new ImagePreview(fileChooser));
                    // Set image file chooser directory
                    File imageDirectory = ((defaultDirectory != null) && defaultDirectory.exists()) ? defaultDirectory
                            : CurrentPathProvider.currentPathProvider.getCurrentPath();
                    fileChooser.setCurrentDirectory(imageDirectory);
                }
                fileChooser.setSelectedFile(currentFile);
                // Show it.
                int returnVal = fileChooser.showDialog(ImagePropertiesPanel.this, resources.getString("SelectImage"));
                // Process the results.
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    currentFile = fileChooser.getSelectedFile();
                    if (currentFile != null) {
                        try {
                            BufferedImage img = ImageIO.read(currentFile);
                            if (rbResize.isSelected() && (img != null)) {
                                nfWidth.setValue(img.getWidth());
                                nfHeight.setValue(img.getHeight());
                                lstatic.setText(currentFile.getName());
                            }
                        } catch (IOException e1) {
                        }
                    }
                }
            }
        });
        rbResize.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                if (rbResize.isSelected()) {
                    if (currentFile != null) {
                        try {
                            BufferedImage img = ImageIO.read(currentFile);
                            if (img != null) {
                                nfWidth.setValue(img.getWidth());
                                nfHeight.setValue(img.getHeight());
                            }
                        } catch (IOException e1) {
                        }
                    }
                }
            }
        });
        // Dynamic image
        cbDynamic = new ActionCheckBox(resources.getString("UseDynamicImages")) {
            public void actionPerformed(ActionEvent e) {
                lcolorMapper.setEnabled(isSelected());
                lchooseDs.setEnabled(isSelected());
                lds.setEnabled(isSelected());
                cmlist.setEnabled(isSelected());
                bnew.setEnabled(isSelected());
                bdelete.setEnabled((isSelected()) && (cmlist.getSelectedValue() != null));
                bduplicate.setEnabled((isSelected()) && (cmlist.getSelectedValue() != null));
                dstree.setEnabled(isSelected());
                updateWarnings();
            }
        };
        // Image mapper
        lcolorMapper = new JLabel(resources.getString("ChooseAnImageMapper"));
        final ImageMapperListModel listModel = new ImageMapperListModel();
        // 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);
        bnew = new JButton(resources.getString("New"));
        bdelete = new JButton(resources.getString("Delete"));
        bduplicate = new JButton(resources.getString("Duplicate"));
       
        // Data Source
        lchooseDs = new JLabel(resources.getString("ChooseADataSource"));
        lds = new JLabel();
        dstree = FilteredSourceTree.getFromPool("PropertiesPanel0");
        dstree.getSourceTree().addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                updateWarnings();
                if (((dstree.getSelectedSourceOrCollection()) instanceof DataSource)
                        && canSetDataSource((DataSource) dstree.getSelectedSourceOrCollection())) {
                    lds.setText(DataInfo.getId(dstree.getSelectedSourceOrCollection()));
                }
            }
        });
        bnew.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ImageMapper im = ImageMapper.createImageMapperDialog(ImagePropertiesPanel.this.getOwner());
                if (im == null) {
                    return;
                }
                if (ImageMapper.imageMappers == null) {
                    ImageMapper.imageMappers = new Vector();
                }
                if (ImageMapper.imageMappers.contains(im)) {
                    String[] message = new String[] { messageWriter.print1args("ReplaceExistingImageMapperNamed?", im
                            .toString()) };
                    int answer = JOptionPane.showConfirmDialog(ImagePropertiesPanel.this.getOwner(), message,
                            messageWriter.print0args("MapperAlreadyExists!"), JOptionPane.YES_NO_OPTION);
                    if (answer == JOptionPane.YES_OPTION) {
                        // Object.equals is magic...
                        int index = ImageMapper.imageMappers.indexOf(im);
                        ImageMapper.imageMappers.set(index, im);
                        listModel.update();
                        cmlist.setSelectedValue(im, true);
                    }
                    return;
                }
                ImageMapper.imageMappers.add(im);
                listModel.update();
                cmlist.setSelectedValue(im, true);
            }
        });
        bdelete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object o = cmlist.getSelectedValue();
                if (o == null) {
                    return;
                }
                if (ImageMapper.imageMappers != null) {
                    ImageMapper.imageMappers.remove(o);
                }
                listModel.update();
                updateWarnings();
            }
        });
        bdelete.setEnabled(false);
       
        bduplicate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object o = cmlist.getSelectedValue();
                if ((o == null) || !(o instanceof ImageMapper)) {
                    return;
                }
                try{
                    ImageMapper im =  (ImageMapper)((ImageMapper)o).clone();
                    if (ImageMapper.imageMappers != null) {
                        ImageMapper.imageMappers.add(im);
                    }
                    listModel.update();
                }catch (CloneNotSupportedException cnse){
                }
            }
        });
        bduplicate.setEnabled(false);
       
        cmlist.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                bdelete.setEnabled(cmlist.getSelectedValue() != null);
                bduplicate.setEnabled(cmlist.getSelectedValue() != null);
                updateWarnings();
            }
        });
        cmlist.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int index = cmlist.locationToIndex(e.getPoint());
                    ((ImageMapper) ImageMapper.imageMappers.get(index))
                            .editDialog(ImagePropertiesPanel.this.getOwner());
                }
            }
        });
        // Create panels
        staticImagePanel = new GridBagPanel();
        staticImagePanel.addOnCurrentRow(bChoose, 2);
        staticImagePanel.addOnCurrentRow(lstatic);
        staticImagePanel.carriageReturn();
        staticImagePanel.addOnCurrentRow(rbFit, 2);
        staticImagePanel.addOnCurrentRow(rbResize, 2);
        staticImagePanel.carriageReturn();
       
        dynamicImagePanel = new GridBagPanel(resources.getString("UseDynamicImages"));
        dynamicImagePanel.addOnCurrentRow(cbDynamic, 4);
        dynamicImagePanel.addOnCurrentRow(lds, 4);
        dynamicImagePanel.carriageReturn();
        dynamicImagePanel.addOnCurrentRow(lcolorMapper, 4);
        dynamicImagePanel.addOnCurrentRow(lchooseDs, 4);
        dynamicImagePanel.carriageReturn();
        dynamicImagePanel.addOnCurrentRow(cmlistScrollPane, 4, true, true, false);
        dynamicImagePanel.addOnCurrentRow(dstree, 4, true, true, true);
        dynamicImagePanel.addOnCurrentRow(bnew);
        dynamicImagePanel.addOnCurrentRow(bdelete);
        dynamicImagePanel.addOnCurrentRow(bduplicate);
        GridBagPanel imagePanel = new GridBagPanel();
        imagePanel.addOnCurrentRow(staticImagePanel, 1, false, false, true);
        imagePanel.addOnCurrentRow(dynamicImagePanel, 1, true, true, true);// TODO
        return imagePanel;
    }

    protected String lineColorChooserTitle() {
        return resources.getString("ChooseTheFrameColor");
    }

    protected String lineColorLabel() {
        return resources.getString("DisplayFrame");
    }

    /*
     * (non-Javadoc)
     *
     * @see jsynoptic.builtin.ui.PropertiesPanel1D#updateWarnings()
     */
    public boolean updateWarnings() {
        boolean res;
        if ((cbDynamic.isSelected()) && (cmlist.getSelectedValue() == null)) {
            displayWarning(resources.getString("selectAnImageMapper"));
            res = true;
           
        } else if ((cbDynamic.isSelected()) && (!(dstree.getSelectedSourceOrCollection() instanceof DataSource))) {
            displayWarning(resources.getString("selectADataSource"));
            res = true;
           
        } else {
            res = super.updateWarnings();
        }
       
        return res;
    }

    protected boolean canSetDataSource(DataSource ds) {
        return true;
    }

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

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

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

    public String[] getPropertyNames() {
        if (_propertyNames == null) {
            _propertyNames = new ImageShapePropertiesNames().getPropertyNames();
        }
        return _propertyNames;
    }

    /*
     * (non-Javadoc)
     *
     * @see simtools.ui.JPropertiesPanel#getPropertyValue(java.lang.String)
     */
    public Object getPropertyValue(String name) {
        Object res = super.getPropertyValue(name);
        if (name.equalsIgnoreCase("IMAGE_FIT_TO_OBJECT")) {
            res = new Boolean(rbFit.isSelected());
        } else if (name.equalsIgnoreCase("IMAGE_FILE")) {
            res = currentFile;
        } else if (name.equalsIgnoreCase("IMAGE_MAPPER")) {
            res = cmlist.getSelectedValue();
        } else if (name.equalsIgnoreCase("IMAGE_MAPPER_SOURCE")) {
            if (cbDynamic.isSelected()) {
                Object o = dstree.getSelectedSourceOrCollection();
                if ((o instanceof DataSource) && canSetDataSource((DataSource) o)) {
                    res = o;
                } else {
                    res = null;
                }
            }
        }
        return res;
    }

    /*
     * (non-Javadoc)
     *
     * @see simtools.ui.JPropertiesPanel#setPropertyValue(java.lang.String,
     *      java.lang.Object)
     */
    public void setPropertyValue(String name, Object value) {
        super.setPropertyValue(name, value);
        if (name.equalsIgnoreCase("IMAGE_FIT_TO_OBJECT")) {
            if (value instanceof Boolean) {
                rbFit.setSelected(((Boolean) value).booleanValue());
                rbResize.setSelected(!rbFit.isSelected());
            }
        } else if (name.equalsIgnoreCase("IMAGE_FILE")) {
            if (value instanceof File) {
                currentFile = (File) value;
                lstatic.setText(currentFile.getName());
            }
        } else if (name.equalsIgnoreCase("IMAGE_MAPPER")) {
            if (value instanceof ImageMapper) {
                mapper = (ImageMapper) value;
                cmlist.setSelectedValue(mapper, true);
            }
        } else if (name.equalsIgnoreCase("IMAGE_MAPPER_SOURCE")) {
            if ((value instanceof DataSource) && canSetDataSource((DataSource) value)) {
                source = (DataSource) value;
                dstree.setSelectedValue(source);
                lds.setText(DataInfo.getId(source));
            }
            cbDynamic.setSelected((source != null) || (mapper != null));
            cbDynamic.apply();
        }
    }
}
TOP

Related Classes of jsynoptic.builtin.ui.ImagePropertiesPanel$ImageMapperListModel

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.