Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
      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.sdkuilib.ui;
     18 
     19 import org.eclipse.swt.SWT;
     20 import org.eclipse.swt.layout.GridData;
     21 import org.eclipse.swt.widgets.Control;
     22 
     23 /**
     24  * A little helper to create a new {@link GridData} and set its properties.
     25  * <p/>
     26  * Example of usage: <br/>
     27  * <code>
     28  *   GridDataHelper.create(myControl).hSpan(2).hAlignCenter().fill();
     29  * </code>
     30  */
     31 public final class GridDataBuilder {
     32 
     33     private GridData mGD;
     34 
     35     private GridDataBuilder() {
     36         mGD = new GridData();
     37     }
     38 
     39     /**
     40      * Creates new {@link GridData} and associates it on the <code>control</code> composite.
     41      */
     42     static public GridDataBuilder create(Control control) {
     43         GridDataBuilder gdh = new GridDataBuilder();
     44         control.setLayoutData(gdh.mGD);
     45         return gdh;
     46     }
     47 
     48     /** Sets <code>widthHint</code> to <code>w</code>. */
     49     public GridDataBuilder wHint(int w) {
     50         mGD.widthHint = w;
     51         return this;
     52     }
     53 
     54     /** Sets <code>heightHint</code> to <code>h</code>. */
     55     public GridDataBuilder hHint(int h) {
     56         mGD.heightHint = h;
     57         return this;
     58     }
     59 
     60     /** Sets <code>horizontalIndent</code> to <code>h</code>. */
     61     public GridDataBuilder hIndent(int h) {
     62         mGD.horizontalIndent = h;
     63         return this;
     64     }
     65 
     66     /** Sets <code>horizontalSpan</code> to <code>h</code>. */
     67     public GridDataBuilder hSpan(int h) {
     68         mGD.horizontalSpan = h;
     69         return this;
     70     }
     71 
     72     /** Sets <code>verticalSpan</code> to <code>v</code>. */
     73     public GridDataBuilder vSpan(int v) {
     74         mGD.verticalSpan = v;
     75         return this;
     76     }
     77 
     78     /** Sets <code>horizontalAlignment</code> to {@link SWT#CENTER}. */
     79     public GridDataBuilder hCenter() {
     80         mGD.horizontalAlignment = SWT.CENTER;
     81         return this;
     82     }
     83 
     84     /** Sets <code>verticalAlignment</code> to {@link SWT#CENTER}. */
     85     public GridDataBuilder vCenter() {
     86         mGD.verticalAlignment = SWT.CENTER;
     87         return this;
     88     }
     89 
     90     /** Sets <code>verticalAlignment</code> to {@link SWT#TOP}. */
     91     public GridDataBuilder vTop() {
     92         mGD.verticalAlignment = SWT.TOP;
     93         return this;
     94     }
     95 
     96     /** Sets <code>horizontalAlignment</code> to {@link GridData#FILL}. */
     97     public GridDataBuilder hFill() {
     98         mGD.horizontalAlignment = GridData.FILL;
     99         return this;
    100     }
    101 
    102     /** Sets <code>verticalAlignment</code> to {@link GridData#FILL}. */
    103     public GridDataBuilder vFill() {
    104         mGD.verticalAlignment = GridData.FILL;
    105         return this;
    106     }
    107 
    108     /**
    109      * Sets both <code>horizontalAlignment</code> and <code>verticalAlignment</code>
    110      * to {@link GridData#FILL}.
    111      */
    112     public GridDataBuilder fill() {
    113         mGD.horizontalAlignment = GridData.FILL;
    114         mGD.verticalAlignment = GridData.FILL;
    115         return this;
    116     }
    117 
    118     /** Sets <code>grabExcessHorizontalSpace</code> to true. */
    119     public GridDataBuilder hGrab() {
    120         mGD.grabExcessHorizontalSpace = true;
    121         return this;
    122     }
    123 
    124     /** Sets <code>grabExcessVerticalSpace</code> to true. */
    125     public GridDataBuilder vGrab() {
    126         mGD.grabExcessVerticalSpace = true;
    127         return this;
    128     }
    129 
    130     /**
    131      * Sets both <code>grabExcessHorizontalSpace</code> and
    132      * <code>grabExcessVerticalSpace</code> to true.
    133      */
    134     public GridDataBuilder grab() {
    135         mGD.grabExcessHorizontalSpace = true;
    136         mGD.grabExcessVerticalSpace = true;
    137         return this;
    138     }
    139 
    140 }
    141