1 /* 2 * Copyright (C) 2007-2008 Esmertec AG. 3 * Copyright (C) 2007-2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mms.dom.smil; 19 20 import com.android.mms.LogTag; 21 22 import org.w3c.dom.DOMException; 23 import org.w3c.dom.smil.SMILDocument; 24 import org.w3c.dom.smil.SMILRegionElement; 25 26 import android.util.Log; 27 28 public class SmilRegionElementImpl extends SmilElementImpl implements 29 SMILRegionElement { 30 31 /* 32 * Internal Interface 33 */ 34 35 private static final String HIDDEN_ATTRIBUTE = "hidden"; 36 private static final String SLICE_ATTRIBUTE = "slice"; 37 private static final String SCROLL_ATTRIBUTE = "scroll"; 38 private static final String MEET_ATTRIBUTE = "meet"; 39 private static final String FILL_ATTRIBUTE = "fill"; 40 private static final String ID_ATTRIBUTE_NAME = "id"; 41 private static final String WIDTH_ATTRIBUTE_NAME = "width"; 42 private static final String TITLE_ATTRIBUTE_NAME = "title"; 43 private static final String HEIGHT_ATTRIBUTE_NAME = "height"; 44 private static final String BACKGROUND_COLOR_ATTRIBUTE_NAME = "backgroundColor"; 45 private static final String Z_INDEX_ATTRIBUTE_NAME = "z-index"; 46 private static final String TOP_ATTRIBUTE_NAME = "top"; 47 private static final String LEFT_ATTRIBUTE_NAME = "left"; 48 private static final String RIGHT_ATTRIBUTE_NAME = "right"; 49 private static final String BOTTOM_ATTRIBUTE_NAME = "bottom"; 50 private static final String FIT_ATTRIBUTE_NAME = "fit"; 51 private static final String TAG = LogTag.TAG; 52 private static final boolean DEBUG = false; 53 private static final boolean LOCAL_LOGV = false; 54 SmilRegionElementImpl(SmilDocumentImpl owner, String tagName)55 SmilRegionElementImpl(SmilDocumentImpl owner, String tagName) { 56 super(owner, tagName); 57 } 58 59 /* 60 * SMILRegionElement Interface 61 */ 62 getFit()63 public String getFit() { 64 String fit = getAttribute(FIT_ATTRIBUTE_NAME); 65 if (FILL_ATTRIBUTE.equalsIgnoreCase(fit)) { 66 return FILL_ATTRIBUTE; 67 } else if (MEET_ATTRIBUTE.equalsIgnoreCase(fit)) { 68 return MEET_ATTRIBUTE; 69 } else if (SCROLL_ATTRIBUTE.equalsIgnoreCase(fit)) { 70 return SCROLL_ATTRIBUTE; 71 } else if (SLICE_ATTRIBUTE.equalsIgnoreCase(fit)) { 72 return SLICE_ATTRIBUTE; 73 } else { 74 return HIDDEN_ATTRIBUTE; 75 } 76 } 77 getLeft()78 public int getLeft() { 79 try { 80 return parseRegionLength(getAttribute(LEFT_ATTRIBUTE_NAME), true); 81 } catch (NumberFormatException _) { 82 if (LOCAL_LOGV) { 83 Log.v(TAG, "Left attribute is not set or incorrect."); 84 } 85 } 86 try { 87 int bbw = ((SMILDocument) getOwnerDocument()).getLayout().getRootLayout().getWidth(); 88 int right = parseRegionLength(getAttribute(RIGHT_ATTRIBUTE_NAME), true); 89 int width = parseRegionLength(getAttribute(WIDTH_ATTRIBUTE_NAME), true); 90 return bbw - right - width; 91 } catch (NumberFormatException _) { 92 if (LOCAL_LOGV) { 93 Log.v(TAG, "Right or width attribute is not set or incorrect."); 94 } 95 } 96 return 0; 97 } 98 getTop()99 public int getTop() { 100 try { 101 return parseRegionLength(getAttribute(TOP_ATTRIBUTE_NAME), false); 102 } catch (NumberFormatException _) { 103 if (LOCAL_LOGV) { 104 Log.v(TAG, "Top attribute is not set or incorrect."); 105 } 106 } 107 try { 108 int bbh = ((SMILDocument) getOwnerDocument()).getLayout().getRootLayout().getHeight(); 109 int bottom = parseRegionLength(getAttribute(BOTTOM_ATTRIBUTE_NAME), false); 110 int height = parseRegionLength(getAttribute(HEIGHT_ATTRIBUTE_NAME), false); 111 return bbh - bottom - height; 112 } catch (NumberFormatException _) { 113 if (LOCAL_LOGV) { 114 Log.v(TAG, "Bottom or height attribute is not set or incorrect."); 115 } 116 } 117 return 0; 118 } 119 getZIndex()120 public int getZIndex() { 121 try { 122 return Integer.parseInt(this.getAttribute(Z_INDEX_ATTRIBUTE_NAME)); 123 } catch (NumberFormatException _) { 124 return 0; 125 } 126 } 127 setFit(String fit)128 public void setFit(String fit) throws DOMException { 129 if (fit.equalsIgnoreCase(FILL_ATTRIBUTE) 130 || fit.equalsIgnoreCase(MEET_ATTRIBUTE) 131 || fit.equalsIgnoreCase(SCROLL_ATTRIBUTE) 132 || fit.equalsIgnoreCase(SLICE_ATTRIBUTE)) { 133 this.setAttribute(FIT_ATTRIBUTE_NAME, fit.toLowerCase()); 134 } else { 135 this.setAttribute(FIT_ATTRIBUTE_NAME, HIDDEN_ATTRIBUTE); 136 } 137 } 138 setLeft(int left)139 public void setLeft(int left) throws DOMException { 140 this.setAttribute(LEFT_ATTRIBUTE_NAME, String.valueOf(left)); 141 } 142 setTop(int top)143 public void setTop(int top) throws DOMException { 144 this.setAttribute(TOP_ATTRIBUTE_NAME, String.valueOf(top)); 145 } 146 setZIndex(int zIndex)147 public void setZIndex(int zIndex) throws DOMException { 148 if (zIndex > 0) { 149 this.setAttribute(Z_INDEX_ATTRIBUTE_NAME, Integer.toString(zIndex)); 150 } else { 151 this.setAttribute(Z_INDEX_ATTRIBUTE_NAME, Integer.toString(0)); 152 } 153 } 154 getBackgroundColor()155 public String getBackgroundColor() { 156 return this.getAttribute(BACKGROUND_COLOR_ATTRIBUTE_NAME); 157 } 158 getHeight()159 public int getHeight() { 160 try { 161 final int height = parseRegionLength(getAttribute(HEIGHT_ATTRIBUTE_NAME), false); 162 return height == 0 ? 163 ((SMILDocument) getOwnerDocument()).getLayout().getRootLayout().getHeight() : 164 height; 165 } catch (NumberFormatException _) { 166 if (LOCAL_LOGV) { 167 Log.v(TAG, "Height attribute is not set or incorrect."); 168 } 169 } 170 int bbh = ((SMILDocument) getOwnerDocument()).getLayout().getRootLayout().getHeight(); 171 try { 172 bbh -= parseRegionLength(getAttribute(TOP_ATTRIBUTE_NAME), false); 173 } catch (NumberFormatException _) { 174 if (LOCAL_LOGV) { 175 Log.v(TAG, "Top attribute is not set or incorrect."); 176 } 177 } 178 try { 179 bbh -= parseRegionLength(getAttribute(BOTTOM_ATTRIBUTE_NAME), false); 180 } catch (NumberFormatException _) { 181 if (LOCAL_LOGV) { 182 Log.v(TAG, "Bottom attribute is not set or incorrect."); 183 } 184 } 185 return bbh; 186 } 187 getTitle()188 public String getTitle() { 189 return this.getAttribute(TITLE_ATTRIBUTE_NAME); 190 } 191 getWidth()192 public int getWidth() { 193 try { 194 final int width = parseRegionLength(getAttribute(WIDTH_ATTRIBUTE_NAME), true); 195 return width == 0 ? 196 ((SMILDocument) getOwnerDocument()).getLayout().getRootLayout().getWidth() : 197 width; 198 } catch (NumberFormatException _) { 199 if (LOCAL_LOGV) { 200 Log.v(TAG, "Width attribute is not set or incorrect."); 201 } 202 } 203 int bbw = ((SMILDocument) getOwnerDocument()).getLayout().getRootLayout().getWidth(); 204 try { 205 bbw -= parseRegionLength(getAttribute(LEFT_ATTRIBUTE_NAME), true); 206 } catch (NumberFormatException _) { 207 if (LOCAL_LOGV) { 208 Log.v(TAG, "Left attribute is not set or incorrect."); 209 } 210 } 211 try { 212 bbw -= parseRegionLength(getAttribute(RIGHT_ATTRIBUTE_NAME), true); 213 } catch (NumberFormatException _) { 214 if (LOCAL_LOGV) { 215 Log.v(TAG, "Right attribute is not set or incorrect."); 216 } 217 } 218 return bbw; 219 } 220 setBackgroundColor(String backgroundColor)221 public void setBackgroundColor(String backgroundColor) throws DOMException { 222 this.setAttribute(BACKGROUND_COLOR_ATTRIBUTE_NAME, backgroundColor); 223 } 224 setHeight(int height)225 public void setHeight(int height) throws DOMException { 226 this.setAttribute(HEIGHT_ATTRIBUTE_NAME, String.valueOf(height) + "px"); 227 } 228 setTitle(String title)229 public void setTitle(String title) throws DOMException { 230 this.setAttribute(TITLE_ATTRIBUTE_NAME, title); 231 } 232 setWidth(int width)233 public void setWidth(int width) throws DOMException { 234 this.setAttribute(WIDTH_ATTRIBUTE_NAME, String.valueOf(width) + "px"); 235 } 236 237 /* 238 * SMILElement Interface 239 */ 240 241 @Override getId()242 public String getId() { 243 return this.getAttribute(ID_ATTRIBUTE_NAME); 244 } 245 246 @Override setId(String id)247 public void setId(String id) throws DOMException { 248 this.setAttribute(ID_ATTRIBUTE_NAME, id); 249 } 250 251 /* 252 * Internal Interface 253 */ 254 parseRegionLength(String length, boolean horizontal)255 private int parseRegionLength(String length, boolean horizontal) { 256 if (length.endsWith("px")) { 257 length = length.substring(0, length.indexOf("px")); 258 return Integer.parseInt(length); 259 } else if (length.endsWith("%")) { 260 double value = 0.01*Integer.parseInt(length.substring(0, length.length() - 1)); 261 if (horizontal) { 262 value *= ((SMILDocument) getOwnerDocument()).getLayout().getRootLayout().getWidth(); 263 } else { 264 value *= ((SMILDocument) getOwnerDocument()).getLayout().getRootLayout().getHeight(); 265 } 266 return (int) Math.round(value); 267 } else { 268 return Integer.parseInt(length); 269 } 270 } 271 272 /* 273 * (non-Javadoc) 274 * @see java.lang.Object#toString() 275 */ 276 @Override toString()277 public String toString() { 278 return super.toString() 279 + ": id=" + getId() 280 + ", width=" + getWidth() 281 + ", height=" + getHeight() 282 + ", left=" + getLeft() 283 + ", top=" + getTop(); 284 } 285 } 286