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