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