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
.ArgeoException
;
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 eclipse 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 // int propertyType = getPic().getProperty(propName).getType();
32 switch (propertyType
) {
33 case PropertyType
.STRING
:
34 if ("".equals((String
) value
)
35 && (!node
.hasProperty(propName
) || node
36 .hasProperty(propName
)
37 && "".equals(node
.getProperty(propName
)
39 // workaround the fact that the Text widget value cannot be
42 else if (node
.hasProperty(propName
)
43 && node
.getProperty(propName
).getString()
44 .equals((String
) value
))
45 // nothing changed yet
48 node
.setProperty(propName
, (String
) value
);
51 case PropertyType
.BOOLEAN
:
52 if (node
.hasProperty(propName
)
53 && node
.getProperty(propName
).getBoolean() == (Boolean
) value
)
54 // nothing changed yet
57 node
.setProperty(propName
, (Boolean
) value
);
60 case PropertyType
.DATE
:
61 if (node
.hasProperty(propName
)
62 && node
.getProperty(propName
).getDate()
63 .equals((Calendar
) value
))
64 // nothing changed yet
67 node
.setProperty(propName
, (Calendar
) value
);
70 case PropertyType
.LONG
:
71 Long lgValue
= (Long
) value
;
76 if (node
.hasProperty(propName
)
77 && node
.getProperty(propName
).getLong() == lgValue
)
78 // nothing changed yet
81 node
.setProperty(propName
, lgValue
);
86 throw new ArgeoException("Unimplemented property save");
88 } catch (RepositoryException re
) {
89 throw new ArgeoException("Unexpected error while setting property",
95 * Creates a new selection adapter in order to provide sorting abitily on a
96 * swt table that display a row list
98 public static SelectionAdapter
getRowSelectionAdapter(final int index
,
99 final int propertyType
, final String selectorName
,
100 final String propertyName
, final RowViewerComparator comparator
,
101 final TableViewer viewer
) {
102 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
103 private static final long serialVersionUID
= -5738918304901437720L;
106 public void widgetSelected(SelectionEvent e
) {
107 Table table
= viewer
.getTable();
108 comparator
.setColumn(propertyType
, selectorName
, propertyName
);
109 int dir
= table
.getSortDirection();
110 if (table
.getSortColumn() == table
.getColumn(index
)) {
111 dir
= dir
== SWT
.UP ? SWT
.DOWN
: SWT
.UP
;
115 table
.setSortDirection(dir
);
116 table
.setSortColumn(table
.getColumn(index
));
120 return selectionAdapter
;
124 * Creates a new selection adapter in order to provide sorting abitily on a
125 * swt table that display a row list
127 public static SelectionAdapter
getNodeSelectionAdapter(final int index
,
128 final int propertyType
, final String propertyName
,
129 final NodeViewerComparator comparator
, final TableViewer viewer
) {
130 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
131 private static final long serialVersionUID
= -1683220869195484625L;
134 public void widgetSelected(SelectionEvent e
) {
135 Table table
= viewer
.getTable();
136 comparator
.setColumn(propertyType
, propertyName
);
137 int dir
= table
.getSortDirection();
138 if (table
.getSortColumn() == table
.getColumn(index
)) {
139 dir
= dir
== SWT
.UP ? SWT
.DOWN
: SWT
.UP
;
143 table
.setSortDirection(dir
);
144 table
.setSortColumn(table
.getColumn(index
));
148 return selectionAdapter
;