1 /*
2  * Copyright (C) 2012 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.configuration;
18 
19 import static com.android.ide.common.resources.configuration.LocaleQualifier.FAKE_VALUE;
20 
21 import com.android.annotations.NonNull;
22 import com.android.annotations.Nullable;
23 import com.android.ide.common.resources.configuration.FolderConfiguration;
24 import com.android.ide.common.resources.configuration.LocaleQualifier;
25 
26 import org.eclipse.swt.graphics.Image;
27 
28 /**
29  * A language,region pair
30  */
31 public class Locale {
32     /**
33      * A special marker region qualifier representing any region
34      */
35     public static final LocaleQualifier ANY_QUALIFIER = new LocaleQualifier(FAKE_VALUE);
36 
37     /**
38      * A locale which matches any language and region
39      */
40     public static final Locale ANY = new Locale(ANY_QUALIFIER);
41 
42     /**
43      * The locale qualifier, or {@link #ANY_QUALIFIER} if this locale matches
44      * any locale
45      */
46     @NonNull
47     public final LocaleQualifier qualifier;
48 
49     /**
50      * Constructs a new {@linkplain Locale} matching a given language in a given
51      * locale.
52      *
53      * @param locale the locale
54      */
Locale(@onNull LocaleQualifier locale)55     private Locale(@NonNull
56     LocaleQualifier locale) {
57         qualifier = locale;
58     }
59 
60     /**
61      * Constructs a new {@linkplain Locale} matching a given language in a given
62      * specific locale.
63      *
64      * @param locale the locale
65      * @return a locale with the given locale
66      */
67     @NonNull
create(@onNull LocaleQualifier locale)68     public static Locale create(@NonNull
69     LocaleQualifier locale) {
70         return new Locale(locale);
71     }
72 
73     /**
74      * Constructs a new {@linkplain Locale} for the given folder configuration
75      *
76      * @param folder the folder configuration
77      * @return a locale with the given language and region
78      */
create(FolderConfiguration folder)79     public static Locale create(FolderConfiguration folder) {
80         LocaleQualifier locale = folder.getLocaleQualifier();
81         if (locale == null) {
82             return ANY;
83         } else {
84             return new Locale(locale);
85         }
86     }
87 
88     /**
89      * Constructs a new {@linkplain Locale} for the given locale string, e.g.
90      * "zh", "en-rUS", or "b+eng+US".
91      *
92      * @param localeString the locale description
93      * @return the corresponding locale
94      */
95     @NonNull
create(@onNull String localeString)96     public static Locale create(@NonNull
97     String localeString) {
98         // Load locale. Note that this can get overwritten by the
99         // project-wide settings read below.
100 
101         LocaleQualifier qualifier = LocaleQualifier.getQualifier(localeString);
102         if (qualifier != null) {
103             return new Locale(qualifier);
104         } else {
105             return ANY;
106         }
107     }
108 
109     /**
110      * Returns a flag image to use for this locale
111      *
112      * @return a flag image, or a default globe icon
113      */
114     @NonNull
getFlagImage()115     public Image getFlagImage() {
116         String languageCode = qualifier.hasLanguage() ? qualifier.getLanguage() : null;
117         if (languageCode == null) {
118             return FlagManager.getGlobeIcon();
119         }
120         String regionCode = hasRegion() ? qualifier.getRegion() : null;
121         FlagManager icons = FlagManager.get();
122         Image image = icons.getFlag(languageCode, regionCode);
123         if (image != null) {
124             return image;
125         } else {
126             return FlagManager.getGlobeIcon();
127         }
128     }
129 
130     /**
131      * Returns true if this locale specifies a specific language. This is true
132      * for all locales except {@link #ANY}.
133      *
134      * @return true if this locale specifies a specific language
135      */
hasLanguage()136     public boolean hasLanguage() {
137         return !qualifier.hasFakeValue();
138     }
139 
140     /**
141      * Returns true if this locale specifies a specific region
142      *
143      * @return true if this locale specifies a region
144      */
hasRegion()145     public boolean hasRegion() {
146         return qualifier.getRegion() != null && !FAKE_VALUE.equals(qualifier.getRegion());
147     }
148 
149     /**
150      * Returns the locale formatted as language-region. If region is not set,
151      * language is returned. If language is not set, empty string is returned.
152      */
toLocaleId()153     public String toLocaleId() {
154         return qualifier == ANY_QUALIFIER ? "" : qualifier.getTag();
155     }
156 
157     @Override
hashCode()158     public int hashCode() {
159         final int prime = 31;
160         int result = 1;
161         result = prime * result + qualifier.hashCode();
162         return result;
163     }
164 
165     @Override
equals(@ullable Object obj)166     public boolean equals(@Nullable
167     Object obj) {
168         if (this == obj)
169             return true;
170         if (obj == null)
171             return false;
172         if (getClass() != obj.getClass())
173             return false;
174         Locale other = (Locale) obj;
175         if (!qualifier.equals(other.qualifier))
176             return false;
177         return true;
178     }
179 
180     @Override
toString()181     public String toString() {
182         return qualifier.getTag();
183     }
184 }
185