Home | History | Annotate | Download | only in configurations
      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 
     21 import org.eclipse.swt.graphics.Image;
     22 
     23 import java.util.regex.Matcher;
     24 import java.util.regex.Pattern;
     25 
     26 /**
     27  * Resource Qualifier for Screen Dimension.
     28  */
     29 public final class ScreenDimensionQualifier extends ResourceQualifier {
     30     /** Default screen size value. This means the property is not set */
     31     final static int DEFAULT_SIZE = -1;
     32 
     33     private final static Pattern sDimensionPattern = Pattern.compile(
     34             "^(\\d+)x(\\d+)$"); //$NON-NLS-1$
     35 
     36     public static final String NAME = "Screen Dimension";
     37 
     38     /** Screen size 1 value. This is not size X or Y because the folder name always
     39      * contains the biggest size first. So if the qualifier is 400x200, size 1 will always be
     40      * 400 but that'll be X in landscape and Y in portrait.
     41      * Default value is <code>DEFAULT_SIZE</code> */
     42     private int mValue1 = DEFAULT_SIZE;
     43 
     44     /** Screen size 2 value. This is not size X or Y because the folder name always
     45      * contains the biggest size first. So if the qualifier is 400x200, size 2 will always be
     46      * 200 but that'll be Y in landscape and X in portrait.
     47      * Default value is <code>DEFAULT_SIZE</code> */
     48     private int mValue2 = DEFAULT_SIZE;
     49 
     50     public ScreenDimensionQualifier() {
     51         // pass
     52     }
     53 
     54     public ScreenDimensionQualifier(int value1, int value2) {
     55         mValue1 = value1;
     56         mValue2 = value2;
     57     }
     58 
     59     public int getValue1() {
     60         return mValue1;
     61     }
     62 
     63     public int getValue2() {
     64         return mValue2;
     65     }
     66 
     67     @Override
     68     public String getName() {
     69         return NAME;
     70     }
     71 
     72     @Override
     73     public String getShortName() {
     74         return "Dimension";
     75     }
     76 
     77     @Override
     78     public Image getIcon() {
     79         return IconFactory.getInstance().getIcon("dimension"); //$NON-NLS-1$
     80     }
     81 
     82     @Override
     83     public boolean isValid() {
     84         return mValue1 != DEFAULT_SIZE && mValue2 != DEFAULT_SIZE;
     85     }
     86 
     87     @Override
     88     public boolean hasFakeValue() {
     89         return false;
     90     }
     91 
     92     @Override
     93     public boolean checkAndSet(String value, FolderConfiguration config) {
     94         Matcher m = sDimensionPattern.matcher(value);
     95         if (m.matches()) {
     96             String d1 = m.group(1);
     97             String d2 = m.group(2);
     98 
     99             ScreenDimensionQualifier qualifier = getQualifier(d1, d2);
    100             if (qualifier != null) {
    101                 config.setScreenDimensionQualifier(qualifier);
    102                 return true;
    103             }
    104         }
    105         return false;
    106     }
    107 
    108     @Override
    109     public boolean equals(Object qualifier) {
    110         if (qualifier instanceof ScreenDimensionQualifier) {
    111             ScreenDimensionQualifier q = (ScreenDimensionQualifier)qualifier;
    112             return (mValue1 == q.mValue1 && mValue2 == q.mValue2);
    113         }
    114 
    115         return false;
    116     }
    117 
    118     @Override
    119     public int hashCode() {
    120         return toString().hashCode();
    121     }
    122 
    123     public static ScreenDimensionQualifier getQualifier(String size1, String size2) {
    124         try {
    125             int s1 = Integer.parseInt(size1);
    126             int s2 = Integer.parseInt(size2);
    127 
    128             ScreenDimensionQualifier qualifier = new ScreenDimensionQualifier();
    129 
    130             if (s1 > s2) {
    131                 qualifier.mValue1 = s1;
    132                 qualifier.mValue2 = s2;
    133             } else {
    134                 qualifier.mValue1 = s2;
    135                 qualifier.mValue2 = s1;
    136             }
    137 
    138             return qualifier;
    139         } catch (NumberFormatException e) {
    140             // looks like the string we extracted wasn't a valid number.
    141         }
    142 
    143         return null;
    144     }
    145 
    146     /**
    147      * Returns the string used to represent this qualifier in the folder name.
    148      */
    149     @Override
    150     public String getFolderSegment() {
    151         return String.format("%1$dx%2$d", mValue1, mValue2); //$NON-NLS-1$
    152     }
    153 
    154     @Override
    155     public String getShortDisplayValue() {
    156         if (mValue1 != -1 && mValue2 != -1) {
    157             return String.format("%1$dx%2$d", mValue1, mValue2);
    158         }
    159 
    160         return ""; //$NON-NLS-1$
    161     }
    162 
    163     @Override
    164     public String getLongDisplayValue() {
    165         if (mValue1 != -1 && mValue2 != -1) {
    166             return String.format("Screen resolution %1$dx%2$d", mValue1, mValue2);
    167         }
    168 
    169         return ""; //$NON-NLS-1$
    170     }
    171 }
    172