Refactor DocBook attributes.
[gpl/argeo-suite.git] / org.argeo.suite.ui / src / org / argeo / suite / ui / RecentItems.java
index d481f2b0eda0a278ce5963017d11419a5978f149..b9aa5b721784ecc309dcd943ff2a495d4eb94fe8 100644 (file)
@@ -17,23 +17,25 @@ import javax.jcr.query.QueryResult;
 
 import org.argeo.cms.ui.CmsTheme;
 import org.argeo.cms.ui.CmsUiProvider;
+import org.argeo.cms.ui.CmsView;
 import org.argeo.cms.ui.util.CmsUiUtils;
-import org.argeo.connect.ui.ConnectUiConstants;
-import org.argeo.connect.ui.util.BasicNodeListContentProvider;
-import org.argeo.connect.ui.widgets.DelayedText;
-import org.argeo.connect.util.XPathUtils;
 import org.argeo.eclipse.ui.EclipseUiUtils;
-import org.argeo.entity.EntityNames;
-import org.argeo.entity.EntityTypes;
+import org.argeo.entity.EntityType;
+import org.argeo.jcr.Jcr;
 import org.argeo.jcr.JcrUtils;
+import org.argeo.suite.ui.widgets.DelayedText;
+import org.argeo.suite.util.XPathUtils;
 import org.eclipse.jface.layout.TableColumnLayout;
 import org.eclipse.jface.viewers.ColumnLabelProvider;
 import org.eclipse.jface.viewers.ColumnWeightData;
+import org.eclipse.jface.viewers.DoubleClickEvent;
+import org.eclipse.jface.viewers.IDoubleClickListener;
 import org.eclipse.jface.viewers.ILabelProvider;
 import org.eclipse.jface.viewers.ISelectionChangedListener;
-import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.IStructuredContentProvider;
 import org.eclipse.jface.viewers.SelectionChangedEvent;
 import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.KeyEvent;
 import org.eclipse.swt.events.KeyListener;
@@ -43,60 +45,111 @@ import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.TableColumn;
 import org.eclipse.swt.widgets.Text;
+import org.eclipse.swt.widgets.ToolBar;
+import org.eclipse.swt.widgets.ToolItem;
 
 /** List recent items. */
 public class RecentItems implements CmsUiProvider {
-       int SEARCH_TEXT_DELAY = 800;
+       private final static int SEARCH_TEXT_DELAY = 800;
+       private final static int SEARCH_DEFAULT_LIMIT = 100;
+
        private CmsTheme theme;
 
        private String entityType;
 
+       static enum Property {
+               entityTypes;
+       }
+
        @Override
        public Control createUi(Composite parent, Node context) throws RepositoryException {
                theme = CmsTheme.getCmsTheme(parent);
                parent.setLayout(new GridLayout());
+//             parent.setLayout(CmsUiUtils.noSpaceGridLayout());
+               parent.setLayout(new GridLayout());
 
-//             Composite top = new Composite(parent, SWT.NONE);
+//             Composite top = new Composite(parent, SWT.BORDER);
+//             CmsUiUtils.style(top, SuiteStyle.recentItems);
 //             top.setLayoutData(CmsUiUtils.fillWidth());
-//             top.setLayout(new GridLayout(2, false));
-
-               Label lbl = new Label(parent, SWT.NONE);
-//             search.setImage(SuiteIcon.search.getSmallIcon(theme));
-               lbl.setText(SuiteMsg.recentItems.lead());
-               CmsUiUtils.style(lbl, SuiteStyle.recentItems);
+//             top.setLayout(CmsUiUtils.noSpaceGridLayout(2));
+//             Label lbl = new Label(top, SWT.FLAT);
+//             lbl.setLayoutData(CmsUiUtils.fillWidth());
+//             lbl.setText(SuiteMsg.recentItems.lead());
+//             CmsUiUtils.style(lbl, SuiteStyle.recentItems);
+//
+//             ToolBar topToolBar = new ToolBar(top, SWT.NONE);
+//             ToolItem addItem = new ToolItem(topToolBar, SWT.FLAT);
+////           CmsUiUtils.style(addItem, SuiteStyle.recentItems);
+//             addItem.setImage(SuiteIcon.add.getSmallIcon(theme));
 
                if (context == null)
                        return null;
                SingleEntityViewer entityViewer = new SingleEntityViewer(parent, SWT.NONE, context.getSession());
-               entityViewer.setLayoutData(CmsUiUtils.fillAll());
                entityViewer.createUi();
-               return entityViewer;
+               entityViewer.getViewer().getTable().setLayoutData(CmsUiUtils.fillAll());
+
+               Composite bottom = new Composite(parent, SWT.NONE);
+               bottom.setLayoutData(CmsUiUtils.fillWidth());
+               bottom.setLayout(CmsUiUtils.noSpaceGridLayout());
+               ToolBar bottomToolBar = new ToolBar(bottom, SWT.NONE);
+               bottomToolBar.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
+               ToolItem deleteItem = new ToolItem(bottomToolBar, SWT.FLAT);
+               deleteItem.setEnabled(false);
+//             CmsUiUtils.style(deleteItem, SuiteStyle.recentItems);
+               deleteItem.setImage(SuiteIcon.delete.getSmallIcon(theme));
+               ToolItem addItem = new ToolItem(bottomToolBar, SWT.FLAT);
+               addItem.setImage(SuiteIcon.add.getSmallIcon(theme));
+               entityViewer.getViewer().addDoubleClickListener(new IDoubleClickListener() {
+
+                       @Override
+                       public void doubleClick(DoubleClickEvent event) {
+                               Node node = (Node) entityViewer.getViewer().getStructuredSelection().getFirstElement();
+                               if (node != null)
+                                       CmsView.getCmsView(parent).sendEvent(SuiteEvent.openNewPart.topic(),
+                                                       SuiteEvent.eventProperties(node));
+
+                       }
+               });
+               entityViewer.getViewer().addSelectionChangedListener(new ISelectionChangedListener() {
+                       public void selectionChanged(SelectionChangedEvent event) {
+                               Node node = (Node) entityViewer.getViewer().getStructuredSelection().getFirstElement();
+                               if (node != null) {
+                                       CmsView.getCmsView(parent).sendEvent(SuiteEvent.refreshPart.topic(),
+                                                       SuiteEvent.eventProperties(node));
+                                       deleteItem.setEnabled(true);
+                               } else {
+                                       deleteItem.setEnabled(false);
+                               }
+                       }
+               });
+
+               return entityViewer.filterTxt;
 
        }
 
        public void init(Map<String, String> properties) {
-               entityType = properties.get(EntityNames.ENTITY_TYPE);
+               // TODO manage multiple entities
+               entityType = properties.get(Property.entityTypes.name());
        }
 
-       class SingleEntityViewer extends Composite {
+       class SingleEntityViewer {
+               Composite parent;
                Text filterTxt;
-               TableViewer entityViewer;
+               TableViewer viewer;
                Session session;
 
                public SingleEntityViewer(Composite parent, int style, Session session) {
-                       super(parent, style);
+                       this.parent = parent;
                        this.session = session;
                }
 
                public void createUi() {
                        // MainLayout
-                       setLayout(new GridLayout());
-                       addFilterPanel(this);
-                       entityViewer = createListPart(this, new SingleEntityLabelProvider());
+                       addFilterPanel(parent);
+                       viewer = createListPart(parent, new SingleEntityLabelProvider());
                        refreshFilteredList();
 
                        try {
@@ -106,7 +159,7 @@ public class RecentItems implements CmsUiProvider {
 
                                        @Override
                                        public void onEvent(EventIterator events) {
-                                               getDisplay().asyncExec(() -> refreshFilteredList());
+                                               parent.getDisplay().asyncExec(() -> refreshFilteredList());
                                        }
                                }, Event.PROPERTY_CHANGED | Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED, "/", true,
                                                null, nodeTypes, false);
@@ -120,7 +173,7 @@ public class RecentItems implements CmsUiProvider {
                        // Use a delayed text: the query won't be done until the user stop
                        // typing for 800ms
                        int style = SWT.BORDER | SWT.SEARCH | SWT.ICON_CANCEL;
-                       DelayedText delayedText = new DelayedText(parent, style, ConnectUiConstants.SEARCH_TEXT_DELAY);
+                       DelayedText delayedText = new DelayedText(parent, style, SEARCH_TEXT_DELAY);
                        filterTxt = delayedText.getText();
                        filterTxt.setLayoutData(EclipseUiUtils.fillWidth());
 
@@ -162,60 +215,70 @@ public class RecentItems implements CmsUiProvider {
                                }
                        });
 
-                       addDisposeListener((e) -> {
+                       parent.addDisposeListener((e) -> {
                                delayedText.close();
                        });
                }
 
                protected TableViewer createListPart(Composite parent, ILabelProvider labelProvider) {
-                       parent.setLayout(new GridLayout());
+//                     parent.setLayout(new GridLayout());
+//                     parent.setLayout(CmsUiUtils.noSpaceGridLayout());
 
                        Composite tableComposite = new Composite(parent, SWT.NONE);
                        GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_VERTICAL
                                        | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
                        tableComposite.setLayoutData(gd);
 
-                       TableViewer v = new TableViewer(tableComposite);
-                       v.setLabelProvider(labelProvider);
+                       TableViewer viewer = new TableViewer(tableComposite, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
+                       viewer.setLabelProvider(labelProvider);
 
-                       TableColumn singleColumn = new TableColumn(v.getTable(), SWT.V_SCROLL);
+                       TableColumn singleColumn = new TableColumn(viewer.getTable(), SWT.V_SCROLL);
                        TableColumnLayout tableColumnLayout = new TableColumnLayout();
                        tableColumnLayout.setColumnData(singleColumn, new ColumnWeightData(85));
                        tableComposite.setLayout(tableColumnLayout);
 
                        // Corresponding table & style
-                       Table table = v.getTable();
+                       Table table = viewer.getTable();
+//                     Listener[] mouseDownListeners = table.getListeners(SWT.MouseDown);
+//                     for (Listener listener :  table.getListeners(SWT.MouseDown))
+//                             table.removeListener(SWT.MouseDown, listener);
+//                     for (Listener listener :  table.getListeners(SWT.MouseUp))
+//                             table.removeListener(SWT.MouseUp, listener);
+//                     for (Listener listener :  table.getListeners(SWT.MouseDoubleClick))
+//                             table.removeListener(SWT.MouseDoubleClick, listener);
+//                     
+//                     table.addMouseListener(new MouseListener() {
+//
+//                             @Override
+//                             public void mouseUp(MouseEvent e) {
+//                                     System.out.println("Mouse up: "+e);
+//                             }
+//
+//                             @Override
+//                             public void mouseDown(MouseEvent e) {
+//                                     System.out.println("Mouse down: "+e);
+//                             }
+//
+//                             @Override
+//                             public void mouseDoubleClick(MouseEvent e) {
+//                                     System.out.println("Mouse double: "+e);
+//
+//                             }
+//                     });
                        table.setLinesVisible(true);
                        table.setHeaderVisible(false);
-                       CmsUiUtils.markup(table);
-                       CmsUiUtils.setItemHeight(table, 26);
-
-                       v.setContentProvider(new BasicNodeListContentProvider());
-                       // v.addDoubleClickListener(new
-                       // JcrViewerDClickListener(systemWorkbenchService));
-                       v.addSelectionChangedListener(new ISelectionChangedListener() {
-                               public void selectionChanged(SelectionChangedEvent event) {
-                                       IStructuredSelection selection = (IStructuredSelection) event.getSelection();
-                                       List<?> lst = selection.toList();
-//                             if (lst != null && !lst.isEmpty())
-//                                     selectionService.setSelection(selection.toList());
-//                             else
-//                                     selectionService.setSelection(null);
-                               }
-                       });
-                       return v;
-               }
-
-//     public void dispose() {
-//             JcrUtils.logoutQuietly(session);
-//     }
+                       // CmsUiUtils.markup(table);
+                       // CmsUiUtils.setItemHeight(table, 26);
 
-               public boolean setFocus() {
-                       refreshFilteredList();
-                       return super.setFocus();
-//             filterTxt.setFocus();
+                       viewer.setContentProvider(new BasicNodeListContentProvider());
+                       return viewer;
                }
 
+//             public boolean setFocus() {
+//                     refreshFilteredList();
+//                     return parent.setFocus();
+//             }
+
                public void forceRefresh(Object object) {
                        refreshFilteredList();
                }
@@ -231,25 +294,31 @@ public class RecentItems implements CmsUiProvider {
 
                                // XPATH Query
                                String xpathQueryStr;
-                               if (entityType != null)
-                                       xpathQueryStr = entityType.contains(":") ? "//element(*, " + entityType + ")"
-                                                       : "//element(*, " + EntityTypes.ENTITY_ENTITY + ")[@entity:type='" + entityType + "']";
-                               else
-                                       xpathQueryStr = "//element(*, " + EntityTypes.ENTITY_ENTITY + ")";
+                               if (entityType != null) {
+                                       int indexColumn = entityType.indexOf(':');
+                                       if (indexColumn > 0) {// JCR node type
+                                               xpathQueryStr = "//element(*, " + entityType + ") order by @jcr:created descending";
+                                       } else {
+                                               xpathQueryStr = entityType.contains(":") ? "//element(*, " + entityType + ")"
+                                                               : "//element(*, " + EntityType.entity.get() + ")[@entity:type='" + entityType + "']";
+                                       }
+                               } else {
+                                       xpathQueryStr = "//element(*, " + EntityType.entity.get() + ")";
+                               }
 //                     String xpathQueryStr = "//element(*, " + ConnectTypes.CONNECT_ENTITY + ")";
                                String xpathFilter = XPathUtils.getFreeTextConstraint(filter);
                                if (notEmpty(xpathFilter))
                                        xpathQueryStr += "[" + xpathFilter + "]";
 
-                               long begin = System.currentTimeMillis();
+//                             long begin = System.currentTimeMillis();
                                // session.refresh(false);
                                Query xpathQuery = XPathUtils.createQuery(session, xpathQueryStr);
 
-                               xpathQuery.setLimit(ConnectUiConstants.SEARCH_DEFAULT_LIMIT);
+                               xpathQuery.setLimit(SEARCH_DEFAULT_LIMIT);
                                QueryResult result = xpathQuery.execute();
 
                                NodeIterator nit = result.getNodes();
-                               entityViewer.setInput(JcrUtils.nodeIteratorToList(nit));
+                               viewer.setInput(JcrUtils.nodeIteratorToList(nit));
 //                             if (log.isTraceEnabled()) {
 //                                     long end = System.currentTimeMillis();
 //                                     log.trace("Quick Search - Found: " + nit.getSize() + " in " + (end - begin)
@@ -260,8 +329,38 @@ public class RecentItems implements CmsUiProvider {
                        }
                }
 
+               public TableViewer getViewer() {
+                       return viewer;
+               }
+
                class SingleEntityLabelProvider extends ColumnLabelProvider {
+                       private static final long serialVersionUID = -2209337675781795677L;
+
+                       @Override
+                       public String getText(Object element) {
+                               return Jcr.getTitle((Node) element);
+                       }
 
                }
+
+               class BasicNodeListContentProvider implements IStructuredContentProvider {
+                       private static final long serialVersionUID = 1L;
+                       // keep a cache of the Nodes in the content provider to be able to
+                       // manage long request
+                       private List<Node> nodes;
+
+                       public void dispose() {
+                       }
+
+                       /** Expects a list of nodes as a new input */
+                       @SuppressWarnings("unchecked")
+                       public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+                               nodes = (List<Node>) newInput;
+                       }
+
+                       public Object[] getElements(Object arg0) {
+                               return nodes.toArray();
+                       }
+               }
        }
 }