Révision 1982

tmp/org.txm.analec.rcp/src/org/txm/annotation/urs/toolbar/UnitConcordanceToolbar.java (revision 1982)
1
package org.txm.annotation.urs.toolbar;
2

  
3
import java.util.ArrayList;
4
import java.util.HashMap;
5
import java.util.List;
6
import java.util.Vector;
7

  
8
import org.eclipse.core.runtime.IProgressMonitor;
9
import org.eclipse.core.runtime.IStatus;
10
import org.eclipse.core.runtime.Status;
11
import org.eclipse.jface.dialogs.InputDialog;
12
import org.eclipse.jface.viewers.ArrayContentProvider;
13
import org.eclipse.jface.viewers.ColumnLabelProvider;
14
import org.eclipse.jface.viewers.ComboViewer;
15
import org.eclipse.jface.viewers.ISelection;
16
import org.eclipse.jface.viewers.ISelectionChangedListener;
17
import org.eclipse.jface.viewers.IStructuredSelection;
18
import org.eclipse.jface.viewers.SelectionChangedEvent;
19
import org.eclipse.jface.viewers.StructuredSelection;
20
import org.eclipse.jface.viewers.TableViewer;
21
import org.eclipse.jface.viewers.TableViewerColumn;
22
import org.eclipse.osgi.util.NLS;
23
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.events.KeyEvent;
25
import org.eclipse.swt.events.KeyListener;
26
import org.eclipse.swt.events.SelectionEvent;
27
import org.eclipse.swt.events.SelectionListener;
28
import org.eclipse.swt.events.TypedEvent;
29
import org.eclipse.swt.graphics.Font;
30
import org.eclipse.swt.graphics.FontData;
31
import org.eclipse.swt.layout.GridData;
32
import org.eclipse.swt.widgets.Button;
33
import org.eclipse.swt.widgets.Composite;
34
import org.eclipse.swt.widgets.Display;
35
import org.eclipse.swt.widgets.Label;
36
import org.eclipse.swt.widgets.TableColumn;
37
import org.eclipse.swt.widgets.Text;
38
import org.txm.annotation.rcp.editor.AnnotationArea;
39
import org.txm.annotation.rcp.editor.AnnotationExtension;
40
import org.txm.annotation.urs.URSCorpora;
41
import org.txm.concordance.core.functions.Concordance;
42
import org.txm.concordance.core.functions.Line;
43
import org.txm.concordance.rcp.editors.ConcordanceEditor;
44
import org.txm.concordance.rcp.editors.ConcordanceEditor.ConcordanceColumnSizeControlListener;
45
import org.txm.core.results.TXMResult;
46
import org.txm.objects.Match;
47
import org.txm.rcp.IImageKeys;
48
import org.txm.rcp.editors.TXMEditor;
49
import org.txm.rcp.swt.GLComposite;
50
import org.txm.rcp.swt.provider.SimpleLabelProvider;
51
import org.txm.rcp.utils.JobHandler;
52
import org.txm.searchengine.cqp.corpus.CQPCorpus;
53
import org.txm.utils.logger.Log;
54

  
55
import visuAnalec.donnees.Corpus;
56
import visuAnalec.elements.Unite;
57

  
58
public class UnitConcordanceToolbar extends AnnotationArea {
59

  
60
	/**
61
	 * the limit number of annotation when a confirm dialog box is shown
62
	 */
63
	protected static final int NALERTAFFECTANNOTATIONS = 100;
64
	public static final String EMPTYTEXT = ""; //$NON-NLS-1$
65

  
66
	protected ConcordanceEditor editor;
67

  
68
	/**
69
	 * All available annotation types
70
	 */
71
	protected List<String> typeValuesList;
72

  
73
	/**
74
	 * display the annotation values of the current shown concordance lines
75
	 */
76
	protected TableColumn annotationColumn;
77

  
78
	protected ComboViewer annotationTypesCombo;
79
	/** add a new type of annotation */
80
	private Button addAnnotationTypeButton;
81
	protected Label equalLabel;
82
	protected Text annotationValuesText;
83
	protected Button affectAnnotationButton;
84
	protected Button deleteAnnotationButton;
85
	private Button affectAllAnnotationButton;
86

  
87
	protected Vector<String> typesList = new Vector<String>(); 
88

  
89
	protected Concordance concordance;
90
	private CQPCorpus corpus;
91
	private TableViewerColumn annotationColumnViewer;
92
	private Font annotationColummnFont;
93
	private Font defaultColummnFont;
94
	private Corpus annotManager;
95
	private HashMap<Line, ArrayList<Unite>> annotations;
96
	
97
	protected String unitType;
98
	protected String unitProperty;
99
	private ComboViewer annotationPropertiesCombo;
100

  
101
	public String getName() {
102
		return "Unit (URS)";
103
	}
104
	
105
	@Override
106
	public boolean allowMultipleAnnotations() {
107
		return true;
108
	}
109

  
110
	public UnitConcordanceToolbar() {
111
	}
112

  
113
	@Override
114
	public boolean install(TXMEditor<? extends TXMResult> txmeditor, AnnotationExtension ext, Composite parent, int position) throws Exception {
115

  
116
		this.extension = ext;
117
		this.editor = (ConcordanceEditor)txmeditor;
118
		this.concordance = this.editor.getConcordance();
119
		this.corpus = concordance.getCorpus();
120
		this.annotations = new HashMap<Line, ArrayList<Unite>>();
121
		this.annotManager = URSCorpora.getCorpus(corpus);
122

  
123
		TableViewer viewer = this.editor.getLineTableViewer();
124

  
125
		// RESULT
126
		if (position < 0) {
127
			position = 3 + extension.getNumberOfAnnotationArea(); // after keyword column and previous annotation columns
128
		}
129
		
130
		typesList.clear();
131

  
132
		for (String type : this.annotManager.getStructure().getUnites()) {
133
			typesList.add(type);
134
		}
135
		if (typesList.size() > 0) {
136
			this.unitType = typesList.get(0);
137
		}
138
		
139
		annotationColumnViewer = new TableViewerColumn(viewer, SWT.CENTER, position);
140
		annotationColumn = annotationColumnViewer.getColumn();
141
		annotationColumn.setText("Unit");
142
		annotationColumn.setToolTipText("Unit (URS)");
143
		annotationColumn.setAlignment(SWT.CENTER);
144
		annotationColumn.pack();
145
		annotationColumn.setResizable(true);
146
		annotationColumn.addControlListener(new ConcordanceColumnSizeControlListener(annotationColumn));
147
		defaultColummnFont = editor.getContainer().getFont();
148
		FontData fdata = defaultColummnFont.getFontData()[0];
149
		annotationColummnFont = new Font(Display.getCurrent(), fdata.getName(), fdata.getHeight(), SWT.ITALIC);
150
		annotationColumnViewer.setLabelProvider(new ColumnLabelProvider() {
151

  
152
			@Override
153
			public String getText(Object element) {
154
				Line line = (Line)element;
155
				ArrayList<Unite> a = annotations.get(line);
156
				if (a.size() == 0) return "<none>";
157
				
158
				StringBuffer buffer = new StringBuffer();
159
				for (Unite unite : a) {
160
					String value = unite.getProp(unitProperty);
161
					
162
					if (value != null) {
163
						if (unite.getDeb() < line.matchGetStart() - line.getLeftContextSize()) {
164
							value = "… "+value; //$NON-NLS-1$
165
						}
166

  
167
						if (unite.getFin() > line.matchGetEnd() + line.getRightContextSize()) {
168
							value = value+" …"; //$NON-NLS-1$
169
						}
170
						if (buffer.length() > 0) buffer.append(", ");
171
						buffer.append(value);
172
					} else {
173
						buffer.append("[]");
174
					}
175
				}
176
				return buffer.toString();
177
			}
178
		});
179

  
180
		if (unitType != null) {
181
			annotationColumn.setText(unitType);
182
		}
183

  
184
		annotationArea = new GLComposite(parent, SWT.NONE, "Unit URS");
185
		annotationArea.getLayout().numColumns = 12;
186
		annotationArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
187

  
188
		GridData gdata = new GridData(SWT.CENTER, SWT.CENTER, false, false);
189
		gdata.widthHint = 90;
190

  
191
		// UNIT TYPE
192
		annotationTypesCombo = new ComboViewer(annotationArea, SWT.SINGLE);
193
		annotationTypesCombo.setContentProvider(new ArrayContentProvider());
194
		annotationTypesCombo.setLabelProvider(new SimpleLabelProvider());
195
		annotationTypesCombo.getCombo().addKeyListener(new KeyListener() {
196
			
197
			@Override
198
			public void keyReleased(KeyEvent e) {
199
				if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
200
					String name = annotationTypesCombo.getCombo().getText();
201
					if (typesList.contains(name)) {
202
						StructuredSelection selection = new StructuredSelection(name);
203
						annotationTypesCombo.setSelection(selection, true);
204
					} else {
205
						createNewType(e, name);
206
					}
207
				}
208
			}
209
			
210
			@Override
211
			public void keyPressed(KeyEvent e) { }
212
		});
213
	
214
		gdata = new GridData(SWT.CENTER, SWT.CENTER, false, false);
215
		gdata.widthHint = 200;
216

  
217
		annotationTypesCombo.getCombo().setLayoutData(gdata);
218
		annotationTypesCombo.setInput(typesList);
219
		annotationTypesCombo.getCombo().select(0);
220

  
221
		annotationTypesCombo.addSelectionChangedListener(new ISelectionChangedListener() {
222
			@Override
223
			public void selectionChanged(SelectionChangedEvent event) {
224
				onAnnotationTypeSelected(event);
225
			}
226
		});
227
		
228
		// WITH
229
		
230
		equalLabel = new Label(annotationArea, SWT.NONE);
231
		equalLabel.setText("with"); //$NON-NLS-1$
232
		equalLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
233
		
234
		// PROPERTY
235
		annotationPropertiesCombo = new ComboViewer(annotationArea, SWT.SINGLE);
236
		annotationPropertiesCombo.setContentProvider(new ArrayContentProvider());
237
		annotationPropertiesCombo.setLabelProvider(new SimpleLabelProvider());
238
		annotationPropertiesCombo.getCombo().addKeyListener(new KeyListener() {
239
			
240
			@Override
241
			public void keyReleased(KeyEvent e) {
242
				if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
243
					String name = annotationTypesCombo.getCombo().getText();
244
					if (typesList.contains(name)) {
245
						StructuredSelection selection = new StructuredSelection(name);
246
						annotationTypesCombo.setSelection(selection, true);
247
					} else {
248
						createNewType(e, name);
249
					}
250
				}
251
			}
252
			
253
			@Override
254
			public void keyPressed(KeyEvent e) { }
255
		});
256
	
257
		gdata = new GridData(SWT.CENTER, SWT.CENTER, false, false);
258
		gdata.widthHint = 200;
259

  
260
		annotationPropertiesCombo.getCombo().setLayoutData(gdata);
261
		ArrayList<String> properties = new ArrayList<String>();
262
		properties.add("");
263
		properties.addAll(annotManager.getStructure().getNomsProps(Unite.class, unitType));
264
		annotationPropertiesCombo.setInput(properties);
265
		annotationPropertiesCombo.getCombo().select(0);
266

  
267
		annotationPropertiesCombo.addSelectionChangedListener(new ISelectionChangedListener() {
268
			@Override
269
			public void selectionChanged(SelectionChangedEvent event) {
270
				onAnnotationPropertySelected(event);
271
			}
272
		});
273

  
274
		equalLabel = new Label(annotationArea, SWT.NONE);
275
		equalLabel.setText("="); //$NON-NLS-1$
276
		equalLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
277

  
278
		annotationValuesText = new Text(annotationArea, SWT.BORDER);
279
		annotationValuesText.setToolTipText("value");
280
		GridData gdata2 = new GridData(SWT.FILL, SWT.CENTER, false, false);
281
		gdata2.widthHint = 200;
282
		annotationValuesText.setLayoutData(gdata2);
283
		annotationValuesText.addKeyListener(new KeyListener() {
284
			@Override
285
			public void keyReleased(KeyEvent e) {
286
				if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
287
					affectAnnotationToSelection(editor.getLineTableViewer().getSelection());
288
				}
289
			}
290

  
291
			@Override
292
			public void keyPressed(KeyEvent e) { }
293
		});
294

  
295
		affectAnnotationButton = new Button(annotationArea, SWT.PUSH);
296
		affectAnnotationButton.setText("Create");
297
		affectAnnotationButton.setToolTipText("Create unit for each selected line.");
298
		affectAnnotationButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
299
		affectAnnotationButton.addSelectionListener(new SelectionListener() {
300
			@Override
301
			public void widgetSelected(SelectionEvent e) {
302
				affectAnnotationToSelection(editor.getLineTableViewer().getSelection());
303
			}
304

  
305
			@Override
306
			public void widgetDefaultSelected(SelectionEvent e) { }
307
		});
308
		
309
		deleteAnnotationButton = new Button(annotationArea, SWT.PUSH);
310
		deleteAnnotationButton.setText("Delete");
311
		deleteAnnotationButton.setToolTipText("Delete unit for each selected line.");
312
		deleteAnnotationButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
313
		deleteAnnotationButton.addSelectionListener(new SelectionListener() {
314
			@Override
315
			public void widgetSelected(SelectionEvent e) {
316
				deleteAnnotationValues(editor.getLineTableViewer().getSelection());
317
			}
318

  
319
			@Override
320
			public void widgetDefaultSelected(SelectionEvent e) { }
321
		});
322

  
323
		affectAllAnnotationButton = new Button(annotationArea, SWT.PUSH);
324
		affectAllAnnotationButton.setText("All");
325
		affectAllAnnotationButton.setToolTipText("Create unit for all lines");
326
		affectAllAnnotationButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
327
		affectAllAnnotationButton.addSelectionListener(new SelectionListener() {
328
			@Override
329
			public void widgetSelected(SelectionEvent e) {
330

  
331
				try {
332
					List<? extends Match> matches = concordance.getMatches();
333
					affectMatchesToSelection(matches);
334
				} catch (Exception e1) {
335
					Log.severe(NLS.bind("** Error: {0}", e1.getLocalizedMessage()));
336
					Log.printStackTrace(e1);
337
					return;
338
				}
339
			}
340

  
341
			@Override
342
			public void widgetDefaultSelected(SelectionEvent e) { }
343
		});
344

  
345
		Button closeButton = new Button(annotationArea, SWT.PUSH);
346
		closeButton.setToolTipText("End URS annotation");
347
		closeButton.setImage(IImageKeys.getImage(IImageKeys.ACTION_REMOVE));
348
		
349
		closeButton.addSelectionListener(new SelectionListener() {
350
			
351
			@Override
352
			public void widgetSelected(SelectionEvent e) {
353
				extension.removeAnnotationArea(UnitConcordanceToolbar.this);
354
			}
355
			
356
			@Override
357
			public void widgetDefaultSelected(SelectionEvent e) {}
358
		});
359

  
360
		updateAnnotationWidgetStates();
361
		editor.layout(true);
362
		ext.getSaveButton().setEnabled(true);
363
		return true;
364
	}
365

  
366
	private void updateAnnotationWidgetStates() {
367
		// TODO Auto-generated method stub
368
		
369
	}
370

  
371
	protected void affectMatchesToSelection(final List<? extends Match> matches) {
372

  
373
		final String value = annotationValuesText.getText();
374
		
375
		JobHandler job = new JobHandler("Creating units...", true) {
376
			@Override
377
			protected IStatus run(IProgressMonitor monitor) {
378
				this.runInit(monitor);
379
				try {
380
					for (Match m : matches) {
381
						HashMap<String, String> props = new HashMap<String, String>();
382
						props.put(unitProperty, value);
383
						Unite u = annotManager.addUniteSaisie(unitType, m.getStart(), m.getEnd(), props);
384
					}
385
					
386
				} catch(Exception e) {
387
					Log.severe(NLS.bind("** Error: {0}", e));
388
					Log.printStackTrace(e);
389
					return Status.CANCEL_STATUS;
390
				} catch(ThreadDeath td) {
391
					System.out.println("Canceled");
392
					return Status.CANCEL_STATUS;
393
				}
394

  
395
				return Status.OK_STATUS;
396
			}
397
		};
398
		job.startJob(true);
399
	}
400

  
401
	protected void deleteAnnotationValues(ISelection selection) {
402
		final IStructuredSelection lineSelection = (IStructuredSelection) selection;
403
		List<Line> lines = lineSelection.toList();
404
		ArrayList<Match> matches = new ArrayList<Match>();
405
		for (Line l : lines) {
406
			matches.add(l.getMatch());
407
		}
408
	}
409

  
410
	protected void affectAnnotationToSelection(ISelection selection) {
411
		
412
		final IStructuredSelection lineSelection = (IStructuredSelection) selection;
413
		List<Line> lines = lineSelection.toList();
414
		ArrayList<Match> matches = new ArrayList<Match>();
415
		for (Line l : lines) {
416
			matches.add(l.getMatch());
417
		}
418

  
419
		affectMatchesToSelection(matches);
420
	}
421

  
422
	protected void onAnnotationPropertySelected(SelectionChangedEvent event) {
423
		
424
		IStructuredSelection sel = (IStructuredSelection) annotationPropertiesCombo.getSelection();
425
		String type = (String) sel.getFirstElement();
426
		if (type == null) return;
427
		
428
		unitProperty = type;
429
		
430
		try {
431
			editor.refresh(false);
432
		} catch (Exception e) {
433
			// TODO Auto-generated catch block
434
			e.printStackTrace();
435
		}
436
		
437
		updateAnnotationWidgetStates();
438
	}
439

  
440
	protected void onAnnotationTypeSelected(SelectionChangedEvent event) {
441
		IStructuredSelection sel = (IStructuredSelection) annotationTypesCombo.getSelection();
442
		String type = (String) sel.getFirstElement();
443
		if (type == null) return;
444
		
445
		unitType = type;
446
		annotationColumn.setText(type);
447
		
448
		ArrayList<String> properties = new ArrayList<String>();
449
		properties.add("");
450
		properties.addAll(annotManager.getStructure().getNomsProps(Unite.class, unitType));
451
		annotationPropertiesCombo.setInput(properties);
452
		annotationPropertiesCombo.getCombo().select(0);
453
		
454
		try {
455
			editor.refresh(false);
456
		} catch (Exception e) {
457
			// TODO Auto-generated catch block
458
			e.printStackTrace();
459
		}
460

  
461
		updateAnnotationWidgetStates();
462
	}
463

  
464
	protected void createNewType(TypedEvent e, String typeName) {
465
		
466
		if (typeName == null) typeName = ""; //$NON-NLS-1$
467
		if (typesList.contains(typeName)) return;
468
		
469
		InputDialog dialog = new InputDialog(e.widget.getDisplay().getActiveShell(), "Nouveau type d'unite", "avec le nom", typeName, null); //$NON-NLS-3$
470
		if (dialog.open() == InputDialog.OK) {
471
			String name = dialog.getValue();
472
			if (name.trim().length() == 0) return;
473
			
474
			annotManager.getStructure().ajouterType(Unite.class, name);
475
			typesList.add(name);
476
			annotationTypesCombo.refresh();
477
			StructuredSelection selection = new StructuredSelection(name);
478
			annotationTypesCombo.setSelection(selection, true);
479

  
480
		} else {
481
			System.out.println("Creation aborted."); //$NON-NLS-1$
482
		}
483
	}
484

  
485
	@Override
486
	public boolean save() {
487
		try {
488
			return URSCorpora.saveCorpus(corpus);
489
		} catch(Exception ex) {
490
			ex.printStackTrace();
491
			return false;
492
		}
493
	}
494
	
495
	@Override
496
	public boolean hasChanges() {
497
		return annotManager.isModifie();
498
	}
499

  
500
	@Override
501
	public boolean notifyEndOfCompute() throws Exception {
502
		return true;
503
	}
504

  
505
	public boolean isAnnotationEnable() {
506
		return annotationArea != null;
507
	}
508

  
509
	@Override
510
	public void notifyStartOfRefresh() throws Exception {
511
		List<Line> lines = concordance.getLines(concordance.getTopIndex(), concordance.getTopIndex()+concordance.getNLinePerPage());
512
		annotations.clear();
513
		
514
		ArrayList<Unite> units = annotManager.getUnites(unitType);
515
		
516
//		int iUnit = 0;
517
//		int iLine = 0;
518
//		for (; iUnit < units.size() && iLine < lines.size();) {
519
//			Line line = lines.get(iLine);
520
//			Unite unite = units.get(iUnit);
521
//			if (unite.getFin() < line.getMatch().getStart()) {
522
//				iUnit++;
523
//			} else if (line.getMatch().getEnd() < unite.getDeb()) {
524
//				iLine++;
525
//			} else {
526
//				annotations.put(line, unite);
527
//				iLine++;
528
//			}
529
//		}
530
		for (Line line : lines) {
531
			ArrayList<Unite> lineUnites = new ArrayList<Unite>();
532
			annotations.put(line, lineUnites); //ok test next one
533
			for (int iUnit = 0; iUnit < units.size(); iUnit++) {
534
				
535
				Unite unite = units.get(iUnit);
536
				if (unite.getFin() < line.getMatch().getStart()) {
537
					continue;
538
				} else if (line.getMatch().getEnd() < unite.getDeb()) {
539
					break; //no need to go further
540
				} else {
541
					lineUnites.add(unite);
542
				}
543
			}
544
		}
545
		
546
		updateAnnotationWidgetStates();
547

  
548
		// update annotation column width
549
		if (annotationArea != null) {
550
			annotationColumn.pack();
551

  
552
			annotationColumn.setResizable(true);
553
		} else { 
554
			annotationColumn.setWidth(0);
555
		}
556
	}
557

  
558
	@Override
559
	protected void _close() {
560
		if (!annotationColumn.isDisposed()) annotationColumn.dispose();
561
		if (!annotationArea.isDisposed()) annotationArea.dispose();
562
		extension.layout();
563
	}
564

  
565
	@Override
566
	public void notifyEndOfRefresh() throws Exception {
567
		// TODO Auto-generated method stub
568
	}
569

  
570
	@Override
571
	public void notifyStartOfCreatePartControl() throws Exception {
572
		// TODO Auto-generated method stub
573
	}
574
	
575
	public void notifyEndOfCreatePartControl() throws Exception {
576
		// TODO Auto-generated method stub
577
	}
578

  
579
	@Override
580
	public Class<? extends TXMResult> getAnnotatedTXMResultClass() {
581
		return Concordance.class;
582
	}
583

  
584
	@Override
585
	public boolean isDirty() {
586
		return annotManager.isModifie();
587
	}
588

  
589
	@Override
590
	public boolean discardChanges() {
591
		return false;
592
	}
593

  
594
	@Override
595
	public boolean needToUpdateIndexes() {
596
		return true;
597
	}
598

  
599
	@Override
600
	public boolean notifyStartOfCompute() throws Exception {
601
		return true;
602
	}
603
}
0 604

  
tmp/org.txm.analec.rcp/plugin.xml (revision 1982)
328 328
            class="org.txm.annotation.urs.toolbar.SchemaToolbar"
329 329
            position="2">
330 330
      </annotationtoolbar>
331
      <annotationtoolbar
332
            class="org.txm.annotation.urs.toolbar.UnitConcordanceToolbar"
333
            position="4">
334
      </annotationtoolbar>
335
      <annotationtoolbar
336
            class="org.txm.annotation.urs.toolbar.UnitConcordanceToolbar"
337
            position="4">
338
      </annotationtoolbar>
339
      <annotationtoolbar
340
            class="org.txm.annotation.urs.rcp.AnnotationArea:1"
341
            position="4">
342
      </annotationtoolbar>
331 343
   </extension>
332 344
   <extension
333 345
         point="org.txm.PostTXMHOMEInstallationStep">

Formats disponibles : Unified diff