]> 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
Full refactoring of the UI distribution view:
[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.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;
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 @Override
114 public void widgetSelected(SelectionEvent e) {
115
116 try {
117
118 comparator.setColumn(index);
119 int dir = viewer.getTable().getSortDirection();
120 if (viewer.getTable().getSortColumn() == column) {
121 dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
122 } else {
123
124 dir = SWT.DOWN;
125 }
126 viewer.getTable().setSortDirection(dir);
127 viewer.getTable().setSortColumn(column);
128 viewer.refresh();
129 } catch (Exception exc) {
130 exc.printStackTrace();
131 }
132 }
133 };
134 return selectionAdapter;
135 }
136
137 /**
138 * provides a label provider that returns the content of a specific cell.
139 * Specific treatment is done for some columns when the query returns a code
140 * that must be translated to the corresponding value at display time.
141 */
142 public ColumnLabelProvider getLabelProvider(final String columnName) {
143 boolean test = false;
144
145 if (test) {
146 return new ColumnLabelProvider() {
147 public String getText(Object element) {
148 return null;
149 }
150
151 public Image getImage(Object element) {
152 return null;
153 }
154 };
155 } else
156 return new ColumnLabelProvider() {
157 public String getText(Object element) {
158 Row row = (Row) element;
159 try {
160 return row.getValue(columnName).getString();
161 } catch (RepositoryException e) {
162 throw new ArgeoException("Cannot display row " + row, e);
163 }
164 }
165
166 public Image getImage(Object element) {
167 return null;
168 }
169 };
170 }
171
172 /** Implements comparator for various types of Artifact Table row */
173 private class CurrentTableComparator extends GenericTableComparator {
174
175 public CurrentTableComparator(int colIndex, int direction) {
176 super(colIndex, direction);
177 }
178
179 @Override
180 public int compare(Viewer viewer, Object e1, Object e2) {
181 int rc = 0;
182
183 if (e1 instanceof Row) {
184 try {
185
186 Value v1 = ((Row) e1).getValue(indexToName
187 .get(propertyIndex));
188 Value v2 = ((Row) e2).getValue(indexToName
189 .get(propertyIndex));
190
191 if (v1.getType() == PropertyType.STRING)
192 rc = v1.getString().compareTo(v2.getString());
193 else if (v1.getType() == PropertyType.DATE)
194 rc = v1.getDate().compareTo(v2.getDate());
195 else
196 throw new ArgeoException("comparator for object type "
197 + v1.getType() + " is not yet implemented");
198 } catch (Exception e) {
199 throw new ArgeoException("rows cannot be compared ", e);
200 }
201 } else
202 throw new ArgeoException("Unsupported row type");
203 // If descending order, flip the direction
204 if (direction == DESCENDING) {
205 rc = -rc;
206 }
207 return rc;
208 }
209 }
210 }