]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/utils/ArtifactsTableConfigurer.java
Fix commands in dist UI
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / 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.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.argeo.ArgeoException;
31 import org.argeo.eclipse.ui.GenericTableComparator;
32 import org.argeo.slc.client.ui.dist.DistConstants;
33 import org.argeo.slc.jcr.SlcNames;
34 import org.argeo.slc.jcr.SlcTypes;
35 import org.eclipse.jface.viewers.ColumnLabelProvider;
36 import org.eclipse.jface.viewers.TableViewer;
37 import org.eclipse.jface.viewers.TableViewerColumn;
38 import org.eclipse.jface.viewers.Viewer;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.events.SelectionAdapter;
41 import org.eclipse.swt.events.SelectionEvent;
42 import org.eclipse.swt.graphics.Image;
43 import org.eclipse.swt.widgets.TableColumn;
44
45 /**
46 * Centralizes and factorizes useful methods to create and manage tables that
47 * display artifacts for both editors and views.
48 */
49 public class ArtifactsTableConfigurer implements SlcNames, SlcTypes,
50 DistConstants {
51 private final static Log log = LogFactory
52 .getLog(ArtifactsTableConfigurer.class);
53 // Used in the comparator to be able to retrieve the value from a row
54 // knowing the corresponding column index.
55 private Map<Integer, String> indexToName = new HashMap<Integer, String>();
56
57 private CurrentTableComparator comparator;
58 private TableViewer viewer;
59
60 protected DateFormat timeFormatter = new SimpleDateFormat(DATE_TIME_FORMAT);
61
62 /**
63 * Create and initialize the table configurer.
64 */
65 public ArtifactsTableConfigurer(TableViewer viewer,
66 int defaultSortColumnIndex, int direction) {
67 this.viewer = viewer;
68 comparator = new CurrentTableComparator(defaultSortColumnIndex,
69 direction);
70 }
71
72 public GenericTableComparator getComparator() {
73 return comparator;
74 }
75
76 /**
77 * Configure column width and header label depending on the value that will
78 * be displayed in the current column.
79 *
80 * @param jcrColumnName
81 * @param column
82 * @param columnIndex
83 */
84 public void configureColumn(String jcrColumnName, TableViewerColumn column,
85 int columnIndex) {
86
87 if (columnIndex != -1
88 && getSelectionAdapter(column.getColumn(), columnIndex) != null) {
89 column.getColumn().addSelectionListener(
90 getSelectionAdapter(column.getColumn(), columnIndex));
91 indexToName.put(new Integer(columnIndex), jcrColumnName);
92 }
93 Object[] objs = DistUiHelpers
94 .getLabelAndDefaultValueWidth(jcrColumnName);
95 column.getColumn().setWidth((Integer) objs[1]);
96 column.getColumn().setText((String) objs[0]);
97 }
98
99 /**
100 * Might be used by client classes to sort the table with based on selected
101 * columns.
102 *
103 * @param column
104 * @param index
105 * @return
106 */
107 public SelectionAdapter getSelectionAdapter(final TableColumn column,
108 final int index) {
109
110 // A comparator must be define
111 if (comparator == null)
112 return null;
113
114 SelectionAdapter selectionAdapter = new SelectionAdapter() {
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 public String getText(Object element) {
150 return null;
151 }
152
153 public Image getImage(Object element) {
154 return null;
155 }
156 };
157 } else
158 return new ColumnLabelProvider() {
159 public String getText(Object element) {
160 Row row = (Row) element;
161 try {
162 return row.getValue(columnName).getString();
163 } catch (RepositoryException e) {
164 throw new ArgeoException("Cannot display row " + row, e);
165 }
166 }
167
168 public Image getImage(Object element) {
169 return null;
170 }
171 };
172 }
173
174 /** Implements comparator for various types of Artifact Table row */
175 private class CurrentTableComparator extends GenericTableComparator {
176
177 public CurrentTableComparator(int colIndex, int direction) {
178 super(colIndex, direction);
179 }
180
181 @Override
182 public int compare(Viewer viewer, Object e1, Object e2) {
183 int rc = 0;
184
185 if (e1 instanceof Row) {
186 try {
187
188 Value v1 = ((Row) e1).getValue(indexToName
189 .get(propertyIndex));
190 Value v2 = ((Row) e2).getValue(indexToName
191 .get(propertyIndex));
192
193 if (v1.getType() == PropertyType.STRING)
194 rc = v1.getString().compareTo(v2.getString());
195 else if (v1.getType() == PropertyType.DATE)
196 rc = v1.getDate().compareTo(v2.getDate());
197 else
198 throw new ArgeoException("comparator for object type "
199 + v1.getType() + " is not yet implemented");
200 } catch (Exception e) {
201 throw new ArgeoException("rows cannot be compared ", e);
202 }
203 } else
204 throw new ArgeoException("Unsupported row type");
205 // If descending order, flip the direction
206 if (direction == DESCENDING) {
207 rc = -rc;
208 }
209 return rc;
210 }
211 }
212 }