Package net.helipilot50.stocktrade.displayproject.beans

Source Code of net.helipilot50.stocktrade.displayproject.beans.ColorEditor

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package net.helipilot50.stocktrade.displayproject.beans;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyEditorSupport;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.colorchooser.AbstractColorChooserPanel;

import net.helipilot50.stocktrade.displayproject.UIutils;


/**
* This class implements a colour editor, based loosely on the standard Java JColorChooser,
* but with an extra panel that looks and functions similar to the Forte colour selection
* including being aware of the Forte colours. This class is also aware that null is a valid
* colour to represent transparency.
* @author Tim
*
*/
@SuppressWarnings("serial")
public class ColorEditor extends PropertyEditorSupport  {
    public class ForteColorChooser extends AbstractColorChooserPanel {
        protected JPanel[] panels = new JPanel[forteColors.length+1];
        protected JPanel otherPanel;
        protected Color currentColor;
        protected JPanel currentPanel = null;
        protected Dimension panelSize = new Dimension(15, 26);
        public ForteColorChooser() {
            MouseListener listener = new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    Color c = (Color)((JComponent)e.getComponent()).getClientProperty("colour");
                    setForteColor(c);
                    if (c != null) {
                        getColorSelectionModel().setSelectedColor(c);
                    }
                }
            };
            JPanel panel = new JPanel();
            panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
            panels[0] = new JPanel();
            panels[0].add(new JLabel("i"));
            panels[0].setBorder(new LineBorder(Color.black, 1));
            panels[0].setToolTipText("inherit colour from parent");
            panels[0].setBackground(null);
            panels[0].setPreferredSize(panelSize);
            panels[0].addMouseListener(listener);
            panel.add(panels[0]);
            for (int i = 0; i < forteColors.length; i++) {
                panels[i+1] = new JPanel();
                panels[i+1].setBackground(forteColors[i].color);
                panels[i+1].setToolTipText(forteColors[i].name);
                panels[i+1].setBorder(new LineBorder(Color.black, 1));
                panels[i+1].setPreferredSize(panelSize);
                panels[i+1].addMouseListener(listener);
                panels[i+1].putClientProperty("colour", forteColors[i].color);
                panel.add(panels[i+1]);
            }
            otherPanel = new JPanel();
            otherPanel.add( new JLabel("Other"));
            otherPanel.setToolTipText("Other color");
            panel.add(otherPanel);
            add(panel);
        }
        @Override
        protected void buildChooser() {
            // All the work has been done in the constructor, nothing to do here...
        }

        public Color getCurrentColor() {
            return currentColor;
        }

        public void setForteColor(Color c) {
            // First, deselect the old colour
            if (currentPanel != null) {
                currentPanel.setBorder(new LineBorder(Color.black, 1));
            }
            isNull = (c == null);
            currentColor = c;
            currentPanel = otherPanel;
            for (int i = 0; i < panels.length; i++) {
                boolean found = false;
                Color background = (Color)panels[i].getClientProperty("colour");
                if (background == null) {
                    if (c == null) {
                        found = true;
                    }
                }
                else if (background.equals(c)) {
                    found = true;
                }
                if (found) {
                    currentPanel = panels[i];
                    break;
                }
            }
            otherPanel.setBackground(c);
            otherPanel.setToolTipText(getColorAsText(c));
            currentPanel.setBorder(new LineBorder(Color.black, 3));
            repaint();
        }
        @Override
        public String getDisplayName() {
            return "Forte";
        }

        @Override
        public Icon getLargeDisplayIcon() {
            return null;
        }

        @Override
        public Icon getSmallDisplayIcon() {
            return null;
        }

        @Override
        public void updateChooser() {
            Color c = getColorSelectionModel().getSelectedColor();
            setForteColor(c);
        }
    }

    private boolean isNull = false;
    private ForteColorChooser forteChooser;
    /**
     * The actual colour chooser we're going to use, which we extend to be null-aware
     */
    private JColorChooser chooser;

    public ColorEditor() {
        forteChooser = new ForteColorChooser();
        chooser = new JColorChooser() {
            @Override
            public void setColor(Color color) {
                if (color != null) {
                    super.setColor(color);
                }
                else {
                    // The standard colour chooser can't handle null, so we just set the Forte colour
                    forteChooser.setForteColor(null);
                }
            }
        };
        chooser.setPreviewPanel(new JPanel());
        AbstractColorChooserPanel[] panels = chooser.getChooserPanels();
        AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[panels.length + 1];
        for (int i = 0; i < panels.length; i++) {
            newPanels[i+1] = panels[i];
        }
        newPanels[0] = new ForteColorChooser();
        chooser.setChooserPanels(newPanels);
    }

    /**
     * A simple container class that allows us to store a standard
     * java colour along with a name for that colour
     */
    private class NamedColor {
        public String name;
        public Color color;
        public NamedColor(String pName, Color pColor) {
            this.name = pName;
            this.color = pColor;
        }
    }

    private NamedColor[] standardColors = new NamedColor[] {
            new NamedColor("black",    Color.black),
            new NamedColor("blue",    Color.blue),
            new NamedColor("cyan",    Color.cyan),
            new NamedColor("darkGray",  Color.darkGray),
            new NamedColor("gray",    Color.gray),
            new NamedColor("green",    Color.green),
            new NamedColor("lightGray",  Color.lightGray),
            new NamedColor("magenta",  Color.magenta),
            new NamedColor("orange",  Color.orange),
            new NamedColor("pink",    Color.pink),
            new NamedColor("red",    Color.red),
            new NamedColor("white",    Color.white),
            new NamedColor("yellow",  Color.yellow)
    };
//PM:24/06/2008:changed colours to constants
    private NamedColor[] forteColors = new NamedColor[] {
            new NamedColor("White",     UIutils.White),
            new NamedColor("Gray1",     UIutils.Gray1),
            new NamedColor("Gray2",     UIutils.Gray2),
            new NamedColor("Gray3",     UIutils.Gray3),
            new NamedColor("Gray4",     UIutils.Gray4),
            new NamedColor("Gray5",     UIutils.Gray5),
            new NamedColor("Gray6",     UIutils.Gray6),
            new NamedColor("Gray7",     UIutils.Gray7),
            new NamedColor("Black",     UIutils.Black),
            new NamedColor("PaleYellow",   UIutils.PaleYellow),
            new NamedColor("BrightYellow",   UIutils.BrightYellow),
            new NamedColor("Yellow",     UIutils.Yellow),
            new NamedColor("PaleGreen",   UIutils.PaleGreen),
            new NamedColor("BrightGreen",   UIutils.BrightGreen),
            new NamedColor("Green",     UIutils.Green),
            new NamedColor("PaleCyan",     UIutils.PaleCyan),
            new NamedColor("BrightCyan",   UIutils.BrightCyan),
            new NamedColor("Cyan",       UIutils.Cyan),
            new NamedColor("PaleBlue",     UIutils.PaleBlue),
            new NamedColor("BrightBlue",   UIutils.BrightBlue),
            new NamedColor("Blue",       UIutils.Blue),
            new NamedColor("PaleMagenta",   UIutils.PaleMagenta),
            new NamedColor("BrightMagenta",  UIutils.BrightMagenta),
            new NamedColor("Magenta",     UIutils.Magenta),
            new NamedColor("PaleRed",     UIutils.PaleRed),
            new NamedColor("BrightRed",    UIutils.BrightRed),
            new NamedColor("Red",       UIutils.Red),
            new NamedColor("PaleBrown",   UIutils.PaleBrown),
            new NamedColor("BrightBrown",  UIutils.BrightBrown),
            new NamedColor("Brown",     UIutils.Brown)
    };

    /**
     * Get the colour as a string, suitable for displaying to an end user. The name
     * will be returned if it's one of the standard colours, otherwise the RGB components
     * @param c the colour to return as text
     * @return the text of the passed colour
     */
    public String getColorAsText(Color c) {
        if (c == null) {
            return "(null)";
        }
        // First, see if it's a standard colour
        for (int i = 0; i < standardColors.length; i++) {
            if (standardColors[i].color.equals(c)) {
                return standardColors[i].name;
            }
        }
        // How about a Forte colour?
        for (int i = 0; i < forteColors.length; i++) {
            if (forteColors[i].color.equals(c)) {
                return forteColors[i].name;
            }
        }
        return "[r=" + c.getRed() + ",g=" + c.getGreen() + ",b=" + c.getBlue() + "]";
    }

    /**
     * Get the colour as a string, suitable for inclusion in a Java program. The name
     * will be returned if it's one of the standard colours, otherwise the RGB components
     * @param c the colour to return as text
     * @return the text of the passed colour
     */
    public String getColorAsJavaText(Color c) {
        if (c == null) {
            return "null";
        }
        // First, see if it's a standard colour
        for (int i = 0; i < standardColors.length; i++) {
            if (standardColors[i].color.equals(c)) {
                return "java.awt.Color." + standardColors[i].name;
            }
        }

        // How about a Forte colour?
        for (int i = 0; i < forteColors.length; i++) {
            if (forteColors[i].color.equals(c)) {
                return "UIutils." + standardColors[i].name;
            }
        }
        return "new java.awt.Color(" + c.getRed() + "," + c.getGreen() + "," + c.getBlue() + ")";
    }

    /**
     * Get the value that is currently mapped to the underlying colour chooser, as a Color
     */
    public Object getValue() {
        return getColor();
    }

    /**
     * Set the value to be displayed to the end user
     */
    public void setValue(Object object) {
        setColor((Color) object);
    }

    /**
     * This method is called when the value is selected in the visual
     * editor, prior to having the custom editor invoked
     */
    public String getAsText() {
        return getColorAsText(getColor());
    }

    public String getJavaInitializationString() {
        return getColorAsJavaText(getColor());
    }

    /**
     * Determines whether the class will honour the paintValue method.
     *
     * @return  True if the class will honour the paintValue method.
     */
    public boolean isPaintable() {
        return true;
    }

    /**
     * This method paints the cell, akin to a cell renderer for the property.
     * In this case, we'll paint a sample of the colour, followed by it's name
     */
    public void paintValue(Graphics graphics, Rectangle rectangle) {
        Color oldColor = graphics.getColor();
        Color newColor = getColor();
        String value = getColorAsText(newColor);
        if (newColor != null) {
            graphics.setColor(newColor);
            graphics.fillRect(rectangle.x, rectangle.y + 3, rectangle.height - 6, rectangle.height - 6);
            graphics.setColor(Color.black);
            graphics.drawRect(rectangle.x, rectangle.y + 3, rectangle.height - 6, rectangle.height - 6);
            graphics.setColor(oldColor);
            graphics.drawString(value, rectangle.height - 3, rectangle.height - 3);
        }
        else {
            graphics.drawString(value, 0, rectangle.height - 3);
        }
    }

    public Component getCustomEditor() {
        return chooser;
    }

    public boolean supportsCustomEditor() {
        return true;
    }

    protected Color getColor() {
        return (isNull ? null : chooser.getSelectionModel().getSelectedColor());
//    return forteChooser.getCurrentColor();
    }

    protected void setColor(Color color) {
        Color oldValue = getColor();
        if (color == null || !color.equals(oldValue)) {
            chooser.getSelectionModel().setSelectedColor(color);
            forteChooser.setForteColor(color);
            // Notify anyone who cares about the selection
            firePropertyChange();
        }
    }
}
TOP

Related Classes of net.helipilot50.stocktrade.displayproject.beans.ColorEditor

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.