Package DisplayProject.actions

Source Code of DisplayProject.actions.FieldWidgetGravity

/*
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 java.awt.GridBagConstraints;

import javax.swing.JComponent;
import javax.swing.JFrame;

import DisplayProject.Constants;
import DisplayProject.GridCell;
import DisplayProject.GridField;
/**
* The Gravity attribute (integer) specifies the default way the field aligns itself when displayed in a matrix field cell. Gravity is translated to the equivalent anchor value in a {@link GridBagContstraint}.
* Gravity accepts the following values:
* <li>Constants.CG_BOTTOMCENTER  Bottom of the cell, centered horizontally.</li>
* <li>Constants.CG_BOTTOMLEFT    Bottom, left corner of the cell.</li>
* <li>Constants.CG_BOTTOMRIGHT    Bottom, right corner of the cell.</li>
* <li>Constants.CG_CENTER      Center of the cell.</li>
* <li>Constants.CG_DEFAULT      Same as CG_CENTER.</li>
* <li>Constants.CG_MIDDLELEFT    Left side of the cell, centered vertically.</li>
* <li>Constants.CG_MIDDLERIGHT    Right side of the cell, centered vertically.</li>
* <li>Constants.CG_TOPCENTER    Top of the cell, centered horizontally.</li>
* <li>Constants.CG_TOPLEFT      Top, left corner of the cell.</li>
* <li>Constants.CG_TOPRIGHT    Top, right corner of the cell.</li>
* @author Peter
*
*/
public class FieldWidgetGravity extends PendingAction {

    private int Gravity;
    public FieldWidgetGravity(Component pComponent, int value) {
        super(pComponent);
        this.Gravity = value;
    }
    public void performAction() {
        if (this._component != null) {
          GridCell gbc = GridField.getConstraints((JComponent)this._component);
            gbc.setCellGravity(Gravity);
        }
    }
    public int getGravity() {
        return Gravity;
    }
    public void setGravity(int row) {
        Gravity = row;
    }

    public static void set(JComponent comp, int value){
        ActionMgr.addAction(new FieldWidgetGravity(comp, value));
    }
    public static void set(JFrame comp, int value){
        ActionMgr.addAction(new FieldWidgetGravity(comp.getContentPane(), value));
    }
    public static int get(JComponent comp){
        FieldWidgetGravity action = ActionMgr.getAction(comp, FieldWidgetGravity.class);
        if (action != null) {
            return action.getGravity();
        }
        GridCell gbc = GridField.getConstraints((JComponent)comp);
        return gbc.getCellGravity();
    }
    public static int anchorToGravity(int anchor){
        switch (anchor){
        case GridBagConstraints.SOUTH:
            return Constants.CG_BOTTOMCENTER;
        case GridBagConstraints.SOUTHWEST:
            return Constants.CG_BOTTOMLEFT;
        case GridBagConstraints.SOUTHEAST:
            return Constants.CG_BOTTOMRIGHT;
        case GridBagConstraints.CENTER:
            return Constants.CG_CENTER;
        case GridBagConstraints.WEST:
            return Constants.CG_MIDDLELEFT;
        case GridBagConstraints.EAST:
            return Constants.CG_MIDDLERIGHT;
        case GridBagConstraints.NORTH:
            return Constants.CG_TOPCENTER;
        case GridBagConstraints.NORTHWEST:
            return Constants.CG_TOPLEFT;
        case GridBagConstraints.NORTHEAST:
            return Constants.CG_TOPRIGHT;
        default:
            return Constants.CG_CENTER;
        }
    }

    public static int gravityToAnchor(int gravity){
        switch (gravity){
        case Constants.CG_BOTTOMCENTER:
            return GridBagConstraints.SOUTH;
        case Constants.CG_BOTTOMLEFT:
            return GridBagConstraints.SOUTHWEST;
        case Constants.CG_BOTTOMRIGHT:
            return GridBagConstraints.SOUTHEAST;
        case Constants.CG_CENTER:
            return GridBagConstraints.CENTER;
        case Constants.CG_MIDDLELEFT:
            return GridBagConstraints.WEST;
        case Constants.CG_MIDDLERIGHT:
            return GridBagConstraints.EAST;
        case Constants.CG_TOPCENTER:
            return GridBagConstraints.NORTH;
        case Constants.CG_TOPLEFT:
            return GridBagConstraints.NORTHWEST;
        case Constants.CG_TOPRIGHT:
            return GridBagConstraints.NORTHEAST;
        default:
            return GridBagConstraints.CENTER;

        }
    }
}
TOP

Related Classes of DisplayProject.actions.FieldWidgetGravity

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.