]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/utils/ArtifactsTableConfigurer.java
Merge branch 'master' of https://github.com/argeo/argeo-slc.git
[gpl/argeo-slc.git] / org.argeo.slc.client.ui.dist / src / org / argeo / slc / client / ui / dist / utils / ArtifactsTableConfigurer.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.client.ui.dist.utils;
17
18 import java.text.DateFormat;
19 import java.text.SimpleDateFormat;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import javax.jcr.PropertyType;
24 import javax.jcr.RepositoryException;
25 import javax.jcr.Value;
26 import javax.jcr.query.Row;
27
28 import org.argeo.eclipse.ui.GenericTableComparator;
29 import org.argeo.slc.SlcException;
30 import org.argeo.slc.SlcNames;
31 import org.argeo.slc.SlcTypes;
32 import org.argeo.slc.client.ui.dist.DistConstants;
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;
42
43 /**
44 * Centralizes and factorizes useful methods to create and manage tables that
45 * display artifacts for both editors and views.
46 */
47 public class ArtifactsTableConfigurer implements SlcNames, SlcTypes,
48 DistConstants {
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>();
54
55 private CurrentTableComparator comparator;
56 private TableViewer viewer;
57
58 protected DateFormat timeFormatter = new SimpleDateFormat(DATE_TIME_FORMAT);
59
60 /**
61 * Create and initialize the table configurer.
62 */
63 public ArtifactsTableConfigurer(TableViewer viewer,
64 int defaultSortColumnIndex, int direction) {
65 this.viewer = viewer;
66 comparator = new CurrentTableComparator(defaultSortColumnIndex,
67 direction);
68 }
69
70 public GenericTableComparator getComparator() {
71 return comparator;
72 }
73
74 /**
75 * Configure column width and header label depending on the value that will
76 * be displayed in the current column.
77 *
78 * @param jcrColumnName
79 * @param column
80 * @param columnIndex
81 */
82 public void configureColumn(String jcrColumnName, TableViewerColumn column,
83 int columnIndex) {
84
85 if (columnIndex != -1
86 && getSelectionAdapter(column.getColumn(), columnIndex) != null) {
87 column.getColumn().addSelectionListener(
88 getSelectionAdapter(column.getColumn(), columnIndex));
89 indexToName.put(new Integer(columnIndex), jcrColumnName);
90 }
91 Object[] objs = DistUiHelpers
92 .getLabelAndDefaultValueWidth(jcrColumnName);
93 column.getColumn().setWidth((Integer) objs[1]);
94 column.getColumn().setText((String) objs[0]);
95 }
96
97 /**
98 * Might be used by client classes to sort the table with based on selected
99 * columns.
100 *
101 * @param column
102 * @param index
103 * @return
104 */
105 public SelectionAdapter getSelectionAdapter(final TableColumn column,
106 final int index) {
107
108 // A comparator must be define
109 if (comparator == null)
110 return null;
111
112 SelectionAdapter selectionAdapter = new SelectionAdapter() {
113 private static final long serialVersionUID = 5239138629878556374L;
114
115 @Override
116 public void widgetSelected(SelectionEvent e) {
117
118 try {
119
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;
124 } else {
125
126 dir = SWT.DOWN;
127 }
128 viewer.getTable().setSortDirection(dir);
129 viewer.getTable().setSortColumn(column);
130 viewer.refresh();
131 } catch (Exception exc) {
132 exc.printStackTrace();
133 }
134 }
135 };
136 return selectionAdapter;
137 }
138
139 /**
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.
143 */
144 public ColumnLabelProvider getLabelProvider(final String columnName) {
145 boolean test = false;
146
147 if (test) {
148 return new ColumnLabelProvider() {
149 private static final long serialVersionUID = 7996387354459551737L;
150
151 public String getText(Object element) {
152 return null;
153 }
154
155 public Image getImage(Object element) {
156 return null;
157 }
158 };
159 } else
160 return new ColumnLabelProvider() {
161 private static final long serialVersionUID = 6746632988975282759L;
162
163 public String getText(Object element) {
164 Row row = (Row) element;
165 try {
166 return row.getValue(columnName).getString();
167 } catch (RepositoryException e) {
168 throw new SlcException("Cannot display row " + row, e);
169 }
170 }
171
172 public Image getImage(Object element) {
173 return null;
174 }
175 };
176 }
177
178 /** Implements comparator for various types of Artifact Table row */
179 private class CurrentTableComparator extends GenericTableComparator {
180 private static final long serialVersionUID = -4737460932326339442L;
181
182 public CurrentTableComparator(int colIndex, int direction) {
183 super(colIndex, direction);
184 }
185
186 @Override
187 public int compare(Viewer viewer, Object e1, Object e2) {
188 int rc = 0;
189
190 if (e1 instanceof Row) {
191 try {
192
193 Value v1 = ((Row) e1).getValue(indexToName
194 .get(propertyIndex));
195 Value v2 = ((Row) e2).getValue(indexToName
196 .get(propertyIndex));
197
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());
202 else
203 throw new SlcException("comparator for object type "
204 + v1.getType() + " is not yet implemented");
205 } catch (Exception e) {
206 throw new SlcException("rows cannot be compared ", e);
207 }
208 } else
209 throw new SlcException("Unsupported row type");
210 // If descending order, flip the direction
211 if (direction == DESCENDING) {
212 rc = -rc;
213 }
214 return rc;
215 }
216 }
217 }