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