View Javadoc
1   package org.argeo.connect.ui;
2   
3   import javax.jcr.Node;
4   
5   import org.argeo.cms.Localized;
6   import org.argeo.cms.util.CmsUtils;
7   import org.argeo.connect.util.ConnectJcrUtils;
8   import org.argeo.connect.util.ConnectUtils;
9   //import org.argeo.connect.workbench.AppWorkbenchService;
10  import org.argeo.eclipse.ui.EclipseUiUtils;
11  import org.eclipse.jface.viewers.TableViewer;
12  import org.eclipse.swt.SWT;
13  import org.eclipse.swt.layout.FormAttachment;
14  import org.eclipse.swt.layout.FormData;
15  import org.eclipse.swt.layout.GridData;
16  import org.eclipse.swt.layout.GridLayout;
17  import org.eclipse.swt.layout.RowData;
18  import org.eclipse.swt.widgets.Button;
19  import org.eclipse.swt.widgets.Composite;
20  import org.eclipse.swt.widgets.Label;
21  import org.eclipse.swt.widgets.Table;
22  import org.eclipse.swt.widgets.Text;
23  
24  /** Helper methods for various Connect UIs */
25  public class ConnectUiUtils {
26  	/** simply add an empty line in a grid data to give some air */
27  	public static Label addEmptyLine(Composite parent, int height, int colSpan) {
28  		// Empty line that act as a padding
29  		Label emptyLbl = new Label(parent, SWT.NONE);
30  		emptyLbl.setText("");
31  		GridData gd = EclipseUiUtils.fillWidth(colSpan);
32  		gd.heightHint = height;
33  		emptyLbl.setLayoutData(gd);
34  		return emptyLbl;
35  	}
36  
37  	/**
38  	 * Calls <code>ConnectJcrUtils.get(Node node, String propName)</code> method and
39  	 * replace any '&' by its html encoding '&amp;' to avoid
40  	 * <code>IllegalArgumentException</code> while rendering html read only snippets
41  	 */
42  	public static String getRwtCompliantString(Node node, String propName) {
43  		String value = ConnectJcrUtils.get(node, propName);
44  		value = ConnectUtils.replaceAmpersand(value);
45  		return value;
46  	}
47  
48  	/**
49  	 * Dispose all control children of this composite. Useful for violent refreshes.
50  	 * 
51  	 * @param parent
52  	 */
53  	// public static void disposeAllChildren(Composite parent) {
54  	// // We redraw the full control at each refresh, might be a more
55  	// // efficient way to do
56  	// Control[] oldChildren = parent.getChildren();
57  	// for (Control child : oldChildren)
58  	// child.dispose();
59  	// }
60  
61  	public static boolean isNumbers(String content) {
62  		int length = content.length();
63  		for (int i = 0; i < length; i++) {
64  			char ch = content.charAt(i);
65  			if (!Character.isDigit(ch)) {
66  				return false;
67  			}
68  		}
69  		return true;
70  	}
71  
72  	/**
73  	 * Shortcut to create a {@link GridLayout} with the given column number with no
74  	 * margin and no spacing (default are normally 5 px). makeColumnsEqualWidth
75  	 * parameter is set to false.
76  	 */
77  	public static GridLayout noSpaceGridLayout(int nbOfCol) {
78  		GridLayout gl = new GridLayout(nbOfCol, false);
79  		gl.marginWidth = gl.marginHeight = gl.horizontalSpacing = gl.verticalSpacing = 0;
80  		return gl;
81  	}
82  
83  	/**
84  	 * Shortcut to refresh the value of a <code>Text</code> given a Node and a
85  	 * property Name
86  	 */
87  	public static String refreshTextWidgetValue(Text text, Node entity, String propName) {
88  		String tmpStr = ConnectJcrUtils.get(entity, propName);
89  		if (EclipseUiUtils.notEmpty(tmpStr))
90  			text.setText(tmpStr);
91  		return tmpStr;
92  	}
93  
94  	/**
95  	 * Cleans a String by replacing any '&' by its HTML encoding '&nbsp;' to insure
96  	 * they are displayed in SWT.Link controls
97  	 */
98  	public static String replaceAmpersandforSWTLink(String value) {
99  		value = value.replaceAll("&", "&&");
100 		return value;
101 	}
102 
103 	/**
104 	 * Creates a basic right-aligned vertical-centered bold label with no specific
105 	 * toolkit.
106 	 */
107 	public static Label createBoldLabel(Composite parent, String value) {
108 		Label label = new Label(parent, SWT.LEAD);
109 		label.setText(" " + value);
110 		label.setFont(EclipseUiUtils.getBoldFont(parent));
111 		label.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
112 		return label;
113 	}
114 
115 	public static Label createBoldLabel(Composite parent, Localized localized) {
116 		Label label = new Label(parent, SWT.LEAD);
117 		label.setText(localized.lead());
118 		label.setFont(EclipseUiUtils.getBoldFont(parent));
119 		label.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
120 		return label;
121 	}
122 
123 	/**
124 	 * Creates a basic right aligned bold label with no specific toolkit. precise
125 	 * vertical alignment
126 	 */
127 	public static Label createBoldLabel(Composite parent, String value, int verticalAlign) {
128 		Label label = new Label(parent, SWT.LEAD);
129 		label.setText(" " + value);
130 		label.setFont(EclipseUiUtils.getBoldFont(parent));
131 		label.setLayoutData(new GridData(SWT.LEAD, verticalAlign, false, false));
132 		return label;
133 	}
134 
135 	/**
136 	 * Shortcut to create a delete button that will be used in composites that
137 	 * display a multi value property in tag-like manner
138 	 */
139 	public static Button createDeleteButton(Composite parent) {
140 		Button button = new Button(parent, SWT.FLAT | SWT.PUSH);
141 		CmsUtils.style(button, ConnectUiStyles.SMALL_DELETE_BTN);
142 		RowData rd = new RowData();
143 		rd.height = 8;
144 		rd.width = 8;
145 		button.setLayoutData(rd);
146 		return button;
147 	}
148 
149 	/** Creates a text widget with RowData already set */
150 	public static Text createRDText(Object toolkit, Composite parent, String msg, String toolTip, int width) {
151 		Text text = new Text(parent, SWT.BORDER | SWT.SINGLE | SWT.LEFT);
152 		text.setText("");
153 		text.setMessage(msg);
154 		text.setToolTipText(toolTip);
155 		text.setLayoutData(width == 0 ? new RowData() : new RowData(width, SWT.DEFAULT));
156 		return text;
157 	}
158 
159 	/**
160 	 * Creates the basic right aligned bold label that is used in various forms
161 	 * using a pre-defined toolkit.
162 	 */
163 	public static Label createBoldLabel(Object toolkit, Composite parent, String value) {
164 
165 		// We add a blank space before to workaround the cropping of the word
166 		// first letter in some OS/Browsers (typically MAC/Firefox 31 )
167 		Label label = new Label(parent, SWT.RIGHT);
168 		label.setText(" " + value);
169 		label.setFont(EclipseUiUtils.getBoldFont(parent));
170 		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
171 		return label;
172 	}
173 
174 	/**
175 	 * Creates a text widget with GridData already set
176 	 * 
177 	 * @param toolkit
178 	 * @param parent
179 	 * @param msg
180 	 * @param toolTip
181 	 * @param width
182 	 * @param colSpan
183 	 * @return
184 	 */
185 	@Deprecated
186 	public static Text createGDText(Object toolkit, Composite parent, String msg, String toolTip, int width,
187 			int colSpan) {
188 		Text text = new Text(parent, SWT.BORDER | SWT.SINGLE | SWT.LEFT);
189 		text.setText("");
190 		text.setMessage(msg);
191 		text.setToolTipText(toolTip);
192 		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
193 		gd.widthHint = width;
194 		gd.horizontalSpan = colSpan;
195 		text.setLayoutData(gd);
196 		return text;
197 	}
198 
199 	public static void setTableDefaultStyle(Table table, int customItemHeight) {
200 		table.setLinesVisible(true);
201 		table.setHeaderVisible(false);
202 		CmsUtils.setItemHeight(table, customItemHeight);
203 		CmsUtils.markup(table);
204 	}
205 
206 	public static void setTableDefaultStyle(TableViewer viewer, int customItemHeight) {
207 		setTableDefaultStyle(viewer.getTable(), customItemHeight);
208 	}
209 
210 	/** shortcut to set form data while dealing with switching panel */
211 	public static void setSwitchingFormData(Composite composite) {
212 		FormData fdLabel = new FormData();
213 		fdLabel.top = new FormAttachment(0, 0);
214 		fdLabel.left = new FormAttachment(0, 0);
215 		fdLabel.right = new FormAttachment(100, 0);
216 		fdLabel.bottom = new FormAttachment(100, 0);
217 		composite.setLayoutData(fdLabel);
218 	}
219 
220 	/**
221 	 * Shortcut to quickly get a FormData object with configured FormAttachment
222 	 * 
223 	 * @param left
224 	 * @param top
225 	 * @param right
226 	 * @param bottom
227 	 * @return
228 	 */
229 	public static FormData createformData(int left, int top, int right, int bottom) {
230 		FormData formData = new FormData();
231 		formData.left = new FormAttachment(left, 0);
232 		formData.top = new FormAttachment(top, 0);
233 		formData.right = new FormAttachment(right, 0);
234 		formData.bottom = new FormAttachment(bottom, 0);
235 		return formData;
236 	}
237 
238 }