View Javadoc
1   package org.argeo.activities.ui;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   import javax.jcr.Node;
8   import javax.jcr.RepositoryException;
9   import javax.jcr.Session;
10  
11  import org.argeo.activities.ActivitiesException;
12  import org.argeo.activities.ActivitiesService;
13  import org.argeo.activities.ActivitiesTypes;
14  import org.argeo.connect.ui.widgets.AbstractConnectContextMenu;
15  import org.argeo.connect.util.ConnectJcrUtils;
16  import org.eclipse.jface.viewers.IStructuredSelection;
17  import org.eclipse.jface.viewers.Viewer;
18  import org.eclipse.swt.graphics.Point;
19  import org.eclipse.swt.widgets.Control;
20  
21  public class TaskViewerContextMenu extends AbstractConnectContextMenu {
22  	// Default known actions
23  	private final static String ACTION_ID_MARK_AS_DONE = "markAsDone";
24  	private final static String ACTION_ID_CANCEL = "cancel";
25  
26  	private final static String[] DEFAULT_ACTIONS = { ACTION_ID_MARK_AS_DONE, ACTION_ID_CANCEL };
27  
28  	private final Viewer viewer;
29  	private final ActivitiesService activityService;
30  	private final Session session;
31  
32  	public TaskViewerContextMenu(Viewer viewer, Session session, ActivitiesService activityService) {
33  		super(viewer.getControl().getDisplay(), DEFAULT_ACTIONS);
34  		this.viewer = viewer;
35  		this.session = session;
36  		this.activityService = activityService;
37  		createControl();
38  	}
39  
40  	public boolean performAction(String actionId) {
41  		boolean hasChanged = false;
42  		switch (actionId) {
43  		case ACTION_ID_MARK_AS_DONE:
44  			hasChanged = markAsDone();
45  			break;
46  		case ACTION_ID_CANCEL:
47  			hasChanged = cancel();
48  			break;
49  		default:
50  			throw new IllegalArgumentException("Unimplemented action " + actionId);
51  		}
52  
53  		return hasChanged;
54  		// if (hasChanged) {
55  		// refreshFilteredList();
56  		// tableViewer.getTable().setFocus();
57  		// }
58  	}
59  
60  	protected String getLabel(String actionId) {
61  		switch (actionId) {
62  		case ACTION_ID_MARK_AS_DONE:
63  			return "Mark as done";
64  		case ACTION_ID_CANCEL:
65  			return "Cancel";
66  		default:
67  			throw new IllegalArgumentException("Unimplemented action " + actionId);
68  		}
69  	}
70  
71  	@Override
72  	protected boolean aboutToShow(Control source, Point location, IStructuredSelection selection) {
73  		boolean emptySel = selection == null || selection.isEmpty();
74  		if (emptySel)
75  			return false;
76  		else {
77  			setVisible(true, ACTION_ID_MARK_AS_DONE, ACTION_ID_CANCEL);
78  			return true;
79  		}
80  	}
81  
82  	private boolean markAsDone() {
83  		IStructuredSelection selection = ((IStructuredSelection) viewer.getSelection());
84  		@SuppressWarnings("unchecked")
85  		Iterator<Node> it = (Iterator<Node>) selection.iterator();
86  		List<String> modifiedPaths = new ArrayList<>();
87  		boolean hasChanged = false;
88  		try {
89  			while (it.hasNext()) {
90  				Node currNode = it.next();
91  				hasChanged |= activityService.updateStatus(ActivitiesTypes.ACTIVITIES_TASK, currNode, "Done",
92  						modifiedPaths);
93  			}
94  			if (hasChanged) {
95  				session.save();
96  				ConnectJcrUtils.checkPoint(session, modifiedPaths, true);
97  			}
98  			return hasChanged;
99  		} catch (RepositoryException e1) {
100 			throw new ActivitiesException("Cannot mark tasks as done", e1);
101 		}
102 	}
103 
104 	private boolean cancel() {
105 		IStructuredSelection selection = ((IStructuredSelection) viewer.getSelection());
106 		@SuppressWarnings("unchecked")
107 		Iterator<Node> it = (Iterator<Node>) selection.iterator();
108 		List<String> modifiedPaths = new ArrayList<>();
109 		boolean hasChanged = false;
110 		try {
111 			while (it.hasNext()) {
112 				Node currNode = it.next();
113 				hasChanged |= activityService.updateStatus(ActivitiesTypes.ACTIVITIES_TASK, currNode, "Canceled",
114 						modifiedPaths);
115 			}
116 			if (hasChanged) {
117 				session.save();
118 				ConnectJcrUtils.checkPoint(session, modifiedPaths, true);
119 			}
120 			return hasChanged;
121 		} catch (RepositoryException e1) {
122 			throw new ActivitiesException("Cannot mark tasks as done", e1);
123 		}
124 	}
125 }