• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GridDataBuilder()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      */
create(Control control)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>. */
wHint(int w)49     public GridDataBuilder wHint(int w) {
50         mGD.widthHint = w;
51         return this;
52     }
53 
54     /** Sets <code>heightHint</code> to <code>h</code>. */
hHint(int h)55     public GridDataBuilder hHint(int h) {
56         mGD.heightHint = h;
57         return this;
58     }
59 
60     /** Sets <code>horizontalIndent</code> to <code>h</code>. */
hIndent(int h)61     public GridDataBuilder hIndent(int h) {
62         mGD.horizontalIndent = h;
63         return this;
64     }
65 
66     /** Sets <code>horizontalSpan</code> to <code>h</code>. */
hSpan(int h)67     public GridDataBuilder hSpan(int h) {
68         mGD.horizontalSpan = h;
69         return this;
70     }
71 
72     /** Sets <code>verticalSpan</code> to <code>v</code>. */
vSpan(int v)73     public GridDataBuilder vSpan(int v) {
74         mGD.verticalSpan = v;
75         return this;
76     }
77 
78     /** Sets <code>horizontalAlignment</code> to {@link SWT#CENTER}. */
hCenter()79     public GridDataBuilder hCenter() {
80         mGD.horizontalAlignment = SWT.CENTER;
81         return this;
82     }
83 
84     /** Sets <code>verticalAlignment</code> to {@link SWT#CENTER}. */
vCenter()85     public GridDataBuilder vCenter() {
86         mGD.verticalAlignment = SWT.CENTER;
87         return this;
88     }
89 
90     /** Sets <code>verticalAlignment</code> to {@link SWT#TOP}. */
vTop()91     public GridDataBuilder vTop() {
92         mGD.verticalAlignment = SWT.TOP;
93         return this;
94     }
95 
96     /** Sets <code>verticalAlignment</code> to {@link SWT#BOTTOM}. */
vBottom()97     public GridDataBuilder vBottom() {
98         mGD.verticalAlignment = SWT.BOTTOM;
99         return this;
100     }
101 
102     /** Sets <code>horizontalAlignment</code> to {@link SWT#LEFT}. */
hLeft()103     public GridDataBuilder hLeft() {
104         mGD.horizontalAlignment = SWT.LEFT;
105         return this;
106     }
107 
108     /** Sets <code>horizontalAlignment</code> to {@link SWT#RIGHT}. */
hRight()109     public GridDataBuilder hRight() {
110         mGD.horizontalAlignment = SWT.RIGHT;
111         return this;
112     }
113 
114     /** Sets <code>horizontalAlignment</code> to {@link GridData#FILL}. */
hFill()115     public GridDataBuilder hFill() {
116         mGD.horizontalAlignment = GridData.FILL;
117         return this;
118     }
119 
120     /** Sets <code>verticalAlignment</code> to {@link GridData#FILL}. */
vFill()121     public GridDataBuilder vFill() {
122         mGD.verticalAlignment = GridData.FILL;
123         return this;
124     }
125 
126     /**
127      * Sets both <code>horizontalAlignment</code> and <code>verticalAlignment</code>
128      * to {@link GridData#FILL}.
129      */
fill()130     public GridDataBuilder fill() {
131         mGD.horizontalAlignment = GridData.FILL;
132         mGD.verticalAlignment = GridData.FILL;
133         return this;
134     }
135 
136     /** Sets <code>grabExcessHorizontalSpace</code> to true. */
hGrab()137     public GridDataBuilder hGrab() {
138         mGD.grabExcessHorizontalSpace = true;
139         return this;
140     }
141 
142     /** Sets <code>grabExcessVerticalSpace</code> to true. */
vGrab()143     public GridDataBuilder vGrab() {
144         mGD.grabExcessVerticalSpace = true;
145         return this;
146     }
147 
148     /**
149      * Sets both <code>grabExcessHorizontalSpace</code> and
150      * <code>grabExcessVerticalSpace</code> to true.
151      */
grab()152     public GridDataBuilder grab() {
153         mGD.grabExcessHorizontalSpace = true;
154         mGD.grabExcessVerticalSpace = true;
155         return this;
156     }
157 
158 }
159