]> git.argeo.org Git - lgpl/argeo-commons.git/blob - EclipseUiUtils.java
2a17d285cc106835022597545ec5a13e6840e92e
[lgpl/argeo-commons.git] / EclipseUiUtils.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 package org.argeo.eclipse.ui;
17
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;
29
30 /** Utilities to simplify UI development. */
31 public class EclipseUiUtils {
32
33 //
34 // Simplify grid layouts management
35 //
36 public static GridLayout noSpaceGridLayout() {
37 return noSpaceGridLayout(new GridLayout());
38 }
39
40 public static GridLayout noSpaceGridLayout(GridLayout layout) {
41 layout.horizontalSpacing = 0;
42 layout.verticalSpacing = 0;
43 layout.marginWidth = 0;
44 layout.marginHeight = 0;
45 return layout;
46 }
47
48 public static GridData fillWidth() {
49 return grabWidth(SWT.FILL, SWT.FILL);
50 }
51
52 public static GridData fillAll() {
53 return new GridData(SWT.FILL, SWT.FILL, true, true);
54 }
55
56 public static GridData grabWidth(int horizontalAlignment,
57 int verticalAlignment) {
58 return new GridData(horizontalAlignment, horizontalAlignment, true,
59 false);
60 }
61
62 //
63 // Simplify Form layout management
64 //
65
66 /**
67 * Creates a basic form data that is attached to the 4 corners of the parent
68 * composite
69 */
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);
76 return formData;
77 }
78
79 /**
80 * Create a label and a text field for a grid layout, the text field
81 * grabbing excess horizontal
82 *
83 * @param parent
84 * the parent composite
85 * @param label
86 * the label to display
87 * @param modifyListener
88 * a {@link ModifyListener} to listen on events on the text, can
89 * be null
90 * @return the created text
91 *
92 */
93 // FIXME why was this deprecated.
94 // * @ deprecated use { @ link #createGridLT(Composite, String)} instead
95 // @ Deprecated
96 public static Text createGridLT(Composite parent, String label,
97 ModifyListener modifyListener) {
98 Label lbl = new Label(parent, SWT.LEAD);
99 lbl.setText(label);
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);
105 return txt;
106 }
107
108 /**
109 * Create a label and a text field for a grid layout, the text field
110 * grabbing excess horizontal
111 */
112 public static Text createGridLT(Composite parent, String label) {
113 return createGridLT(parent, label, null);
114 }
115
116 /**
117 * Creates one label and a text field not editable with background colour of
118 * the parent (like a label but with selectable text)
119 */
120 public static Text createGridLL(Composite parent, String label, String text) {
121 Text txt = createGridLT(parent, label);
122 txt.setText(text);
123 txt.setEditable(false);
124 txt.setBackground(parent.getBackground());
125 return txt;
126 }
127
128 /**
129 * Create a label and a text field with password display for a grid layout,
130 * the text field grabbing excess horizontal
131 */
132 public static Text createGridLP(Composite parent, String label,
133 ModifyListener modifyListener) {
134 Label lbl = new Label(parent, SWT.LEAD);
135 lbl.setText(label);
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));
139 if (txt != null)
140 txt.addModifyListener(modifyListener);
141 return txt;
142 }
143
144 //
145 // FONTS
146 //
147
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());
152 }
153
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());
158 }
159
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());
165 }
166 }