]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/utils/ArtifactsTableConfigurer.java
Merge remote-tracking branch 'origin/master' into testing
[gpl/argeo-slc.git] / legacy / org.argeo.slc.client.ui.dist / src / org / argeo / slc / client / ui / dist / utils / ArtifactsTableConfigurer.java
1 package org.argeo.slc.client.ui.dist.utils;
2
3 import java.text.DateFormat;
4 import java.text.SimpleDateFormat;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import javax.jcr.PropertyType;
9 import javax.jcr.RepositoryException;
10 import javax.jcr.Value;
11 import javax.jcr.query.Row;
12
13 import org.argeo.eclipse.ui.GenericTableComparator;
14 import org.argeo.slc.SlcException;
15 import org.argeo.slc.SlcNames;
16 import org.argeo.slc.SlcTypes;
17 import org.argeo.slc.client.ui.dist.DistConstants;
18 import org.eclipse.jface.viewers.ColumnLabelProvider;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.jface.viewers.TableViewerColumn;
21 import org.eclipse.jface.viewers.Viewer;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.widgets.TableColumn;
27
28 /**
29 * Centralizes and factorizes useful methods to create and manage tables that
30 * display artifacts for both editors and views.
31 */
32 public class ArtifactsTableConfigurer implements SlcNames, SlcTypes,
33 DistConstants {
34 // private final static Log log = LogFactory
35 // .getLog(ArtifactsTableConfigurer.class);
36 // Used in the comparator to be able to retrieve the value from a row
37 // knowing the corresponding column index.
38 private Map<Integer, String> indexToName = new HashMap<Integer, String>();
39
40 private CurrentTableComparator comparator;
41 private TableViewer viewer;
42
43 protected DateFormat timeFormatter = new SimpleDateFormat(DATE_TIME_FORMAT);
44
45 /**
46 * Create and initialize the table configurer.
47 */
48 public ArtifactsTableConfigurer(TableViewer viewer,
49 int defaultSortColumnIndex, int direction) {
50 this.viewer = viewer;
51 comparator = new CurrentTableComparator(defaultSortColumnIndex,
52 direction);
53 }
54
55 public GenericTableComparator getComparator() {
56 return comparator;
57 }
58
59 /**
60 * Configure column width and header label depending on the value that will
61 * be displayed in the current column.
62 *
63 * @param jcrColumnName
64 * @param column
65 * @param columnIndex
66 */
67 public void configureColumn(String jcrColumnName, TableViewerColumn column,
68 int columnIndex) {
69
70 if (columnIndex != -1
71 && getSelectionAdapter(column.getColumn(), columnIndex) != null) {
72 column.getColumn().addSelectionListener(
73 getSelectionAdapter(column.getColumn(), columnIndex));
74 indexToName.put(new Integer(columnIndex), jcrColumnName);
75 }
76 Object[] objs = DistUiHelpers
77 .getLabelAndDefaultValueWidth(jcrColumnName);
78 column.getColumn().setWidth((Integer) objs[1]);
79 column.getColumn().setText((String) objs[0]);
80 }
81
82 /**
83 * Might be used by client classes to sort the table with based on selected
84 * columns.
85 *
86 * @param column
87 * @param index
88 * @return
89 */
90 public SelectionAdapter getSelectionAdapter(final TableColumn column,
91 final int index) {
92
93 // A comparator must be define
94 if (comparator == null)
95 return null;
96
97 SelectionAdapter selectionAdapter = new SelectionAdapter() {
98 private static final long serialVersionUID = 5239138629878556374L;
99
100 @Override
101 public void widgetSelected(SelectionEvent e) {
102
103 try {
104
105 comparator.setColumn(index);
106 int dir = viewer.getTable().getSortDirection();
107 if (viewer.getTable().getSortColumn() == column) {
108 dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
109 } else {
110
111 dir = SWT.DOWN;
112 }
113 viewer.getTable().setSortDirection(dir);
114 viewer.getTable().setSortColumn(column);
115 viewer.refresh();
116 } catch (Exception exc) {
117 exc.printStackTrace();
118 }
119 }
120 };
121 return selectionAdapter;
122 }
123
124 /**
125 * provides a label provider that returns the content of a specific cell.
126 * Specific treatment is done for some columns when the query returns a code
127 * that must be translated to the corresponding value at display time.
128 */
129 public ColumnLabelProvider getLabelProvider(final String columnName) {
130 boolean test = false;
131
132 if (test) {
133 return new ColumnLabelProvider() {
134 private static final long serialVersionUID = 7996387354459551737L;
135
136 public String getText(Object element) {
137 return null;
138 }
139
140 public Image getImage(Object element) {
141 return null;
142 }
143 };
144 } else
145 return new ColumnLabelProvider() {
146 private static final long serialVersionUID = 6746632988975282759L;
147
148 public String getText(Object element) {
149 Row row = (Row) element;
150 try {
151 return row.getValue(columnName).getString();
152 } catch (RepositoryException e) {
153 throw new SlcException("Cannot display row " + row, e);
154 }
155 }
156
157 public Image getImage(Object element) {
158 return null;
159 }
160 };
161 }
162
163 /** Implements comparator for various types of Artifact Table row */
164 private class CurrentTableComparator extends GenericTableComparator {
165 private static final long serialVersionUID = -4737460932326339442L;
166
167 public CurrentTableComparator(int colIndex, int direction) {
168 super(colIndex, direction);
169 }
170
171 @Override
172 public int compare(Viewer viewer, Object e1, Object e2) {
173 int rc = 0;
174
175 if (e1 instanceof Row) {
176 try {
177
178 Value v1 = ((Row) e1).getValue(indexToName
179 .get(propertyIndex));
180 Value v2 = ((Row) e2).getValue(indexToName
181 .get(propertyIndex));
182
183 if (v1.getType() == PropertyType.STRING)
184 rc = v1.getString().compareTo(v2.getString());
185 else if (v1.getType() == PropertyType.DATE)
186 rc = v1.getDate().compareTo(v2.getDate());
187 else
188 throw new SlcException("comparator for object type "
189 + v1.getType() + " is not yet implemented");
190 } catch (Exception e) {
191 throw new SlcException("rows cannot be compared ", e);
192 }
193 } else
194 throw new SlcException("Unsupported row type");
195 // If descending order, flip the direction
196 if (direction == DESCENDING) {
197 rc = -rc;
198 }
199 return rc;
200 }
201 }
202 }