• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 
25 
26 /**
27  * Resource Qualifier for keyboard state.
28  */
29 public final class KeyboardStateQualifier extends ResourceQualifier {
30 
31     public static final String NAME = "Keyboard State";
32 
33     private KeyboardState mValue = null;
34 
35     /**
36      * Screen Orientation enum.
37      */
38     public static enum KeyboardState {
39         EXPOSED("keysexposed", "Exposed"), //$NON-NLS-1$
40         HIDDEN("keyshidden", "Hidden"),    //$NON-NLS-1$
41         SOFT("keyssoft", "Soft");          //$NON-NLS-1$
42 
43         private String mValue;
44         private String mDisplayValue;
45 
KeyboardState(String value, String displayValue)46         private KeyboardState(String value, String displayValue) {
47             mValue = value;
48             mDisplayValue = displayValue;
49         }
50 
51         /**
52          * Returns the enum for matching the provided qualifier value.
53          * @param value The qualifier value.
54          * @return the enum for the qualifier value or null if no matching was found.
55          */
getEnum(String value)56         public static KeyboardState getEnum(String value) {
57             for (KeyboardState orient : values()) {
58                 if (orient.mValue.equals(value)) {
59                     return orient;
60                 }
61             }
62 
63             return null;
64         }
65 
getValue()66         public String getValue() {
67             return mValue;
68         }
69 
getDisplayValue()70         public String getDisplayValue() {
71             return mDisplayValue;
72         }
73 
getIndex(KeyboardState value)74         public static int getIndex(KeyboardState value) {
75             int i = 0;
76             for (KeyboardState input : values()) {
77                 if (value == input) {
78                     return i;
79                 }
80 
81                 i++;
82             }
83 
84             return -1;
85         }
86 
getByIndex(int index)87         public static KeyboardState getByIndex(int index) {
88             int i = 0;
89             for (KeyboardState value : values()) {
90                 if (i == index) {
91                     return value;
92                 }
93                 i++;
94             }
95             return null;
96         }
97     }
98 
KeyboardStateQualifier()99     public KeyboardStateQualifier() {
100         // pass
101     }
102 
KeyboardStateQualifier(KeyboardState value)103     public KeyboardStateQualifier(KeyboardState value) {
104         mValue = value;
105     }
106 
getValue()107     public KeyboardState getValue() {
108         return mValue;
109     }
110 
111     @Override
getName()112     public String getName() {
113         return NAME;
114     }
115 
116     @Override
getShortName()117     public String getShortName() {
118         return "Keyboard";
119     }
120 
121     @Override
getIcon()122     public Image getIcon() {
123         return IconFactory.getInstance().getIcon("keyboard"); //$NON-NLS-1$
124     }
125 
126     @Override
isValid()127     public boolean isValid() {
128         return mValue != null;
129     }
130 
131     @Override
checkAndSet(String value, FolderConfiguration config)132     public boolean checkAndSet(String value, FolderConfiguration config) {
133         KeyboardState orientation = KeyboardState.getEnum(value);
134         if (orientation != null) {
135             KeyboardStateQualifier qualifier = new KeyboardStateQualifier();
136             qualifier.mValue = orientation;
137             config.setKeyboardStateQualifier(qualifier);
138             return true;
139         }
140 
141         return false;
142     }
143 
144     @Override
isMatchFor(ResourceQualifier qualifier)145     public boolean isMatchFor(ResourceQualifier qualifier) {
146         if (qualifier instanceof KeyboardStateQualifier) {
147             KeyboardStateQualifier referenceQualifier = (KeyboardStateQualifier)qualifier;
148 
149             // special case where EXPOSED can be used for SOFT
150             if (referenceQualifier.mValue == KeyboardState.SOFT &&
151                     mValue == KeyboardState.EXPOSED) {
152                 return true;
153             }
154 
155             return referenceQualifier.mValue == mValue;
156         }
157 
158         return false;
159     }
160 
161     @Override
isBetterMatchThan(ResourceQualifier compareTo, ResourceQualifier reference)162     public boolean isBetterMatchThan(ResourceQualifier compareTo, ResourceQualifier reference) {
163         if (compareTo == null) {
164             return true;
165         }
166 
167         KeyboardStateQualifier compareQualifier = (KeyboardStateQualifier)compareTo;
168         KeyboardStateQualifier referenceQualifier = (KeyboardStateQualifier)reference;
169         if (referenceQualifier.mValue == KeyboardState.SOFT) { // only case where there could be a
170                                                                // better qualifier
171             // only return true if it's a better value.
172             if (compareQualifier.mValue == KeyboardState.EXPOSED && mValue == KeyboardState.SOFT) {
173                 return true;
174             }
175         }
176 
177         return false;
178     }
179 
180     @Override
equals(Object qualifier)181     public boolean equals(Object qualifier) {
182         if (qualifier instanceof KeyboardStateQualifier) {
183             return mValue == ((KeyboardStateQualifier)qualifier).mValue;
184         }
185 
186         return false;
187     }
188 
189     @Override
hashCode()190     public int hashCode() {
191         if (mValue != null) {
192             return mValue.hashCode();
193         }
194 
195         return 0;
196     }
197 
198     /**
199      * Returns the string used to represent this qualifier in the folder name.
200      */
201     @Override
getFolderSegment(IAndroidTarget target)202     public String getFolderSegment(IAndroidTarget target) {
203         if (mValue != null) {
204             return mValue.getValue();
205         }
206 
207         return ""; //$NON-NLS-1$
208     }
209 
210     @Override
getStringValue()211     public String getStringValue() {
212         if (mValue != null) {
213             return mValue.getDisplayValue();
214         }
215 
216         return ""; //$NON-NLS-1$
217     }
218 }
219