Package DisplayProject.actions

Source Code of DisplayProject.actions.MaxCharacters

/*
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.Component;

import javax.swing.JComponent;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

import DisplayProject.ArrayColumn;
import DisplayProject.Constants;
import DisplayProject.FixedLengthDocument;
import DisplayProject.controls.FillInField;
/**
* Gets and sets the Max characters allowed in a text component or a text component used as a column template in an ArrayField
* @author Peter
*
*/
public class MaxCharacters extends ReversableAction implements ColumnAction {
    private ArrayColumn arrayColumn;
    private int value;
    public MaxCharacters(Component pComponent, int pValue) {
        super(pComponent);
        this.value = pValue;
    }
    public MaxCharacters(ArrayColumn column, int pValue) {
        super((JComponent)column.getCellEditor());
        this.value = pValue;
        this.arrayColumn = column;
    }

    public int getValue() {
        return this.value;
    }

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

    public void performAction(Component component) {
        if (this.arrayColumn != null){
            this.arrayColumn.setMaxCharacters(this.value);
            if ((this.value != 0) && (SizePolicy.get(this.arrayColumn) == Constants.FP_FIXED)){
                this.arrayColumn.setMinWidth(this.value);
            }
        }
        else {
            if (this._component instanceof JTextComponent)
            { 
                Document doc = ((JTextComponent)component).getDocument();
                if (doc instanceof FixedLengthDocument){
                    ((FixedLengthDocument)doc).setMaxLength(this.value);
                }
            }
            // TF:20/7/07: Made this work on a FillInField rather than a specific data model of a combo box
            else if (component instanceof FillInField) {
                FillInField jComboBox = (FillInField)component;
                jComboBox.setMaxCharacters(this.value);
            }
        }

    }
    public static void set(JTextComponent comp, int value){
        ActionMgr.addAction(new MaxCharacters(comp, value));
    }

    public static void set(ArrayColumn column, int value){
        ActionMgr.addAction(new MaxCharacters(column, value));
    }
    public static int get(ArrayColumn comp){
        MaxCharacters action = ActionMgr.getAction(comp, MaxCharacters.class);
        if (action != null) {
            return action.getValue();
        }
        int max = 0;
        ArrayColumn ac = (ArrayColumn)comp;
        if ((ac.getCellEditor() != null)
                && (ac.getCellEditor() instanceof JTextComponent)){
            Document doc = ((JTextComponent)ac.getCellEditor()).getDocument();
            if ((doc != null) && (doc instanceof FixedLengthDocument)){
                max = ((FixedLengthDocument)doc).getMaxLength();
            }
        } else {
            max = ac.getMaxCharacters();
        }
        return max;
    }

    public static int get(JTextComponent comp){
        MaxCharacters action = ActionMgr.getAction(comp, MaxCharacters.class);
        if (action != null) {
            return action.getValue();
        }
        int max = 0;
        Document doc = ((JTextComponent)comp).getDocument();
        if ((doc != null) && (doc instanceof FixedLengthDocument)){
            max = ((FixedLengthDocument)doc).getMaxLength();
        }
        return max;
    }
    public static void set(FillInField comp, int value){
        ActionMgr.addAction(new MaxCharacters(comp, value));
    }

    public static int get(FillInField comp){
        MaxCharacters action = ActionMgr.getAction(comp, MaxCharacters.class);
        if (action != null) {
            return action.getValue();
        }
        return comp.getMaxCharacters();
    }

    public PendingAction createReverseAction() {
        return new MaxCharacters(this._component, get((JTextComponent)this._component));
    }

    public boolean isSameAction(PendingAction a) {
        return (a instanceof MaxCharacters) && (a._component == this._component);
    }
    /* (non-Javadoc)
     * @see ColumnAction#getColumn()
     */
    public ArrayColumn getColumn() {
        return this.arrayColumn;
    }
    @Override
    public PendingAction applyActionTo(Component component) {
        PendingAction result = null;
        if (component instanceof JTextComponent) {
            int max = 0;
            Document doc = ((JTextComponent)component).getDocument();
            if ((doc != null) && (doc instanceof FixedLengthDocument)){
                max = ((FixedLengthDocument)doc).getMaxLength();
            }
            result = new MaxCharacters(component, max);
        }
        performAction(component);
        return result;
    }

}
TOP

Related Classes of DisplayProject.actions.MaxCharacters

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.