Package DisplayProject.actions

Source Code of DisplayProject.actions.ColourChange

/*
Copyright (c) 2003-2008 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 DisplayProject.actions;

import java.awt.Color;
import java.awt.Component;
import java.awt.SystemColor;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.text.JTextComponent;

import DisplayProject.ArrayColumn;
import DisplayProject.ArrayColumnModel;
import DisplayProject.CompoundField;
import DisplayProject.Constants;
import DisplayProject.DataField;
import DisplayProject.GridField;
import DisplayProject.UIutils;
import DisplayProject.controls.ArrayField;
import DisplayProject.controls.ListView;
import DisplayProject.controls.Panel;
import DisplayProject.controls.TabFolder;
import DisplayProject.table.ArrayFieldCellEditor;
import DisplayProject.table.ArrayFieldCellHelper;
import DisplayProject.table.ArrayFieldCellRenderer;

/**
* This class provides the colour transformation for Forte FillColor (Background) and PenColor (Foreground) attributes.
*
* @author Tim
*
*/
public class ColourChange extends ReversableAction {
    public static final int cBACKGROUND = 1;
    public static final int cFOREGROUND = 2;

    protected Color newColour;
    protected int colourType;
    protected int forteColour;
    protected boolean useColourIndex;
    protected ArrayColumn column;

    /**
     * This constructor takes a Java colour
     *
     * @param pComponent
     *            Target Component
     * @param pNewColour
     *            java.awt.Color
     * @param pType
     */
    public ColourChange(Component pComponent, Color pNewColour, int pType) {
        super(pComponent);
        this.newColour = pNewColour;
        this.colourType = pType;
        this.useColourIndex = false;
    }

    public ColourChange(ArrayColumn pComponent, Color pNewColour, int pType) {
        super(null);
        this.newColour = pNewColour;
        this.colourType = pType;
        this.useColourIndex = false;
        this.column = pComponent;
    }
    /**
     * this constructor takes a forte colour constant
     *
     * @param pComponent
     *            Target Component
     * @param pForteColour
     *            Forte colour constant
     * @param pType
     */
    public ColourChange(Component pComponent, int pForteColour, int pType) {
        super(pComponent);
        this.forteColour = pForteColour;
        this.colourType = pType;
        this.useColourIndex = true;
    }

    public ColourChange(ArrayColumn pComponent, int pForteColour, int pType) {
        super(null);
        this.forteColour = pForteColour;
        this.colourType = pType;
        this.useColourIndex = true;
        this.column = pComponent;
    }

    public void performAction() {
        this.performAction(this._component);
    }

    private void performAction(Component comp) {
        Color c;
        Component innerComp = null;
        // We want to set the colour on the widget inside the JScrollPane
        if (comp instanceof JScrollPane &&
                ((JScrollPane)comp).getViewport().getComponentCount() > 0) {
            innerComp = ((JScrollPane)comp).getViewport().getComponent(0);
        }

        if (this.useColourIndex) {
            if (this.forteColour == Constants.C_DEFAULT) {
                // if we're setting the foreground colour, set it to the default
                // control text
                // colour, otherwise set it to the default control colour
                switch (this.colourType) {
                case cBACKGROUND:
                    if (comp instanceof JTree || (innerComp != null && innerComp instanceof JTree)){
                        c = UIutils.White;
                    } else {
                      // TF:20/07/2009:Use the UI routines to get the default colour, not the system
                      // colour. For example, if we have a grid field which contains data fields, and
                      // the datafields inherit their colour, then setting this to the system colour
                      // will make the data fields a fixed colour, not white
                        // c = SystemColor.control;
                      c = UIutils.forteToJavaColor(Constants.C_INHERIT);
                    }
                    break;
                case cFOREGROUND:
                    c = SystemColor.controlText;
                    break;
                default: // keep the compiler happy so it knows c has been
                            // initialised...
                    c = SystemColor.control;
                    break;
                }
            } else {
                c = UIutils.forteToJavaColor(this.forteColour);
            }
        } else {
            c = this.newColour;
        }

        // TF:22/11/07:If this component is a template for an array column, get the array column first
        ArrayColumn column = this.column;
        JTable t = ArrayFieldCellHelper.getArrayField(this._component);
        if (t != null) {
            int col = ArrayFieldCellHelper.getArrayFieldColumn((JComponent)this._component);
            ArrayColumnModel model = (ArrayColumnModel) t.getColumnModel();
            column = (ArrayColumn)model.getRealColumn(col);
        }

        switch (this.colourType) {
        case cBACKGROUND:
            if (column != null) {
                //PM:22/11/07 fixed case for new cell renderers/editors
                if (column.getCellEditor() instanceof ArrayFieldCellEditor){
                    ((ArrayFieldCellEditor)column.getCellEditor()).setBackground(c);
                    ((ArrayFieldCellRenderer)column.getCellRenderer()).setBackground(c);
                }else {
                    ((JComponent)column.getCellEditor()).setBackground(c);
                    ((JComponent)column.getCellRenderer()).setBackground(c);
                }
            }
       
            // TF:02/12/2009:Even if we are affecting something that is a table column template,
            // we must still affect the underlying component. Failure to do this means that if
            // the renderer or editor derives the colour to paint the component from the original
            // template (as it should) then this won't be set. This is particularly prevalent
            // in say fill-in fields that are on unused rows in array fields.
            // Owing to the risk involved in this change, removing the else { ... } around this
            // section of code has only been done for the background colour, not the foreground colour
            /*
             * this is a special case for DataFields
             * TF:8/11/07:Added in the check for using the colour index
             */
            if (((comp instanceof JTextComponent) || (comp instanceof JComboBox))
                    && (this.forteColour == Constants.C_DEFAULT) && useColourIndex) {
                c = Color.WHITE;
            }
            if (comp instanceof ListView) {
                c = Color.WHITE;
            }
            if (comp instanceof JTree) {
                comp.getParent().getParent().setBackground(c);
            }
            if (comp != null) {
              comp.setBackground(c);
            }
            if (innerComp != null) {
                innerComp.setBackground(c);
            }
            break;
        case cFOREGROUND:
            if (this.column != null) {
                //PM:22/11/07 fixed case for new cell renderers/editors
                if (column.getCellEditor() instanceof ArrayFieldCellEditor){
                    ((ArrayFieldCellEditor)column.getCellEditor()).setForeground(c);
                    ((ArrayFieldCellRenderer)column.getCellRenderer()).setForeground(c);
                }else {
                    ((JComponent)column.getCellEditor()).setForeground(c);
                    ((JComponent)column.getCellRenderer()).setForeground(c);
                }
            } else if (comp instanceof DataField){
                comp.setForeground(c);
                ((DataField)comp).setDisabledTextColor(c);
            } else {
                comp.setForeground(c);
            }
            break;
        }
        this.repaint();
    }

    /**
     * set background colour using java.awt.Color
     *
     * @param pComponent
     * @param pColour
     */
    public static void setBackground(Component pComponent, Color pColour) {
        ActionMgr.addAction(new ColourChange(pComponent, pColour, cBACKGROUND));
    }

    /**
     * set background colour forte color constant
     *
     * @param pComponent
     * @param pColour
     */
    public static void setBackground(Component pComponent, int pColour) {
        ActionMgr.addAction(new ColourChange(pComponent, pColour, cBACKGROUND));
    }

    public static void setBackground(ArrayColumn pComponent, int pColour) {
        ActionMgr.addAction(new ColourChange(pComponent, pColour, cBACKGROUND));
    }

    public static void setBackground(GridField pComponent, int pColour) {
        ActionMgr.addAction(new ColourChange(pComponent, pColour, cBACKGROUND));
    }

    public static void setBackground(ArrayField pComponent, int pColour) {
        ActionMgr.addAction(new ColourChange(pComponent, pColour, cBACKGROUND));
    }

    public static void setBackground(Panel pComponent, int pColour) {
        ActionMgr.addAction(new ColourChange(pComponent, pColour, cBACKGROUND));
    }

    public static void setBackground(TabFolder pComponent, int pColour) { // CraigM:18/11/2008.
      ActionMgr.addAction(new ColourChange(pComponent, pColour, cBACKGROUND));
    }

    public static void setBackground(CompoundField pComponent, int pColour) {
        if (pComponent instanceof JComponent) {
            ActionMgr.addAction(new ColourChange((JComponent) pComponent, pColour, cBACKGROUND));
        }
    }

    /**
     * set foreground colour usinf java.awt.Color
     *
     * @param pComponent
     * @param pColour
     */
    public static void setForeground(Component pComponent, Color pColour) {
        ActionMgr.addAction(new ColourChange(pComponent, pColour, cFOREGROUND));
    }

    /**
     * set foreground colour forte color constant
     *
     * @param pComponent
     * @param pColour
     */
    public static void setForeground(Component pComponent, int pColour) {
        ActionMgr.addAction(new ColourChange(pComponent, pColour, cFOREGROUND));
    }

    /**
     * Return the current background colour of the component as a Forte colour. If the colour is unknown within the
     * Forte colour space, C_INHERIT is returned
     * @param pComponent
     * @return
     */
    public static int getBackground(final Component pComponent) {
        ColourChange action = (ColourChange)ActionMgr.getAction(new ActionMgr.Filter() {
            public boolean filter(PendingAction action) {
                return action instanceof ColourChange && action.getComponent() == pComponent && ((ColourChange)action).colourType ==cBACKGROUND;
            }
        });
        if (action != null) {
            if (action.useColourIndex) {
                return action.forteColour;
            }
            else {
                return UIutils.javaToForteColor(action.newColour);
            }
        }
        else {
            return UIutils.javaToForteColor(pComponent.getBackground());
        }
    }

    /**
     * Return the current foregorund colour of the component as a Forte colour. If the colour is unknown within the
     * Forte colour space, C_INHERIT is returned
     * @param pComponent
     * @return
     */
    public static int getForeground(final Component pComponent) {
        ColourChange action = (ColourChange)ActionMgr.getAction(new ActionMgr.Filter() {
            public boolean filter(PendingAction action) {
                return action instanceof ColourChange && action.getComponent() == pComponent && ((ColourChange)action).colourType ==cFOREGROUND;
            }
        });
        if (action != null) {
            if (action.useColourIndex) {
                return action.forteColour;
            }
            else {
                return UIutils.javaToForteColor(action.newColour);
            }
        }
        else {
            return UIutils.javaToForteColor(pComponent.getForeground());
        }
    }

    public PendingAction createReverseAction() {
        if (colourType == cBACKGROUND) {
            return new ColourChange(this._component, this._component
                    .getBackground(), colourType);
        } else {
            return new ColourChange(this._component, this._component
                    .getForeground(), colourType);
        }
    }

    public boolean isSameAction(PendingAction a) {
        return (a instanceof ColourChange) && (a._component == this._component)
                && (((ColourChange) a).colourType == this.colourType);
    }

    public ArrayColumn getColumn() {
        return column;
    }

    @Override
    public PendingAction applyActionTo(Component component) {
        PendingAction reverse;
        if (colourType == cBACKGROUND) {
            reverse  = new ColourChange(component, component.getBackground(), colourType);
        } else {
            reverse  = new ColourChange(component, component.getForeground(), colourType);
        }
        performAction(component);
        return reverse;
    }
}
TOP

Related Classes of DisplayProject.actions.ColourChange

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.