1 |
1 |
package org.txm.searchengine.cqp.rcp.editor;
|
2 |
2 |
|
3 |
3 |
import org.apache.commons.lang.StringUtils;
|
|
4 |
import org.eclipse.jface.layout.GridDataFactory;
|
|
5 |
import org.eclipse.jface.viewers.CellLabelProvider;
|
|
6 |
import org.eclipse.jface.viewers.ISelection;
|
|
7 |
import org.eclipse.jface.viewers.IStructuredSelection;
|
|
8 |
import org.eclipse.jface.viewers.ITreeContentProvider;
|
|
9 |
import org.eclipse.jface.viewers.TableViewer;
|
|
10 |
import org.eclipse.jface.viewers.TableViewerColumn;
|
|
11 |
import org.eclipse.jface.viewers.ViewerCell;
|
|
12 |
import org.eclipse.osgi.util.NLS;
|
4 |
13 |
import org.eclipse.swt.SWT;
|
|
14 |
import org.eclipse.swt.events.KeyEvent;
|
|
15 |
import org.eclipse.swt.events.KeyListener;
|
|
16 |
import org.eclipse.swt.events.ModifyEvent;
|
|
17 |
import org.eclipse.swt.events.ModifyListener;
|
|
18 |
import org.eclipse.swt.events.SelectionEvent;
|
|
19 |
import org.eclipse.swt.events.SelectionListener;
|
5 |
20 |
import org.eclipse.swt.layout.GridData;
|
|
21 |
import org.eclipse.swt.widgets.Button;
|
|
22 |
import org.eclipse.swt.widgets.Event;
|
6 |
23 |
import org.eclipse.swt.widgets.Label;
|
|
24 |
import org.eclipse.swt.widgets.Text;
|
|
25 |
import org.txm.core.preferences.TXMPreferences;
|
|
26 |
import org.txm.core.results.Parameter;
|
7 |
27 |
import org.txm.rcp.editors.TXMEditor;
|
|
28 |
import org.txm.rcp.editors.listeners.ComputeKeyListener;
|
|
29 |
import org.txm.rcp.swt.widget.AssistedQueryWidget;
|
|
30 |
import org.txm.rcp.views.corpora.CorporaView;
|
|
31 |
import org.txm.searchengine.cqp.clientExceptions.CqiClientException;
|
|
32 |
import org.txm.searchengine.cqp.corpus.Part;
|
8 |
33 |
import org.txm.searchengine.cqp.corpus.Partition;
|
9 |
|
import org.txm.searchengine.cqp.corpus.Subcorpus;
|
|
34 |
import org.txm.utils.logger.Log;
|
10 |
35 |
|
11 |
36 |
public class PartitionEditor extends TXMEditor<Partition> {
|
12 |
37 |
|
13 |
|
Label partitionInfo;
|
14 |
|
Label partsInfo;
|
|
38 |
TableViewer partsViewer;
|
|
39 |
TableViewerColumn nameColumn;
|
|
40 |
TableViewerColumn queryColumn;
|
|
41 |
TableViewerColumn sizeColumn;
|
|
42 |
private Label miniInfoLabel;
|
15 |
43 |
|
|
44 |
@Parameter(key=TXMPreferences.USER_NAME)
|
|
45 |
private Text nameWidget;
|
|
46 |
|
|
47 |
private Text partNameWidget;
|
|
48 |
private AssistedQueryWidget queryWidget;
|
|
49 |
|
16 |
50 |
@Override
|
17 |
51 |
public void _createPartControl() throws Exception {
|
18 |
|
partitionInfo = new Label(this.getResultArea(), SWT.NONE);
|
19 |
|
partitionInfo.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));
|
20 |
52 |
|
21 |
|
partsInfo = new Label(this.getResultArea(), SWT.NONE);
|
22 |
|
partitionInfo.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, true));
|
|
53 |
getResult().compute(false); // ensure Parts are ready
|
|
54 |
|
|
55 |
ComputeKeyListener computeKeyListener = new ComputeKeyListener(this);
|
|
56 |
|
|
57 |
getMainParametersComposite().getLayout().numColumns = 7;
|
|
58 |
|
|
59 |
Label l = new Label(getMainParametersComposite(), SWT.NONE);
|
|
60 |
l.setText("Name");
|
|
61 |
l.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
|
|
62 |
|
|
63 |
nameWidget = new Text(getMainParametersComposite(), SWT.BORDER);
|
|
64 |
nameWidget.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
|
|
65 |
nameWidget.addModifyListener(new ModifyListener() {
|
|
66 |
|
|
67 |
@Override
|
|
68 |
public void modifyText(ModifyEvent e) {
|
|
69 |
getResult().setUserName(nameWidget.getText());
|
|
70 |
getResult().saveParameter(TXMPreferences.USER_NAME, nameWidget.getText());
|
|
71 |
|
|
72 |
CorporaView.refreshObject(getResult());
|
|
73 |
}
|
|
74 |
});
|
|
75 |
|
|
76 |
l = new Label(getMainParametersComposite(), SWT.SEPARATOR | SWT.VERTICAL);
|
|
77 |
l.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
|
|
78 |
|
|
79 |
miniInfoLabel = new Label(getMainParametersComposite(), SWT.NONE);
|
|
80 |
miniInfoLabel.setText("Add a part");
|
|
81 |
miniInfoLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
|
|
82 |
|
|
83 |
partNameWidget = new Text(getMainParametersComposite(), SWT.BORDER);
|
|
84 |
partNameWidget.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
|
|
85 |
|
|
86 |
// [ (v)]
|
|
87 |
// queryWidget = new QueryWidget(queryArea, SWT.DROP_DOWN);
|
|
88 |
queryWidget = new AssistedQueryWidget(getMainParametersComposite(), SWT.DROP_DOWN, getResult().getCorpus());
|
|
89 |
queryWidget.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
|
|
90 |
queryWidget.addKeyListener(new ComputeKeyListener(this) {
|
|
91 |
@Override
|
|
92 |
public void keyPressed(KeyEvent e) {
|
|
93 |
if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
|
|
94 |
try {
|
|
95 |
if (queryWidget.getQueryString().length() > 0 && onPlusButtonPressed(null, queryWidget.getQueryString())) {
|
|
96 |
super.keyPressed(e); // recompute only if the query has been added
|
|
97 |
queryWidget.clearQuery();
|
|
98 |
} else {
|
|
99 |
queryWidget.setText(queryWidget.getQueryString());
|
|
100 |
}
|
|
101 |
} catch (Exception e1) {
|
|
102 |
// TODO Auto-generated catch block
|
|
103 |
e1.printStackTrace();
|
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|
|
107 |
});
|
|
108 |
queryWidget.getQueryWidget().addModifyListener(computeKeyListener);
|
|
109 |
|
|
110 |
Button addNewQueryButton = new Button(getMainParametersComposite(), SWT.PUSH);
|
|
111 |
addNewQueryButton.setText("+");
|
|
112 |
addNewQueryButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
|
|
113 |
addNewQueryButton.addSelectionListener(new SelectionListener() {
|
|
114 |
|
|
115 |
@Override
|
|
116 |
public void widgetSelected(SelectionEvent e) {
|
|
117 |
try {
|
|
118 |
if (queryWidget.getQueryString().length() > 0 && onPlusButtonPressed(null, queryWidget.getQueryString())) {
|
|
119 |
compute(true);
|
|
120 |
queryWidget.clearQuery();
|
|
121 |
} else {
|
|
122 |
queryWidget.setText(queryWidget.getQueryString());
|
|
123 |
}
|
|
124 |
} catch (Exception e1) {
|
|
125 |
// TODO Auto-generated catch block
|
|
126 |
e1.printStackTrace();
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
|
130 |
@Override
|
|
131 |
public void widgetDefaultSelected(SelectionEvent e) { }
|
|
132 |
});
|
|
133 |
|
|
134 |
partsViewer = new TableViewer(this.getResultArea());
|
|
135 |
partsViewer.getTable().setLayoutData(GridDataFactory.fillDefaults().align(GridData.FILL, GridData.FILL).grab(true, true).create());
|
|
136 |
partsViewer.getTable().setHeaderVisible(true);
|
|
137 |
partsViewer.getTable().setLinesVisible(true);
|
|
138 |
|
|
139 |
partsViewer.getTable().addKeyListener(new KeyListener() {
|
|
140 |
|
|
141 |
@Override
|
|
142 |
public void keyReleased(KeyEvent e) {
|
|
143 |
if (e.keyCode == SWT.DEL) {
|
|
144 |
ISelection isel = partsViewer.getSelection();
|
|
145 |
IStructuredSelection sel = (IStructuredSelection)isel;
|
|
146 |
Object o = sel.getFirstElement();
|
|
147 |
if (o != null && o instanceof Part) {
|
|
148 |
Part p = (Part)o;
|
|
149 |
p.delete();
|
|
150 |
partsViewer.setInput(getResult());
|
|
151 |
try {
|
|
152 |
updateEditorFromResult(false);
|
|
153 |
} catch (Exception e1) {
|
|
154 |
// TODO Auto-generated catch block
|
|
155 |
e1.printStackTrace();
|
|
156 |
}
|
|
157 |
}
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
@Override
|
|
162 |
public void keyPressed(KeyEvent e) {}
|
|
163 |
});
|
|
164 |
|
|
165 |
partsViewer.setContentProvider(new ITreeContentProvider() {
|
|
166 |
|
|
167 |
@Override
|
|
168 |
public Object[] getElements(Object inputElement) {
|
|
169 |
if (inputElement instanceof Partition) {
|
|
170 |
return ((Partition)inputElement).getParts().toArray();
|
|
171 |
}
|
|
172 |
|
|
173 |
return new Object[0];
|
|
174 |
}
|
|
175 |
|
|
176 |
@Override
|
|
177 |
public Object[] getChildren(Object parentElement) {
|
|
178 |
return new Object[0];
|
|
179 |
}
|
|
180 |
|
|
181 |
@Override
|
|
182 |
public Object getParent(Object element) {
|
|
183 |
return getResult();
|
|
184 |
}
|
|
185 |
|
|
186 |
@Override
|
|
187 |
public boolean hasChildren(Object element) {
|
|
188 |
return false;
|
|
189 |
}
|
|
190 |
});
|
|
191 |
|
|
192 |
nameColumn = new TableViewerColumn(partsViewer, SWT.NONE);
|
|
193 |
nameColumn.getColumn().setText("Name");
|
|
194 |
nameColumn.getColumn().pack();
|
|
195 |
nameColumn.setLabelProvider(new CellLabelProvider() {
|
|
196 |
|
|
197 |
@Override
|
|
198 |
public void update(ViewerCell cell) {
|
|
199 |
Object element = cell.getElement();
|
|
200 |
|
|
201 |
if (element instanceof Part) {
|
|
202 |
Part part = (Part)element;
|
|
203 |
cell.setText(part.getName());
|
|
204 |
} else {
|
|
205 |
cell.setText("not part");
|
|
206 |
}
|
|
207 |
}
|
|
208 |
});
|
|
209 |
|
|
210 |
queryColumn = new TableViewerColumn(partsViewer, SWT.NONE);
|
|
211 |
queryColumn.getColumn().setText("Query");
|
|
212 |
queryColumn.getColumn().pack();
|
|
213 |
queryColumn.setLabelProvider(new CellLabelProvider() {
|
|
214 |
|
|
215 |
@Override
|
|
216 |
public void update(ViewerCell cell) {
|
|
217 |
Object element = cell.getElement();
|
|
218 |
|
|
219 |
if (element instanceof Part) {
|
|
220 |
Part part = (Part)element;
|
|
221 |
cell.setText(part.getQuery().toString());
|
|
222 |
} else {
|
|
223 |
cell.setText("not part");
|
|
224 |
}
|
|
225 |
}
|
|
226 |
});
|
|
227 |
|
|
228 |
sizeColumn = new TableViewerColumn(partsViewer, SWT.NONE);
|
|
229 |
sizeColumn.getColumn().setText("Size");
|
|
230 |
sizeColumn.getColumn().pack();
|
|
231 |
sizeColumn.setLabelProvider(new CellLabelProvider() {
|
|
232 |
|
|
233 |
@Override
|
|
234 |
public void update(ViewerCell cell) {
|
|
235 |
Object element = cell.getElement();
|
|
236 |
|
|
237 |
if (element instanceof Part) {
|
|
238 |
Part part = (Part)element;
|
|
239 |
try {
|
|
240 |
cell.setText(""+part.getSize());
|
|
241 |
} catch (CqiClientException e) {
|
|
242 |
// TODO Auto-generated catch block
|
|
243 |
e.printStackTrace();
|
|
244 |
}
|
|
245 |
} else {
|
|
246 |
cell.setText("not part");
|
|
247 |
}
|
|
248 |
}
|
|
249 |
});
|
|
250 |
|
|
251 |
partsViewer.setInput(getResult());
|
23 |
252 |
}
|
24 |
253 |
|
|
254 |
/**
|
|
255 |
*
|
|
256 |
* @param event
|
|
257 |
* @param query
|
|
258 |
* @return
|
|
259 |
* @throws Exception
|
|
260 |
*/
|
|
261 |
public boolean onPlusButtonPressed(Event event, String query) throws Exception {
|
|
262 |
System.out.println("Add part: "+query);
|
|
263 |
String pName = partNameWidget.getText().trim();
|
|
264 |
if (pName.length() == 0) {
|
|
265 |
Log.warning("no part name set.");
|
|
266 |
return false;
|
|
267 |
}
|
|
268 |
if (query.length() == 0) {
|
|
269 |
Log.warning("no part query set.");
|
|
270 |
return false;
|
|
271 |
}
|
|
272 |
if (getResult().getPartForName(pName) != null) {
|
|
273 |
Log.warning(NLS.bind("A part with the '{0}' name already exists.", pName));
|
|
274 |
return false;
|
|
275 |
}
|
|
276 |
|
|
277 |
Part p = new Part(getResult(), pName, queryWidget.getQueryString());
|
|
278 |
if (p.compute()) { // the part is OK, update the partition parameters
|
|
279 |
getResult().getQueriesParameter().add(query);
|
|
280 |
getResult().getPartNamesParameter().add(pName);
|
|
281 |
compute(false);
|
|
282 |
|
|
283 |
updateEditorFromResult(false);
|
|
284 |
} else {
|
|
285 |
Log.warning("Can't compute the part with query: "+query);
|
|
286 |
p.delete();
|
|
287 |
}
|
|
288 |
|
|
289 |
return false;
|
|
290 |
}
|
|
291 |
|
25 |
292 |
@Override
|
26 |
293 |
public void updateResultFromEditor() {
|
27 |
294 |
// nothing to do
|
... | ... | |
29 |
296 |
|
30 |
297 |
@Override
|
31 |
298 |
public void updateEditorFromResult(boolean update) throws Exception {
|
32 |
|
partitionInfo.setText(getResult().getDetails());
|
33 |
|
partsInfo.setText(StringUtils.join(getResult().getParts(), "\n"));
|
|
299 |
partsViewer.setInput(getResult());
|
34 |
300 |
}
|
35 |
301 |
}
|