1 package org
.argeo
.slc
.client
.ui
.dist
.utils
;
3 import java
.text
.DateFormat
;
4 import java
.text
.SimpleDateFormat
;
5 import java
.util
.HashMap
;
8 import javax
.jcr
.PropertyType
;
9 import javax
.jcr
.RepositoryException
;
10 import javax
.jcr
.Value
;
11 import javax
.jcr
.query
.Row
;
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
;
29 * Centralizes and factorizes useful methods to create and manage tables that
30 * display artifacts for both editors and views.
32 public class ArtifactsTableConfigurer
implements SlcNames
, SlcTypes
,
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
>();
40 private CurrentTableComparator comparator
;
41 private TableViewer viewer
;
43 protected DateFormat timeFormatter
= new SimpleDateFormat(DATE_TIME_FORMAT
);
46 * Create and initialize the table configurer.
48 public ArtifactsTableConfigurer(TableViewer viewer
,
49 int defaultSortColumnIndex
, int direction
) {
51 comparator
= new CurrentTableComparator(defaultSortColumnIndex
,
55 public GenericTableComparator
getComparator() {
60 * Configure column width and header label depending on the value that will
61 * be displayed in the current column.
63 * @param jcrColumnName
67 public void configureColumn(String jcrColumnName
, TableViewerColumn column
,
71 && getSelectionAdapter(column
.getColumn(), columnIndex
) != null) {
72 column
.getColumn().addSelectionListener(
73 getSelectionAdapter(column
.getColumn(), columnIndex
));
74 indexToName
.put(new Integer(columnIndex
), jcrColumnName
);
76 Object
[] objs
= DistUiHelpers
77 .getLabelAndDefaultValueWidth(jcrColumnName
);
78 column
.getColumn().setWidth((Integer
) objs
[1]);
79 column
.getColumn().setText((String
) objs
[0]);
83 * Might be used by client classes to sort the table with based on selected
90 public SelectionAdapter
getSelectionAdapter(final TableColumn column
,
93 // A comparator must be define
94 if (comparator
== null)
97 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
98 private static final long serialVersionUID
= 5239138629878556374L;
101 public void widgetSelected(SelectionEvent e
) {
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
;
113 viewer
.getTable().setSortDirection(dir
);
114 viewer
.getTable().setSortColumn(column
);
116 } catch (Exception exc
) {
117 exc
.printStackTrace();
121 return selectionAdapter
;
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.
129 public ColumnLabelProvider
getLabelProvider(final String columnName
) {
130 boolean test
= false;
133 return new ColumnLabelProvider() {
134 private static final long serialVersionUID
= 7996387354459551737L;
136 public String
getText(Object element
) {
140 public Image
getImage(Object element
) {
145 return new ColumnLabelProvider() {
146 private static final long serialVersionUID
= 6746632988975282759L;
148 public String
getText(Object element
) {
149 Row row
= (Row
) element
;
151 return row
.getValue(columnName
).getString();
152 } catch (RepositoryException e
) {
153 throw new SlcException("Cannot display row " + row
, e
);
157 public Image
getImage(Object element
) {
163 /** Implements comparator for various types of Artifact Table row */
164 private class CurrentTableComparator
extends GenericTableComparator
{
165 private static final long serialVersionUID
= -4737460932326339442L;
167 public CurrentTableComparator(int colIndex
, int direction
) {
168 super(colIndex
, direction
);
172 public int compare(Viewer viewer
, Object e1
, Object e2
) {
175 if (e1
instanceof Row
) {
178 Value v1
= ((Row
) e1
).getValue(indexToName
179 .get(propertyIndex
));
180 Value v2
= ((Row
) e2
).getValue(indexToName
181 .get(propertyIndex
));
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());
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
);
194 throw new SlcException("Unsupported row type");
195 // If descending order, flip the direction
196 if (direction
== DESCENDING
) {