1 29 30 package nextapp.echo2.webcontainer.propertyrender; 31 32 import org.w3c.dom.Element ; 33 34 import nextapp.echo2.app.Alignment; 35 import nextapp.echo2.app.Component; 36 import nextapp.echo2.app.LayoutDirection; 37 import nextapp.echo2.webrender.output.CssStyle; 38 39 43 public class AlignmentRender { 44 45 56 public static int getRenderedHorizontal(Alignment alignment, Component component) { 57 LayoutDirection layoutDirection; 58 if (component == null) { 59 layoutDirection = LayoutDirection.LTR; 60 } else { 61 layoutDirection = component.getRenderLayoutDirection(); 62 } 63 switch (alignment.getHorizontal()) { 64 case Alignment.LEADING: 65 return layoutDirection.isLeftToRight() ? Alignment.LEFT : Alignment.RIGHT; 66 case Alignment.TRAILING: 67 return layoutDirection.isLeftToRight() ? Alignment.RIGHT : Alignment.LEFT; 68 default: 69 return alignment.getHorizontal(); 70 } 71 } 72 73 82 public static void renderToElement(Element element, Alignment alignment) { 83 renderToElement(element, alignment, null); 84 } 85 86 98 public static void renderToElement(Element element, Alignment alignment, Component component) { 99 if (alignment == null) { 100 return; 101 } 102 103 String horizontal = getHorizontalCssAttributeValue(alignment, component); 104 if (horizontal != null) { 105 element.setAttribute("align", horizontal); 106 } 107 String vertical = getVerticalCssAttributeValue(alignment); 108 if (vertical != null) { 109 element.setAttribute("valign", vertical); 110 } 111 } 112 113 122 public static void renderToStyle(CssStyle cssStyle, Alignment alignment) { 123 renderToStyle(cssStyle, alignment, null); 124 } 125 126 138 public static void renderToStyle(CssStyle cssStyle, Alignment alignment, Component component) { 139 if (alignment == null) { 140 return; 141 } 142 143 String horizontal = getHorizontalCssAttributeValue(alignment, component); 144 if (horizontal != null) { 145 cssStyle.setAttribute("text-align", horizontal); 146 } 147 String vertical = getVerticalCssAttributeValue(alignment); 148 if (vertical != null) { 149 cssStyle.setAttribute("vertical-align", vertical); 150 } 151 } 152 153 168 private static String getHorizontalCssAttributeValue(Alignment alignment, Component component) { 169 switch (getRenderedHorizontal(alignment, component)) { 170 case Alignment.LEFT: 171 return "left"; 172 case Alignment.CENTER: 173 return "center"; 174 case Alignment.RIGHT: 175 return "right"; 176 default: 177 return null; 178 } 179 } 180 181 189 private static String getVerticalCssAttributeValue(Alignment alignment) { 190 switch (alignment.getVertical()) { 191 case Alignment.TOP: 192 return "top"; 193 case Alignment.CENTER: 194 return "middle"; 195 case Alignment.BOTTOM: 196 return "bottom"; 197 default: 198 return null; 199 } 200 } 201 202 203 private AlignmentRender() { } 204 } 205 | Popular Tags |