]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.akb.ui/src/main/java/org/argeo/slc/akb/ui/AkbUiUtils.java
Add AKB icons
[gpl/argeo-slc.git] / plugins / org.argeo.slc.akb.ui / src / main / java / org / argeo / slc / akb / ui / AkbUiUtils.java
1 package org.argeo.slc.akb.ui;
2
3 import java.util.Calendar;
4
5 import javax.jcr.Node;
6 import javax.jcr.PropertyType;
7 import javax.jcr.RepositoryException;
8
9 import org.argeo.slc.akb.AkbException;
10 import org.argeo.slc.akb.utils.AkbJcrUtils;
11 import org.eclipse.jface.viewers.TableViewer;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.ModifyEvent;
14 import org.eclipse.swt.events.ModifyListener;
15 import org.eclipse.swt.layout.FormAttachment;
16 import org.eclipse.swt.layout.FormData;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.layout.RowData;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Table;
23 import org.eclipse.swt.widgets.Text;
24 import org.eclipse.ui.forms.AbstractFormPart;
25 import org.eclipse.ui.forms.widgets.FormToolkit;
26
27 /** Some helper methods that factorize widely used snippets in people UI */
28 public class AkbUiUtils {
29
30 /**
31 * Shortcut to refresh the value of a <code>Text</code> given a Node and a
32 * property Name
33 */
34 public static String refreshTextWidgetValue(Text text, Node entity,
35 String propName) {
36 String tmpStr = AkbJcrUtils.get(entity, propName);
37 if (AkbJcrUtils.checkNotEmptyString(tmpStr))
38 text.setText(tmpStr);
39 return tmpStr;
40 }
41
42 /**
43 * Shortcut to refresh a <code>Text</code> widget given a Node in a form and
44 * a property Name. Also manages its enable state
45 */
46 public static String refreshFormTextWidget(Text text, Node entity,
47 String propName) {
48 String tmpStr = AkbJcrUtils.get(entity, propName);
49 if (AkbJcrUtils.checkNotEmptyString(tmpStr))
50 text.setText(tmpStr);
51 text.setEnabled(AkbJcrUtils.isNodeCheckedOutByMe(entity));
52 return tmpStr;
53 }
54
55
56 /**
57 * Shortcut to refresh a <code>Text</code> widget given a Node in a form and
58 * a property Name. Also manages its enable state and set a default message if corresponding Text value is empty
59 */
60 public static String refreshFormTextWidget(Text text, Node entity,
61 String propName, String defaultMsg) {
62 String tmpStr = refreshFormTextWidget(text, entity, propName);
63 if (AkbJcrUtils.isEmptyString(tmpStr) && AkbJcrUtils.checkNotEmptyString(defaultMsg))
64 text.setMessage(defaultMsg);
65 return tmpStr;
66 }
67
68 /**
69 * Shortcut to refresh a Check box <code>Button</code> widget given a Node
70 * in a form and a property Name.
71 */
72 public static boolean refreshCheckBoxWidget(Button button, Node entity,
73 String propName) {
74 Boolean tmp = null;
75 try {
76 if (entity.hasProperty(propName)) {
77 tmp = entity.getProperty(propName).getBoolean();
78 button.setSelection(tmp);
79 }
80 } catch (RepositoryException re) {
81 throw new AkbException("unable get boolean value for property "
82 + propName);
83 }
84 return tmp;
85 }
86
87 /**
88 * Shortcut to add a default modify listeners to a <code>Text</code> widget
89 * that is bound a JCR String Property. Any change in the text is
90 * immediately stored in the active session, but no save is done.
91 */
92 public static void addTextModifyListener(final Text text, final Node node,
93 final String propName, final AbstractFormPart part) {
94 text.addModifyListener(new ModifyListener() {
95 @Override
96 public void modifyText(ModifyEvent event) {
97 if (setJcrProperty(node, propName, PropertyType.STRING,
98 text.getText()))
99 part.markDirty();
100 }
101 });
102 }
103
104 /**
105 * Centralizes management of updating property value. Among other to avoid
106 * infinite loop when the new value is the same as the ones that is already
107 * stored in JCR.
108 *
109 * @return true if the value as changed
110 */
111 public static boolean setJcrProperty(Node node, String propName,
112 int propertyType, Object value) {
113 try {
114 // int propertyType = getPic().getProperty(propName).getType();
115 switch (propertyType) {
116 case PropertyType.STRING:
117 if ("".equals((String) value)
118 && (!node.hasProperty(propName) || node
119 .hasProperty(propName)
120 && "".equals(node.getProperty(propName)
121 .getString())))
122 // workaround the fact that the Text widget value cannot be
123 // set to null
124 return false;
125 else if (node.hasProperty(propName)
126 && node.getProperty(propName).getString()
127 .equals((String) value))
128 // nothing changed yet
129 return false;
130 else {
131 node.setProperty(propName, (String) value);
132 return true;
133 }
134 case PropertyType.BOOLEAN:
135 if (node.hasProperty(propName)
136 && node.getProperty(propName).getBoolean() == (Boolean) value)
137 // nothing changed yet
138 return false;
139 else {
140 node.setProperty(propName, (Boolean) value);
141 return true;
142 }
143 case PropertyType.DATE:
144 if (node.hasProperty(propName)
145 && node.getProperty(propName).getDate()
146 .equals((Calendar) value))
147 // nothing changed yet
148 return false;
149 else {
150 node.setProperty(propName, (Calendar) value);
151 return true;
152 }
153 case PropertyType.LONG:
154 Long lgValue = (Long) value;
155
156 if (lgValue == null)
157 lgValue = 0L;
158
159 if (node.hasProperty(propName)
160 && node.getProperty(propName).getLong() == lgValue)
161 // nothing changed yet
162 return false;
163 else {
164 node.setProperty(propName, lgValue);
165 return true;
166 }
167
168 default:
169 throw new AkbException("Unimplemented save for property type: "
170 + propertyType + " - property: " + propName);
171
172 }
173 } catch (RepositoryException re) {
174 throw new AkbException("Error while setting property" + propName
175 + " - propertyType: " + propertyType, re);
176 }
177 }
178
179 // ////////////////////////
180 // LAYOUTS AND STYLES
181
182 /** shortcut to set form data while dealing with switching panel */
183 public static void setSwitchingFormData(Composite composite) {
184 FormData fdLabel = new FormData();
185 fdLabel.top = new FormAttachment(0, 0);
186 fdLabel.left = new FormAttachment(0, 0);
187 fdLabel.right = new FormAttachment(100, 0);
188 fdLabel.bottom = new FormAttachment(100, 0);
189 composite.setLayoutData(fdLabel);
190 }
191
192 public static void setTableDefaultStyle(TableViewer viewer,
193 int customItemHeight) {
194 Table table = viewer.getTable();
195 table.setLinesVisible(true);
196 table.setHeaderVisible(false);
197 }
198
199 /**
200 * Shortcut to provide a gridlayout with no margin and no spacing (dafault
201 * are normally 5 px)
202 */
203 public static GridLayout gridLayoutNoBorder() {
204 return gridLayoutNoBorder(1);
205 }
206
207 /**
208 * Shortcut to provide a gridlayout with no margin and no spacing (default
209 * are normally 5 px) with the given column number (equals width is false).
210 */
211 public static GridLayout gridLayoutNoBorder(int nbOfCol) {
212 GridLayout gl = new GridLayout(nbOfCol, false);
213 gl.marginWidth = gl.marginHeight = gl.horizontalSpacing = gl.verticalSpacing = 0;
214 return gl;
215 }
216
217 /** Creates a text widget with RowData already set */
218 public static Text createRDText(FormToolkit toolkit, Composite parent,
219 String msg, String toolTip, int width) {
220 Text text = toolkit.createText(parent, "", SWT.BORDER | SWT.SINGLE
221 | SWT.LEFT);
222 text.setMessage(msg);
223 text.setToolTipText(toolTip);
224 text.setLayoutData(new RowData(width, SWT.DEFAULT));
225 return text;
226 }
227
228 /**
229 * Creates a text widget with GridData already set
230 *
231 * @param toolkit
232 * @param parent
233 * @param msg
234 * @param toolTip
235 * @param width
236 * @param colSpan
237 * @return
238 */
239 public static Text createGDText(FormToolkit toolkit, Composite parent,
240 String msg, String toolTip, int width, int colSpan) {
241 Text text = toolkit.createText(parent, "", SWT.BORDER | SWT.SINGLE
242 | SWT.LEFT);
243 text.setMessage(msg);
244 text.setToolTipText(toolTip);
245 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
246 gd.widthHint = width;
247 gd.horizontalSpan = colSpan;
248 text.setLayoutData(gd);
249 return text;
250 }
251
252 /**
253 * Shortcut to quickly get a FormData object with configured FormAttachment
254 *
255 * @param left
256 * @param top
257 * @param right
258 * @param bottom
259 * @return
260 */
261 public static FormData createformData(int left, int top, int right,
262 int bottom) {
263 FormData formData = new FormData();
264 formData.left = new FormAttachment(left, 0);
265 formData.top = new FormAttachment(top, 0);
266 formData.right = new FormAttachment(right, 0);
267 formData.bottom = new FormAttachment(bottom, 0);
268 return formData;
269 }
270 }