• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
25  * Resource Qualifier for Screen Orientation.
26  */
27 public final class ScreenOrientationQualifier extends ResourceQualifier {
28 
29     public static final String NAME = "Screen Orientation";
30 
31     private ScreenOrientation mValue = null;
32 
33     /**
34      * Screen Orientation enum.
35      */
36     public static enum ScreenOrientation {
37         PORTRAIT("port", "Portrait"), //$NON-NLS-1$
38         LANDSCAPE("land", "Landscape"), //$NON-NLS-1$
39         SQUARE("square", "Square"); //$NON-NLS-1$
40 
41         private String mValue;
42         private String mDisplayValue;
43 
ScreenOrientation(String value, String displayValue)44         private ScreenOrientation(String value, String displayValue) {
45             mValue = value;
46             mDisplayValue = displayValue;
47         }
48 
49         /**
50          * Returns the enum for matching the provided qualifier value.
51          * @param value The qualifier value.
52          * @return the enum for the qualifier value or null if no matching was found.
53          */
getEnum(String value)54         public static ScreenOrientation getEnum(String value) {
55             for (ScreenOrientation orient : values()) {
56                 if (orient.mValue.equals(value)) {
57                     return orient;
58                 }
59             }
60 
61             return null;
62         }
63 
getValue()64         public String getValue() {
65             return mValue;
66         }
67 
getDisplayValue()68         public String getDisplayValue() {
69             return mDisplayValue;
70         }
71 
getIndex(ScreenOrientation orientation)72         public static int getIndex(ScreenOrientation orientation) {
73             int i = 0;
74             for (ScreenOrientation orient : values()) {
75                 if (orient == orientation) {
76                     return i;
77                 }
78 
79                 i++;
80             }
81 
82             return -1;
83         }
84 
getByIndex(int index)85         public static ScreenOrientation getByIndex(int index) {
86             int i = 0;
87             for (ScreenOrientation orient : values()) {
88                 if (i == index) {
89                     return orient;
90                 }
91                 i++;
92             }
93 
94             return null;
95         }
96     }
97 
ScreenOrientationQualifier()98     public ScreenOrientationQualifier() {
99     }
100 
ScreenOrientationQualifier(ScreenOrientation value)101     public ScreenOrientationQualifier(ScreenOrientation value) {
102         mValue = value;
103     }
104 
getValue()105     public ScreenOrientation getValue() {
106         return mValue;
107     }
108 
109     @Override
getName()110     public String getName() {
111         return NAME;
112     }
113 
114     @Override
getShortName()115     public String getShortName() {
116         return "Orientation";
117     }
118 
119     @Override
getIcon()120     public Image getIcon() {
121         return IconFactory.getInstance().getIcon("orientation"); //$NON-NLS-1$
122     }
123 
124     @Override
isValid()125     public boolean isValid() {
126         return mValue != null;
127     }
128 
129     @Override
checkAndSet(String value, FolderConfiguration config)130     public boolean checkAndSet(String value, FolderConfiguration config) {
131         ScreenOrientation orientation = ScreenOrientation.getEnum(value);
132         if (orientation != null) {
133             ScreenOrientationQualifier qualifier = new ScreenOrientationQualifier(orientation);
134             config.setScreenOrientationQualifier(qualifier);
135             return true;
136         }
137 
138         return false;
139     }
140 
141     @Override
equals(Object qualifier)142     public boolean equals(Object qualifier) {
143         if (qualifier instanceof ScreenOrientationQualifier) {
144             return mValue == ((ScreenOrientationQualifier)qualifier).mValue;
145         }
146 
147         return false;
148     }
149 
150     @Override
hashCode()151     public int hashCode() {
152         if (mValue != null) {
153             return mValue.hashCode();
154         }
155 
156         return 0;
157     }
158 
159     /**
160      * Returns the string used to represent this qualifier in the folder name.
161      */
162     @Override
getFolderSegment(IAndroidTarget target)163     public String getFolderSegment(IAndroidTarget target) {
164         if (mValue != null) {
165             return mValue.getValue();
166         }
167 
168         return ""; //$NON-NLS-1$
169     }
170 
171     @Override
getStringValue()172     public String getStringValue() {
173         if (mValue != null) {
174             return mValue.getDisplayValue();
175         }
176 
177         return ""; //$NON-NLS-1$
178     }
179 }
180