1 package org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.parts
;
3 import java
.util
.ArrayList
;
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
;
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
;
41 /** Executes any JCR query. */
42 public abstract class AbstractJcrQueryEditor
extends EditorPart
{
43 private final static Log log
= LogFactory
.getLog(AbstractJcrQueryEditor
.class);
45 protected String initialQuery
;
46 protected String initialQueryType
;
48 /* DEPENDENCY INJECTION */
49 private Session session
;
52 private TableViewer viewer
;
53 private List
<TableViewerColumn
> tableViewerColumns
= new ArrayList
<TableViewerColumn
>();
54 private GenericTableComparator comparator
;
56 /** Override to layout a form enabling the end user to build his query */
57 protected abstract void createQueryForm(Composite parent
);
60 public void init(IEditorSite site
, IEditorInput input
) throws PartInitException
{
61 JcrQueryEditorInput editorInput
= (JcrQueryEditorInput
) input
;
62 initialQuery
= editorInput
.getQuery();
63 initialQueryType
= editorInput
.getQueryType();
65 setInput(editorInput
);
69 public final void createPartControl(final Composite parent
) {
70 parent
.setLayout(new FillLayout());
72 SashForm sashForm
= new SashForm(parent
, SWT
.VERTICAL
);
73 sashForm
.setSashWidth(4);
74 sashForm
.setLayout(new FillLayout());
76 Composite top
= new Composite(sashForm
, SWT
.NONE
);
77 GridLayout gl
= new GridLayout(1, false);
82 Composite bottom
= new Composite(sashForm
, SWT
.NONE
);
83 bottom
.setLayout(new GridLayout(1, false));
84 sashForm
.setWeights(getWeights());
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());
92 if (getComparator() != null) {
93 comparator
= getComparator();
94 viewer
.setComparator(comparator
);
96 if (getTableDoubleClickListener() != null)
97 viewer
.addDoubleClickListener(getTableDoubleClickListener());
101 protected void executeQuery(String statement
) {
103 if (log
.isDebugEnabled())
104 log
.debug("Query : " + statement
);
106 QueryResult qr
= session
.getWorkspace().getQueryManager().createQuery(statement
, initialQueryType
)
109 // remove previous columns
110 for (TableViewerColumn tvc
: tableViewerColumns
)
111 tvc
.getColumn().dispose();
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
);
122 // Must create a local list: QueryResults can only be read once.
124 List
<Row
> rows
= new ArrayList
<Row
>();
125 RowIterator rit
= qr
.getRows();
126 while (rit
.hasNext()) {
127 rows
.add(rit
.nextRow());
129 viewer
.setInput(rows
);
130 } catch (RepositoryException e
) {
131 throw new EclipseUiException("Cannot read query result", e
);
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()));
141 * To be overidden to adapt size of form and result frames.
145 protected int[] getWeights() {
146 return new int[] { 30, 70 };
150 * To be overidden to implement a doubleclick Listener on one of the rows of
155 protected IDoubleClickListener
getTableDoubleClickListener() {
160 * To be overiden in order to implement a specific
161 * QueryResultContentProvider
163 protected IStructuredContentProvider
getQueryResultContentProvider() {
164 return new QueryResultContentProvider();
168 * Enable specific implementation for columns
170 protected List
<TableViewerColumn
> getTableViewerColumns() {
171 return tableViewerColumns
;
175 * Enable specific implementation for columns
177 protected TableViewer
getTableViewer() {
182 * To be overridden in order to configure column label providers .
184 protected ColumnLabelProvider
getLabelProvider(final String columnName
) {
185 return new ColumnLabelProvider() {
186 private static final long serialVersionUID
= -3539689333250152606L;
188 public String
getText(Object element
) {
189 Row row
= (Row
) element
;
191 return row
.getValue(columnName
).getString();
192 } catch (RepositoryException e
) {
193 throw new EclipseUiException("Cannot display row " + row
, e
);
197 public Image
getImage(Object element
) {
204 * To be overridden in order to configure the columns.
207 * {@link AbstractJcrQueryEditor#configureColumn(String, TableViewerColumn , int )}
210 protected void configureColumn(String jcrColumnName
, TableViewerColumn column
) {
211 column
.getColumn().setWidth(50);
212 column
.getColumn().setText(jcrColumnName
);
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
);
221 private class QueryResultContentProvider
implements IStructuredContentProvider
{
222 private static final long serialVersionUID
= -5421095459600554741L;
224 public void dispose() {
227 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
230 public Object
[] getElements(Object inputElement
) {
232 if (inputElement
instanceof List
)
233 return ((List
<?
>) inputElement
).toArray();
235 // Never reached might be deleted in future release
236 if (!(inputElement
instanceof QueryResult
))
237 return new String
[] {};
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());
247 // List<Node> elems = new ArrayList<Node>();
248 // NodeIterator nit = queryResult.getNodes();
249 // while (nit.hasNext()) {
250 // elems.add(nit.nextNode());
252 return rows
.toArray();
253 } catch (RepositoryException e
) {
254 throw new EclipseUiException("Cannot read query result", e
);
261 * Might be used by children classes to sort columns.
267 protected SelectionAdapter
getSelectionAdapter(final TableColumn column
, final int index
) {
269 // A comparator must be define
270 if (comparator
== null)
273 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
274 private static final long serialVersionUID
= 239829307927778349L;
277 public void widgetSelected(SelectionEvent e
) {
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
;
289 viewer
.getTable().setSortDirection(dir
);
290 viewer
.getTable().setSortColumn(column
);
292 } catch (Exception exc
) {
293 exc
.printStackTrace();
297 return selectionAdapter
;
301 * To be overridden to enable sorting.
303 protected GenericTableComparator
getComparator() {
308 public boolean isDirty() {
313 public void doSave(IProgressMonitor monitor
) {
314 // TODO save the query in JCR?
318 public void doSaveAs() {
322 public boolean isSaveAsAllowed() {
326 /** Returns the injected current session */
327 protected Session
getSession() {
331 /* DEPENDENCY INJECTION */
332 public void setSession(Session session
) {
333 this.session
= session
;