]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/JcrUiUtils.java
Merge from commons 1.2.x, see following bugs:
[lgpl/argeo-commons.git] / base / runtime / org.argeo.eclipse.ui.jcr / src / main / java / org / argeo / eclipse / ui / jcr / JcrUiUtils.java
1 package org.argeo.eclipse.ui.jcr;
2
3 import java.util.Calendar;
4
5 import javax.jcr.Node;
6 import javax.jcr.PropertyType;
7 import javax.jcr.RepositoryException;
8
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;
17
18 /** Utility methods to simplify UI development using eclipse and JCR. */
19 public class JcrUiUtils {
20
21 /**
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
24 * stored in JCR.
25 *
26 * @return true if the value as changed
27 */
28 public static boolean setJcrProperty(Node node, String propName,
29 int propertyType, Object value) {
30 try {
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)
38 .getString())))
39 // workaround the fact that the Text widget value cannot be
40 // set to null
41 return false;
42 else if (node.hasProperty(propName)
43 && node.getProperty(propName).getString()
44 .equals((String) value))
45 // nothing changed yet
46 return false;
47 else {
48 node.setProperty(propName, (String) value);
49 return true;
50 }
51 case PropertyType.BOOLEAN:
52 if (node.hasProperty(propName)
53 && node.getProperty(propName).getBoolean() == (Boolean) value)
54 // nothing changed yet
55 return false;
56 else {
57 node.setProperty(propName, (Boolean) value);
58 return true;
59 }
60 case PropertyType.DATE:
61 if (node.hasProperty(propName)
62 && node.getProperty(propName).getDate()
63 .equals((Calendar) value))
64 // nothing changed yet
65 return false;
66 else {
67 node.setProperty(propName, (Calendar) value);
68 return true;
69 }
70 case PropertyType.LONG:
71 Long lgValue = (Long) value;
72
73 if (lgValue == null)
74 lgValue = 0L;
75
76 if (node.hasProperty(propName)
77 && node.getProperty(propName).getLong() == lgValue)
78 // nothing changed yet
79 return false;
80 else {
81 node.setProperty(propName, lgValue);
82 return true;
83 }
84
85 default:
86 throw new ArgeoException("Unimplemented property save");
87 }
88 } catch (RepositoryException re) {
89 throw new ArgeoException("Unexpected error while setting property",
90 re);
91 }
92 }
93
94 /**
95 * Creates a new selection adapter in order to provide sorting abitily on a
96 * swt table that display a row list
97 **/
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 @Override
104 public void widgetSelected(SelectionEvent e) {
105 Table table = viewer.getTable();
106 comparator.setColumn(propertyType, selectorName, propertyName);
107 int dir = table.getSortDirection();
108 if (table.getSortColumn() == table.getColumn(index)) {
109 dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
110 } else {
111 dir = SWT.DOWN;
112 }
113 table.setSortDirection(dir);
114 table.setSortColumn(table.getColumn(index));
115 viewer.refresh();
116 }
117 };
118 return selectionAdapter;
119 }
120
121 /**
122 * Creates a new selection adapter in order to provide sorting abitily on a
123 * swt table that display a row list
124 **/
125 public static SelectionAdapter getNodeSelectionAdapter(final int index,
126 final int propertyType, final String propertyName,
127 final NodeViewerComparator comparator, final TableViewer viewer) {
128 SelectionAdapter selectionAdapter = new SelectionAdapter() {
129 @Override
130 public void widgetSelected(SelectionEvent e) {
131 Table table = viewer.getTable();
132 comparator.setColumn(propertyType, propertyName);
133 int dir = table.getSortDirection();
134 if (table.getSortColumn() == table.getColumn(index)) {
135 dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
136 } else {
137 dir = SWT.DOWN;
138 }
139 table.setSortDirection(dir);
140 table.setSortColumn(table.getColumn(index));
141 viewer.refresh();
142 }
143 };
144 return selectionAdapter;
145 }
146 }