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