2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.eclipse
.ui
;
18 import org
.eclipse
.jface
.resource
.JFaceResources
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.events
.ModifyListener
;
21 import org
.eclipse
.swt
.graphics
.Font
;
22 import org
.eclipse
.swt
.layout
.FormAttachment
;
23 import org
.eclipse
.swt
.layout
.FormData
;
24 import org
.eclipse
.swt
.layout
.GridData
;
25 import org
.eclipse
.swt
.layout
.GridLayout
;
26 import org
.eclipse
.swt
.widgets
.Composite
;
27 import org
.eclipse
.swt
.widgets
.Label
;
28 import org
.eclipse
.swt
.widgets
.Text
;
30 /** Utilities to simplify UI development. */
31 public class EclipseUiUtils
{
34 // Simplify grid layouts management
36 public static GridLayout
noSpaceGridLayout() {
37 return noSpaceGridLayout(new GridLayout());
40 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
41 layout
.horizontalSpacing
= 0;
42 layout
.verticalSpacing
= 0;
43 layout
.marginWidth
= 0;
44 layout
.marginHeight
= 0;
48 public static GridData
fillWidth() {
49 return grabWidth(SWT
.FILL
, SWT
.FILL
);
52 public static GridData
fillAll() {
53 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
56 public static GridData
grabWidth(int horizontalAlignment
,
57 int verticalAlignment
) {
58 return new GridData(horizontalAlignment
, horizontalAlignment
, true,
63 // Simplify Form layout management
67 * Creates a basic form data that is attached to the 4 corners of the parent
70 public static FormData
fillFormData() {
71 FormData formData
= new FormData();
72 formData
.top
= new FormAttachment(0, 0);
73 formData
.left
= new FormAttachment(0, 0);
74 formData
.right
= new FormAttachment(100, 0);
75 formData
.bottom
= new FormAttachment(100, 0);
80 * Create a label and a text field for a grid layout, the text field
81 * grabbing excess horizontal
84 * the parent composite
86 * the label to display
87 * @param modifyListener
88 * a {@link ModifyListener} to listen on events on the text, can
90 * @return the created text
93 // FIXME why was this deprecated.
94 // * @ deprecated use { @ link #createGridLT(Composite, String)} instead
96 public static Text
createGridLT(Composite parent
, String label
,
97 ModifyListener modifyListener
) {
98 Label lbl
= new Label(parent
, SWT
.LEAD
);
100 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
101 Text txt
= new Text(parent
, SWT
.LEAD
| SWT
.BORDER
);
102 txt
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
103 if (modifyListener
!= null)
104 txt
.addModifyListener(modifyListener
);
109 * Create a label and a text field for a grid layout, the text field
110 * grabbing excess horizontal
112 public static Text
createGridLT(Composite parent
, String label
) {
113 return createGridLT(parent
, label
, null);
117 * Creates one label and a text field not editable with background colour of
118 * the parent (like a label but with selectable text)
120 public static Text
createGridLL(Composite parent
, String label
, String text
) {
121 Text txt
= createGridLT(parent
, label
);
123 txt
.setEditable(false);
124 txt
.setBackground(parent
.getBackground());
129 * Create a label and a text field with password display for a grid layout,
130 * the text field grabbing excess horizontal
132 public static Text
createGridLP(Composite parent
, String label
,
133 ModifyListener modifyListener
) {
134 Label lbl
= new Label(parent
, SWT
.LEAD
);
136 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
137 Text txt
= new Text(parent
, SWT
.LEAD
| SWT
.BORDER
| SWT
.PASSWORD
);
138 txt
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
140 txt
.addModifyListener(modifyListener
);
148 /** Shortcut to retrieve default italic font from display */
149 public static Font
getItalicFont(Composite parent
) {
150 return JFaceResources
.getFontRegistry().defaultFontDescriptor()
151 .setStyle(SWT
.ITALIC
).createFont(parent
.getDisplay());
154 /** Shortcut to retrieve default bold font from display */
155 public static Font
getBoldFont(Composite parent
) {
156 return JFaceResources
.getFontRegistry().defaultFontDescriptor()
157 .setStyle(SWT
.BOLD
).createFont(parent
.getDisplay());
160 /** Shortcut to retrieve default bold italic font from display */
161 public static Font
getBoldItalicFont(Composite parent
) {
162 return JFaceResources
.getFontRegistry().defaultFontDescriptor()
163 .setStyle(SWT
.BOLD
| SWT
.ITALIC
)
164 .createFont(parent
.getDisplay());