]>
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
1 package org
.argeo
.slc
.akb
.ui
;
3 import java
.util
.Calendar
;
6 import javax
.jcr
.PropertyType
;
7 import javax
.jcr
.RepositoryException
;
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
;
27 /** Some helper methods that factorize widely used snippets in people UI */
28 public class AkbUiUtils
{
31 * Shortcut to refresh the value of a <code>Text</code> given a Node and a
34 public static String
refreshTextWidgetValue(Text text
, Node entity
,
36 String tmpStr
= AkbJcrUtils
.get(entity
, propName
);
37 if (AkbJcrUtils
.checkNotEmptyString(tmpStr
))
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
46 public static String
refreshFormTextWidget(Text text
, Node entity
,
48 String tmpStr
= AkbJcrUtils
.get(entity
, propName
);
49 if (AkbJcrUtils
.checkNotEmptyString(tmpStr
))
51 text
.setEnabled(AkbJcrUtils
.isNodeCheckedOutByMe(entity
));
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
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
);
69 * Shortcut to refresh a Check box <code>Button</code> widget given a Node
70 * in a form and a property Name.
72 public static boolean refreshCheckBoxWidget(Button button
, Node entity
,
76 if (entity
.hasProperty(propName
)) {
77 tmp
= entity
.getProperty(propName
).getBoolean();
78 button
.setSelection(tmp
);
80 } catch (RepositoryException re
) {
81 throw new AkbException("unable get boolean value for property "
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.
92 public static void addTextModifyListener(final Text text
, final Node node
,
93 final String propName
, final AbstractFormPart part
) {
94 text
.addModifyListener(new ModifyListener() {
96 public void modifyText(ModifyEvent event
) {
97 if (setJcrProperty(node
, propName
, PropertyType
.STRING
,
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
109 * @return true if the value as changed
111 public static boolean setJcrProperty(Node node
, String propName
,
112 int propertyType
, Object value
) {
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
)
122 // workaround the fact that the Text widget value cannot be
125 else if (node
.hasProperty(propName
)
126 && node
.getProperty(propName
).getString()
127 .equals((String
) value
))
128 // nothing changed yet
131 node
.setProperty(propName
, (String
) value
);
134 case PropertyType
.BOOLEAN
:
135 if (node
.hasProperty(propName
)
136 && node
.getProperty(propName
).getBoolean() == (Boolean
) value
)
137 // nothing changed yet
140 node
.setProperty(propName
, (Boolean
) value
);
143 case PropertyType
.DATE
:
144 if (node
.hasProperty(propName
)
145 && node
.getProperty(propName
).getDate()
146 .equals((Calendar
) value
))
147 // nothing changed yet
150 node
.setProperty(propName
, (Calendar
) value
);
153 case PropertyType
.LONG
:
154 Long lgValue
= (Long
) value
;
159 if (node
.hasProperty(propName
)
160 && node
.getProperty(propName
).getLong() == lgValue
)
161 // nothing changed yet
164 node
.setProperty(propName
, lgValue
);
169 throw new AkbException("Unimplemented save for property type: "
170 + propertyType
+ " - property: " + propName
);
173 } catch (RepositoryException re
) {
174 throw new AkbException("Error while setting property" + propName
175 + " - propertyType: " + propertyType
, re
);
179 // ////////////////////////
180 // LAYOUTS AND STYLES
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
);
192 public static void setTableDefaultStyle(TableViewer viewer
,
193 int customItemHeight
) {
194 Table table
= viewer
.getTable();
195 table
.setLinesVisible(true);
196 table
.setHeaderVisible(false);
200 * Shortcut to provide a gridlayout with no margin and no spacing (dafault
203 public static GridLayout
gridLayoutNoBorder() {
204 return gridLayoutNoBorder(1);
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).
211 public static GridLayout
gridLayoutNoBorder(int nbOfCol
) {
212 GridLayout gl
= new GridLayout(nbOfCol
, false);
213 gl
.marginWidth
= gl
.marginHeight
= gl
.horizontalSpacing
= gl
.verticalSpacing
= 0;
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
222 text
.setMessage(msg
);
223 text
.setToolTipText(toolTip
);
224 text
.setLayoutData(new RowData(width
, SWT
.DEFAULT
));
229 * Creates a text widget with GridData already set
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
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
);
253 * Shortcut to quickly get a FormData object with configured FormAttachment
261 public static FormData
createformData(int left
, int top
, int right
,
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);