KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > shell > GridPanel


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.shell;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23
24 /**
25  * Shared boilerplate for grid-style UIs.
26  */

27 public class GridPanel extends Composite {
28
29   protected static final int FILL = GridData.FILL;
30
31   protected static final int CENTER = GridData.CENTER;
32
33   protected static final int MIDDLE = GridData.CENTER;
34
35   protected static final int LEFT = GridData.BEGINNING;
36
37   protected static final int RIGHT = GridData.END;
38
39   protected static final int TOP = GridData.BEGINNING;
40   protected static final int BOTTOM = GridData.END;
41
42   public GridPanel(Composite parent, int style, int numCols,
43       boolean equalWidthCols) {
44     this(parent, style, numCols, equalWidthCols, 5, 5);
45   }
46
47   public GridPanel(Composite parent, int style, int numCols,
48       boolean equalWidthCols, int marginWidth, int marginHeight) {
49     super(parent, style);
50     GridLayout gridLayout = new GridLayout();
51     gridLayout.numColumns = numCols;
52     gridLayout.makeColumnsEqualWidth = equalWidthCols;
53     gridLayout.marginWidth = marginWidth;
54     gridLayout.marginHeight = marginHeight;
55     gridLayout.horizontalSpacing = 1;
56     gridLayout.verticalSpacing = 1;
57     setLayout(gridLayout);
58   }
59
60   protected GridData getGridData(Control control) {
61     GridData gridData = (GridData) control.getLayoutData();
62     if (gridData == null) {
63       gridData = new GridData();
64       control.setLayoutData(gridData);
65     }
66     return gridData;
67   }
68
69   protected GridData setGridData(Control control, int rowSpan, int colSpan,
70       int hAlign, int vAlign, boolean hGrab, boolean vGrab) {
71     return setGridData(control, rowSpan, colSpan, hAlign, vAlign, hGrab, vGrab,
72         SWT.DEFAULT, SWT.DEFAULT);
73   }
74
75   protected GridData setGridData(Control control, int rowSpan, int colSpan,
76       int hAlign, int vAlign, boolean hGrab, boolean vGrab, int widthHint,
77       int heightHint) {
78     GridData gridData = getGridData(control);
79     gridData.horizontalSpan = colSpan;
80     gridData.verticalSpan = rowSpan;
81     gridData.horizontalAlignment = hAlign;
82     gridData.verticalAlignment = vAlign;
83     gridData.grabExcessHorizontalSpace = hGrab;
84     gridData.grabExcessVerticalSpace = vGrab;
85     if (heightHint != SWT.DEFAULT) {
86       gridData.heightHint = heightHint;
87     }
88
89     if (widthHint != SWT.DEFAULT) {
90       gridData.widthHint = widthHint;
91     }
92
93     control.setLayoutData(gridData);
94     return gridData;
95   }
96 }
97
Popular Tags