X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Fruntime%2Forg.argeo.eclipse.ui.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Feclipse%2Fui%2Fjcr%2Feditors%2FAbstractJcrQueryEditor.java;h=f41c7ca80c12460630d0500e43677ff9976a466f;hb=1d5afdce3e91054f07ddd3c98309c363b4cf1d46;hp=76ea5508e06e429d15ca73680984a58946b77a75;hpb=92daeb190be66653f39544ae9b5a2f2c31a60797;p=lgpl%2Fargeo-commons.git diff --git a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/editors/AbstractJcrQueryEditor.java b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/editors/AbstractJcrQueryEditor.java index 76ea5508e..f41c7ca80 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/editors/AbstractJcrQueryEditor.java +++ b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/editors/AbstractJcrQueryEditor.java @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2007-2012 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.argeo.eclipse.ui.jcr.editors; import java.util.ArrayList; @@ -9,6 +24,8 @@ import javax.jcr.query.QueryResult; import javax.jcr.query.Row; import javax.jcr.query.RowIterator; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.argeo.ArgeoException; import org.argeo.eclipse.ui.GenericTableComparator; import org.eclipse.core.runtime.IProgressMonitor; @@ -38,17 +55,21 @@ import org.eclipse.ui.part.EditorPart; /** Executes any JCR query. */ public abstract class AbstractJcrQueryEditor extends EditorPart { + private final static Log log = LogFactory + .getLog(AbstractJcrQueryEditor.class); protected String initialQuery; protected String initialQueryType; - // IoC + /* DEPENDENCY INJECTION */ private Session session; + // Widgets private TableViewer viewer; private List tableViewerColumns = new ArrayList(); private GenericTableComparator comparator; + /** Override to layout a form enabling the end user to build his query */ protected abstract void createQueryForm(Composite parent); @Override @@ -97,6 +118,9 @@ public abstract class AbstractJcrQueryEditor extends EditorPart { protected void executeQuery(String statement) { try { + if (log.isDebugEnabled()) + log.debug("Query : " + statement); + QueryResult qr = session.getWorkspace().getQueryManager() .createQuery(statement, initialQueryType).execute(); @@ -104,20 +128,31 @@ public abstract class AbstractJcrQueryEditor extends EditorPart { for (TableViewerColumn tvc : tableViewerColumns) tvc.getColumn().dispose(); + int i = 0; for (final String columnName : qr.getColumnNames()) { TableViewerColumn tvc = new TableViewerColumn(viewer, SWT.NONE); - configureColumn(columnName, tvc); + configureColumn(columnName, tvc, i); tvc.setLabelProvider(getLabelProvider(columnName)); tableViewerColumns.add(tvc); + i++; + } + + // Must create a local list: QueryResults can only be read once. + try { + List rows = new ArrayList(); + RowIterator rit = qr.getRows(); + while (rit.hasNext()) { + rows.add(rit.nextRow()); + } + viewer.setInput(rows); + } catch (RepositoryException e) { + throw new ArgeoException("Cannot read query result", e); } - viewer.setInput(qr); } catch (RepositoryException e) { ErrorDialog.openError(null, "Error", "Cannot execute JCR query: " + statement, new Status(IStatus.ERROR, "org.argeo.eclipse.ui.jcr", e.getMessage())); - // throw new ArgeoException("Cannot execute JCR query " + statement, - // e); } } @@ -182,13 +217,27 @@ public abstract class AbstractJcrQueryEditor extends EditorPart { }; } - /** To be overridden in order to configure the columns. */ + /** + * To be overridden in order to configure the columns. + * + * @deprecated use {@link + * org.argeo.eclipse.ui.jcr.editors.AbstractJcrQueryEditor. + * configureColumn(String jcrColumnName, TableViewerColumn + * column, int columnIndex)} instead + */ protected void configureColumn(String jcrColumnName, TableViewerColumn column) { column.getColumn().setWidth(50); column.getColumn().setText(jcrColumnName); } + /** To be overridden in order to configure the columns. */ + protected void configureColumn(String jcrColumnName, + TableViewerColumn column, int columnIndex) { + column.getColumn().setWidth(50); + column.getColumn().setText(jcrColumnName); + } + private class QueryResultContentProvider implements IStructuredContentProvider { @@ -199,6 +248,11 @@ public abstract class AbstractJcrQueryEditor extends EditorPart { } public Object[] getElements(Object inputElement) { + + if (inputElement instanceof List) + return ((List) inputElement).toArray(); + + // Never reached might be deleted in future release if (!(inputElement instanceof QueryResult)) return new String[] {}; @@ -233,12 +287,14 @@ public abstract class AbstractJcrQueryEditor extends EditorPart { protected SelectionAdapter getSelectionAdapter(final TableColumn column, final int index) { + // A comparator must be define + if (comparator == null) + return null; + SelectionAdapter selectionAdapter = new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - if (true) - return; try { comparator.setColumn(index); @@ -261,10 +317,7 @@ public abstract class AbstractJcrQueryEditor extends EditorPart { } /** - * To be overriden to enable sorting. - * - * @author bsinou - * + * To be overridden to enable sorting. */ protected GenericTableComparator getComparator() { return null; @@ -278,7 +331,6 @@ public abstract class AbstractJcrQueryEditor extends EditorPart { @Override public void doSave(IProgressMonitor monitor) { // TODO save the query in JCR? - } @Override @@ -290,7 +342,12 @@ public abstract class AbstractJcrQueryEditor extends EditorPart { return false; } - // IoC + /** Returns the injected current session */ + protected Session getSession() { + return session; + } + + /* DEPENDENCY INJECTION */ public void setSession(Session session) { this.session = session; }