Remove core module
[gpl/argeo-suite.git] / org.argeo.suite.workbench.rap / src / org / argeo / suite / workbench / parts / QuickSearchView.java
1 package org.argeo.suite.workbench.parts;
2
3 import static org.argeo.eclipse.ui.EclipseUiUtils.notEmpty;
4
5 import javax.jcr.NodeIterator;
6 import javax.jcr.Repository;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.Session;
9 import javax.jcr.query.Query;
10 import javax.jcr.query.QueryResult;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.argeo.activities.ActivitiesService;
15 import org.argeo.cms.util.CmsUtils;
16 import org.argeo.connect.ConnectTypes;
17 import org.argeo.connect.resources.ResourcesService;
18 import org.argeo.connect.ui.ConnectUiConstants;
19 import org.argeo.connect.ui.util.BasicNodeListContentProvider;
20 import org.argeo.connect.ui.widgets.DelayedText;
21 import org.argeo.connect.util.ConnectJcrUtils;
22 import org.argeo.connect.util.XPathUtils;
23 import org.argeo.connect.workbench.Refreshable;
24 import org.argeo.connect.workbench.SystemWorkbenchService;
25 import org.argeo.connect.workbench.util.JcrViewerDClickListener;
26 import org.argeo.eclipse.ui.EclipseUiUtils;
27 import org.argeo.jcr.JcrUtils;
28 import org.argeo.people.PeopleService;
29 import org.argeo.suite.workbench.SuiteWorkbenchException;
30 import org.argeo.suite.workbench.AsUiPlugin;
31 import org.argeo.suite.workbench.internal.EntitySingleColumnLabelProvider;
32 import org.eclipse.jface.layout.TableColumnLayout;
33 import org.eclipse.jface.viewers.ColumnWeightData;
34 import org.eclipse.jface.viewers.ILabelProvider;
35 import org.eclipse.jface.viewers.StructuredSelection;
36 import org.eclipse.jface.viewers.TableViewer;
37 import org.eclipse.rap.rwt.service.ServerPushSession;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.events.KeyEvent;
40 import org.eclipse.swt.events.KeyListener;
41 import org.eclipse.swt.events.ModifyEvent;
42 import org.eclipse.swt.events.ModifyListener;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.layout.GridLayout;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Table;
47 import org.eclipse.swt.widgets.TableColumn;
48 import org.eclipse.ui.part.ViewPart;
49
50 /** A table with a quick search field. */
51 public class QuickSearchView extends ViewPart implements Refreshable {
52 private final static Log log = LogFactory.getLog(QuickSearchView.class);
53 public static final String ID = AsUiPlugin.PLUGIN_ID + ".quickSearchView";
54
55 /* DEPENDENCY INJECTION */
56 private Repository repository;
57 private Session session;
58 private ResourcesService resourcesService;
59 private ActivitiesService activitiesService;
60 private PeopleService peopleService;
61 private SystemWorkbenchService systemWorkbenchService;
62
63 // This page widgets
64 private TableViewer entityViewer;
65 private DelayedText filterTxt;
66
67 @Override
68 public void createPartControl(Composite parent) {
69 session = ConnectJcrUtils.login(repository);
70 // MainLayout
71 parent.setLayout(new GridLayout());
72 addFilterPanel(parent);
73 entityViewer = createListPart(parent, new EntitySingleColumnLabelProvider(resourcesService, activitiesService,
74 peopleService, systemWorkbenchService));
75 refreshFilteredList();
76 }
77
78 public void addFilterPanel(Composite parent) {
79 // Use a delayed text: the query won't be done until the user stop
80 // typing for 800ms
81 int style = SWT.BORDER | SWT.SEARCH | SWT.ICON_CANCEL;
82 filterTxt = new DelayedText(parent, style, ConnectUiConstants.SEARCH_TEXT_DELAY);
83 filterTxt.setLayoutData(EclipseUiUtils.fillWidth());
84
85 final ServerPushSession pushSession = new ServerPushSession();
86 filterTxt.addDelayedModifyListener(pushSession, new ModifyListener() {
87 private static final long serialVersionUID = 5003010530960334977L;
88
89 public void modifyText(ModifyEvent event) {
90 filterTxt.getDisplay().asyncExec(new Runnable() {
91 @Override
92 public void run() {
93 refreshFilteredList();
94 }
95 });
96 pushSession.stop();
97 }
98 });
99
100 // Jump to the first item of the list using the down arrow
101 filterTxt.addKeyListener(new KeyListener() {
102 private static final long serialVersionUID = -4523394262771183968L;
103
104 @Override
105 public void keyReleased(KeyEvent e) {
106 }
107
108 @Override
109 public void keyPressed(KeyEvent e) {
110 // boolean shiftPressed = (e.stateMask & SWT.SHIFT) != 0;
111 // boolean altPressed = (e.stateMask & SWT.ALT) != 0;
112 if (e.keyCode == SWT.ARROW_DOWN || e.keyCode == SWT.TAB) {
113 Object first = entityViewer.getElementAt(0);
114 if (first != null) {
115 entityViewer.getTable().setFocus();
116 entityViewer.setSelection(new StructuredSelection(first), true);
117 }
118 e.doit = false;
119 }
120 }
121 });
122 }
123
124 protected TableViewer createListPart(Composite parent, ILabelProvider labelProvider) {
125 parent.setLayout(new GridLayout());
126
127 Composite tableComposite = new Composite(parent, SWT.NONE);
128 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_VERTICAL
129 | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
130 tableComposite.setLayoutData(gd);
131
132 TableViewer v = new TableViewer(tableComposite);
133 v.setLabelProvider(labelProvider);
134
135 TableColumn singleColumn = new TableColumn(v.getTable(), SWT.V_SCROLL);
136 TableColumnLayout tableColumnLayout = new TableColumnLayout();
137 tableColumnLayout.setColumnData(singleColumn, new ColumnWeightData(85));
138 tableComposite.setLayout(tableColumnLayout);
139
140 // Corresponding table & style
141 Table table = v.getTable();
142 table.setLinesVisible(true);
143 table.setHeaderVisible(false);
144 CmsUtils.markup(table);
145 CmsUtils.setItemHeight(table, 26);
146
147 v.setContentProvider(new BasicNodeListContentProvider());
148 v.addDoubleClickListener(new JcrViewerDClickListener());
149 return v;
150 }
151
152 @Override
153 public void dispose() {
154 JcrUtils.logoutQuietly(session);
155 super.dispose();
156 }
157
158 @Override
159 public void setFocus() {
160 refreshFilteredList();
161 filterTxt.setFocus();
162 }
163
164 @Override
165 public void forceRefresh(Object object) {
166 refreshFilteredList();
167 }
168
169 protected void refreshFilteredList() {
170 try {
171 String filter = filterTxt.getText();
172 // Prevents the query on the full repository
173 // if (isEmpty(filter)) {
174 // entityViewer.setInput(null);
175 // return;
176 // }
177
178 // XPATH Query
179 String xpathQueryStr = "//element(*, " + ConnectTypes.CONNECT_ENTITY + ")";
180 String xpathFilter = XPathUtils.getFreeTextConstraint(filter);
181 if (notEmpty(xpathFilter))
182 xpathQueryStr += "[" + xpathFilter + "]";
183
184 // boolean doOrder = orderResultsBtn != null
185 // && !(orderResultsBtn.isDisposed())
186 // && orderResultsBtn.getSelection();
187 // if (doOrder) {
188 // xpathQueryStr += " order by jcr:title";
189 // }
190
191 long begin = System.currentTimeMillis();
192 Query xpathQuery = XPathUtils.createQuery(session, xpathQueryStr);
193
194 xpathQuery.setLimit(ConnectUiConstants.SEARCH_DEFAULT_LIMIT);
195 QueryResult result = xpathQuery.execute();
196
197 NodeIterator nit = result.getNodes();
198 entityViewer.setInput(JcrUtils.nodeIteratorToList(nit));
199 if (log.isDebugEnabled()) {
200 long end = System.currentTimeMillis();
201 log.debug("Quick Search - Found: " + nit.getSize() + " in " + (end - begin)
202 + " ms by executing XPath query (" + xpathQueryStr + ").");
203 }
204 } catch (RepositoryException e) {
205 throw new SuiteWorkbenchException("Unable to list entities", e);
206 }
207 }
208
209 /* DEPENDENCY INJECTION */
210 public void setRepository(Repository repository) {
211 this.repository = repository;
212 }
213
214 public void setResourcesService(ResourcesService resourcesService) {
215 this.resourcesService = resourcesService;
216 }
217
218 public void setActivitiesService(ActivitiesService activitiesService) {
219 this.activitiesService = activitiesService;
220 }
221
222 public void setPeopleService(PeopleService peopleService) {
223 this.peopleService = peopleService;
224 }
225
226 public void setSystemWorkbenchService(SystemWorkbenchService systemWorkbenchService) {
227 this.systemWorkbenchService = systemWorkbenchService;
228 }
229 }