blob: cd5ac3390451511f025b1e4672ac8d0a1b952913 [file] [log] [blame]
kchong38cbf172006-03-29 03:38:21 +00001/*******************************************************************************
kchongee4b4cf2007-05-02 17:49:40 +00002 * Copyright (c) 2001, 2006 IBM Corporation and others.
kchong38cbf172006-03-29 03:38:21 +00003 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.wst.xsd.ui.internal.util;
12
13import org.eclipse.swt.SWT;
14import org.eclipse.swt.graphics.Font;
15import org.eclipse.swt.graphics.FontData;
16import org.eclipse.swt.layout.GridData;
17import org.eclipse.swt.layout.GridLayout;
18import org.eclipse.swt.widgets.Button;
19import org.eclipse.swt.widgets.Combo;
20import org.eclipse.swt.widgets.Composite;
21import org.eclipse.swt.widgets.Display;
22import org.eclipse.swt.widgets.Label;
23import org.eclipse.swt.widgets.Text;
24
25// issue (cs) can we get rid of this class?
26// I've stripped it down a whole lot... but it'd be great to get rid of it
27//
28public class ViewUtility
29{
30 private static Font font;
31
32 private static Font getFont()
33 {
34 if (font == null)
35 {
36 font = new Font(Display.getCurrent(), "ms sans serif", 8, SWT.NORMAL);
37 }
38 return font;
39 }
40
41 public static void setComposite(Composite comp)
42 {
43 // deprecated. Remove later
44 }
45 public static Composite createComposite(Composite parent, int numColumns)
46 {
47 Composite composite = new Composite(parent, SWT.NONE);
48
49 composite.setFont(getFont());
50
51 GridLayout layout = new GridLayout();
52 layout.numColumns = numColumns;
53 composite.setLayout(layout);
54
55 GridData data = new GridData();
56 data.verticalAlignment = GridData.FILL;
57 data.horizontalAlignment = GridData.FILL;
58 composite.setLayoutData(data);
59 return composite;
60 }
61
62 public static Composite createComposite(Composite parent, int numColumns, boolean horizontalFill)
63 {
64 if (!horizontalFill)
65 {
66 createComposite(parent, numColumns);
67 }
68
69 Composite composite = new Composite(parent, SWT.NONE);
70
71 composite.setFont(getFont());
72
73 GridLayout layout = new GridLayout();
74 layout.numColumns = numColumns;
75 composite.setLayout(layout);
76
77 GridData data = new GridData();
78 data.verticalAlignment = GridData.FILL;
79 data.horizontalAlignment = GridData.FILL;
80 data.grabExcessHorizontalSpace = true;
81 composite.setLayoutData(data);
82
83 return composite;
84 }
85
86 public static Label createHorizontalFiller(Composite parent, int horizontalSpan)
87 {
88 Label label = new Label(parent, SWT.LEFT);
89 GridData data = new GridData();
90 data.horizontalAlignment = GridData.FILL;
91 data.horizontalSpan = horizontalSpan;
92 label.setLayoutData(data);
93 return label;
94 }
95
96 /**
97 * Helper method for creating labels.
98 */
99 public static Label createLabel(Composite parent, String text)
100 {
101 Label label = new Label(parent, SWT.LEFT);
102 label.setText(text);
103
104 GridData data = new GridData();
105 data.horizontalAlignment = GridData.FILL;
106 label.setLayoutData(data);
107 return label;
108 }
109
110 public Label createLabel(Composite parent, int style, String text)
111 {
112 Label label = new Label(parent, style);
113// setColor(label);
114 label.setText(text);
115
116 GridData data = new GridData();
117 data.horizontalAlignment = GridData.FILL;
118 label.setLayoutData(data);
119 return label;
120 }
121
122 public static Label createLabel(Composite parent, String text, int alignment)
123 {
124 Label label = new Label(parent, SWT.LEFT);
125 label.setText(text);
126
127 GridData data = new GridData();
128 data.horizontalAlignment = GridData.FILL;
129 data.verticalAlignment = alignment;
130 label.setLayoutData(data);
131 return label;
132 }
133
134
135
136
137 /**
138 * Create radio button
139 */
140 public static Button createRadioButton(Composite parent, String label)
141 {
142 Button button = new Button(parent, SWT.RADIO);
143 button.setText(label);
144
145 GridData data = new GridData();
146 data.horizontalAlignment = GridData.FILL;
147 button.setLayoutData(data);
148
149 return button;
150 }
151
152 /**
153 * Helper method for creating check box
154 */
155 public static Button createCheckBox(Composite parent, String label)
156 {
157 Button button = new Button(parent, SWT.CHECK);
158 button.setText(label);
159
160 GridData data = new GridData();
161 data.horizontalAlignment = GridData.FILL;
162 button.setLayoutData(data);
163 return button;
164 }
165
166 public static Combo createComboBox(Composite parent)
167 {
168 return createComboBox(parent, true);
169 }
170
171 public static Combo createComboBox(Composite parent, boolean isReadOnly)
172 {
173 int style = isReadOnly == true ? SWT.READ_ONLY : SWT.DROP_DOWN;
174
175 Combo combo = new Combo(parent, style);
176
177 GridData data = new GridData();
178 data.horizontalAlignment = GridData.FILL;
179 data.grabExcessHorizontalSpace = true;
180 combo.setLayoutData(data);
181 return combo;
182 }
183
184
185 public static Text createTextField(Composite parent, int width)
186 {
187 Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
188
189 GridData data = new GridData();
190 data.horizontalAlignment = GridData.FILL;
191 data.grabExcessHorizontalSpace = true;
192 data.widthHint = width;
193 text.setLayoutData(data);
194
195 return text;
196 }
197
198 /**
199 * <code>createWrappedMultiTextField</code> creates a wrapped multitext field
200 *
201 * @param parent a <code>Composite</code> value
202 * @param width an <code>int</code> value
203 * @param numLines an <code>int</code> value representing number of characters in height
204 * @param verticalFill a <code>boolean</code> value
205 * @return a <code>Text</code> value
206 */
207 public static Text createWrappedMultiTextField(Composite parent, int width, int numLines, boolean verticalFill)
208 {
209 Text text = new Text(parent, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
210
211 GridData data = new GridData();
212 data.horizontalAlignment = GridData.FILL;
213 data.grabExcessHorizontalSpace = true;
214 if (verticalFill)
215 {
216 data.verticalAlignment = GridData.FILL;
217 data.grabExcessVerticalSpace = true;
218 }
219 data.widthHint = width;
220 FontData[] fontData = getFont().getFontData();
221 // hack for now where on Windows, only 1 fontdata exists
222 data.heightHint = numLines * fontData[0].getHeight();
223 text.setLayoutData(data);
224
225 return text;
226 }
227
228 public static Text createMultiTextField(Composite parent, int width, int height, boolean verticalFill)
229 {
230 Text text = new Text(parent, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
231
232 GridData data = new GridData();
233 data.horizontalAlignment = GridData.FILL;
234 data.grabExcessHorizontalSpace = true;
235 if (verticalFill)
236 {
237 data.verticalAlignment = GridData.FILL;
238 data.grabExcessVerticalSpace = true;
239 }
240 data.widthHint = width;
241 data.heightHint = height;
242 text.setLayoutData(data);
243
244 return text;
245 }
246}