1 /* 2 * Copyright (C) 2009 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.resources.configurations; 18 19 import com.android.ide.eclipse.adt.internal.editors.IconFactory; 20 import com.android.sdklib.AndroidVersion; 21 import com.android.sdklib.IAndroidTarget; 22 23 import org.eclipse.swt.graphics.Image; 24 25 public class ScreenRatioQualifier extends ResourceQualifier { 26 27 public static final String NAME = "Screen Ratio"; 28 29 private ScreenRatio mValue = null; 30 31 /** 32 * Screen Orientation enum. 33 */ 34 public static enum ScreenRatio { 35 NOTLONG("notlong", "Not Long"), //$NON-NLS-1$ 36 LONG("long", "Long"); //$NON-NLS-1$ 37 38 private String mValue; 39 private String mDisplayValue; 40 ScreenRatio(String value, String displayValue)41 private ScreenRatio(String value, String displayValue) { 42 mValue = value; 43 mDisplayValue = displayValue; 44 } 45 46 /** 47 * Returns the enum for matching the provided qualifier value. 48 * @param value The qualifier value. 49 * @return the enum for the qualifier value or null if no matching was found. 50 */ getEnum(String value)51 public static ScreenRatio getEnum(String value) { 52 for (ScreenRatio orient : values()) { 53 if (orient.mValue.equals(value)) { 54 return orient; 55 } 56 } 57 58 return null; 59 } 60 getValue()61 public String getValue() { 62 return mValue; 63 } 64 getDisplayValue()65 public String getDisplayValue() { 66 return mDisplayValue; 67 } 68 getIndex(ScreenRatio orientation)69 public static int getIndex(ScreenRatio orientation) { 70 int i = 0; 71 for (ScreenRatio orient : values()) { 72 if (orient == orientation) { 73 return i; 74 } 75 76 i++; 77 } 78 79 return -1; 80 } 81 getByIndex(int index)82 public static ScreenRatio getByIndex(int index) { 83 int i = 0; 84 for (ScreenRatio orient : values()) { 85 if (i == index) { 86 return orient; 87 } 88 i++; 89 } 90 91 return null; 92 } 93 } 94 ScreenRatioQualifier()95 public ScreenRatioQualifier() { 96 } 97 ScreenRatioQualifier(ScreenRatio value)98 public ScreenRatioQualifier(ScreenRatio value) { 99 mValue = value; 100 } 101 getValue()102 public ScreenRatio getValue() { 103 return mValue; 104 } 105 106 @Override getName()107 public String getName() { 108 return NAME; 109 } 110 111 @Override getShortName()112 public String getShortName() { 113 return "Ratio"; 114 } 115 116 @Override getIcon()117 public Image getIcon() { 118 return IconFactory.getInstance().getIcon("ratio"); //$NON-NLS-1$ 119 } 120 121 @Override isValid()122 public boolean isValid() { 123 return mValue != null; 124 } 125 126 @Override checkAndSet(String value, FolderConfiguration config)127 public boolean checkAndSet(String value, FolderConfiguration config) { 128 ScreenRatio size = ScreenRatio.getEnum(value); 129 if (size != null) { 130 ScreenRatioQualifier qualifier = new ScreenRatioQualifier(size); 131 config.setScreenRatioQualifier(qualifier); 132 return true; 133 } 134 135 return false; 136 } 137 138 @Override equals(Object qualifier)139 public boolean equals(Object qualifier) { 140 if (qualifier instanceof ScreenRatioQualifier) { 141 return mValue == ((ScreenRatioQualifier)qualifier).mValue; 142 } 143 144 return false; 145 } 146 147 @Override hashCode()148 public int hashCode() { 149 if (mValue != null) { 150 return mValue.hashCode(); 151 } 152 153 return 0; 154 } 155 156 /** 157 * Returns the string used to represent this qualifier in the folder name. 158 */ 159 @Override getFolderSegment(IAndroidTarget target)160 public String getFolderSegment(IAndroidTarget target) { 161 if (mValue != null) { 162 if (target == null) { 163 // Default behavior (when target==null) is qualifier is supported 164 return mValue.getValue(); 165 } 166 167 AndroidVersion version = target.getVersion(); 168 if (version.getApiLevel() >= 4 || 169 (version.getApiLevel() == 3 && "Donut".equals(version.getCodename()))) { 170 return mValue.getValue(); 171 } 172 } 173 174 return ""; //$NON-NLS-1$ 175 } 176 177 @Override getStringValue()178 public String getStringValue() { 179 if (mValue != null) { 180 return mValue.getDisplayValue(); 181 } 182 183 return ""; //$NON-NLS-1$ 184 } 185 } 186