1 package org.argeo.documents.composites;
2
3 import static org.argeo.documents.ui.DocumentsUiService.ACTION_ID_DELETE_BOOKMARK;
4 import static org.argeo.documents.ui.DocumentsUiService.ACTION_ID_RENAME_BOOKMARK;
5
6 import javax.jcr.Node;
7
8 import org.argeo.connect.ui.widgets.AbstractConnectContextMenu;
9 import org.argeo.documents.ui.DocumentsUiService;
10 import org.eclipse.jface.viewers.IStructuredSelection;
11 import org.eclipse.jface.viewers.TableViewer;
12 import org.eclipse.swt.graphics.Point;
13 import org.eclipse.swt.widgets.Control;
14
15
16 public class BookmarksContextMenu extends AbstractConnectContextMenu {
17
18 private final DocumentsUiService documentsUiService;
19 private final Node bookmarkParent;
20 private final TableViewer viewer;
21
22 private final static String[] DEFAULT_ACTIONS = { ACTION_ID_DELETE_BOOKMARK, ACTION_ID_RENAME_BOOKMARK };
23
24 public BookmarksContextMenu(Node bookmarkParent, TableViewer viewer, DocumentsUiService documentsUiService) {
25 super(viewer.getControl().getDisplay(), DEFAULT_ACTIONS);
26 this.documentsUiService = documentsUiService;
27 this.bookmarkParent = bookmarkParent;
28 this.viewer = viewer;
29 createControl();
30 }
31
32 protected boolean performAction(String actionId) {
33 switch (actionId) {
34 case ACTION_ID_DELETE_BOOKMARK:
35 return deleteBookmark();
36
37 case ACTION_ID_RENAME_BOOKMARK:
38 return renameBookmark();
39
40 default:
41 throw new IllegalArgumentException("Unimplemented action " + actionId);
42 }
43 }
44
45 @Override
46 protected boolean aboutToShow(Control source, Point location, IStructuredSelection selection) {
47 boolean emptySel = selection == null || selection.isEmpty();
48 if (emptySel)
49 return false;
50 boolean multiSel = !emptySel && selection.size() > 1;
51 if (multiSel) {
52 setVisible(true, ACTION_ID_DELETE_BOOKMARK);
53 setVisible(false, ACTION_ID_RENAME_BOOKMARK);
54 } else
55 setVisible(true, ACTION_ID_DELETE_BOOKMARK, ACTION_ID_RENAME_BOOKMARK);
56 return true;
57 }
58
59 private boolean deleteBookmark() {
60 IStructuredSelection selection = ((IStructuredSelection) viewer.getSelection());
61 return documentsUiService.deleteBookmark(getParentShell(), selection, bookmarkParent);
62 }
63
64 private boolean renameBookmark() {
65 IStructuredSelection selection = ((IStructuredSelection) viewer.getSelection());
66 return documentsUiService.renameBookmark(selection);
67 }
68
69 @Override
70 protected String getLabel(String actionId) {
71 return documentsUiService.getLabel(actionId);
72 }
73 }