]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/jcr/JcrUiUtils.java
Add some utilities to eclipse UI Utils to ease UI implementation.
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / 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 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 switch (propertyType) {
32 case PropertyType.STRING:
33 if ("".equals((String) value)
34 && (!node.hasProperty(propName) || node
35 .hasProperty(propName)
36 && "".equals(node.getProperty(propName)
37 .getString())))
38 // workaround the fact that the Text widget value cannot be
39 // set to null
40 return false;
41 else if (node.hasProperty(propName)
42 && node.getProperty(propName).getString()
43 .equals((String) value))
44 // nothing changed yet
45 return false;
46 else {
47 node.setProperty(propName, (String) value);
48 return true;
49 }
50 case PropertyType.BOOLEAN:
51 if (node.hasProperty(propName)
52 && node.getProperty(propName).getBoolean() == (Boolean) value)
53 // nothing changed yet
54 return false;
55 else {
56 node.setProperty(propName, (Boolean) value);
57 return true;
58 }
59 case PropertyType.DATE:
60 if (node.hasProperty(propName)
61 && node.getProperty(propName).getDate()
62 .equals((Calendar) value))
63 // nothing changed yet
64 return false;
65 else {
66 node.setProperty(propName, (Calendar) value);
67 return true;
68 }
69 case PropertyType.LONG:
70 Long lgValue = (Long) value;
71
72 if (lgValue == null)
73 lgValue = 0L;
74
75 if (node.hasProperty(propName)
76 && node.getProperty(propName).getLong() == lgValue)
77 // nothing changed yet
78 return false;
79 else {
80 node.setProperty(propName, lgValue);
81 return true;
82 }
83
84 default:
85 throw new ArgeoException("Unimplemented property save");
86 }
87 } catch (RepositoryException re) {
88 throw new ArgeoException("Unexpected error while setting property",
89 re);
90 }
91 }
92
93 /**
94 * Creates a new selection adapter in order to provide sorting abitily on a
95 * SWT Table that display a row list
96 **/
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;
103
104 @Override
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;
111 } else {
112 dir = SWT.DOWN;
113 }
114 table.setSortDirection(dir);
115 table.setSortColumn(table.getColumn(index));
116 viewer.refresh();
117 }
118 };
119 return selectionAdapter;
120 }
121
122 /**
123 * Creates a new selection adapter in order to provide sorting abitily on a
124 * swt table that display a row list
125 **/
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;
131
132 @Override
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;
139 } else {
140 dir = SWT.DOWN;
141 }
142 table.setSortDirection(dir);
143 table.setSortColumn(table.getColumn(index));
144 viewer.refresh();
145 }
146 };
147 return selectionAdapter;
148 }
149 }