1 package org
.argeo
.eclipse
.ui
.jcr
;
3 import java
.util
.Calendar
;
6 import javax
.jcr
.PropertyType
;
7 import javax
.jcr
.RepositoryException
;
9 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
10 import org
.argeo
.eclipse
.ui
.jcr
.lists
.NodeViewerComparator
;
11 import org
.argeo
.eclipse
.ui
.jcr
.lists
.RowViewerComparator
;
12 import org
.eclipse
.jface
.viewers
.TableViewer
;
13 import org
.eclipse
.swt
.SWT
;
14 import org
.eclipse
.swt
.events
.SelectionAdapter
;
15 import org
.eclipse
.swt
.events
.SelectionEvent
;
16 import org
.eclipse
.swt
.widgets
.Table
;
18 /** Utility methods to simplify UI development using SWT (or RWT), jface and JCR. */
19 public class JcrUiUtils
{
22 * Centralizes management of updating property value. Among other to avoid
23 * infinite loop when the new value is the same as the ones that is already
26 * @return true if the value as changed
28 public static boolean setJcrProperty(Node node
, String propName
,
29 int propertyType
, Object value
) {
31 switch (propertyType
) {
32 case PropertyType
.STRING
:
33 if ("".equals((String
) value
)
34 && (!node
.hasProperty(propName
) || node
35 .hasProperty(propName
)
36 && "".equals(node
.getProperty(propName
)
38 // workaround the fact that the Text widget value cannot be
41 else if (node
.hasProperty(propName
)
42 && node
.getProperty(propName
).getString()
43 .equals((String
) value
))
44 // nothing changed yet
47 node
.setProperty(propName
, (String
) value
);
50 case PropertyType
.BOOLEAN
:
51 if (node
.hasProperty(propName
)
52 && node
.getProperty(propName
).getBoolean() == (Boolean
) value
)
53 // nothing changed yet
56 node
.setProperty(propName
, (Boolean
) value
);
59 case PropertyType
.DATE
:
60 if (node
.hasProperty(propName
)
61 && node
.getProperty(propName
).getDate()
62 .equals((Calendar
) value
))
63 // nothing changed yet
66 node
.setProperty(propName
, (Calendar
) value
);
69 case PropertyType
.LONG
:
70 Long lgValue
= (Long
) value
;
75 if (node
.hasProperty(propName
)
76 && node
.getProperty(propName
).getLong() == lgValue
)
77 // nothing changed yet
80 node
.setProperty(propName
, lgValue
);
85 throw new EclipseUiException("Unimplemented property save");
87 } catch (RepositoryException re
) {
88 throw new EclipseUiException("Unexpected error while setting property",
94 * Creates a new selection adapter in order to provide sorting abitily on a
95 * SWT Table that display a row list
97 public static SelectionAdapter
getRowSelectionAdapter(final int index
,
98 final int propertyType
, final String selectorName
,
99 final String propertyName
, final RowViewerComparator comparator
,
100 final TableViewer viewer
) {
101 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
102 private static final long serialVersionUID
= -5738918304901437720L;
105 public void widgetSelected(SelectionEvent e
) {
106 Table table
= viewer
.getTable();
107 comparator
.setColumn(propertyType
, selectorName
, propertyName
);
108 int dir
= table
.getSortDirection();
109 if (table
.getSortColumn() == table
.getColumn(index
)) {
110 dir
= dir
== SWT
.UP ? SWT
.DOWN
: SWT
.UP
;
114 table
.setSortDirection(dir
);
115 table
.setSortColumn(table
.getColumn(index
));
119 return selectionAdapter
;
123 * Creates a new selection adapter in order to provide sorting abitily on a
124 * swt table that display a row list
126 public static SelectionAdapter
getNodeSelectionAdapter(final int index
,
127 final int propertyType
, final String propertyName
,
128 final NodeViewerComparator comparator
, final TableViewer viewer
) {
129 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
130 private static final long serialVersionUID
= -1683220869195484625L;
133 public void widgetSelected(SelectionEvent e
) {
134 Table table
= viewer
.getTable();
135 comparator
.setColumn(propertyType
, propertyName
);
136 int dir
= table
.getSortDirection();
137 if (table
.getSortColumn() == table
.getColumn(index
)) {
138 dir
= dir
== SWT
.UP ? SWT
.DOWN
: SWT
.UP
;
142 table
.setSortDirection(dir
);
143 table
.setSortColumn(table
.getColumn(index
));
147 return selectionAdapter
;