1 /*
2 * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /* $Id: HAlign.java,v 8afef6dd0d58 2007/09/14 08:29:58 maxberger $ */
18
19 package net.sourceforge.jeuclid.elements.support.attributes;
20
21 import net.sourceforge.jeuclid.layout.LayoutInfo;
22 import net.sourceforge.jeuclid.layout.LayoutStage;
23
24 /**
25 * @version $Revision: 8afef6dd0d58 $
26 */
27 public enum HAlign {
28 /** Left align. */
29 LEFT,
30 /** Center align. */
31 CENTER,
32 /** Right align. */
33 RIGHT;
34 /** constant for center align. */
35 public static final String ALIGN_CENTER = "center";
36
37 /** constant for right align. */
38 public static final String ALIGN_RIGHT = "right";
39
40 /**
41 * Parse an Alignment String.
42 *
43 * @param alignString
44 * String to parse.
45 * @param defaultt
46 * default value.
47 * @return a HAlign value.
48 */
49 public static HAlign parseString(final String alignString,
50 final HAlign defaultt) {
51 final HAlign retVal;
52 if (HAlign.ALIGN_CENTER.equalsIgnoreCase(alignString)) {
53 retVal = CENTER;
54 } else if ("left".equalsIgnoreCase(alignString)) {
55 retVal = LEFT;
56 } else if (HAlign.ALIGN_RIGHT.equalsIgnoreCase(alignString)) {
57 retVal = RIGHT;
58 } else {
59 retVal = defaultt;
60 }
61 return retVal;
62 }
63
64 /**
65 * Retrieve H-Align offset.
66 *
67 * @param stage
68 * current Layout Stage
69 * @param info
70 * Info object to examine
71 * @param width
72 * Total width
73 * @return Alignment offset to add to left.
74 */
75 public float getHAlignOffset(final LayoutStage stage,
76 final LayoutInfo info, final float width) {
77 final float offset;
78 switch (this) {
79 case LEFT:
80 offset = 0.0f;
81 break;
82 case RIGHT:
83 offset = width - info.getWidth(stage);
84 break;
85 case CENTER:
86 offset = width / 2.0f - info.getHorizontalCenterOffset(stage);
87 break;
88 default:
89 assert false;
90 offset = 0.0f;
91 }
92 return offset;
93 }
94
95 }