Home | History | Annotate | Download | only in layout
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
      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 package com.android.ide.common.layout;
     18 
     19 import static com.android.ide.common.layout.LayoutConstants.ANDROID_URI;
     20 import static com.android.ide.common.layout.LayoutConstants.ATTR_ID;
     21 import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_HEIGHT;
     22 import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_WIDTH;
     23 import static com.android.ide.common.layout.LayoutConstants.ATTR_ORIENTATION;
     24 import static com.android.ide.common.layout.LayoutConstants.FQCN_FRAME_LAYOUT;
     25 import static com.android.ide.common.layout.LayoutConstants.FQCN_LINEAR_LAYOUT;
     26 import static com.android.ide.common.layout.LayoutConstants.FQCN_TAB_WIDGET;
     27 import static com.android.ide.common.layout.LayoutConstants.VALUE_VERTICAL;
     28 import static com.android.ide.common.layout.LayoutConstants.VALUE_WRAP_CONTENT;
     29 
     30 import com.android.ide.common.api.INode;
     31 import com.android.ide.common.api.IViewRule;
     32 import com.android.ide.common.api.InsertType;
     33 
     34 /**
     35  * An {@link IViewRule} for android.widget.TabHost.
     36  */
     37 public class TabHostRule extends IgnoredLayoutRule {
     38     // The TabHost layout states in its documentation that you typically
     39     // manipulate its children via the TabHost rather than directly manipulating
     40     // the child elements yourself, e.g. via addTab() etc.
     41 
     42     @Override
     43     public void onCreate(INode node, INode parent, InsertType insertType) {
     44         super.onCreate(node, parent, insertType);
     45 
     46         if (insertType.isCreate()) {
     47             String fillParent = getFillParentValueName();
     48 
     49             // Configure default Table setup as described in the Table tutorial
     50             node.setAttribute(ANDROID_URI, ATTR_ID, "@android:id/tabhost"); //$NON-NLS-1$
     51             node.setAttribute(ANDROID_URI, ATTR_LAYOUT_WIDTH, fillParent);
     52             node.setAttribute(ANDROID_URI, ATTR_LAYOUT_HEIGHT, fillParent);
     53 
     54             INode linear = node.appendChild(FQCN_LINEAR_LAYOUT);
     55             linear.setAttribute(ANDROID_URI, ATTR_LAYOUT_WIDTH, fillParent);
     56             linear.setAttribute(ANDROID_URI, ATTR_LAYOUT_HEIGHT, fillParent);
     57             linear.setAttribute(ANDROID_URI, ATTR_ORIENTATION,
     58                     VALUE_VERTICAL);
     59 
     60             INode tab = linear.appendChild(FQCN_TAB_WIDGET);
     61             tab.setAttribute(ANDROID_URI, ATTR_LAYOUT_WIDTH, fillParent);
     62             tab.setAttribute(ANDROID_URI, ATTR_LAYOUT_HEIGHT, VALUE_WRAP_CONTENT);
     63             tab.setAttribute(ANDROID_URI, ATTR_ID, "@android:id/tabs"); //$NON-NLS-1$
     64 
     65             INode frame = linear.appendChild(FQCN_FRAME_LAYOUT);
     66             frame.setAttribute(ANDROID_URI, ATTR_LAYOUT_WIDTH, fillParent);
     67             frame.setAttribute(ANDROID_URI, ATTR_LAYOUT_HEIGHT, fillParent);
     68             frame.setAttribute(ANDROID_URI, ATTR_ID, "@android:id/tabcontent"); //$NON-NLS-1$
     69 
     70             for (int i = 0; i < 3; i++) {
     71                 INode child = frame.appendChild(FQCN_LINEAR_LAYOUT);
     72                 child.setAttribute(ANDROID_URI, ATTR_LAYOUT_WIDTH, fillParent);
     73                 child.setAttribute(ANDROID_URI, ATTR_LAYOUT_HEIGHT, fillParent);
     74                 child.setAttribute(ANDROID_URI, ATTR_ID,
     75                         String.format("@+id/tab%d", i + 1)); //$NON-NLS-1$
     76             }
     77         }
     78     }
     79 
     80 }
     81