001    /*
002     * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: HAlign.java,v 8afef6dd0d58 2007/09/14 08:29:58 maxberger $ */
018    
019    package net.sourceforge.jeuclid.elements.support.attributes;
020    
021    import net.sourceforge.jeuclid.layout.LayoutInfo;
022    import net.sourceforge.jeuclid.layout.LayoutStage;
023    
024    /**
025     * @version $Revision: 8afef6dd0d58 $
026     */
027    public enum HAlign {
028        /** Left align. */
029        LEFT,
030        /** Center align. */
031        CENTER,
032        /** Right align. */
033        RIGHT;
034        /** constant for center align. */
035        public static final String ALIGN_CENTER = "center";
036    
037        /** constant for right align. */
038        public static final String ALIGN_RIGHT = "right";
039    
040        /**
041         * Parse an Alignment String.
042         * 
043         * @param alignString
044         *            String to parse.
045         * @param defaultt
046         *            default value.
047         * @return a HAlign value.
048         */
049        public static HAlign parseString(final String alignString,
050                final HAlign defaultt) {
051            final HAlign retVal;
052            if (HAlign.ALIGN_CENTER.equalsIgnoreCase(alignString)) {
053                retVal = CENTER;
054            } else if ("left".equalsIgnoreCase(alignString)) {
055                retVal = LEFT;
056            } else if (HAlign.ALIGN_RIGHT.equalsIgnoreCase(alignString)) {
057                retVal = RIGHT;
058            } else {
059                retVal = defaultt;
060            }
061            return retVal;
062        }
063    
064        /**
065         * Retrieve H-Align offset.
066         * 
067         * @param stage
068         *            current Layout Stage
069         * @param info
070         *            Info object to examine
071         * @param width
072         *            Total width
073         * @return Alignment offset to add to left.
074         */
075        public float getHAlignOffset(final LayoutStage stage,
076                final LayoutInfo info, final float width) {
077            final float offset;
078            switch (this) {
079            case LEFT:
080                offset = 0.0f;
081                break;
082            case RIGHT:
083                offset = width - info.getWidth(stage);
084                break;
085            case CENTER:
086                offset = width / 2.0f - info.getHorizontalCenterOffset(stage);
087                break;
088            default:
089                assert false;
090                offset = 0.0f;
091            }
092            return offset;
093        }
094    
095    }