]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/EclipseUiUtils.java
06f569dce7c216467543d3a4dbfe90fed8c9cf2e
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / 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.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Text;
27
28 /** Utilities to simplify UI development. */
29 public class EclipseUiUtils {
30
31
32 //
33 // Simplify grid layouts management
34 //
35 public static GridLayout noSpaceGridLayout() {
36 return noSpaceGridLayout(new GridLayout());
37 }
38
39 public static GridLayout noSpaceGridLayout(GridLayout layout) {
40 layout.horizontalSpacing = 0;
41 layout.verticalSpacing = 0;
42 layout.marginWidth = 0;
43 layout.marginHeight = 0;
44 return layout;
45 }
46
47 public static GridData fillWidth() {
48 return grabWidth(SWT.FILL, SWT.FILL);
49 }
50
51 public static GridData fillAll() {
52 return new GridData(SWT.FILL, SWT.FILL, true, true);
53 }
54
55 public static GridData grabWidth(int horizontalAlignment,
56 int verticalAlignment) {
57 return new GridData(horizontalAlignment, horizontalAlignment, true,
58 false);
59 }
60
61
62
63
64
65 /**
66 * Create a label and a text field for a grid layout, the text field grabbing
67 * excess horizontal
68 *
69 * @param parent
70 * the parent composite
71 * @param label
72 * the label to display
73 * @param modifyListener
74 * a {@link ModifyListener} to listen on events on the text, can
75 * be null
76 * @return the created text
77 *
78 */
79 // FIXME why was this deprecated.
80 // * @ deprecated use { @ link #createGridLT(Composite, String)} instead
81 // @ Deprecated
82 public static Text createGridLT(Composite parent, String label,
83 ModifyListener modifyListener) {
84 Label lbl = new Label(parent, SWT.LEAD);
85 lbl.setText(label);
86 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
87 Text txt = new Text(parent, SWT.LEAD | SWT.BORDER);
88 txt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
89 if (modifyListener != null)
90 txt.addModifyListener(modifyListener);
91 return txt;
92 }
93
94 /**
95 * Create a label and a text field for a grid layout, the text field
96 * grabbing excess horizontal
97 */
98 public static Text createGridLT(Composite parent, String label) {
99 return createGridLT(parent, label, null);
100 }
101
102 /**
103 * Creates one label and a text field not editable with background colour of
104 * the parent (like a label but with selectable text)
105 */
106 public static Text createGridLL(Composite parent, String label, String text) {
107 Text txt = createGridLT(parent, label);
108 txt.setText(text);
109 txt.setEditable(false);
110 txt.setBackground(parent.getBackground());
111 return txt;
112 }
113
114 /**
115 * Create a label and a text field with password display for a grid layout,
116 * the text field grabbing excess horizontal
117 */
118 public static Text createGridLP(Composite parent, String label,
119 ModifyListener modifyListener) {
120 Label lbl = new Label(parent, SWT.LEAD);
121 lbl.setText(label);
122 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
123 Text txt = new Text(parent, SWT.LEAD | SWT.BORDER | SWT.PASSWORD);
124 txt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
125 if (txt != null)
126 txt.addModifyListener(modifyListener);
127 return txt;
128 }
129
130 /** Shortcut to retrieve default italic font from display */
131 public static Font getItalicFont(Composite parent) {
132 return JFaceResources.getFontRegistry().defaultFontDescriptor()
133 .setStyle(SWT.ITALIC).createFont(parent.getDisplay());
134 }
135
136 /** Shortcut to retrieve default bold font from display */
137 public static Font getBoldFont(Composite parent) {
138 return JFaceResources.getFontRegistry().defaultFontDescriptor()
139 .setStyle(SWT.BOLD).createFont(parent.getDisplay());
140 }
141
142 /** Shortcut to retrieve default bold italic font from display */
143 public static Font getBoldItalicFont(Composite parent) {
144 return JFaceResources.getFontRegistry().defaultFontDescriptor()
145 .setStyle(SWT.BOLD | SWT.ITALIC)
146 .createFont(parent.getDisplay());
147 }
148 }