Révision 3095

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

  
3
import java.text.Collator;
3 4
import java.util.ArrayList;
5
import java.util.Arrays;
4 6
import java.util.HashMap;
5 7
import java.util.List;
8
import java.util.Locale;
6 9
import java.util.Vector;
7 10

  
8 11
import org.eclipse.core.runtime.IProgressMonitor;
9 12
import org.eclipse.core.runtime.IStatus;
10 13
import org.eclipse.core.runtime.Status;
14
import org.eclipse.jface.bindings.keys.KeyStroke;
11 15
import org.eclipse.jface.dialogs.InputDialog;
16
import org.eclipse.jface.fieldassist.ComboContentAdapter;
17
import org.eclipse.jface.fieldassist.TXMAutoCompleteField;
12 18
import org.eclipse.jface.viewers.ArrayContentProvider;
13 19
import org.eclipse.jface.viewers.ColumnLabelProvider;
14 20
import org.eclipse.jface.viewers.ComboViewer;
......
30 36
import org.eclipse.swt.graphics.FontData;
31 37
import org.eclipse.swt.layout.GridData;
32 38
import org.eclipse.swt.widgets.Button;
39
import org.eclipse.swt.widgets.Combo;
33 40
import org.eclipse.swt.widgets.Composite;
34 41
import org.eclipse.swt.widgets.Display;
35 42
import org.eclipse.swt.widgets.Label;
......
54 61

  
55 62
import visuAnalec.donnees.Corpus;
56 63
import visuAnalec.elements.Unite;
64
import visuAnalec.vue.Vue;
57 65

  
58 66
public class UnitConcordanceToolbar extends AnnotationArea {
59 67
	
......
83 91
	
84 92
	protected Label equalLabel;
85 93
	
86
	protected Text annotationValuesText;
94
	protected Combo annotationValuesText;
87 95
	
88 96
	protected Button affectAnnotationButton;
89 97
	
......
113 121
	
114 122
	private ComboViewer annotationPropertiesCombo;
115 123
	
124
	private Vue vue;
125
	
116 126
	@Override
117 127
	public String getName() {
118 128
		return "Unit (URS)";
......
134 144
		this.corpus = concordance.getCorpus();
135 145
		this.annotations = new HashMap<>();
136 146
		this.annotManager = URSCorpora.getCorpus(corpus);
147
		this.vue = URSCorpora.getVue(corpus);
137 148
		
138 149
		TableViewer viewer = this.editor.getLineTableViewer();
139 150
		
......
142 153
			position = 3 + extension.getNumberOfAnnotationArea(); // after keyword column and previous annotation columns
143 154
		}
144 155
		
156
		if (annotManager.getStructure().isVide()) {
157
			annotManager.ajouterType(Unite.class, "Entity");
158
			annotManager.ajouterProp(Unite.class, "Entity", "Property");
159
		}
160
		
145 161
		typesList.clear();
146 162
		
147 163
		for (String type : this.annotManager.getStructure().getUnites()) {
......
168 184
			public String getText(Object element) {
169 185
				Line line = (Line) element;
170 186
				ArrayList<Unite> a = annotations.get(line);
171
				if (a.size() == 0) return "<none>";
187
				if (a == null || a.size() == 0) return "<none>";
172 188
				
173 189
				StringBuffer buffer = new StringBuffer();
174 190
				for (Unite unite : a) {
175
					String value = unite.getProp(unitProperty);
176
					
177
					if (value != null) {
178
						if (unite.getDeb() < line.matchGetStart() - line.getLeftContextSize()) {
179
							value = "… " + value; //$NON-NLS-1$
180
						}
191
					if (unitProperty.length() > 0) {
192
						String value = unite.getProp(unitProperty);
181 193
						
182
						if (unite.getFin() > line.matchGetEnd() + line.getRightContextSize()) {
183
							value = value + " …"; //$NON-NLS-1$
194
						if (value != null) {
195
							if (unite.getDeb() < line.matchGetStart() - line.getLeftContextSize()) {
196
								value = "… " + value; //$NON-NLS-1$
197
							}
198
							
199
							if (unite.getFin() > line.matchGetEnd() + line.getRightContextSize()) {
200
								value = value + " …"; //$NON-NLS-1$
201
							}
202
							if (buffer.length() > 0) buffer.append(", ");
203
							buffer.append(value);
184 204
						}
185
						if (buffer.length() > 0) buffer.append(", ");
186
						buffer.append(value);
187 205
					}
188 206
					else {
189
						buffer.append("[]");
207
						buffer.append(unite.getProps().toString());
190 208
					}
191 209
				}
192 210
				return buffer.toString();
193 211
			}
194 212
		});
195 213
		
196
		if (unitType != null) {
197
			annotationColumn.setText(unitType);
198
		}
199
		
200 214
		annotationArea = new GLComposite(parent, SWT.NONE, "Unit URS");
201 215
		annotationArea.getLayout().numColumns = 12;
216
		annotationArea.getLayout().horizontalSpacing = 1;
202 217
		annotationArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
203 218
		
219
		
220
		Button closeButton = new Button(annotationArea, SWT.PUSH);
221
		closeButton.setToolTipText("End URS annotation");
222
		closeButton.setImage(IImageKeys.getImage(IImageKeys.ACTION_DELETE));
223
		
224
		closeButton.addSelectionListener(new SelectionListener() {
225
			
226
			@Override
227
			public void widgetSelected(SelectionEvent e) {
228
				extension.closeArea(UnitConcordanceToolbar.this, true);
229
			}
230
			
231
			@Override
232
			public void widgetDefaultSelected(SelectionEvent e) {}
233
		});
234
		
235
		
204 236
		GridData gdata = new GridData(SWT.CENTER, SWT.CENTER, false, false);
205 237
		gdata.widthHint = 90;
206 238
		
......
278 310
		
279 311
		annotationPropertiesCombo.getCombo().setLayoutData(gdata);
280 312
		ArrayList<String> properties = new ArrayList<>();
281
		properties.add("");
313
		properties.add(""); // to manage the unit itself
282 314
		properties.addAll(annotManager.getStructure().getNomsProps(Unite.class, unitType));
283 315
		annotationPropertiesCombo.setInput(properties);
284 316
		annotationPropertiesCombo.getCombo().select(0);
......
295 327
		equalLabel.setText("="); //$NON-NLS-1$
296 328
		equalLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
297 329
		
298
		annotationValuesText = new Text(annotationArea, SWT.BORDER);
299
		annotationValuesText.setToolTipText("value");
330
		annotationValuesText = new Combo(annotationArea, SWT.BORDER);
331
		annotationValuesText.setToolTipText("available values");
300 332
		GridData gdata2 = new GridData(SWT.FILL, SWT.CENTER, false, false);
301 333
		gdata2.widthHint = 200;
302 334
		annotationValuesText.setLayoutData(gdata2);
......
313 345
			public void keyPressed(KeyEvent e) {}
314 346
		});
315 347
		
348
		
316 349
		affectAnnotationButton = new Button(annotationArea, SWT.PUSH);
317
		affectAnnotationButton.setText("Create");
350
		affectAnnotationButton.setText("OK");
318 351
		affectAnnotationButton.setToolTipText("Create unit for each selected line.");
319 352
		affectAnnotationButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
320 353
		affectAnnotationButton.addSelectionListener(new SelectionListener() {
......
343 376
			public void widgetDefaultSelected(SelectionEvent e) {}
344 377
		});
345 378
		
346
		affectAllAnnotationButton = new Button(annotationArea, SWT.PUSH);
347
		affectAllAnnotationButton.setText("All");
348
		affectAllAnnotationButton.setToolTipText("Create unit for all lines");
349
		affectAllAnnotationButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
350
		affectAllAnnotationButton.addSelectionListener(new SelectionListener() {
351
			
352
			@Override
353
			public void widgetSelected(SelectionEvent e) {
354
				
355
				try {
356
					List<? extends Match> matches = concordance.getMatches();
357
					affectMatchesToSelection(matches);
358
				}
359
				catch (Exception e1) {
360
					Log.severe(NLS.bind("** Error: {0}", e1.getLocalizedMessage()));
361
					Log.printStackTrace(e1);
362
					return;
363
				}
364
			}
365
			
366
			@Override
367
			public void widgetDefaultSelected(SelectionEvent e) {}
368
		});
379
		// affectAllAnnotationButton = new Button(annotationArea, SWT.PUSH);
380
		// affectAllAnnotationButton.setText("All");
381
		// affectAllAnnotationButton.setToolTipText("Create unit for all lines");
382
		// affectAllAnnotationButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
383
		// affectAllAnnotationButton.addSelectionListener(new SelectionListener() {
384
		//
385
		// @Override
386
		// public void widgetSelected(SelectionEvent e) {
387
		//
388
		// try {
389
		// // List<? extends Match> matches = concordance.getMatches();
390
		// affectMatchesToLines(concordance.getLines());
391
		// }
392
		// catch (Exception e1) {
393
		// Log.severe(NLS.bind("** Error: {0}", e1.getLocalizedMessage()));
394
		// Log.printStackTrace(e1);
395
		// return;
396
		// }
397
		// }
398
		//
399
		// @Override
400
		// public void widgetDefaultSelected(SelectionEvent e) {}
401
		// });
369 402
		
370
		Button closeButton = new Button(annotationArea, SWT.PUSH);
371
		closeButton.setToolTipText("End URS annotation");
372
		closeButton.setImage(IImageKeys.getImage(IImageKeys.ACTION_REMOVE));
373 403
		
374
		closeButton.addSelectionListener(new SelectionListener() {
375
			
376
			@Override
377
			public void widgetSelected(SelectionEvent e) {
378
				extension.closeArea(UnitConcordanceToolbar.this, true);
379
			}
380
			
381
			@Override
382
			public void widgetDefaultSelected(SelectionEvent e) {}
383
		});
404
		onAnnotationTypeSelected(null);
384 405
		
385
		updateAnnotationWidgetStates();
386 406
		editor.layout(true);
387 407
		ext.getSaveButton().setEnabled(true);
388 408
		return true;
389 409
	}
390 410
	
391 411
	private void updateAnnotationWidgetStates() {
392
		// TODO Auto-generated method stub
393
		
412
		annotationValuesText.setEnabled(unitProperty.length() > 0);
394 413
	}
395 414
	
396
	protected void affectMatchesToSelection(final List<? extends Match> matches) {
415
	protected void affectMatchesToLines(final List<Line> lines) {
397 416
		
398 417
		final String value = annotationValuesText.getText();
399 418
		
......
403 422
			protected IStatus run(IProgressMonitor monitor) {
404 423
				this.runInit(monitor);
405 424
				try {
406
					for (Match m : matches) {
407
						HashMap<String, String> props = new HashMap<>();
408
						props.put(unitProperty, value);
409
						Unite u = annotManager.addUniteSaisie(unitType, m.getStart(), m.getEnd(), props);
425
					if (lines.size() == 0) return Status.CANCEL_STATUS;
426
					
427
					for (Line l : lines) {
428
						
429
						Match m = l.getMatch();
430
						if (annotations.containsKey(l)) { // update existing unites
431
							
432
							if (unitProperty.length() > 0) {
433
								ArrayList<Unite> unites = annotations.get(l);
434
								for (Unite u : unites) {
435
									u.getProps().put(unitProperty, value);
436
								}
437
							}
438
						}
439
						else {
440
							ArrayList<Unite> lineUnites = new ArrayList<>();
441
							annotations.put(l, lineUnites);
442
							
443
							HashMap<String, String> props = new HashMap<>();
444
							if (unitProperty.length() > 0) {
445
								props.put(unitProperty, value);
446
							}
447
							
448
							lineUnites.add(annotManager.addUniteSaisie(unitType, m.getStart(), m.getEnd(), props));
449
						}
410 450
					}
411 451
					
452
					editor.getLineTableViewer().getTable().getDisplay().syncExec(new Runnable() {
453
						
454
						@Override
455
						public void run() {
456
							editor.getLineTableViewer().refresh();
457
						}
458
					});
459
					
412 460
				}
413 461
				catch (Exception e) {
414 462
					Log.severe(NLS.bind("** Error: {0}", e));
......
429 477
	protected void deleteAnnotationValues(ISelection selection) {
430 478
		final IStructuredSelection lineSelection = (IStructuredSelection) selection;
431 479
		List<Line> lines = lineSelection.toList();
432
		ArrayList<Match> matches = new ArrayList<>();
433
		for (Line l : lines) {
434
			matches.add(l.getMatch());
480
		if (lines.size() == 0) return;
481
		
482
		if (unitProperty.length() > 0) {
483
			for (Line l : lines) {
484
				if (!annotations.containsKey(l)) continue;
485
				
486
				for (Unite u : annotations.get(l)) {
487
					u.getProps().remove(unitProperty);
488
				}
489
			}
435 490
		}
491
		else { // remove the units
492
			for (Line l : lines) {
493
				if (!annotations.containsKey(l)) continue;
494
				for (Unite u : annotations.get(l)) {
495
					annotManager.supUnite(u);
496
				}
497
				
498
				annotations.remove(l);
499
			}
500
		}
501
		
502
		editor.getLineTableViewer().refresh();
436 503
	}
437 504
	
438 505
	protected void affectAnnotationToSelection(ISelection selection) {
439 506
		
440 507
		final IStructuredSelection lineSelection = (IStructuredSelection) selection;
441 508
		List<Line> lines = lineSelection.toList();
442
		ArrayList<Match> matches = new ArrayList<>();
443
		for (Line l : lines) {
444
			matches.add(l.getMatch());
445
		}
509
		// ArrayList<Match> matches = new ArrayList<>();
510
		// for (Line l : lines) {
511
		// matches.add(l.getMatch());
512
		// }
446 513
		
447
		affectMatchesToSelection(matches);
514
		affectMatchesToLines(lines);
448 515
	}
449 516
	
450 517
	protected void onAnnotationPropertySelected(SelectionChangedEvent event) {
......
455 522
		
456 523
		unitProperty = type;
457 524
		
525
		if (unitProperty.length() > 0) {
526
			annotationColumn.setText(unitType + "@" + unitProperty);
527
		}
528
		else {
529
			annotationColumn.setText(unitType);
530
		}
531
		
532
		setAvailableValues();
533
		
458 534
		try {
459 535
			editor.refresh(false);
460 536
		}
......
466 542
		updateAnnotationWidgetStates();
467 543
	}
468 544
	
545
	private void setAvailableValues() {
546
		if (unitType.length() == 0) {
547
			annotationValuesText.setItems(new String[0]);
548
		}
549
		else {
550
			String[] items = vue.getValeursChamp(Unite.class, unitType, unitProperty);
551
			Arrays.sort(items, Collator.getInstance(Locale.getDefault()));
552
			annotationValuesText.setItems(items);
553
			KeyStroke keys = KeyStroke.getInstance(SWT.CONTROL, SWT.SPACE);
554
			new TXMAutoCompleteField(annotationValuesText, new ComboContentAdapter(), items, keys);
555
		}
556
	}
557
	
469 558
	protected void onAnnotationTypeSelected(SelectionChangedEvent event) {
470 559
		IStructuredSelection sel = (IStructuredSelection) annotationTypesCombo.getSelection();
471 560
		String type = (String) sel.getFirstElement();
......
475 564
		annotationColumn.setText(type);
476 565
		
477 566
		ArrayList<String> properties = new ArrayList<>();
478
		properties.add("");
567
		properties.add(""); // to mange the unit itself
479 568
		properties.addAll(annotManager.getStructure().getNomsProps(Unite.class, unitType));
480 569
		annotationPropertiesCombo.setInput(properties);
481 570
		annotationPropertiesCombo.getCombo().select(0);
482 571
		
483
		try {
484
			editor.refresh(false);
485
		}
486
		catch (Exception e) {
487
			// TODO Auto-generated catch block
488
			e.printStackTrace();
489
		}
490
		
491
		updateAnnotationWidgetStates();
572
		onAnnotationPropertySelected(event);
492 573
	}
493 574
	
494 575
	protected void createNewType(TypedEvent e, String typeName) {
......
562 643
		// }
563 644
		for (Line line : lines) {
564 645
			ArrayList<Unite> lineUnites = new ArrayList<>();
565
			annotations.put(line, lineUnites); // ok test next one
646
			
566 647
			for (int iUnit = 0; iUnit < units.size(); iUnit++) {
567 648
				
568 649
				Unite unite = units.get(iUnit);
......
576 657
					lineUnites.add(unite);
577 658
				}
578 659
			}
660
			if (lineUnites.size() > 0) {
661
				annotations.put(line, lineUnites);
662
			}
579 663
		}
580 664
		
581 665
		updateAnnotationWidgetStates();
......
629 713
	
630 714
	@Override
631 715
	public boolean needToUpdateIndexes() {
632
		return true;
716
		return false;
633 717
	}
634 718
	
635 719
	@Override
tmp/org.txm.analec.rcp/plugin.xml (revision 3095)
431 431
            class="org.txm.annotation.urs.toolbar.SchemaToolbar"
432 432
            position="2">
433 433
      </annotationtoolbar>
434
      <annotationtoolbar
435
            class="org.txm.annotation.urs.toolbar.UnitConcordanceToolbar"
436
            position="4">
437
      </annotationtoolbar>
434 438
   </extension>
435 439
   <extension
436 440
         point="org.txm.PostTXMHOMEInstallationStep">

Formats disponibles : Unified diff