1 /* 2 * Copyright (C) 2014 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.eclipse.adt.internal.editors.layout; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 22 import org.w3c.dom.Element; 23 24 import com.android.ide.common.rendering.api.ActionBarCallback; 25 import com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart; 26 import com.android.ide.eclipse.adt.internal.editors.manifest.ManifestInfo; 27 import com.android.ide.eclipse.adt.internal.editors.manifest.ManifestInfo.ActivityAttributes; 28 import com.google.common.base.Splitter; 29 import com.google.common.collect.Iterables; 30 31 import static com.android.SdkConstants.TOOLS_URI; 32 import static com.android.SdkConstants.VALUE_SPLIT_ACTION_BAR_WHEN_NARROW; 33 34 public class ActionBarHandler extends ActionBarCallback { 35 36 private final GraphicalEditorPart mEditor; 37 38 ActionBarHandler(GraphicalEditorPart editor) { 39 mEditor = editor; 40 } 41 42 @Override 43 public List<String> getMenuIdNames() { 44 String commaSeparatedMenus = getXmlAttribute(ATTR_MENU); 45 List<String> menus = new ArrayList<String>(); 46 Iterables.addAll(menus, Splitter.on(',').trimResults().omitEmptyStrings() 47 .split(commaSeparatedMenus)); 48 return menus; 49 } 50 51 @Override 52 public boolean getSplitActionBarWhenNarrow() { 53 ActivityAttributes attributes = getActivityAttributes(); 54 if (attributes != null) { 55 return VALUE_SPLIT_ACTION_BAR_WHEN_NARROW.equals(attributes.getUiOptions()); 56 } 57 return false; 58 } 59 60 @Override 61 public int getNavigationMode() { 62 String navMode = getXmlAttribute(ATTR_NAV_MODE); 63 if (navMode.equalsIgnoreCase(VALUE_NAV_MODE_TABS)) { 64 return NAVIGATION_MODE_TABS; 65 } 66 if (navMode.equalsIgnoreCase(VALUE_NAV_MODE_LIST)) { 67 return NAVIGATION_MODE_LIST; 68 } 69 return NAVIGATION_MODE_STANDARD; 70 } 71 72 @Override 73 public HomeButtonStyle getHomeButtonStyle() { 74 ActivityAttributes attributes = getActivityAttributes(); 75 if (attributes != null && attributes.getParentActivity() != null) { 76 return HomeButtonStyle.SHOW_HOME_AS_UP; 77 } 78 return HomeButtonStyle.NONE; 79 } 80 81 private ActivityAttributes getActivityAttributes() { 82 ManifestInfo manifest = ManifestInfo.get(mEditor.getProject()); 83 String activity = mEditor.getConfigurationChooser().getConfiguration().getActivity(); 84 return manifest.getActivityAttributes(activity); 85 } 86 87 private String getXmlAttribute(String name) { 88 Element element = mEditor.getModel().getUiRoot().getXmlDocument().getDocumentElement(); 89 String value = element.getAttributeNS(TOOLS_URI, name); 90 if (value == null) { 91 return ""; 92 } 93 return value.trim(); 94 } 95 } 96