Home | History | Annotate | Download | only in api
      1 /*
      2  * Copyright (C) 2011 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 package com.android.ide.common.api;
     17 
     18 /**
     19  * A {@link ResizePolicy} records state for whether a widget is resizable, and if so, in
     20  * which directions
     21  */
     22 public class ResizePolicy {
     23     private static final int NONE = 0;
     24     private static final int LEFT_EDGE = 1;
     25     private static final int RIGHT_EDGE = 2;
     26     private static final int TOP_EDGE = 4;
     27     private static final int BOTTOM_EDGE = 8;
     28     private static final int PRESERVE_RATIO = 16;
     29 
     30     // Aliases
     31     private static final int HORIZONTAL = LEFT_EDGE | RIGHT_EDGE;
     32     private static final int VERTICAL = TOP_EDGE | BOTTOM_EDGE;
     33     private static final int ANY = HORIZONTAL | VERTICAL;
     34 
     35     // Shared objects for common policies
     36 
     37     public static final ResizePolicy sAny = new ResizePolicy(ANY);
     38     private static final ResizePolicy sNone = new ResizePolicy(NONE);
     39     private static final ResizePolicy sHorizontal = new ResizePolicy(HORIZONTAL);
     40     private static final ResizePolicy sVertical = new ResizePolicy(VERTICAL);
     41     private static final ResizePolicy sScaled = new ResizePolicy(ANY | PRESERVE_RATIO);
     42 
     43     private final int mFlags;
     44 
     45 
     46     // Use factory methods to construct
     47     private ResizePolicy(int flags) {
     48         mFlags = flags;
     49     }
     50 
     51     /**
     52      * Returns true if this policy allows resizing in at least one direction
     53      *
     54      * @return true if this policy allows resizing in at least one direction
     55      */
     56     public boolean isResizable() {
     57         return (mFlags & ANY) != 0;
     58     }
     59 
     60     /**
     61      * Returns true if this policy allows resizing the top edge
     62      *
     63      * @return true if this policy allows resizing the top edge
     64      */
     65     public boolean topAllowed() {
     66         return (mFlags & TOP_EDGE) != 0;
     67     }
     68 
     69     /**
     70      * Returns true if this policy allows resizing the right edge
     71      *
     72      * @return true if this policy allows resizing the right edge
     73      */
     74     public boolean rightAllowed() {
     75         return (mFlags & RIGHT_EDGE) != 0;
     76     }
     77 
     78     /**
     79      * Returns true if this policy allows resizing the bottom edge
     80      *
     81      * @return true if this policy allows resizing the bottom edge
     82      */
     83     public boolean bottomAllowed() {
     84         return (mFlags & BOTTOM_EDGE) != 0;
     85     }
     86 
     87     /**
     88      * Returns true if this policy allows resizing the left edge
     89      *
     90      * @return true if this policy allows resizing the left edge
     91      */
     92     public boolean leftAllowed() {
     93         return (mFlags & LEFT_EDGE) != 0;
     94     }
     95 
     96     /**
     97      * Returns true if this policy requires resizing in an aspect-ratio preserving manner
     98      *
     99      * @return true if this policy requires resizing in an aspect-ratio preserving manner
    100      */
    101     public boolean isAspectPreserving() {
    102         return (mFlags & PRESERVE_RATIO) != 0;
    103     }
    104 
    105     /**
    106      * Returns a resize policy allowing resizing in any direction
    107      *
    108      * @return a resize policy allowing resizing in any direction
    109      */
    110     public static ResizePolicy full() {
    111         return sAny;
    112     }
    113 
    114     /**
    115      * Returns a resize policy not allowing any resizing
    116      *
    117      * @return a policy which does not allow any resizing
    118      */
    119     public static ResizePolicy none() {
    120         return sNone;
    121     }
    122 
    123     /**
    124      * Returns a resize policy allowing horizontal resizing only
    125      *
    126      * @return a policy which allows horizontal resizing only
    127      */
    128     public static ResizePolicy horizontal() {
    129         return sHorizontal;
    130     }
    131 
    132     /**
    133      * Returns a resize policy allowing vertical resizing only
    134      *
    135      * @return a policy which allows vertical resizing only
    136      */
    137     public static ResizePolicy vertical() {
    138         return sVertical;
    139     }
    140 
    141     /**
    142      * Returns a resize policy allowing scaled / aspect-ratio preserving resizing only
    143      *
    144      * @return a resize policy allowing scaled / aspect-ratio preserving resizing only
    145      */
    146     public static ResizePolicy scaled() {
    147         return sScaled;
    148     }
    149 
    150     /**
    151      * Returns a resize policy with the specified resizability along the edges and the
    152      * given aspect ratio behavior
    153      * @param top whether the top edge is resizable
    154      * @param right whether the right edge is resizable
    155      * @param bottom whether the bottom edge is resizable
    156      * @param left whether the left edge is resizable
    157      * @param preserve whether the policy requires the aspect ratio to be preserved
    158      * @return a resize policy recording the constraints required by the parameters
    159      */
    160     public static ResizePolicy create(boolean top, boolean right, boolean bottom, boolean left,
    161             boolean preserve) {
    162         int mask = NONE;
    163         if (top) mask |= TOP_EDGE;
    164         if (right) mask |= RIGHT_EDGE;
    165         if (bottom) mask |= BOTTOM_EDGE;
    166         if (left) mask |= LEFT_EDGE;
    167         if (preserve) mask |= PRESERVE_RATIO;
    168 
    169         return new ResizePolicy(mask);
    170     }
    171 }
    172