]> git.argeo.org Git - gpl/argeo-suite.git/blob - library/org.argeo.documents.ui/src/org/argeo/documents/ui/DocumentsContextMenu.java
Support for docbook image object.
[gpl/argeo-suite.git] / library / org.argeo.documents.ui / src / org / argeo / documents / ui / DocumentsContextMenu.java
1 package org.argeo.documents.ui;
2
3 import static org.argeo.documents.ui.DocumentsUiService.ACTION_ID_BOOKMARK_FOLDER;
4 import static org.argeo.documents.ui.DocumentsUiService.ACTION_ID_CREATE_FOLDER;
5 import static org.argeo.documents.ui.DocumentsUiService.ACTION_ID_DELETE;
6 import static org.argeo.documents.ui.DocumentsUiService.ACTION_ID_DOWNLOAD_FOLDER;
7 import static org.argeo.documents.ui.DocumentsUiService.ACTION_ID_RENAME;
8 import static org.argeo.documents.ui.DocumentsUiService.ACTION_ID_SHARE_FOLDER;
9 import static org.argeo.documents.ui.DocumentsUiService.ACTION_ID_UPLOAD_FILE;
10
11 import java.nio.file.Files;
12 import java.nio.file.Path;
13
14 import org.argeo.suite.ui.widgets.AbstractConnectContextMenu;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.swt.widgets.Control;
18
19 /** Generic popup context menu to manage NIO Path in a Viewer. */
20 public class DocumentsContextMenu extends AbstractConnectContextMenu {
21 // Local context
22 private final DocumentsFolderComposite browser;
23 private final DocumentsUiService uiService;
24 // private final Repository repository;
25
26 private final static String[] DEFAULT_ACTIONS = { ACTION_ID_CREATE_FOLDER, ACTION_ID_BOOKMARK_FOLDER,
27 ACTION_ID_SHARE_FOLDER, ACTION_ID_DOWNLOAD_FOLDER, ACTION_ID_UPLOAD_FILE, ACTION_ID_RENAME,
28 ACTION_ID_DELETE };
29
30 private Path currFolderPath;
31
32 public DocumentsContextMenu(DocumentsFolderComposite browser,
33 DocumentsUiService documentsUiService) {
34 super(browser.getDisplay(), DEFAULT_ACTIONS);
35 this.browser = browser;
36 this.uiService = documentsUiService;
37 // this.repository = repository;
38
39 createControl();
40 }
41
42 public void setCurrFolderPath(Path currFolderPath) {
43 this.currFolderPath = currFolderPath;
44 }
45
46 protected boolean aboutToShow(Control source, Point location, IStructuredSelection selection) {
47 boolean emptySel = true;
48 boolean multiSel = false;
49 boolean isFolder = true;
50 if (selection != null && !selection.isEmpty()) {
51 emptySel = false;
52 multiSel = selection.size() > 1;
53 if (!multiSel && selection.getFirstElement() instanceof Path) {
54 isFolder = Files.isDirectory((Path) selection.getFirstElement());
55 }
56 }
57 if (emptySel) {
58 setVisible(true, ACTION_ID_CREATE_FOLDER, ACTION_ID_UPLOAD_FILE, ACTION_ID_BOOKMARK_FOLDER);
59 setVisible(false, ACTION_ID_SHARE_FOLDER, ACTION_ID_DOWNLOAD_FOLDER, ACTION_ID_RENAME, ACTION_ID_DELETE
60 );
61 } else if (multiSel) {
62 setVisible(true, ACTION_ID_CREATE_FOLDER, ACTION_ID_UPLOAD_FILE, ACTION_ID_DELETE,
63 ACTION_ID_BOOKMARK_FOLDER);
64 setVisible(false, ACTION_ID_SHARE_FOLDER, ACTION_ID_DOWNLOAD_FOLDER, ACTION_ID_RENAME);
65 } else if (isFolder) {
66 setVisible(true, ACTION_ID_CREATE_FOLDER, ACTION_ID_UPLOAD_FILE, ACTION_ID_RENAME, ACTION_ID_DELETE,
67 ACTION_ID_BOOKMARK_FOLDER);
68 setVisible(false,
69 // to be implemented
70 ACTION_ID_SHARE_FOLDER, ACTION_ID_DOWNLOAD_FOLDER);
71 } else {
72 setVisible(true, ACTION_ID_CREATE_FOLDER, ACTION_ID_UPLOAD_FILE, ACTION_ID_RENAME,
73 ACTION_ID_DELETE);
74 setVisible(false, ACTION_ID_SHARE_FOLDER, ACTION_ID_DOWNLOAD_FOLDER, ACTION_ID_BOOKMARK_FOLDER);
75 }
76 return true;
77 }
78
79 public void show(Control source, Point location, IStructuredSelection selection, Path currFolderPath) {
80 // TODO find a better way to retrieve the parent path (cannot be deduced
81 // from table content because it will fail on an empty folder)
82 this.currFolderPath = currFolderPath;
83 super.show(source, location, selection);
84
85 }
86
87 @Override
88 protected boolean performAction(String actionId) {
89 switch (actionId) {
90 case ACTION_ID_CREATE_FOLDER:
91 createFolder();
92 break;
93 case ACTION_ID_BOOKMARK_FOLDER:
94 bookmarkFolder();
95 break;
96 case ACTION_ID_RENAME:
97 renameItem();
98 break;
99 case ACTION_ID_DELETE:
100 deleteItems();
101 break;
102 // case ACTION_ID_OPEN:
103 // openFile();
104 // break;
105 case ACTION_ID_UPLOAD_FILE:
106 uploadFiles();
107 break;
108 default:
109 throw new IllegalArgumentException("Unimplemented action " + actionId);
110 // case ACTION_ID_SHARE_FOLDER:
111 // return "Share Folder";
112 // case ACTION_ID_DOWNLOAD_FOLDER:
113 // return "Download as zip archive";
114 }
115 browser.setFocus();
116 return false;
117 }
118
119 @Override
120 protected String getLabel(String actionId) {
121 return uiService.getLabel(actionId);
122 }
123
124 private void openFile() {
125 IStructuredSelection selection = ((IStructuredSelection) browser.getViewer().getSelection());
126 if (selection.isEmpty() || selection.size() > 1)
127 // Should never happen
128 return;
129 Path toOpenPath = ((Path) selection.getFirstElement());
130 uiService.openFile(toOpenPath);
131 }
132
133 private void deleteItems() {
134 IStructuredSelection selection = ((IStructuredSelection) browser.getViewer().getSelection());
135 if (selection.isEmpty())
136 return;
137 else if (uiService.deleteItems(getParentShell(), selection))
138 browser.refresh();
139 }
140
141 private void renameItem() {
142 IStructuredSelection selection = ((IStructuredSelection) browser.getViewer().getSelection());
143 if (selection.isEmpty() || selection.size() > 1)
144 // Should never happen
145 return;
146 Path toRenamePath = ((Path) selection.getFirstElement());
147 if (uiService.renameItem(getParentShell(), currFolderPath, toRenamePath))
148 browser.refresh();
149 }
150
151 private void createFolder() {
152 if (uiService.createFolder(getParentShell(), currFolderPath))
153 browser.refresh();
154 }
155
156 private void bookmarkFolder() {
157 Path toBookmarkPath = null;
158 IStructuredSelection selection = ((IStructuredSelection) browser.getViewer().getSelection());
159 if (selection.isEmpty())
160 toBookmarkPath = currFolderPath;
161 else if (selection.size() > 1)
162 toBookmarkPath = currFolderPath;
163 else if (selection.size() == 1) {
164 Path currSelected = ((Path) selection.getFirstElement());
165 if (Files.isDirectory(currSelected))
166 toBookmarkPath = currSelected;
167 else
168 return;
169 }
170 //uiService.bookmarkFolder(toBookmarkPath, repository, null);
171 }
172
173 private void uploadFiles() {
174 if (uiService.uploadFiles(getParentShell(), currFolderPath))
175 browser.refresh();
176 }
177 }