]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/argeo-commons/org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/internal/jcr/parts/AbstractJcrQueryEditor.java
Remove old license headers
[gpl/argeo-slc.git] / legacy / argeo-commons / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / internal / jcr / parts / AbstractJcrQueryEditor.java
1 package org.argeo.cms.ui.workbench.internal.jcr.parts;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.jcr.RepositoryException;
7 import javax.jcr.Session;
8 import javax.jcr.query.QueryResult;
9 import javax.jcr.query.Row;
10 import javax.jcr.query.RowIterator;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.argeo.eclipse.ui.EclipseUiException;
15 import org.argeo.eclipse.ui.GenericTableComparator;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.jface.dialogs.ErrorDialog;
20 import org.eclipse.jface.viewers.ColumnLabelProvider;
21 import org.eclipse.jface.viewers.IDoubleClickListener;
22 import org.eclipse.jface.viewers.IStructuredContentProvider;
23 import org.eclipse.jface.viewers.TableViewer;
24 import org.eclipse.jface.viewers.TableViewerColumn;
25 import org.eclipse.jface.viewers.Viewer;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.custom.SashForm;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.layout.FillLayout;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.TableColumn;
36 import org.eclipse.ui.IEditorInput;
37 import org.eclipse.ui.IEditorSite;
38 import org.eclipse.ui.PartInitException;
39 import org.eclipse.ui.part.EditorPart;
40
41 /** Executes any JCR query. */
42 public abstract class AbstractJcrQueryEditor extends EditorPart {
43 private final static Log log = LogFactory.getLog(AbstractJcrQueryEditor.class);
44
45 protected String initialQuery;
46 protected String initialQueryType;
47
48 /* DEPENDENCY INJECTION */
49 private Session session;
50
51 // Widgets
52 private TableViewer viewer;
53 private List<TableViewerColumn> tableViewerColumns = new ArrayList<TableViewerColumn>();
54 private GenericTableComparator comparator;
55
56 /** Override to layout a form enabling the end user to build his query */
57 protected abstract void createQueryForm(Composite parent);
58
59 @Override
60 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
61 JcrQueryEditorInput editorInput = (JcrQueryEditorInput) input;
62 initialQuery = editorInput.getQuery();
63 initialQueryType = editorInput.getQueryType();
64 setSite(site);
65 setInput(editorInput);
66 }
67
68 @Override
69 public final void createPartControl(final Composite parent) {
70 parent.setLayout(new FillLayout());
71
72 SashForm sashForm = new SashForm(parent, SWT.VERTICAL);
73 sashForm.setSashWidth(4);
74 sashForm.setLayout(new FillLayout());
75
76 Composite top = new Composite(sashForm, SWT.NONE);
77 GridLayout gl = new GridLayout(1, false);
78 top.setLayout(gl);
79
80 createQueryForm(top);
81
82 Composite bottom = new Composite(sashForm, SWT.NONE);
83 bottom.setLayout(new GridLayout(1, false));
84 sashForm.setWeights(getWeights());
85
86 viewer = new TableViewer(bottom);
87 viewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
88 viewer.getTable().setHeaderVisible(true);
89 viewer.setContentProvider(getQueryResultContentProvider());
90 viewer.setInput(getEditorSite());
91
92 if (getComparator() != null) {
93 comparator = getComparator();
94 viewer.setComparator(comparator);
95 }
96 if (getTableDoubleClickListener() != null)
97 viewer.addDoubleClickListener(getTableDoubleClickListener());
98
99 }
100
101 protected void executeQuery(String statement) {
102 try {
103 if (log.isDebugEnabled())
104 log.debug("Query : " + statement);
105
106 QueryResult qr = session.getWorkspace().getQueryManager().createQuery(statement, initialQueryType)
107 .execute();
108
109 // remove previous columns
110 for (TableViewerColumn tvc : tableViewerColumns)
111 tvc.getColumn().dispose();
112
113 int i = 0;
114 for (final String columnName : qr.getColumnNames()) {
115 TableViewerColumn tvc = new TableViewerColumn(viewer, SWT.NONE);
116 configureColumn(columnName, tvc, i);
117 tvc.setLabelProvider(getLabelProvider(columnName));
118 tableViewerColumns.add(tvc);
119 i++;
120 }
121
122 // Must create a local list: QueryResults can only be read once.
123 try {
124 List<Row> rows = new ArrayList<Row>();
125 RowIterator rit = qr.getRows();
126 while (rit.hasNext()) {
127 rows.add(rit.nextRow());
128 }
129 viewer.setInput(rows);
130 } catch (RepositoryException e) {
131 throw new EclipseUiException("Cannot read query result", e);
132 }
133
134 } catch (RepositoryException e) {
135 ErrorDialog.openError(null, "Error", "Cannot execute JCR query: " + statement,
136 new Status(IStatus.ERROR, "org.argeo.eclipse.ui.jcr", e.getMessage()));
137 }
138 }
139
140 /**
141 * To be overidden to adapt size of form and result frames.
142 *
143 * @return
144 */
145 protected int[] getWeights() {
146 return new int[] { 30, 70 };
147 }
148
149 /**
150 * To be overidden to implement a doubleclick Listener on one of the rows of
151 * the table.
152 *
153 * @return
154 */
155 protected IDoubleClickListener getTableDoubleClickListener() {
156 return null;
157 }
158
159 /**
160 * To be overiden in order to implement a specific
161 * QueryResultContentProvider
162 */
163 protected IStructuredContentProvider getQueryResultContentProvider() {
164 return new QueryResultContentProvider();
165 }
166
167 /**
168 * Enable specific implementation for columns
169 */
170 protected List<TableViewerColumn> getTableViewerColumns() {
171 return tableViewerColumns;
172 }
173
174 /**
175 * Enable specific implementation for columns
176 */
177 protected TableViewer getTableViewer() {
178 return viewer;
179 }
180
181 /**
182 * To be overridden in order to configure column label providers .
183 */
184 protected ColumnLabelProvider getLabelProvider(final String columnName) {
185 return new ColumnLabelProvider() {
186 private static final long serialVersionUID = -3539689333250152606L;
187
188 public String getText(Object element) {
189 Row row = (Row) element;
190 try {
191 return row.getValue(columnName).getString();
192 } catch (RepositoryException e) {
193 throw new EclipseUiException("Cannot display row " + row, e);
194 }
195 }
196
197 public Image getImage(Object element) {
198 return null;
199 }
200 };
201 }
202
203 /**
204 * To be overridden in order to configure the columns.
205 *
206 * @deprecated use
207 * {@link AbstractJcrQueryEditor#configureColumn(String, TableViewerColumn , int )}
208 * instead
209 */
210 protected void configureColumn(String jcrColumnName, TableViewerColumn column) {
211 column.getColumn().setWidth(50);
212 column.getColumn().setText(jcrColumnName);
213 }
214
215 /** To be overridden in order to configure the columns. */
216 protected void configureColumn(String jcrColumnName, TableViewerColumn column, int columnIndex) {
217 column.getColumn().setWidth(50);
218 column.getColumn().setText(jcrColumnName);
219 }
220
221 private class QueryResultContentProvider implements IStructuredContentProvider {
222 private static final long serialVersionUID = -5421095459600554741L;
223
224 public void dispose() {
225 }
226
227 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
228 }
229
230 public Object[] getElements(Object inputElement) {
231
232 if (inputElement instanceof List)
233 return ((List<?>) inputElement).toArray();
234
235 // Never reached might be deleted in future release
236 if (!(inputElement instanceof QueryResult))
237 return new String[] {};
238
239 try {
240 QueryResult queryResult = (QueryResult) inputElement;
241 List<Row> rows = new ArrayList<Row>();
242 RowIterator rit = queryResult.getRows();
243 while (rit.hasNext()) {
244 rows.add(rit.nextRow());
245 }
246
247 // List<Node> elems = new ArrayList<Node>();
248 // NodeIterator nit = queryResult.getNodes();
249 // while (nit.hasNext()) {
250 // elems.add(nit.nextNode());
251 // }
252 return rows.toArray();
253 } catch (RepositoryException e) {
254 throw new EclipseUiException("Cannot read query result", e);
255 }
256 }
257
258 }
259
260 /**
261 * Might be used by children classes to sort columns.
262 *
263 * @param column
264 * @param index
265 * @return
266 */
267 protected SelectionAdapter getSelectionAdapter(final TableColumn column, final int index) {
268
269 // A comparator must be define
270 if (comparator == null)
271 return null;
272
273 SelectionAdapter selectionAdapter = new SelectionAdapter() {
274 private static final long serialVersionUID = 239829307927778349L;
275
276 @Override
277 public void widgetSelected(SelectionEvent e) {
278
279 try {
280
281 comparator.setColumn(index);
282 int dir = viewer.getTable().getSortDirection();
283 if (viewer.getTable().getSortColumn() == column) {
284 dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
285 } else {
286
287 dir = SWT.DOWN;
288 }
289 viewer.getTable().setSortDirection(dir);
290 viewer.getTable().setSortColumn(column);
291 viewer.refresh();
292 } catch (Exception exc) {
293 exc.printStackTrace();
294 }
295 }
296 };
297 return selectionAdapter;
298 }
299
300 /**
301 * To be overridden to enable sorting.
302 */
303 protected GenericTableComparator getComparator() {
304 return null;
305 }
306
307 @Override
308 public boolean isDirty() {
309 return false;
310 }
311
312 @Override
313 public void doSave(IProgressMonitor monitor) {
314 // TODO save the query in JCR?
315 }
316
317 @Override
318 public void doSaveAs() {
319 }
320
321 @Override
322 public boolean isSaveAsAllowed() {
323 return false;
324 }
325
326 /** Returns the injected current session */
327 protected Session getSession() {
328 return session;
329 }
330
331 /* DEPENDENCY INJECTION */
332 public void setSession(Session session) {
333 this.session = session;
334 }
335 }