Enhance integration of the various apps
[gpl/argeo-suite.git] / org.argeo.suite.workbench.rap / src / org / argeo / suite / workbench / parts / MyFilesView.java
1 /*
2  * Copyright (C) 2007-2012 Argeo GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.argeo.suite.workbench.parts;
17
18 import static org.argeo.eclipse.ui.EclipseUiUtils.notEmpty;
19
20 import java.nio.file.spi.FileSystemProvider;
21
22 import javax.jcr.NodeIterator;
23 import javax.jcr.Repository;
24 import javax.jcr.RepositoryException;
25 import javax.jcr.Session;
26 import javax.jcr.query.Query;
27 import javax.jcr.query.QueryManager;
28 import javax.jcr.query.QueryResult;
29
30 import org.argeo.cms.util.CmsUtils;
31 import org.argeo.connect.ConnectConstants;
32 import org.argeo.connect.documents.DocumentsService;
33 import org.argeo.connect.people.PeopleException;
34 import org.argeo.connect.people.workbench.rap.providers.BasicNodeListContentProvider;
35 import org.argeo.connect.ui.ConnectUiConstants;
36 import org.argeo.connect.ui.widgets.DelayedText;
37 import org.argeo.connect.ui.workbench.AppWorkbenchService;
38 import org.argeo.connect.util.ConnectJcrUtils;
39 import org.argeo.connect.util.XPathUtils;
40 import org.argeo.eclipse.ui.EclipseUiUtils;
41 import org.argeo.eclipse.ui.fs.FsTableViewer;
42 import org.argeo.jcr.JcrUtils;
43 import org.argeo.suite.workbench.AsUiPlugin;
44 import org.argeo.suite.workbench.fs.FsSingleColumnLabelProvider;
45 import org.eclipse.jface.layout.TableColumnLayout;
46 import org.eclipse.jface.viewers.ColumnWeightData;
47 import org.eclipse.jface.viewers.DoubleClickEvent;
48 import org.eclipse.jface.viewers.IDoubleClickListener;
49 import org.eclipse.jface.viewers.ILabelProvider;
50 import org.eclipse.jface.viewers.IStructuredSelection;
51 import org.eclipse.jface.viewers.StructuredSelection;
52 import org.eclipse.jface.viewers.TableViewer;
53 import org.eclipse.rap.rwt.service.ServerPushSession;
54 import org.eclipse.swt.SWT;
55 import org.eclipse.swt.events.KeyEvent;
56 import org.eclipse.swt.events.KeyListener;
57 import org.eclipse.swt.events.ModifyEvent;
58 import org.eclipse.swt.events.ModifyListener;
59 import org.eclipse.swt.layout.GridData;
60 import org.eclipse.swt.layout.GridLayout;
61 import org.eclipse.swt.widgets.Composite;
62 import org.eclipse.swt.widgets.Label;
63 import org.eclipse.swt.widgets.Table;
64 import org.eclipse.swt.widgets.TableColumn;
65 import org.eclipse.ui.part.ViewPart;
66
67 /** Browse the node file system. */
68 public class MyFilesView extends ViewPart implements IDoubleClickListener {
69         public final static String ID = AsUiPlugin.PLUGIN_ID + ".myFilesView";
70
71         private Repository repository;
72         private Session session;
73         private AppWorkbenchService appWorkbenchService;
74         private FileSystemProvider nodeFileSystemProvider;
75         private DocumentsService documentsService;
76
77         private DelayedText filterTxt;
78         private TableViewer searchResultsViewer;
79         private Composite searchCmp;
80
81         @Override
82         public void createPartControl(Composite parent) {
83                 session = ConnectJcrUtils.login(repository);
84                 // MainLayout
85                 parent.setLayout(new GridLayout());
86                 addFilterPanel(parent);
87                 searchCmp = new Composite(parent, SWT.NO_FOCUS);
88                 searchCmp.setLayout(EclipseUiUtils.noSpaceGridLayout());
89                 searchResultsViewer = createListPart(searchCmp, new FsSingleColumnLabelProvider());
90                 GridData gd = EclipseUiUtils.fillWidth();
91                 gd.heightHint = 0;
92                 searchCmp.setLayoutData(gd);
93
94                 Composite bookmarkCmp = new Composite(parent, SWT.NO_FOCUS);
95                 bookmarkCmp.setLayoutData(EclipseUiUtils.fillAll());
96                 populateBookmarks(bookmarkCmp);
97         }
98
99         public void addFilterPanel(Composite parent) {
100                 // Use a delayed text: the query won't be done until the user stop
101                 // typing for 800ms
102                 int style = SWT.BORDER | SWT.SEARCH | SWT.ICON_CANCEL;
103                 filterTxt = new DelayedText(parent, style, ConnectUiConstants.SEARCH_TEXT_DELAY);
104                 filterTxt.setLayoutData(EclipseUiUtils.fillWidth());
105
106                 final ServerPushSession pushSession = new ServerPushSession();
107                 filterTxt.addDelayedModifyListener(pushSession, new ModifyListener() {
108                         private static final long serialVersionUID = 5003010530960334977L;
109
110                         public void modifyText(ModifyEvent event) {
111                                 filterTxt.getDisplay().asyncExec(new Runnable() {
112                                         @Override
113                                         public void run() {
114                                                 int resultNb = refreshFilteredList();
115                                                 if (resultNb > 0)
116                                                         ((GridData) searchCmp.getLayoutData()).heightHint = 120;
117                                                 else
118                                                         ((GridData) searchCmp.getLayoutData()).heightHint = 0;
119                                                 parent.layout(true, true);
120                                         }
121                                 });
122                                 pushSession.stop();
123                         }
124                 });
125
126                 // Jump to the first item of the list using the down arrow
127                 filterTxt.addKeyListener(new KeyListener() {
128                         private static final long serialVersionUID = -4523394262771183968L;
129
130                         @Override
131                         public void keyReleased(KeyEvent e) {
132                         }
133
134                         @Override
135                         public void keyPressed(KeyEvent e) {
136                                 // boolean shiftPressed = (e.stateMask & SWT.SHIFT) != 0;
137                                 // boolean altPressed = (e.stateMask & SWT.ALT) != 0;
138                                 if (e.keyCode == SWT.ARROW_DOWN || e.keyCode == SWT.TAB) {
139                                         Object first = searchResultsViewer.getElementAt(0);
140                                         if (first != null) {
141                                                 searchResultsViewer.getTable().setFocus();
142                                                 searchResultsViewer.setSelection(new StructuredSelection(first), true);
143                                         }
144                                         e.doit = false;
145                                 }
146                         }
147                 });
148         }
149
150         protected TableViewer createListPart(Composite parent, ILabelProvider labelProvider) {
151                 parent.setLayout(new GridLayout());
152
153                 Composite tableComposite = new Composite(parent, SWT.NONE);
154                 tableComposite.setLayoutData(EclipseUiUtils.fillAll());
155
156                 TableViewer v = new TableViewer(tableComposite);
157                 v.setLabelProvider(labelProvider);
158
159                 TableColumn singleColumn = new TableColumn(v.getTable(), SWT.V_SCROLL);
160                 TableColumnLayout tableColumnLayout = new TableColumnLayout();
161                 tableColumnLayout.setColumnData(singleColumn, new ColumnWeightData(100));
162                 tableComposite.setLayout(tableColumnLayout);
163
164                 // Corresponding table & style
165                 Table table = v.getTable();
166                 table.setLinesVisible(false);
167                 table.setHeaderVisible(false);
168                 CmsUtils.markup(table);
169                 CmsUtils.setItemHeight(table, 26);
170
171                 v.setContentProvider(new BasicNodeListContentProvider());
172                 v.addDoubleClickListener(this);
173                 return v;
174         }
175
176         @Override
177         public void dispose() {
178                 JcrUtils.logoutQuietly(session);
179                 super.dispose();
180         }
181
182         protected int refreshFilteredList() {
183                 try {
184                         String filter = filterTxt.getText();
185                         if (EclipseUiUtils.isEmpty(filter)) {
186                                 searchResultsViewer.setInput(null);
187                                 return 0;
188                         }
189
190                         // XPATH Query
191                         // TODO manage cleanly jcr: prefix
192                         // String xpathQueryStr = "//element(*, " + NodeType.NT_FILE + ")";
193                         String xpathQueryStr = "//element(*, nt:file)";
194                         String xpathFilter = XPathUtils.getFreeTextConstraint(filter);
195                         if (notEmpty(xpathFilter))
196                                 xpathQueryStr += "[" + xpathFilter + "]";
197
198                         QueryManager queryManager = session.getWorkspace().getQueryManager();
199                         Query xpathQuery = queryManager.createQuery(xpathQueryStr, ConnectConstants.QUERY_XPATH);
200                         // xpathQuery.setLimit(TrackerUiConstants.SEARCH_DEFAULT_LIMIT);
201                         QueryResult result = xpathQuery.execute();
202                         NodeIterator nit = result.getNodes();
203                         searchResultsViewer.setInput(JcrUtils.nodeIteratorToList(nit));
204
205                         return (int) nit.getSize();
206                 } catch (RepositoryException e) {
207                         throw new PeopleException("Unable to list files", e);
208                 }
209         }
210
211         private void populateBookmarks(Composite parent) {
212                 CmsUtils.clear(parent);
213                 parent.setLayout(new GridLayout());
214                 int bookmarkColWith = 200;
215
216                 FsTableViewer homeViewer = new FsTableViewer(parent, SWT.SINGLE | SWT.NO_SCROLL);
217                 Table table = homeViewer.configureDefaultSingleColumnTable(bookmarkColWith);
218                 GridData gd = EclipseUiUtils.fillWidth();
219                 gd.horizontalIndent = 10;
220                 table.setLayoutData(gd);
221                 homeViewer.addDoubleClickListener(this);
222                 homeViewer.setPathsInput(documentsService.getMyDocumentsPath(nodeFileSystemProvider, session));
223
224                 appendTitle(parent, "Shared files");
225                 FsTableViewer groupsViewer = new FsTableViewer(parent, SWT.SINGLE | SWT.NO_SCROLL);
226                 table = groupsViewer.configureDefaultSingleColumnTable(bookmarkColWith);
227                 gd = EclipseUiUtils.fillWidth();
228                 gd.horizontalIndent = 10;
229                 table.setLayoutData(gd);
230                 groupsViewer.addDoubleClickListener(this);
231                 groupsViewer.setPathsInput(documentsService.getMyGroupsFilesPath(nodeFileSystemProvider, session));
232
233                 appendTitle(parent, "My bookmarks");
234                 FsTableViewer bookmarksViewer = new FsTableViewer(parent, SWT.SINGLE | SWT.NO_SCROLL);
235                 table = bookmarksViewer.configureDefaultSingleColumnTable(bookmarkColWith);
236                 gd = EclipseUiUtils.fillWidth();
237                 gd.horizontalIndent = 10;
238                 table.setLayoutData(gd);
239                 bookmarksViewer.addDoubleClickListener(this);
240                 bookmarksViewer.setPathsInput(documentsService.getMyBookmarks(nodeFileSystemProvider, session));
241         }
242
243         private Label appendTitle(Composite parent, String value) {
244                 Label titleLbl = new Label(parent, SWT.NONE);
245                 titleLbl.setText(value);
246                 titleLbl.setFont(EclipseUiUtils.getBoldFont(parent));
247                 GridData gd = EclipseUiUtils.fillWidth();
248                 gd.horizontalIndent = 5;
249                 gd.verticalIndent = 5;
250                 titleLbl.setLayoutData(gd);
251                 return titleLbl;
252         }
253
254         @Override
255         public void doubleClick(DoubleClickEvent event) {
256                 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
257                 if (selection.isEmpty())
258                         return;
259                 else {
260
261                         // currNode = ConnectJcrUtils.getNodeFromElement(obj, selectorName);
262                         // if (currNode != null)
263                         // CommandUtils
264                         // .callCommand(peopleWorkbenchService
265                         // .getOpenEntityEditorCmdId(),
266                         // OpenEntityEditor.PARAM_JCR_ID, currNode
267                         // .getIdentifier());
268                         System.out.println("Double clicked");
269                         // TODO open corresponding editor
270                         // Path newSelected = (Path) selection.getFirstElement();
271                         // if (newSelected.equals(currDisplayedFolder) &&
272                         // newSelected.equals(initialPath))
273                         // return;
274                         // initialPath = newSelected;
275                         // setInput(newSelected);
276                 }
277         }
278
279         @Override
280         public void setFocus() {
281         }
282
283         /* DEPENDENCY INJECTION */
284         public void setRepository(Repository repository) {
285                 this.repository = repository;
286         }
287
288         public void setAppWorkbenchService(AppWorkbenchService appWorkbenchService) {
289                 this.appWorkbenchService = appWorkbenchService;
290         }
291
292         public void setNodeFileSystemProvider(FileSystemProvider nodeFileSystemProvider) {
293                 this.nodeFileSystemProvider = nodeFileSystemProvider;
294         }
295
296         public void setDocumentsService(DocumentsService documentsService) {
297                 this.documentsService = documentsService;
298         }
299 }