• 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.layout.GridLayout;
20 import org.eclipse.swt.widgets.Composite;
21 
22 /**
23  * A little helper to create a new {@link GridLayout}, associate to a {@link Composite}
24  * and set its common attributes.
25  * <p/>
26  * Example of usage: <br/>
27  * <code>
28  *    GridLayoutHelper.create(myComposite).noMargins().vSpacing(0).columns(2);
29  * </code>
30  */
31 public final class GridLayoutBuilder {
32 
33     private static GridLayout mGL;
34 
GridLayoutBuilder()35     private GridLayoutBuilder() {
36         mGL = new GridLayout();
37     }
38 
39     /**
40      * Creates new {@link GridLayout} and associates it on the <code>parent</code> composite.
41      */
create(Composite parent)42     static public GridLayoutBuilder create(Composite parent) {
43         GridLayoutBuilder glh = new GridLayoutBuilder();
44         parent.setLayout(GridLayoutBuilder.mGL);
45         return glh;
46     }
47 
48     /** Sets all margins to 0. */
noMargins()49     public GridLayoutBuilder noMargins() {
50         mGL.marginHeight = 0;
51         mGL.marginWidth = 0;
52         mGL.marginLeft = 0;
53         mGL.marginTop = 0;
54         mGL.marginRight = 0;
55         mGL.marginBottom = 0;
56         return this;
57     }
58 
59     /** Sets all margins to <code>n</code>. */
margins(int n)60     public GridLayoutBuilder margins(int n) {
61         mGL.marginHeight = n;
62         mGL.marginWidth = n;
63         mGL.marginLeft = n;
64         mGL.marginTop = n;
65         mGL.marginRight = n;
66         mGL.marginBottom = n;
67         return this;
68     }
69 
70     /** Sets <code>numColumns</code> to <code>n</code>. */
columns(int n)71     public GridLayoutBuilder columns(int n) {
72         mGL.numColumns = n;
73         return this;
74     }
75 
76     /** Sets <code>makeColumnsEqualWidth</code> to true. */
columnsEqual()77     public GridLayoutBuilder columnsEqual() {
78         mGL.makeColumnsEqualWidth = true;
79         return this;
80     }
81 
82     /** Sets <code>verticalSpacing</code> to <code>v</code>. */
vSpacing(int v)83     public GridLayoutBuilder vSpacing(int v) {
84         mGL.verticalSpacing = v;
85         return this;
86     }
87 
88     /** Sets <code>horizontalSpacing</code> to <code>h</code>. */
hSpacing(int h)89     public GridLayoutBuilder hSpacing(int h) {
90         mGL.horizontalSpacing = h;
91         return this;
92     }
93 
94     /**
95      * Sets <code>horizontalSpacing</code> and <code>verticalSpacing</code>
96      * to <code>s</code>.
97      */
spacing(int s)98     public GridLayoutBuilder spacing(int s) {
99         mGL.verticalSpacing = s;
100         mGL.horizontalSpacing = s;
101         return this;
102     }
103 }
104