1 /* 2 * Copyright (C) 2007 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 21 import org.eclipse.swt.graphics.Image; 22 23 import java.util.regex.Matcher; 24 import java.util.regex.Pattern; 25 26 /** 27 * Resource Qualifier for Region. 28 */ 29 public final class RegionQualifier extends ResourceQualifier { 30 private final static Pattern sRegionPattern = Pattern.compile("^r([A-Z]{2})$"); //$NON-NLS-1$ 31 32 public static final String FAKE_REGION_VALUE = "__"; //$NON-NLS-1$ 33 public static final String NAME = "Region"; 34 35 private String mValue; 36 37 /** 38 * Creates and returns a qualifier from the given folder segment. If the segment is incorrect, 39 * <code>null</code> is returned. 40 * @param segment the folder segment from which to create a qualifier. 41 * @return a new {@link RegionQualifier} object or <code>null</code> 42 */ 43 public static RegionQualifier getQualifier(String segment) { 44 Matcher m = sRegionPattern.matcher(segment); 45 if (m.matches()) { 46 RegionQualifier qualifier = new RegionQualifier(); 47 qualifier.mValue = m.group(1); 48 49 return qualifier; 50 } 51 return null; 52 } 53 54 /** 55 * Returns the folder name segment for the given value. This is equivalent to calling 56 * {@link #toString()} on a {@link RegionQualifier} object. 57 * @param value the value of the qualifier, as returned by {@link #getValue()}. 58 */ 59 public static String getFolderSegment(String value) { 60 if (value != null) { 61 String segment = "r" + value.toUpperCase(); //$NON-NLS-1$ 62 if (sRegionPattern.matcher(segment).matches()) { 63 return segment; 64 } 65 } 66 67 return ""; //$NON-NLS-1$ 68 } 69 70 public RegionQualifier() { 71 72 } 73 74 public RegionQualifier(String value) { 75 mValue = value; 76 } 77 78 public String getValue() { 79 if (mValue != null) { 80 return mValue; 81 } 82 83 return ""; //$NON-NLS-1$ 84 } 85 86 @Override 87 public String getName() { 88 return NAME; 89 } 90 91 @Override 92 public String getShortName() { 93 return NAME; 94 } 95 96 @Override 97 public Image getIcon() { 98 return IconFactory.getInstance().getIcon("region"); //$NON-NLS-1$ 99 } 100 101 @Override 102 public boolean isValid() { 103 return mValue != null; 104 } 105 106 @Override 107 public boolean hasFakeValue() { 108 return FAKE_REGION_VALUE.equals(mValue); 109 } 110 111 @Override 112 public boolean checkAndSet(String value, FolderConfiguration config) { 113 RegionQualifier qualifier = getQualifier(value); 114 if (qualifier != null) { 115 config.setRegionQualifier(qualifier); 116 return true; 117 } 118 119 return false; 120 } 121 122 @Override 123 public boolean equals(Object qualifier) { 124 if (qualifier instanceof RegionQualifier) { 125 if (mValue == null) { 126 return ((RegionQualifier)qualifier).mValue == null; 127 } 128 return mValue.equals(((RegionQualifier)qualifier).mValue); 129 } 130 131 return false; 132 } 133 134 @Override 135 public int hashCode() { 136 if (mValue != null) { 137 return mValue.hashCode(); 138 } 139 140 return 0; 141 } 142 143 /** 144 * Returns the string used to represent this qualifier in the folder name. 145 */ 146 @Override 147 public String getFolderSegment() { 148 return getFolderSegment(mValue); 149 } 150 151 @Override 152 public String getShortDisplayValue() { 153 if (mValue != null) { 154 return mValue; 155 } 156 157 return ""; //$NON-NLS-1$ 158 } 159 160 @Override 161 public String getLongDisplayValue() { 162 if (mValue != null) { 163 return String.format("Region %s", mValue); 164 } 165 166 return ""; //$NON-NLS-1$ 167 } 168 } 169