Package jsynoptic.plugins.svg

Source Code of jsynoptic.plugins.svg.SvgMapper$ImageRenderer

/* ========================
* 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-2006, by :
*     Corporate:
*         EADS Astrium SAS
*     Individual:
*         Mathias Choquet
*
*
* $$Id$$
*
*/
package jsynoptic.plugins.svg;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ResourceBundle;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import jsynoptic.plugins.svg.ui.SvgShapePropertiesPanel;
import jsynoptic.plugins.svg.ui.SvgShapePropertiesPanel.SvgFileFilter;
import simtools.images.svg.SVGImageFactory;
import simtools.ui.GenericMapper;
import simtools.ui.ResourceFinder;
import simtools.util.CurrentPathProvider;

/**
* This class maps data sources and index to Svg. The mapping is user defined,
* and based for example on the value of the data source For example, green
* value < 10, red above. Useful for making plots with "alarm" colors
*
* The data source shall not be saved into the object. Rather, this class goal
* is to implement a mapping independent of the data source given. The same is
* true for the Svg
*
*/
public class SvgMapper extends GenericMapper {
    static final long serialVersionUID = 3227030744000505758L;

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

    /** The svg mappers List are application global */
    public transient static ArrayList svgMappers;
    static {
        svgMappers = new ArrayList();
    }

    /**
     * If the mapper already exists in the list, update its values Otherwise add
     * it to the list of existing mappers
     *
     * @param m,
     *            the mapper to update
     * @return
     */
    public static SvgMapper updateSvgMapper(SvgMapper m) {
        SvgMapper em = SvgMapper.getSvgMapper(m.toString());
        if (em != null) {
            return em;
        }
        svgMappers.add(m);
        return m;
    }

    /**
     * Method <b>getSvgMapperFromName<\b> returns mapper linked to specified
     * name return null if no mapper were found
     *
     * @param name
     * @return
     */
    public static SvgMapper getSvgMapper(String id) {
        if (svgMappers != null) {
            for (Iterator it = svgMappers.iterator(); it.hasNext();) {
                SvgMapper sm = (SvgMapper) it.next();
                if ((sm.toString() != null) && (sm.toString().equals(id))) {
                    return sm;
                }
            }
        }
        return null;
    }

    /**
     * Constructor. empty constructor call the Abstract2Shape consructor
     */
    public SvgMapper() {
        super();
    }

    /**
     * Constructor. call the Abstract2Shape consructor with name in parameters
     *
     * @param name
     *            the name of the mapper
     */
    public SvgMapper(String name) {
        super(name);
    }

    /**
     * Class SvgMapper. Summary: this class represent the object specific to
     * svgMapper used by the mapper
     */
    public static class SvgFile implements Serializable {
        /**
         *
         */
        private static final long serialVersionUID = -8216120002258506232L;

        /**
         * the svg is represented only by his file
         */
        private transient File file = null;

        /*
         * (non-Javadoc)
         *
         * @see java.lang.Object#toString()
         */
        public String toString() {
            if (file == null) {
                return "null";
            }
            return file.getName();
        }

        /**
         * Method <b>writeObject</b> method to write the SvgFile in an
         * OutputStream (for example a file) Parameters:
         *
         * @param out
         *            the stream in which the object is saved
         * @throws java.io.IOException
         */
        private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
            // Serialize relative path to file
            SvgShape.serializer.write(out, file, CurrentPathProvider.currentPathProvider.getCurrentPath());
        }

        /**
         * Method <b>readObject</b> method to read the SvgFile from an
         * InputStream (for example a file) Parameters:
         *
         * @param in
         *            out the stream from which the object is read
         * @throws java.lang.ClassNotFoundException
         * @throws java.io.IOException
         */
        private void readObject(java.io.ObjectInputStream in) throws java.lang.ClassNotFoundException,
                java.io.IOException {
            // Get relative path to file
            setFile(SvgShape.serializer.read(in, CurrentPathProvider.currentPathProvider.getCurrentPath()));
        }

        /**
         * Method <b>setFile</b> set the file to the mapper Parameters:
         *
         * @param f
         *            set the file to the mapper
         */
        public void setFile(File f) {
            file = f;
        }

        /**
         * Method <b>getFile</b>
         *
         * @return the file to the mapper
         */
        public File getfile() {
            return file;
        }
    }

    /*
     * The editor button that brings up the dialog. We extend DefaultCellEditor
     * for convenience, even though it mean we have to create a dummy check box.
     * Another approach would be to copy the implementation of TableCellEditor
     * methods from the source code for DefaultCellEditor.
     */
    protected class SvgEditor extends DefaultCellEditor implements SVGImageFactory.FactoryListener {
        SvgFile currentFile = null;

        BufferedImage fileChooserImage = null;

        public SvgEditor(JButton b) {
            super(new JCheckBox()); // Unfortunately, the constructor
            // expects a check box, combo box,
            // or text field.
            editorComponent = b;
            setClickCountToStart(2); // This is usually 1 or 2.
            // Must do this so that editing stops when appropriate.
            b.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    fireEditingStopped();
                }
            });
        }

        /*
         * (non-Javadoc)
         *
         * @see javax.swing.AbstractCellEditor#fireEditingStopped()
         */
        protected void fireEditingStopped() {
            super.fireEditingStopped();
        }

        /*
         * (non-Javadoc)
         *
         * @see javax.swing.DefaultCellEditor#getCellEditorValue()
         */
        public Object getCellEditorValue() {
            // return the SvgFile currentFile as the mapper object
            return currentFile;
        }

        /*
         * (non-Javadoc)
         *
         * @see javax.swing.DefaultCellEditor#getTableCellEditorComponent(javax.swing.JTable,
         *      java.lang.Object, boolean, int, int)
         */
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            ((JButton) editorComponent).setText(value.toString());
            // set the SvgFile currentFile
            currentFile = (SvgFile) value;
            return editorComponent;
        }

        /*
         * (non-Javadoc)
         *
         * @see simtools.images.svg.SVGImageFactory.FactoryListener#done(java.awt.image.BufferedImage)
         */
        public void done(BufferedImage image) {
            fileChooserImage = image;
        }

        /*
         * (non-Javadoc)
         *
         * @see simtools.images.svg.SVGImageFactory.FactoryListener#failed(java.lang.String)
         */
        public void failed(String msg) {
            System.err.println("Cannot load " + currentFile.getfile().getName() + " reason is : " + msg);
        }
    }

    /**
     * Class SvgMapper. textual representation of the SvgFile for the table cell
     */
    protected static class ImageRenderer extends JLabel implements TableCellRenderer {
        /*
         * (non-Javadoc)
         *
         * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
         *      java.lang.Object, boolean, boolean, int, int)
         */
        public Component getTableCellRendererComponent(JTable table, Object o, boolean isSelected, boolean hasFocus,
                int row, int column) {
            // get the SvgFile from the table cell
            SvgFile svgFile = (SvgFile) o;
            if (o == null) {
                setText("null");
            } else {
                setText(svgFile.toString());
            }
            return this;
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see simtools.ui.GenericMapper#createModel()
     */
    public MapperTableModel createModel() {
        return new SvgMapperTableModel();
    }

    /**
     * Class SvgMapper. represent the Svg mapper table, extends the
     * simtools.ui.GenericMapper.ExpressionMappingTable
     */
    protected class SvgMappingTable extends ExpressionMappingTable {
        /**
         * Constructor. call the GenericMapper constructor
         *
         * @param owner
         */
        public SvgMappingTable(JDialog owner) {
            super(owner);
        }

        /**
         * Sets up the renderer to a color renderer
         *
         * @see simtools.ui.GenericMapper.ExpressionMappingTable#setUpRenderer()
         */
        protected void setUpRenderer() {
            setDefaultRenderer(SvgFile.class, new ImageRenderer());
        }

        /**
         * Sets up the editor to a color editor
         *
         * @see simtools.ui.GenericMapper.ExpressionMappingTable#setUpEditor()
         */
        protected void setUpEditor() {
            // First, set up the button that brings up the dialog.
            final JButton button = new JButton("");
            // Now create an editor to encapsulate the button, and
            // set it up as the editor for all Color cells.
            final SvgEditor svgEditor = new SvgEditor(button);
            setDefaultEditor(SvgFile.class, svgEditor);
            // Here's the code that brings up the dialog.
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // Set up the file chooser.
                    if (SvgShapePropertiesPanel.fileChooser == null) {
                        SvgShapePropertiesPanel.fileChooser = new JFileChooser();
                        // Add a custom file filter and disable the default
                        SvgShapePropertiesPanel.fileChooser.setAcceptAllFileFilterUsed(true);
                        // add the optionnal (Accept All) file filter.
                        SvgShapePropertiesPanel.fileChooser.addChoosableFileFilter(new SvgFileFilter());
                        // Add the preview pane.
                        SvgShapePropertiesPanel.fileChooser.setAccessory(new SvgImagePreview(
                                SvgShapePropertiesPanel.fileChooser));
                        File svgDirectory = ((SvgShapePropertiesPanel.defaultDirectory != null) && SvgShapePropertiesPanel.defaultDirectory
                                .exists()) ? SvgShapePropertiesPanel.defaultDirectory
                                : CurrentPathProvider.currentPathProvider.getCurrentPath();
                        SvgShapePropertiesPanel.fileChooser.setCurrentDirectory(svgDirectory);
                    }
                    if ((svgEditor.currentFile != null) && (svgEditor.currentFile.getfile() != null)) {
                        SvgShapePropertiesPanel.fileChooser.setSelectedFile(svgEditor.currentFile.getfile());
                    } else {
                        SvgShapePropertiesPanel.fileChooser.setSelectedFile(null);
                    }
                    // Show it.
                    int returnVal = SvgShapePropertiesPanel.fileChooser.showDialog(SvgMappingTable.this, resources
                            .getString("SelectSvg"));
                    // Process the results.
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        svgEditor.currentFile.file = SvgShapePropertiesPanel.fileChooser.getSelectedFile();
                        SvgShape.factory.load(svgEditor.currentFile.file, svgEditor);
                    }
                    // Reset the file chooser for the next time it's shown.
                    SvgShapePropertiesPanel.fileChooser.setSelectedFile(null);
                }
            });
        }
    }

    /**
     * Class SvgMapper. Summary: Model of the Svg mapper table
     */
    protected class SvgMapperTableModel extends MapperTableModel {
        /*
         * (non-Javadoc)
         *
         * @see simtools.ui.GenericMapper.MapperTableModel#getColumnClass(int)
         */
        public Class getColumnClass(int c) {
            if (c == 1) {
                return (SvgFile.class);
            }
            return super.getColumnClass(c);
        }

        /*
         * (non-Javadoc)
         *
         * @see simtools.ui.GenericMapper.MapperTableModel#getColumnName(int)
         */
        public String getColumnName(int col) {
            if (col == 1) {
                return resources.getString("Svg");
            }
            return super.getColumnName(col);
        }

        /*
         * (non-Javadoc)
         *
         * @see simtools.ui.GenericMapper.MapperTableModel#isCellEditable(int,
         *      int)
         */
        public boolean isCellEditable(int row, int col) {
            if (col == 1) {
                return true;
            }
            return super.isCellEditable(row, col);
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see simtools.ui.GenericMapper#createTable(java.awt.Frame)
     */
    protected ExpressionMappingTable createTable(JDialog parent) {
        return new SvgMappingTable(parent);
    }

    /*
     * (non-Javadoc)
     *
     * @see simtools.ui.GenericMapper#createNewValue()
     */
    protected Object createNewValue() {
        return new SvgFile();
    }

    /**
     * Method <b>createSvgMapperDialog</b> static metod to instanciate a
     * SvgMapperDialog Parameters:
     *
     * @param owner
     *            the frame owner of this dialog
     * @return an instance of a SvgMapper
     */
    public static SvgMapper createSvgMapperDialog(JDialog owner) {
        SvgMapper svgm = new SvgMapper();
        svgm.editDialog(owner);
        return svgm;
    }
}
TOP

Related Classes of jsynoptic.plugins.svg.SvgMapper$ImageRenderer

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.