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 Screen Dimension. 29 */ 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; 33 34 private final static Pattern sDimensionPattern = Pattern.compile( 35 "^(\\d+)x(\\d+)$"); //$NON-NLS-1$ 36 37 public static final String NAME = "Screen Dimension"; 38 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; 44 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; 50 ScreenDimensionQualifier()51 public ScreenDimensionQualifier() { 52 // pass 53 } 54 ScreenDimensionQualifier(int value1, int value2)55 public ScreenDimensionQualifier(int value1, int value2) { 56 mValue1 = value1; 57 mValue2 = value2; 58 } 59 getValue1()60 public int getValue1() { 61 return mValue1; 62 } 63 getValue2()64 public int getValue2() { 65 return mValue2; 66 } 67 68 @Override getName()69 public String getName() { 70 return NAME; 71 } 72 73 @Override getShortName()74 public String getShortName() { 75 return "Dimension"; 76 } 77 78 @Override getIcon()79 public Image getIcon() { 80 return IconFactory.getInstance().getIcon("dimension"); //$NON-NLS-1$ 81 } 82 83 @Override isValid()84 public boolean isValid() { 85 return mValue1 != DEFAULT_SIZE && mValue2 != DEFAULT_SIZE; 86 } 87 88 @Override checkAndSet(String value, FolderConfiguration config)89 public boolean checkAndSet(String value, FolderConfiguration config) { 90 Matcher m = sDimensionPattern.matcher(value); 91 if (m.matches()) { 92 String d1 = m.group(1); 93 String d2 = m.group(2); 94 95 ScreenDimensionQualifier qualifier = getQualifier(d1, d2); 96 if (qualifier != null) { 97 config.setScreenDimensionQualifier(qualifier); 98 return true; 99 } 100 } 101 return false; 102 } 103 104 @Override equals(Object 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); 109 } 110 111 return false; 112 } 113 114 @Override hashCode()115 public int hashCode() { 116 return toString().hashCode(); 117 } 118 getQualifier(String size1, String size2)119 public static ScreenDimensionQualifier getQualifier(String size1, String size2) { 120 try { 121 int s1 = Integer.parseInt(size1); 122 int s2 = Integer.parseInt(size2); 123 124 ScreenDimensionQualifier qualifier = new ScreenDimensionQualifier(); 125 126 if (s1 > s2) { 127 qualifier.mValue1 = s1; 128 qualifier.mValue2 = s2; 129 } else { 130 qualifier.mValue1 = s2; 131 qualifier.mValue2 = s1; 132 } 133 134 return qualifier; 135 } catch (NumberFormatException e) { 136 // looks like the string we extracted wasn't a valid number. 137 } 138 139 return null; 140 } 141 142 /** 143 * Returns the string used to represent this qualifier in the folder name. 144 */ 145 @Override getFolderSegment(IAndroidTarget target)146 public String getFolderSegment(IAndroidTarget target) { 147 return String.format("%1$dx%2$d", mValue1, mValue2); //$NON-NLS-1$ 148 } 149 150 @Override getStringValue()151 public String getStringValue() { 152 if (mValue1 != -1 && mValue2 != -1) { 153 return String.format("%1$dx%2$d", mValue1, mValue2); 154 } 155 156 return ""; //$NON-NLS-1$ 157 } 158 } 159