2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.client
.ui
.dist
.utils
;
18 import java
.text
.DateFormat
;
19 import java
.text
.SimpleDateFormat
;
20 import java
.util
.HashMap
;
23 import javax
.jcr
.PropertyType
;
24 import javax
.jcr
.RepositoryException
;
25 import javax
.jcr
.Value
;
26 import javax
.jcr
.query
.Row
;
28 import org
.argeo
.ArgeoException
;
29 import org
.argeo
.eclipse
.ui
.GenericTableComparator
;
30 import org
.argeo
.slc
.client
.ui
.dist
.DistConstants
;
31 import org
.argeo
.slc
.jcr
.SlcNames
;
32 import org
.argeo
.slc
.jcr
.SlcTypes
;
33 import org
.eclipse
.jface
.viewers
.ColumnLabelProvider
;
34 import org
.eclipse
.jface
.viewers
.TableViewer
;
35 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
36 import org
.eclipse
.jface
.viewers
.Viewer
;
37 import org
.eclipse
.swt
.SWT
;
38 import org
.eclipse
.swt
.events
.SelectionAdapter
;
39 import org
.eclipse
.swt
.events
.SelectionEvent
;
40 import org
.eclipse
.swt
.graphics
.Image
;
41 import org
.eclipse
.swt
.widgets
.TableColumn
;
44 * Centralizes and factorizes useful methods to create and manage tables that
45 * display artifacts for both editors and views.
47 public class ArtifactsTableConfigurer
implements SlcNames
, SlcTypes
,
49 // private final static Log log = LogFactory
50 // .getLog(ArtifactsTableConfigurer.class);
51 // Used in the comparator to be able to retrieve the value from a row
52 // knowing the corresponding column index.
53 private Map
<Integer
, String
> indexToName
= new HashMap
<Integer
, String
>();
55 private CurrentTableComparator comparator
;
56 private TableViewer viewer
;
58 protected DateFormat timeFormatter
= new SimpleDateFormat(DATE_TIME_FORMAT
);
61 * Create and initialize the table configurer.
63 public ArtifactsTableConfigurer(TableViewer viewer
,
64 int defaultSortColumnIndex
, int direction
) {
66 comparator
= new CurrentTableComparator(defaultSortColumnIndex
,
70 public GenericTableComparator
getComparator() {
75 * Configure column width and header label depending on the value that will
76 * be displayed in the current column.
78 * @param jcrColumnName
82 public void configureColumn(String jcrColumnName
, TableViewerColumn column
,
86 && getSelectionAdapter(column
.getColumn(), columnIndex
) != null) {
87 column
.getColumn().addSelectionListener(
88 getSelectionAdapter(column
.getColumn(), columnIndex
));
89 indexToName
.put(new Integer(columnIndex
), jcrColumnName
);
91 Object
[] objs
= DistUiHelpers
92 .getLabelAndDefaultValueWidth(jcrColumnName
);
93 column
.getColumn().setWidth((Integer
) objs
[1]);
94 column
.getColumn().setText((String
) objs
[0]);
98 * Might be used by client classes to sort the table with based on selected
105 public SelectionAdapter
getSelectionAdapter(final TableColumn column
,
108 // A comparator must be define
109 if (comparator
== null)
112 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
113 private static final long serialVersionUID
= 5239138629878556374L;
116 public void widgetSelected(SelectionEvent e
) {
120 comparator
.setColumn(index
);
121 int dir
= viewer
.getTable().getSortDirection();
122 if (viewer
.getTable().getSortColumn() == column
) {
123 dir
= dir
== SWT
.UP ? SWT
.DOWN
: SWT
.UP
;
128 viewer
.getTable().setSortDirection(dir
);
129 viewer
.getTable().setSortColumn(column
);
131 } catch (Exception exc
) {
132 exc
.printStackTrace();
136 return selectionAdapter
;
140 * provides a label provider that returns the content of a specific cell.
141 * Specific treatment is done for some columns when the query returns a code
142 * that must be translated to the corresponding value at display time.
144 public ColumnLabelProvider
getLabelProvider(final String columnName
) {
145 boolean test
= false;
148 return new ColumnLabelProvider() {
149 private static final long serialVersionUID
= 7996387354459551737L;
151 public String
getText(Object element
) {
155 public Image
getImage(Object element
) {
160 return new ColumnLabelProvider() {
161 private static final long serialVersionUID
= 6746632988975282759L;
163 public String
getText(Object element
) {
164 Row row
= (Row
) element
;
166 return row
.getValue(columnName
).getString();
167 } catch (RepositoryException e
) {
168 throw new ArgeoException("Cannot display row " + row
, e
);
172 public Image
getImage(Object element
) {
178 /** Implements comparator for various types of Artifact Table row */
179 private class CurrentTableComparator
extends GenericTableComparator
{
180 private static final long serialVersionUID
= -4737460932326339442L;
182 public CurrentTableComparator(int colIndex
, int direction
) {
183 super(colIndex
, direction
);
187 public int compare(Viewer viewer
, Object e1
, Object e2
) {
190 if (e1
instanceof Row
) {
193 Value v1
= ((Row
) e1
).getValue(indexToName
194 .get(propertyIndex
));
195 Value v2
= ((Row
) e2
).getValue(indexToName
196 .get(propertyIndex
));
198 if (v1
.getType() == PropertyType
.STRING
)
199 rc
= v1
.getString().compareTo(v2
.getString());
200 else if (v1
.getType() == PropertyType
.DATE
)
201 rc
= v1
.getDate().compareTo(v2
.getDate());
203 throw new ArgeoException("comparator for object type "
204 + v1
.getType() + " is not yet implemented");
205 } catch (Exception e
) {
206 throw new ArgeoException("rows cannot be compared ", e
);
209 throw new ArgeoException("Unsupported row type");
210 // If descending order, flip the direction
211 if (direction
== DESCENDING
) {