]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/JcrUiUtils.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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 SWT (or RWT), jface 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 private static final long serialVersionUID = -5738918304901437720L;
104
105 @Override
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;
112 } else {
113 dir = SWT.DOWN;
114 }
115 table.setSortDirection(dir);
116 table.setSortColumn(table.getColumn(index));
117 viewer.refresh();
118 }
119 };
120 return selectionAdapter;
121 }
122
123 /**
124 * Creates a new selection adapter in order to provide sorting abitily on a
125 * swt table that display a row list
126 **/
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;
132
133 @Override
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;
140 } else {
141 dir = SWT.DOWN;
142 }
143 table.setSortDirection(dir);
144 table.setSortColumn(table.getColumn(index));
145 viewer.refresh();
146 }
147 };
148 return selectionAdapter;
149 }
150 }