|
1 |
package org.txm.searchengine.rcp.preferences;
|
|
2 |
|
|
3 |
import java.util.LinkedHashMap;
|
|
4 |
|
|
5 |
import org.eclipse.jface.action.MenuManager;
|
|
6 |
import org.eclipse.jface.viewers.IStructuredSelection;
|
|
7 |
import org.eclipse.jface.viewers.ITreeContentProvider;
|
|
8 |
import org.eclipse.jface.viewers.LabelProvider;
|
|
9 |
import org.eclipse.jface.viewers.StructuredSelection;
|
|
10 |
import org.eclipse.jface.viewers.TreeViewer;
|
|
11 |
import org.eclipse.swt.SWT;
|
|
12 |
import org.eclipse.swt.events.KeyEvent;
|
|
13 |
import org.eclipse.swt.events.KeyListener;
|
|
14 |
import org.eclipse.swt.events.SelectionAdapter;
|
|
15 |
import org.eclipse.swt.events.SelectionEvent;
|
|
16 |
import org.eclipse.swt.graphics.Image;
|
|
17 |
import org.eclipse.swt.layout.GridData;
|
|
18 |
import org.eclipse.swt.layout.GridLayout;
|
|
19 |
import org.eclipse.swt.widgets.Button;
|
|
20 |
import org.eclipse.swt.widgets.Composite;
|
|
21 |
import org.eclipse.swt.widgets.Menu;
|
|
22 |
import org.eclipse.swt.widgets.TreeItem;
|
|
23 |
import org.eclipse.ui.IViewPart;
|
|
24 |
import org.eclipse.ui.IWorkbenchPage;
|
|
25 |
import org.eclipse.ui.PartInitException;
|
|
26 |
import org.eclipse.ui.PlatformUI;
|
|
27 |
import org.eclipse.ui.part.ViewPart;
|
|
28 |
import org.txm.core.results.TXMResult;
|
|
29 |
import org.txm.objects.Workspace;
|
|
30 |
import org.txm.rcp.IImageKeys;
|
|
31 |
import org.txm.rcp.messages.TXMUIMessages;
|
|
32 |
import org.txm.rcp.swt.dialog.MultipleValueDialog;
|
|
33 |
import org.txm.rcp.utils.IOClipboard;
|
|
34 |
import org.txm.searchengine.core.IQuery;
|
|
35 |
import org.txm.searchengine.core.QueryHistory;
|
|
36 |
|
|
37 |
public class QueryView extends ViewPart {
|
|
38 |
|
|
39 |
protected TreeViewer tv;
|
|
40 |
protected IQuery copiedQuery;
|
|
41 |
|
|
42 |
@Override
|
|
43 |
public void createPartControl(Composite parent) {
|
|
44 |
|
|
45 |
parent.setLayout(new GridLayout());
|
|
46 |
|
|
47 |
Button go = new Button(parent, SWT.NONE);
|
|
48 |
go.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, false, false));
|
|
49 |
go.setText("Refresh");
|
|
50 |
go.setToolTipText(TXMUIMessages.common_refresh);
|
|
51 |
go.addSelectionListener(new SelectionAdapter() {
|
|
52 |
|
|
53 |
@Override
|
|
54 |
public void widgetSelected(SelectionEvent e) {
|
|
55 |
tv.refresh();
|
|
56 |
}
|
|
57 |
});
|
|
58 |
|
|
59 |
tv = new TreeViewer(parent, SWT.VIRTUAL);
|
|
60 |
tv.getTree().setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
|
|
61 |
|
|
62 |
tv.setContentProvider(new ITreeContentProvider() {
|
|
63 |
|
|
64 |
@Override
|
|
65 |
public Object[] getElements(Object inputElement) {
|
|
66 |
if (inputElement instanceof TXMResult) {
|
|
67 |
return ((TXMResult) inputElement).getDeepChildren(QueryHistory.class).toArray();
|
|
68 |
} else {
|
|
69 |
return new Object[0];
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
@Override
|
|
74 |
public Object[] getChildren(Object parentElement) {
|
|
75 |
if (parentElement instanceof QueryHistory) {
|
|
76 |
return ((QueryHistory) parentElement).getQueries().toArray();
|
|
77 |
} else {
|
|
78 |
return new Object[0];
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
@Override
|
|
83 |
public Object getParent(Object element) {
|
|
84 |
if (element instanceof TXMResult) {
|
|
85 |
return ((TXMResult) element).getParent();
|
|
86 |
} else {
|
|
87 |
return null;
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
@Override
|
|
92 |
public boolean hasChildren(Object parentElement) {
|
|
93 |
if (parentElement instanceof QueryHistory) {
|
|
94 |
QueryHistory h = ((QueryHistory) parentElement);
|
|
95 |
try {
|
|
96 |
h.compute(false); // silent
|
|
97 |
} catch (InterruptedException e) {
|
|
98 |
e.printStackTrace();
|
|
99 |
}
|
|
100 |
|
|
101 |
return h.getQueries().size() > 0;
|
|
102 |
} else {
|
|
103 |
return false;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
});
|
|
108 |
tv.setUseHashlookup(true);
|
|
109 |
tv.setLabelProvider(new LabelProvider() {
|
|
110 |
|
|
111 |
/*
|
|
112 |
* (non-Javadoc)
|
|
113 |
* @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
|
|
114 |
*/
|
|
115 |
@Override
|
|
116 |
public String getText(Object element) {
|
|
117 |
if (element instanceof QueryHistory) {
|
|
118 |
return ((QueryHistory) element).getParent().getName();
|
|
119 |
} else if (element instanceof IQuery) {
|
|
120 |
IQuery q = (IQuery)element;
|
|
121 |
return queryToLabel(q);
|
|
122 |
} else {
|
|
123 |
return ""+element;
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
@Override
|
|
128 |
public final Image getImage(Object element) {
|
|
129 |
if (element instanceof QueryHistory) {
|
|
130 |
return IImageKeys.getImage(IImageKeys.CORPUS);
|
|
131 |
} else {
|
|
132 |
return null;
|
|
133 |
}
|
|
134 |
}
|
|
135 |
});
|
|
136 |
tv.setInput(Workspace.getInstance());
|
|
137 |
|
|
138 |
tv.getTree().addKeyListener(new KeyListener() {
|
|
139 |
|
|
140 |
@Override
|
|
141 |
public void keyReleased(KeyEvent e) { }
|
|
142 |
|
|
143 |
@Override
|
|
144 |
public void keyPressed(KeyEvent e) {
|
|
145 |
|
|
146 |
if (e.keyCode == SWT.F5) {
|
|
147 |
QueryView.this.tv.refresh();
|
|
148 |
return;
|
|
149 |
}
|
|
150 |
|
|
151 |
IStructuredSelection sel = tv.getStructuredSelection();
|
|
152 |
Object o = tv.getStructuredSelection().getFirstElement();
|
|
153 |
|
|
154 |
if (o instanceof IQuery) {
|
|
155 |
IQuery q = (IQuery)o;
|
|
156 |
|
|
157 |
if (e.keyCode == SWT.DEL) {
|
|
158 |
|
|
159 |
TreeItem selectedItem = tv.getTree().getSelection()[0];
|
|
160 |
TreeItem parentItem = selectedItem.getParentItem();
|
|
161 |
Object parentObject = parentItem.getData();
|
|
162 |
if (parentObject instanceof QueryHistory) {
|
|
163 |
QueryHistory qh = (QueryHistory)parentObject;
|
|
164 |
System.out.println("Delete query: "+o+" to "+qh.getParent().getName());
|
|
165 |
qh.removeQuery(q);
|
|
166 |
|
|
167 |
tv.refresh(qh);
|
|
168 |
}
|
|
169 |
|
|
170 |
} else if (e.keyCode == SWT.KEYPAD_CR || e.keyCode == SWT.CR) {
|
|
171 |
|
|
172 |
// System.out.println("Edit query: "+o);
|
|
173 |
LinkedHashMap<String, String> values = new LinkedHashMap<String, String>();
|
|
174 |
values.put("name", q.getName());
|
|
175 |
values.put("query", q.getQueryString());
|
|
176 |
MultipleValueDialog mvd = new MultipleValueDialog(tv.getTree().getDisplay().getActiveShell(), "Edit Query", values);
|
|
177 |
if (mvd.open() == MultipleValueDialog.OK) {
|
|
178 |
if (!mvd.getValues().get("name").isEmpty()) q.setName(mvd.getValues().get("name"));
|
|
179 |
if (!mvd.getValues().get("query").isEmpty()) q.setQuery(mvd.getValues().get("query"));
|
|
180 |
|
|
181 |
tv.refresh(q);
|
|
182 |
}
|
|
183 |
}
|
|
184 |
else {
|
|
185 |
|
|
186 |
if ((char) e.keyCode == 'c' & ((e.stateMask & SWT.CTRL) != 0)) {
|
|
187 |
System.out.println("Copied query: "+queryToLabel(q));
|
|
188 |
copiedQuery = q;
|
|
189 |
IOClipboard.write(q.getQueryString());
|
|
190 |
}
|
|
191 |
// else if ((char) e.keyCode == 'v' & ((e.stateMask & SWT.CTRL) != 0)) {
|
|
192 |
// System.out.println("Paste query: "+o);
|
|
193 |
// }
|
|
194 |
|
|
195 |
}
|
|
196 |
} else if (o instanceof QueryHistory) {
|
|
197 |
if ((char) e.keyCode == 'v' & ((e.stateMask & SWT.CTRL) != 0) && copiedQuery != null) {
|
|
198 |
|
|
199 |
QueryHistory qh = (QueryHistory)o;
|
|
200 |
System.out.println("Paste query: "+copiedQuery+" to "+qh);
|
|
201 |
qh.addQuery(copiedQuery);
|
|
202 |
tv.refresh(qh);
|
|
203 |
} else if (e.keyCode == SWT.KEYPAD_CR || e.keyCode == SWT.CR) {
|
|
204 |
|
|
205 |
QueryHistory qh = (QueryHistory)o;
|
|
206 |
|
|
207 |
LinkedHashMap<String, String> values = new LinkedHashMap<String, String>();
|
|
208 |
values.put("engine", "CQP");
|
|
209 |
values.put("name", "");
|
|
210 |
values.put("query", "");
|
|
211 |
|
|
212 |
MultipleValueDialog mvd = new MultipleValueDialog(tv.getTree().getDisplay().getActiveShell(), "Edit Query", values);
|
|
213 |
|
|
214 |
if (mvd.open() == MultipleValueDialog.OK) {
|
|
215 |
if (!mvd.getValues().get("query").isEmpty() && !mvd.getValues().get("engine").isEmpty()) {
|
|
216 |
|
|
217 |
IQuery newQuery = IQuery.fromPrefString(mvd.getValues().get("engine")+"\t"+mvd.getValues().get("query"));
|
|
218 |
if (!mvd.getValues().get("name").isEmpty()) newQuery.setName(mvd.getValues().get("name"));
|
|
219 |
System.out.println("New query: "+newQuery+" to "+qh);
|
|
220 |
qh.addQuery(newQuery);
|
|
221 |
tv.refresh(qh);
|
|
222 |
}
|
|
223 |
|
|
224 |
|
|
225 |
}
|
|
226 |
|
|
227 |
|
|
228 |
}
|
|
229 |
}
|
|
230 |
}
|
|
231 |
});
|
|
232 |
|
|
233 |
MenuManager menuManager = new MenuManager();
|
|
234 |
Menu menu = menuManager.createContextMenu(tv.getTree());
|
|
235 |
|
|
236 |
// Set the MenuManager
|
|
237 |
tv.getTree().setMenu(menu);
|
|
238 |
getSite().registerContextMenu(menuManager, tv);
|
|
239 |
// Make the selection available
|
|
240 |
getSite().setSelectionProvider(tv);
|
|
241 |
}
|
|
242 |
|
|
243 |
public static String queryToLabel(IQuery q) {
|
|
244 |
|
|
245 |
if (q == null) return "";
|
|
246 |
|
|
247 |
if (q.getName().equals(q.getQueryString())) {
|
|
248 |
return q.getSearchEngine().getName() + " = " + q.getQueryString();
|
|
249 |
} else {
|
|
250 |
return q.getSearchEngine().getName() + " - " + q.getName()+" = "+q.getQueryString();
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
@Override
|
|
255 |
public void setFocus() {
|
|
256 |
if (tv != null && !tv.getTree().isDisposed()) {
|
|
257 |
tv.getTree().setFocus();
|
|
258 |
}
|
|
259 |
}
|
|
260 |
|
|
261 |
/**
|
|
262 |
* Refresh.
|
|
263 |
*/
|
|
264 |
public static void refresh() {
|
|
265 |
QueryView v = (QueryView) PlatformUI.getWorkbench()
|
|
266 |
.getActiveWorkbenchWindow().getActivePage().findView(
|
|
267 |
QueryView.class.getName());
|
|
268 |
|
|
269 |
if (v == null) return;
|
|
270 |
|
|
271 |
v.tv.refresh();
|
|
272 |
}
|
|
273 |
|
|
274 |
/**
|
|
275 |
* Refresh.
|
|
276 |
*/
|
|
277 |
public static void refresh(Object f) {
|
|
278 |
if (f == null) return;
|
|
279 |
|
|
280 |
QueryView v = (QueryView) PlatformUI.getWorkbench()
|
|
281 |
.getActiveWorkbenchWindow().getActivePage().findView(
|
|
282 |
QueryView.class.getName());
|
|
283 |
|
|
284 |
if (v == null) return;
|
|
285 |
|
|
286 |
v.tv.refresh(f);
|
|
287 |
}
|
|
288 |
|
|
289 |
/**
|
|
290 |
* Opens the File Explorer
|
|
291 |
*/
|
|
292 |
public static void open() {
|
|
293 |
open(null);
|
|
294 |
}
|
|
295 |
|
|
296 |
/**
|
|
297 |
* Opens the Fiel Explorer
|
|
298 |
*/
|
|
299 |
public static void open(Object f) {
|
|
300 |
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
|
|
301 |
|
|
302 |
try {
|
|
303 |
IViewPart v = page.showView(QueryView.class.getName());
|
|
304 |
if (v instanceof QueryView) {
|
|
305 |
QueryView e = (QueryView)v;
|
|
306 |
e.tv.setSelection(new StructuredSelection(f));
|
|
307 |
}
|
|
308 |
} catch (PartInitException e) {
|
|
309 |
// TODO Auto-generated catch block
|
|
310 |
e.printStackTrace();
|
|
311 |
}
|
|
312 |
}
|
|
313 |
}
|
0 |
314 |
|