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