Adapt to changes in Connect
[gpl/argeo-suite.git] / org.argeo.suite.workbench.rap / src / org / argeo / suite / workbench / parts / DefaultDashboardEditor.java
index d879d7e54c2017af63c0aaba321921b409dc0938..d24ad972cdb6f560e78a73f4d9ec7074bc3e5f20 100644 (file)
@@ -14,28 +14,36 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.activities.ActivitiesNames;
 import org.argeo.activities.ActivitiesService;
+import org.argeo.activities.workbench.parts.TaskViewerContextMenu;
 import org.argeo.cms.auth.CurrentUser;
 import org.argeo.cms.ui.workbench.util.CommandUtils;
 import org.argeo.cms.util.CmsUtils;
 import org.argeo.connect.ConnectNames;
 import org.argeo.connect.util.ConnectJcrUtils;
+import org.argeo.connect.workbench.ConnectWorkbenchUtils;
 import org.argeo.connect.workbench.Refreshable;
 import org.argeo.connect.workbench.commands.OpenEntityEditor;
 import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.node.NodeUtils;
-import org.argeo.suite.SuiteException;
 import org.argeo.suite.workbench.AsUiPlugin;
+import org.argeo.suite.workbench.SuiteWorkbenchException;
 import org.argeo.tracker.TrackerNames;
 import org.argeo.tracker.TrackerService;
+import org.argeo.tracker.TrackerTypes;
 import org.argeo.tracker.core.TrackerUtils;
 import org.argeo.tracker.ui.TaskListLabelProvider;
 import org.argeo.tracker.ui.TaskVirtualListComposite;
 import org.eclipse.jface.viewers.ColumnLabelProvider;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.TableViewer;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.MouseAdapter;
+import org.eclipse.swt.events.MouseEvent;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.layout.RowLayout;
@@ -44,6 +52,9 @@ import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Group;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Link;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorSite;
+import org.eclipse.ui.PartInitException;
 
 /** Argeo Suite Default Dashboard */
 public class DefaultDashboardEditor extends AbstractSuiteDashboard implements Refreshable {
@@ -59,6 +70,11 @@ public class DefaultDashboardEditor extends AbstractSuiteDashboard implements Re
        private Composite taskListCmp;
        private TaskVirtualListComposite tvlc;
 
+       @Override
+       public void init(IEditorSite site, IEditorInput input) throws PartInitException {
+               super.init(site, input);
+       }
+
        @Override
        public void createPartControl(Composite parent) {
                super.createPartControl(parent);
@@ -67,41 +83,102 @@ public class DefaultDashboardEditor extends AbstractSuiteDashboard implements Re
                bodyCmp.setLayoutData(EclipseUiUtils.fillAll());
                bodyCmp.setLayout(new GridLayout());
 
-               // Header
-               try {
-                       headerCmp = createHeaderPart(bodyCmp, NodeUtils.getUserHome(getSession()));
-                       headerCmp.setLayoutData(EclipseUiUtils.fillWidth());
-
-               } catch (RepositoryException e) {
-                       throw new SuiteException("Cannot create dashboard overview", e);
-               }
+               headerCmp = new Composite(bodyCmp, SWT.NO_FOCUS);
+               headerCmp.setLayoutData(EclipseUiUtils.fillWidth());
 
                taskListCmp = new Composite(bodyCmp, SWT.NO_FOCUS);
                taskListCmp.setLayoutData(EclipseUiUtils.fillAll());
-
-               populateTaskListCmp();
+               forceRefresh(null);
        }
 
-       private void populateTaskListCmp() {
+       @Override
+       public void forceRefresh(Object object) {
+               CmsUtils.clear(headerCmp);
+               populateHeaderPart(headerCmp, NodeUtils.getUserHome(getSession()));
+
                CmsUtils.clear(taskListCmp);
-               taskListCmp.setLayout(EclipseUiUtils.noSpaceGridLayout());
-               // Composite innerCmp = new Composite(taskListCmp, SWT.NO_FOCUS);
-               // innerCmp.setLayoutData(EclipseUiUtils.fillAll());
+               populateTaskListCmp(taskListCmp);
 
-               TaskListLabelProvider labelProvider = new TaskListLabelProvider(trackerService);
-               tvlc = new TaskVirtualListComposite(taskListCmp, SWT.NO_FOCUS, labelProvider, 54);
-               tvlc.setLayoutData(EclipseUiUtils.fillAll());
-               forceRefresh(null);
+               headerCmp.getParent().layout(true, true);
        }
 
-       @Override
-       public void forceRefresh(Object object) {
+       private void populateTaskListCmp(Composite parent) {
+               parent.setLayout(EclipseUiUtils.noSpaceGridLayout());
                NodeIterator nit = activitiesService.getMyTasks(getSession(), true);
-               tvlc.getTableViewer().setInput(JcrUtils.nodeIteratorToList(nit).toArray());
+               if (!nit.hasNext()) {
+                       Composite noTaskCmp = new Composite(parent, SWT.NO_FOCUS);
+                       noTaskCmp.setLayoutData(EclipseUiUtils.fillAll());
+                       noTaskCmp.setLayout(new GridLayout());
+
+                       Label noTaskLbl = new Label(noTaskCmp, SWT.CENTER);
+                       noTaskLbl.setText("<i> <big> You have no pending Task. </big> </i>");
+                       CmsUtils.markup(noTaskLbl);
+                       noTaskLbl.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, true));
+
+                       final Link createTaskLk = new Link(noTaskCmp, SWT.CENTER);
+                       createTaskLk.setText("<a> Create a task </a>");
+                       createTaskLk.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, true));
+
+                       createTaskLk.addSelectionListener(new SelectionAdapter() {
+                               private static final long serialVersionUID = -9028457805156989935L;
+
+                               @Override
+                               public void widgetSelected(SelectionEvent e) {
+                                       String mainMixin = TrackerTypes.TRACKER_TASK;
+                                       String pathCreated = ConnectWorkbenchUtils.createAndConfigureEntity(createTaskLk.getShell(), getSession(),
+                                                       getSystemAppService(), getSystemWorkbenchService(), mainMixin);
+                                       if (EclipseUiUtils.notEmpty(pathCreated))
+                                               forceRefresh(null);
+                               }
+                       });
+
+               } else {
+                       TaskListLabelProvider labelProvider = new TaskListLabelProvider(trackerService);
+                       tvlc = new TaskVirtualListComposite(parent, SWT.NO_FOCUS, labelProvider, 54);
+                       tvlc.setLayoutData(EclipseUiUtils.fillAll());
+                       final TableViewer viewer = tvlc.getTableViewer();
+                       viewer.setInput(JcrUtils.nodeIteratorToList(nit).toArray());
+                       final TaskViewerContextMenu contextMenu = new TaskViewerContextMenu(viewer, getSession(),
+                                       activitiesService) {
+                               @Override
+                               public boolean performAction(String actionId) {
+                                       boolean hasChanged = super.performAction(actionId);
+                                       if (hasChanged) {
+                                               viewer.getTable().setFocus();
+                                               forceRefresh(null);
+                                               // NodeIterator nit =
+                                               // activitiesService.getMyTasks(getSession(), true);
+                                               // viewer.setInput(JcrUtils.nodeIteratorToList(nit).toArray());
+                                       }
+                                       return hasChanged;
+                               }
+                       };
+                       viewer.getTable().addMouseListener(new MouseAdapter() {
+                               private static final long serialVersionUID = 6737579410648595940L;
+
+                               @Override
+                               public void mouseDown(MouseEvent e) {
+                                       if (e.button == 3) {
+                                               // contextMenu.setCurrFolderPath(currDisplayedFolder);
+                                               contextMenu.show(viewer.getTable(), new Point(e.x, e.y),
+                                                               (IStructuredSelection) viewer.getSelection());
+                                       }
+                               }
+                       });
+
+               }
        }
 
-       private Composite createHeaderPart(Composite parent, Node context) throws RepositoryException {
-               Composite bodyCmp = new Composite(parent, SWT.NO_FOCUS);
+       private boolean isOverdue(Node node, String propName) {
+               try {
+                       Calendar now = GregorianCalendar.getInstance();
+                       return node.hasProperty(propName) && node.getProperty(propName).getDate().before(now);
+               } catch (RepositoryException e) {
+                       throw new SuiteWorkbenchException("Cannot check overdue status with property " + propName + " on " + node, e);
+               }
+       }
+
+       private void populateHeaderPart(Composite bodyCmp, Node context) {
                bodyCmp.setLayout(EclipseUiUtils.noSpaceGridLayout(new GridLayout(2, true)));
 
                Composite leftCmp = new Composite(bodyCmp, SWT.NO_FOCUS);
@@ -121,15 +198,12 @@ public class DefaultDashboardEditor extends AbstractSuiteDashboard implements Re
                gd.horizontalIndent = 10;
                titleLbl.setLayoutData(gd);
 
-               Calendar now = GregorianCalendar.getInstance();
-
                NodeIterator nit = activitiesService.getMyTasks(getSession(), true);
                if (nit.hasNext()) {
                        List<Node> overdueTasks = new ArrayList<>();
                        while (nit.hasNext()) {
                                Node currNode = nit.nextNode();
-                               if (currNode.hasProperty(ActivitiesNames.ACTIVITIES_DUE_DATE)
-                                               && currNode.getProperty(ActivitiesNames.ACTIVITIES_DUE_DATE).getDate().before(now))
+                               if (isOverdue(currNode, ActivitiesNames.ACTIVITIES_DUE_DATE))
                                        overdueTasks.add(currNode);
                        }
                        if (!overdueTasks.isEmpty()) {
@@ -148,8 +222,7 @@ public class DefaultDashboardEditor extends AbstractSuiteDashboard implements Re
                        while (nit.hasNext()) {
                                Node currNode = nit.nextNode();
                                openMilestones.add(currNode);
-                               if (currNode.hasProperty(TrackerNames.TRACKER_TARGET_DATE)
-                                               && currNode.getProperty(TrackerNames.TRACKER_TARGET_DATE).getDate().before(now))
+                               if (isOverdue(currNode, TrackerNames.TRACKER_TARGET_DATE))
                                        overdueMilestones.add(currNode);
                        }
                        if (!overdueMilestones.isEmpty()) {
@@ -177,7 +250,6 @@ public class DefaultDashboardEditor extends AbstractSuiteDashboard implements Re
                        myMilestoneGp.setLayoutData(EclipseUiUtils.fillWidth());
                        populateMuliValueClickableList(myMilestoneGp, openMilestones.toArray(new Node[0]), new MilestoneLp(), null);
                }
-               return bodyCmp;
        }
 
        private class ProjectLp extends ColumnLabelProvider {
@@ -197,7 +269,8 @@ public class DefaultDashboardEditor extends AbstractSuiteDashboard implements Re
                        if (allNb < 1)
                                percent = "empty";
                        else {
-                               double result = (allNb - openNb) / allNb * 100;
+                               double num = allNb - openNb;
+                               double result = num / allNb * 100;
                                percent = String.format("%.1f", result) + "% done";
                        }
                        StringBuilder builder = new StringBuilder();
@@ -230,7 +303,8 @@ public class DefaultDashboardEditor extends AbstractSuiteDashboard implements Re
                        if (allNb < 1)
                                percent = "empty";
                        else {
-                               double result = (allNb - openNb) / allNb * 100;
+                               double num = allNb - openNb;
+                               double result = num / allNb * 100;
                                percent = String.format("%.1f", result) + "% done";
                        }
                        StringBuilder builder = new StringBuilder();
@@ -296,7 +370,7 @@ public class DefaultDashboardEditor extends AbstractSuiteDashboard implements Re
        private void populateMuliValueClickableList(Composite parent, Node[] nodes, ColumnLabelProvider lp,
                        String listLabel) {
                CmsUtils.clear(parent);
-               RowLayout rl = new RowLayout(SWT.HORIZONTAL);
+               RowLayout rl = new RowLayout(SWT.HORIZONTAL | SWT.WRAP);
                rl.wrap = true;
                rl.marginLeft = rl.marginTop = rl.marginBottom = 0;
                rl.marginRight = 8;
@@ -314,9 +388,9 @@ public class DefaultDashboardEditor extends AbstractSuiteDashboard implements Re
                        CmsUtils.markup(link);
                        link.setText(lp.getText(node) + (i != nodes.length ? ", " : ""));
                        i++;
-//                     Color fc = lp.getForeground(node);
-//                     if (fc != null)
-//                             link.setForeground(fc);
+                       // Color fc = lp.getForeground(node);
+                       // if (fc != null)
+                       // link.setForeground(fc);
 
                        link.addSelectionListener(new SelectionAdapter() {
                                private static final long serialVersionUID = 1L;