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 import java.util.regex.Matcher; 26 import java.util.regex.Pattern; 27 28 /** 29 * Resource Qualifier for Platform Version. 30 */ 31 public final class VersionQualifier extends ResourceQualifier { 32 /** Default pixel density value. This means the property is not set. */ 33 private final static int DEFAULT_VERSION = -1; 34 35 private final static Pattern sCountryCodePattern = Pattern.compile("^v(\\d+)$");//$NON-NLS-1$ 36 37 private int mVersion = DEFAULT_VERSION; 38 39 public static final String NAME = "Platform Version"; 40 41 /** 42 * Creates and returns a qualifier from the given folder segment. If the segment is incorrect, 43 * <code>null</code> is returned. 44 * @param segment the folder segment from which to create a qualifier. 45 * @return a new {@link VersionQualifier} object or <code>null</code> 46 */ getQualifier(String segment)47 public static VersionQualifier getQualifier(String segment) { 48 Matcher m = sCountryCodePattern.matcher(segment); 49 if (m.matches()) { 50 String v = m.group(1); 51 52 int code = -1; 53 try { 54 code = Integer.parseInt(v); 55 } catch (NumberFormatException e) { 56 // looks like the string we extracted wasn't a valid number. 57 return null; 58 } 59 60 VersionQualifier qualifier = new VersionQualifier(); 61 qualifier.mVersion = code; 62 return qualifier; 63 } 64 65 return null; 66 } 67 68 /** 69 * Returns the folder name segment for the given value. This is equivalent to calling 70 * {@link #toString()} on a {@link VersionQualifier} object. 71 * @param version the value of the qualifier, as returned by {@link #getVersion()}. 72 */ getFolderSegment(int version)73 public static String getFolderSegment(int version) { 74 if (version != DEFAULT_VERSION) { 75 return String.format("v%1$d", version); //$NON-NLS-1$ 76 } 77 78 return ""; //$NON-NLS-1$ 79 } 80 getVersion()81 public int getVersion() { 82 return mVersion; 83 } 84 85 @Override getName()86 public String getName() { 87 return NAME; 88 } 89 90 @Override getShortName()91 public String getShortName() { 92 return "Version"; 93 } 94 95 @Override getIcon()96 public Image getIcon() { 97 return IconFactory.getInstance().getIcon("version"); //$NON-NLS-1$ 98 } 99 100 @Override isValid()101 public boolean isValid() { 102 return mVersion != DEFAULT_VERSION; 103 } 104 105 @Override checkAndSet(String value, FolderConfiguration config)106 public boolean checkAndSet(String value, FolderConfiguration config) { 107 VersionQualifier qualifier = getQualifier(value); 108 if (qualifier != null) { 109 config.setVersionQualifier(qualifier); 110 return true; 111 } 112 113 return false; 114 } 115 116 @Override equals(Object qualifier)117 public boolean equals(Object qualifier) { 118 if (qualifier instanceof VersionQualifier) { 119 return mVersion == ((VersionQualifier)qualifier).mVersion; 120 } 121 122 return false; 123 } 124 125 @Override hashCode()126 public int hashCode() { 127 return mVersion; 128 } 129 130 /** 131 * Returns the string used to represent this qualifier in the folder name. 132 */ 133 @Override getFolderSegment(IAndroidTarget target)134 public String getFolderSegment(IAndroidTarget target) { 135 if (target == null) { 136 // Default behavior (when target==null) is qualifier is supported 137 return getFolderSegment(mVersion); 138 } 139 140 AndroidVersion version = target.getVersion(); 141 if (version.getApiLevel() >= 3) { 142 return getFolderSegment(mVersion); 143 } 144 145 return ""; //$NON-NLS-1$ 146 } 147 148 @Override getStringValue()149 public String getStringValue() { 150 if (mVersion != DEFAULT_VERSION) { 151 return String.format("API %1$d", mVersion); 152 } 153 154 return ""; //$NON-NLS-1$ 155 } 156 } 157