1 package org
.argeo
.slc
.akb
.ui
;
3 import java
.util
.Calendar
;
7 import javax
.jcr
.PropertyType
;
8 import javax
.jcr
.RepositoryException
;
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
;
34 /** Some helper methods that factorize widely used snippets in people UI */
35 public class AkbUiUtils
{
38 * Shortcut to refresh the value of a <code>Text</code> given a Node and a
41 public static String
refreshTextWidgetValue(Text text
, Node entity
,
43 String tmpStr
= AkbJcrUtils
.get(entity
, propName
);
44 if (AkbJcrUtils
.checkNotEmptyString(tmpStr
))
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
53 public static String
refreshFormTextWidget(Text text
, Node entity
,
55 String tmpStr
= AkbJcrUtils
.get(entity
, propName
);
56 if (AkbJcrUtils
.checkNotEmptyString(tmpStr
))
58 text
.setEnabled(AkbJcrUtils
.isNodeCheckedOutByMe(entity
));
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
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
);
77 * Shortcut to refresh a Check box <code>Button</code> widget given a Node
78 * in a form and a property Name.
80 public static boolean refreshCheckBoxWidget(Button button
, Node entity
,
84 if (entity
.hasProperty(propName
)) {
85 tmp
= entity
.getProperty(propName
).getBoolean();
86 button
.setSelection(tmp
);
88 } catch (RepositoryException re
) {
89 throw new AkbException("unable get boolean value for property "
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.
100 public static void addTextModifyListener(final Text text
, final Node node
,
101 final String propName
, final AbstractFormPart part
) {
102 text
.addModifyListener(new ModifyListener() {
104 public void modifyText(ModifyEvent event
) {
105 if (setJcrProperty(node
, propName
, PropertyType
.STRING
,
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
117 * @return true if the value as changed
119 public static boolean setJcrProperty(Node node
, String propName
,
120 int propertyType
, Object value
) {
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
)
130 // workaround the fact that the Text widget value cannot be
133 else if (node
.hasProperty(propName
)
134 && node
.getProperty(propName
).getString()
135 .equals((String
) value
))
136 // nothing changed yet
139 node
.setProperty(propName
, (String
) value
);
142 case PropertyType
.BOOLEAN
:
143 if (node
.hasProperty(propName
)
144 && node
.getProperty(propName
).getBoolean() == (Boolean
) value
)
145 // nothing changed yet
148 node
.setProperty(propName
, (Boolean
) value
);
151 case PropertyType
.DATE
:
152 if (node
.hasProperty(propName
)
153 && node
.getProperty(propName
).getDate()
154 .equals((Calendar
) value
))
155 // nothing changed yet
158 node
.setProperty(propName
, (Calendar
) value
);
161 case PropertyType
.LONG
:
162 Long lgValue
= (Long
) value
;
167 if (node
.hasProperty(propName
)
168 && node
.getProperty(propName
).getLong() == lgValue
)
169 // nothing changed yet
172 node
.setProperty(propName
, lgValue
);
177 throw new AkbException("Unimplemented save for property type: "
178 + propertyType
+ " - property: " + propName
);
181 } catch (RepositoryException re
) {
182 throw new AkbException("Error while setting property" + propName
183 + " - propertyType: " + propertyType
, re
);
187 // ////////////////////////
188 // LAYOUTS AND STYLES
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
);
200 public static void setTableDefaultStyle(TableViewer viewer
,
201 int customItemHeight
) {
202 Table table
= viewer
.getTable();
203 table
.setLinesVisible(true);
204 table
.setHeaderVisible(false);
208 * Shortcut to provide a gridlayout with no margin and no spacing (dafault
211 public static GridLayout
gridLayoutNoBorder() {
212 return gridLayoutNoBorder(1);
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).
219 public static GridLayout
gridLayoutNoBorder(int nbOfCol
) {
220 GridLayout gl
= new GridLayout(nbOfCol
, false);
221 gl
.marginWidth
= gl
.marginHeight
= gl
.horizontalSpacing
= gl
.verticalSpacing
= 0;
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
230 text
.setMessage(msg
);
231 text
.setToolTipText(toolTip
);
232 text
.setLayoutData(new RowData(width
, SWT
.DEFAULT
));
237 * Creates a text widget with GridData already set
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
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
);
261 * Shortcut to quickly get a FormData object with configured FormAttachment
269 public static FormData
createformData(int left
, int top
, int right
,
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);
279 // //////////////////////////////
281 public static CommandContributionItem
createContributionItem(
282 IMenuManager menuManager
, IServiceLocator locator
, String itemId
,
283 String cmdId
, String label
, ImageDescriptor icon
,
284 Map
<String
, String
> params
) {
286 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(
287 locator
, itemId
, cmdId
, SWT
.PUSH
);
289 contributionItemParameter
.label
= label
;
290 contributionItemParameter
.icon
= icon
;
293 contributionItemParameter
.parameters
= params
;
294 CommandContributionItem cci
= new CommandContributionItem(
295 contributionItemParameter
);
300 * Commodities the refresh of a single command with a map of parameters in a
301 * Menu.aboutToShow method to simplify further development
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
);
316 menuManager
.remove(ici
);
318 menuManager
.add(createContributionItem(menuManager
, locator
,
319 itemId
, cmdId
, label
, icon
, params
));