Enhance non-IT projects support
[gpl/argeo-suite.git] / org.argeo.suite.workbench.rap / src / org / argeo / suite / workbench / parts / DefaultDashboardEditor.java
1 package org.argeo.suite.workbench.parts;
2
3 import java.util.ArrayList;
4 import java.util.Calendar;
5 import java.util.GregorianCalendar;
6 import java.util.List;
7
8 import javax.jcr.Node;
9 import javax.jcr.NodeIterator;
10 import javax.jcr.RepositoryException;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.argeo.activities.ActivitiesNames;
15 import org.argeo.activities.ActivitiesService;
16 import org.argeo.cms.auth.CurrentUser;
17 import org.argeo.cms.util.CmsUtils;
18 import org.argeo.connect.workbench.Refreshable;
19 import org.argeo.eclipse.ui.EclipseUiUtils;
20 import org.argeo.jcr.JcrUtils;
21 import org.argeo.node.NodeUtils;
22 import org.argeo.suite.workbench.AsUiPlugin;
23 import org.argeo.tracker.TrackerNames;
24 import org.argeo.tracker.TrackerService;
25 import org.argeo.tracker.ui.TaskListLabelProvider;
26 import org.argeo.tracker.ui.TaskVirtualListComposite;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Label;
33
34 /** Argeo Suite Default Dashboard */
35 public class DefaultDashboardEditor extends AbstractSuiteDashboard implements Refreshable {
36 final static Log log = LogFactory.getLog(DefaultDashboardEditor.class);
37 public final static String ID = AsUiPlugin.PLUGIN_ID + ".defaultDashboardEditor";
38
39 private ActivitiesService activitiesService;
40 private TrackerService trackerService;
41
42 private Composite headerCmp;
43 private Composite taskListCmp;
44 private TaskVirtualListComposite tvlc;
45
46 @Override
47 public void createPartControl(Composite parent) {
48 super.createPartControl(parent);
49 parent.setLayout(EclipseUiUtils.noSpaceGridLayout());
50 Composite bodyCmp = new Composite(parent, SWT.NO_FOCUS);
51 bodyCmp.setLayoutData(EclipseUiUtils.fillAll());
52 bodyCmp.setLayout(new GridLayout());
53
54 // Header
55 try {
56 // Control overviewCmp =
57 createUi(bodyCmp, NodeUtils.getUserHome(getSession()));
58 } catch (RepositoryException e) {
59 e.printStackTrace();
60 }
61
62 taskListCmp = new Composite(bodyCmp, SWT.NO_FOCUS);
63 taskListCmp.setLayoutData(EclipseUiUtils.fillAll());
64
65 populateTaskListCmp();
66 }
67
68 private void populateTaskListCmp() {
69 CmsUtils.clear(taskListCmp);
70 taskListCmp.setLayout(EclipseUiUtils.noSpaceGridLayout());
71 // Composite innerCmp = new Composite(taskListCmp, SWT.NO_FOCUS);
72 // innerCmp.setLayoutData(EclipseUiUtils.fillAll());
73
74 TaskListLabelProvider labelProvider = new TaskListLabelProvider(trackerService);
75 tvlc = new TaskVirtualListComposite(taskListCmp, SWT.NO_FOCUS, labelProvider, 54);
76 tvlc.setLayoutData(EclipseUiUtils.fillAll());
77 forceRefresh(null);
78 }
79
80 @Override
81 public void forceRefresh(Object object) {
82 NodeIterator nit = activitiesService.getMyTasks(getSession(), true);
83 tvlc.getTableViewer().setInput(JcrUtils.nodeIteratorToList(nit).toArray());
84 }
85
86 private Control createUi(Composite parent, Node context) throws RepositoryException {
87 Composite bodyCmp = new Composite(parent, SWT.NO_FOCUS);
88 bodyCmp.setLayout(new GridLayout());
89
90 // Title
91 Label titleLbl = new Label(bodyCmp, SWT.WRAP | SWT.LEAD);
92 CmsUtils.markup(titleLbl);
93 String titleStr = "<big><b> Hello " + CurrentUser.getDisplayName() + " </b></big>";
94 titleLbl.setText(titleStr);
95 GridData gd = new GridData(SWT.CENTER, SWT.BOTTOM, false, false);
96 gd.verticalIndent = 5;
97 gd.horizontalIndent = 10;
98 titleLbl.setLayoutData(gd);
99
100 Calendar now = GregorianCalendar.getInstance();
101
102 NodeIterator nit = activitiesService.getMyTasks(getSession(), true);
103 if (nit.hasNext()) {
104 List<Node> overdueTasks = new ArrayList<>();
105 while (nit.hasNext()) {
106 Node currNode = nit.nextNode();
107 if (currNode.hasProperty(ActivitiesNames.ACTIVITIES_DUE_DATE)
108 && currNode.getProperty(ActivitiesNames.ACTIVITIES_DUE_DATE).getDate().before(now))
109 overdueTasks.add(currNode);
110 }
111 if (!overdueTasks.isEmpty()) {
112 Label overdueLbl = new Label(bodyCmp, SWT.WRAP | SWT.LEAD);
113 CmsUtils.markup(overdueLbl);
114 long size = overdueTasks.size();
115 String overdueStr = "You have " + size + " overdue task" + (size > 1 ? "s" : "") + ".";
116 overdueLbl.setText(overdueStr);
117 }
118 }
119
120 nit = trackerService.getMyMilestones(getSession(), true);
121 if (nit.hasNext()) {
122 List<Node> overdueMilestones = new ArrayList<>();
123 while (nit.hasNext()) {
124 Node currNode = nit.nextNode();
125 if (currNode.hasProperty(TrackerNames.TRACKER_TARGET_DATE)
126 && currNode.getProperty(TrackerNames.TRACKER_TARGET_DATE).getDate().before(now))
127 overdueMilestones.add(currNode);
128 }
129 if (!overdueMilestones.isEmpty()) {
130 Label overdueLbl = new Label(bodyCmp, SWT.WRAP | SWT.LEAD);
131 CmsUtils.markup(overdueLbl);
132 long size = overdueMilestones.size();
133 String overdueStr = "You have " + size + " overdue milestone" + (size > 1 ? "s" : "") + ".";
134 overdueLbl.setText(overdueStr);
135 }
136 }
137 return bodyCmp;
138 }
139
140 @Override
141 public void setFocus() {
142 // refreshDocListGadget();
143 }
144
145 public void setActivitiesService(ActivitiesService activitiesService) {
146 this.activitiesService = activitiesService;
147 }
148
149 public void setTrackerService(TrackerService trackerService) {
150 this.trackerService = trackerService;
151 }
152 }