OSDN Git Service

android-2.1_r1 snapshot
[android-x86/sdk.git] / eclipse / plugins / com.android.ide.eclipse.adt / src / com / android / ide / eclipse / adt / internal / resources / configurations / RegionQualifier.java
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 import com.android.sdklib.IAndroidTarget;
21
22 import org.eclipse.swt.graphics.Image;
23
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 /**
28  * Resource Qualifier for Region.
29  */
30 public final class RegionQualifier extends ResourceQualifier {
31     private final static Pattern sRegionPattern = Pattern.compile("^r([A-Z]{2})$"); //$NON-NLS-1$
32
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 checkAndSet(String value, FolderConfiguration config) {
108         RegionQualifier qualifier = getQualifier(value);
109         if (qualifier != null) {
110             config.setRegionQualifier(qualifier);
111             return true;
112         }
113
114         return false;
115     }
116
117     @Override
118     public boolean equals(Object qualifier) {
119         if (qualifier instanceof RegionQualifier) {
120             if (mValue == null) {
121                 return ((RegionQualifier)qualifier).mValue == null;
122             }
123             return mValue.equals(((RegionQualifier)qualifier).mValue);
124         }
125
126         return false;
127     }
128
129     @Override
130     public int hashCode() {
131         if (mValue != null) {
132             return mValue.hashCode();
133         }
134
135         return 0;
136     }
137
138     /**
139      * Returns the string used to represent this qualifier in the folder name.
140      */
141     @Override
142     public String getFolderSegment(IAndroidTarget target) {
143         return getFolderSegment(mValue);
144     }
145
146     @Override
147     public String getStringValue() {
148         if (mValue != null) {
149             return mValue;
150         }
151
152         return ""; //$NON-NLS-1$
153     }
154 }