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