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