]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/editors/AbstractJcrQueryEditor.java
Update license headers
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui.jcr / src / main / java / org / argeo / eclipse / ui / jcr / editors / AbstractJcrQueryEditor.java
index dbe4a5576561c6ffbcf4029fbaf44b18585c98fa..f41c7ca80c12460630d0500e43677ff9976a466f 100644 (file)
@@ -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<TableViewerColumn> tableViewerColumns = new ArrayList<TableViewerColumn>();
        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<Row> rows = new ArrayList<Row>();
+                               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,38 +287,37 @@ 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);
-//                                     int dir = viewer.getTable().getSortDirection();
-//                                     if (viewer.getTable().getSortColumn() == column) {
-//                                             dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
-//                                     } else {
-//
-//                                             dir = SWT.DOWN;
-//                                     }
-//                                     viewer.getTable().setSortDirection(dir);
-//                                     viewer.getTable().setSortColumn(column);
-//                                     viewer.refresh();
-//                             } catch (Exception exc) {
-//                                     exc.printStackTrace();
-//                             }
+                               try {
+
+                                       comparator.setColumn(index);
+                                       int dir = viewer.getTable().getSortDirection();
+                                       if (viewer.getTable().getSortColumn() == column) {
+                                               dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
+                                       } else {
+
+                                               dir = SWT.DOWN;
+                                       }
+                                       viewer.getTable().setSortDirection(dir);
+                                       viewer.getTable().setSortColumn(column);
+                                       viewer.refresh();
+                               } catch (Exception exc) {
+                                       exc.printStackTrace();
+                               }
                        }
                };
                return selectionAdapter;
        }
 
        /**
-        * 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;
        }