2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.eclipse.org/org/documents/epl-v10.php
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.
17 package com.android.ide.eclipse.adt.internal.resources.configurations;
19 import com.android.ide.eclipse.adt.internal.editors.IconFactory;
20 import com.android.sdklib.IAndroidTarget;
22 import org.eclipse.swt.graphics.Image;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
28 * Resource Qualifier for Screen Dimension.
30 public final class ScreenDimensionQualifier extends ResourceQualifier {
31 /** Default screen size value. This means the property is not set */
32 final static int DEFAULT_SIZE = -1;
34 private final static Pattern sDimensionPattern = Pattern.compile(
35 "^(\\d+)x(\\d+)$"); //$NON-NLS-1$
37 public static final String NAME = "Screen Dimension";
39 /** Screen size 1 value. This is not size X or Y because the folder name always
40 * contains the biggest size first. So if the qualifier is 400x200, size 1 will always be
41 * 400 but that'll be X in landscape and Y in portrait.
42 * Default value is <code>DEFAULT_SIZE</code> */
43 private int mValue1 = DEFAULT_SIZE;
45 /** Screen size 2 value. This is not size X or Y because the folder name always
46 * contains the biggest size first. So if the qualifier is 400x200, size 2 will always be
47 * 200 but that'll be Y in landscape and X in portrait.
48 * Default value is <code>DEFAULT_SIZE</code> */
49 private int mValue2 = DEFAULT_SIZE;
51 public ScreenDimensionQualifier() {
55 public ScreenDimensionQualifier(int value1, int value2) {
60 public int getValue1() {
64 public int getValue2() {
69 public String getName() {
74 public String getShortName() {
79 public Image getIcon() {
80 return IconFactory.getInstance().getIcon("dimension"); //$NON-NLS-1$
84 public boolean isValid() {
85 return mValue1 != DEFAULT_SIZE && mValue2 != DEFAULT_SIZE;
89 public boolean checkAndSet(String value, FolderConfiguration config) {
90 Matcher m = sDimensionPattern.matcher(value);
92 String d1 = m.group(1);
93 String d2 = m.group(2);
95 ScreenDimensionQualifier qualifier = getQualifier(d1, d2);
96 if (qualifier != null) {
97 config.setScreenDimensionQualifier(qualifier);
105 public boolean equals(Object qualifier) {
106 if (qualifier instanceof ScreenDimensionQualifier) {
107 ScreenDimensionQualifier q = (ScreenDimensionQualifier)qualifier;
108 return (mValue1 == q.mValue1 && mValue2 == q.mValue2);
115 public int hashCode() {
116 return toString().hashCode();
119 public static ScreenDimensionQualifier getQualifier(String size1, String size2) {
121 int s1 = Integer.parseInt(size1);
122 int s2 = Integer.parseInt(size2);
124 ScreenDimensionQualifier qualifier = new ScreenDimensionQualifier();
127 qualifier.mValue1 = s1;
128 qualifier.mValue2 = s2;
130 qualifier.mValue1 = s2;
131 qualifier.mValue2 = s1;
135 } catch (NumberFormatException e) {
136 // looks like the string we extracted wasn't a valid number.
143 * Returns the string used to represent this qualifier in the folder name.
146 public String getFolderSegment(IAndroidTarget target) {
147 return String.format("%1$dx%2$d", mValue1, mValue2); //$NON-NLS-1$
151 public String getStringValue() {
152 if (mValue1 != -1 && mValue2 != -1) {
153 return String.format("%1$dx%2$d", mValue1, mValue2);
156 return ""; //$NON-NLS-1$