Révision 782

tmp/org.txm.concordance.rcp/src/org/txm/concordance/rcp/editors/ConcordanceEditor.java (revision 782)
820 820
		from = Math.max(from, 0);
821 821
		
822 822
		int to = from + concordance.getNLinePerPage() - 1;
823
		to = Math.min(to, concordance.getNLines() - 1);
823
		to = Math.min(to, concordance.getNLines());
824 824
		
825 825
		viewer.getControl().setRedraw(false);
826 826
		viewer2.getControl().setRedraw(false);
......
838 838
			lines = new ArrayList<Line>();
839 839
		}
840 840

  
841
		for (ConcordanceEditorExtension ext : extensions) {
842
			try {
843
				ext.update(lines, from, to);
844
			} catch (Exception e) {
845
				// TODO Auto-generated catch block
846
				e.printStackTrace();
847
			}
848
		}
841 849
		viewer.setInput(lines);
842 850
		viewer2.setInput(lines);
843 851

  
tmp/org.txm.concordance.rcp/src/org/txm/concordance/rcp/editors/ConcordanceEditorExtension.java (revision 782)
1 1
package org.txm.concordance.rcp.editors;
2 2

  
3
import java.util.List;
3 4

  
5
import org.txm.concordance.core.functions.Line;
4 6

  
7

  
8

  
5 9
/**
6 10
 * Extension point class to add a stuff to the concordance editor.
7 11
 * 
......
48 52
	 * @return
49 53
	 */
50 54
	public abstract boolean notifyEndOfCompute() throws Exception;
55

  
56
	public abstract void update(List<Line> lines, int from, int to) throws Exception;
51 57
}
tmp/org.txm.rcp/src/main/java/org/txm/rcp/editors/TXMEditorToolBar.java (revision 782)
312 312
			this.buttons.get(groupTitle).dispose();			
313 313
		}
314 314
	}
315

  
316
	public OpenCloseButton getGroupButton(String groupTitle) {
317
		return this.buttons.get(groupTitle);
318
	}
315 319
}
tmp/org.txm.core/src/java/org/txm/utils/BundleUtils.java (revision 782)
6 6
import org.eclipse.core.runtime.Platform;
7 7
import org.osgi.framework.Bundle;
8 8
import org.osgi.framework.Version;
9
import org.osgi.framework.wiring.BundleWiring;
9 10
import org.txm.core.preferences.TBXPreferences;
10 11
import org.txm.core.preferences.TXMPreferences;
11 12
import org.txm.utils.io.FileCopy;
......
179 180
		}
180 181
		return path;
181 182
	}
183

  
184
	public static ClassLoader getLoader(String bundle_id) {
185
		Bundle bundle = Platform.getBundle(bundle_id);
186
		if (bundle == null) {
187
			System.out.println("Error while initializing Oriflamms plugin: bundle not found.");
188
			return null;
189
		}
190
		
191
		BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
192
		ClassLoader classLoader = bundleWiring.getClassLoader();
193
		return classLoader;
194
	}
182 195
}
tmp/org.txm.annotation.rcp/META-INF/MANIFEST.MF (revision 782)
16 16
 org.txm.annotation.rcp.commands.krview,
17 17
 org.txm.annotation.rcp.concordance,
18 18
 org.txm.annotation.rcp.editors.imports.sections,
19
 org.txm.annotation.rcp.messages,
19 20
 org.txm.annotation.rcp.preferences,
20 21
 org.txm.annotation.rcp.views.knowledgerepositories,
21 22
 org.txm.rcp.tests
tmp/org.txm.annotation.rcp/src/org/txm/annotation/rcp/concordance/ConcordanceAnnotationExtension.java (revision 782)
1
package org.txm.annotation.rcp.concordance;
2

  
3
import java.io.File;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Vector;
8
import java.util.logging.Level;
9

  
10
import org.eclipse.core.runtime.IProgressMonitor;
11
import org.eclipse.core.runtime.IStatus;
12
import org.eclipse.core.runtime.Status;
13
import org.eclipse.jface.dialogs.InputDialog;
14
import org.eclipse.jface.dialogs.MessageDialog;
15
import org.eclipse.jface.viewers.ArrayContentProvider;
16
import org.eclipse.jface.viewers.ColumnLabelProvider;
17
import org.eclipse.jface.viewers.ComboViewer;
18
import org.eclipse.jface.viewers.ISelection;
19
import org.eclipse.jface.viewers.ISelectionChangedListener;
20
import org.eclipse.jface.viewers.IStructuredSelection;
21
import org.eclipse.jface.viewers.SelectionChangedEvent;
22
import org.eclipse.jface.viewers.TableViewer;
23
import org.eclipse.jface.viewers.TableViewerColumn;
24
import org.eclipse.swt.SWT;
25
import org.eclipse.swt.events.KeyEvent;
26
import org.eclipse.swt.events.KeyListener;
27
import org.eclipse.swt.events.SelectionEvent;
28
import org.eclipse.swt.events.SelectionListener;
29
import org.eclipse.swt.graphics.Point;
30
import org.eclipse.swt.layout.GridData;
31
import org.eclipse.swt.layout.GridLayout;
32
import org.eclipse.swt.widgets.Button;
33
import org.eclipse.swt.widgets.Combo;
34
import org.eclipse.swt.widgets.Composite;
35
import org.eclipse.swt.widgets.Display;
36
import org.eclipse.swt.widgets.Label;
37
import org.eclipse.swt.widgets.TableColumn;
38
import org.eclipse.swt.widgets.Text;
39
import org.eclipse.ui.IWorkbenchWindow;
40
import org.eclipse.ui.PartInitException;
41
import org.eclipse.ui.dialogs.ListDialog;
42
import org.txm.annotation.core.Annotation;
43
import org.txm.annotation.core.AnnotationManager;
44
import org.txm.annotation.core.KRAnnotationEngine;
45
import org.txm.annotation.core.preferences.AnnotationPreferences;
46
import org.txm.annotation.core.repository.AnnotationType;
47
import org.txm.annotation.core.repository.KnowledgeRepository;
48
import org.txm.annotation.core.repository.KnowledgeRepositoryManager;
49
import org.txm.annotation.core.repository.LocalKnowledgeRepository;
50
import org.txm.annotation.core.repository.SQLKnowledgeRepository;
51
import org.txm.annotation.core.repository.TypedValue;
52
import org.txm.annotation.rcp.commands.InitializeKnowledgeRepository;
53
import org.txm.annotation.rcp.commands.SaveAnnotations;
54
import org.txm.annotation.rcp.commands.krview.OpenKRView;
55
import org.txm.annotation.rcp.messages.AnnotationUIMessages;
56
import org.txm.annotation.rcp.views.knowledgerepositories.KRView;
57
import org.txm.concordance.core.functions.Concordance;
58
import org.txm.concordance.core.functions.Line;
59
import org.txm.concordance.rcp.editors.ConcordanceEditor;
60
import org.txm.concordance.rcp.editors.ConcordanceEditor.ConcordanceColumnSizeControlListener;
61
import org.txm.concordance.rcp.editors.ConcordanceEditorExtension;
62
import org.txm.concordance.rcp.messages.ConcordanceUIMessages;
63
import org.txm.core.preferences.TXMPreferences;
64
import org.txm.rcp.IImageKeys;
65
import org.txm.rcp.commands.OpenBrowser;
66
import org.txm.rcp.messages.TXMUIMessages;
67
import org.txm.rcp.preferences.RCPPreferences;
68
import org.txm.rcp.swt.dialog.ConfirmDialog;
69
import org.txm.rcp.swt.provider.SimpleLabelProvider;
70
import org.txm.rcp.utils.JobHandler;
71
import org.txm.rcp.views.corpora.CorporaView;
72
import org.txm.searchengine.cqp.clientExceptions.CqiClientException;
73
import org.txm.searchengine.cqp.corpus.Corpus;
74
import org.txm.searchengine.cqp.corpus.query.Match;
75
import org.txm.utils.AsciiUtils;
76
import org.txm.utils.logger.Log;
77

  
78
public class ConcordanceAnnotationExtension extends ConcordanceEditorExtension {
79

  
80
	/**
81
	 * the limit number of annotation when a confirm dialog box is shown
82
	 */
83
	protected static final int NALERTAFFECTANNOTATIONS = 100;
84

  
85
	/** The annotation service */
86
	protected AnnotationManager  annotManager;
87
	
88
	//boolean annotation_expert_mode = TxmPreferences.getBoolean(AnnotationPreferencePage.MODE, false);
89

  
90
	/** The annotation area. */
91
	protected Composite annotationArea;
92
	
93
	protected Point annotationSize;
94

  
95
	/** The annot btn. */
96
	protected Button annotationButton;
97
	private Button addAnnotationTypeLink;
98

  
99
	protected Text annotationValuesText;
100
	protected ComboViewer annotationTypesCombo;
101
	
102
	protected List<TypedValue> typeValuesList;
103

  
104
	protected Point annotSize;
105

  
106
	protected TableColumn annotationColumn;
107

  
108
	protected Combo addRemoveCombo;
109
	protected Combo affectCombo;
110
	protected Button affectAnnotationButton;
111

  
112
	protected Button infosAnnotTypeSelectedLink;
113
	
114
	protected HashMap<AnnotationType, ArrayList<TypedValue>> history = new HashMap<AnnotationType, ArrayList<TypedValue>>();
115

  
116
	protected KnowledgeRepository currentKnowledgeRepository = null;
117
	protected Vector<AnnotationType> typesList = new Vector<AnnotationType>(); 
118

  
119
	protected Button addTypedValueLink;
120

  
121
	protected Label withLabel;
122

  
123
	protected AnnotationType tagAnnotationType; // the tag annotation type if annotation mode is simple
124

  
125
	protected Label equalLabel;
126
	protected Boolean advanced_annotation_mode = false;
127
	
128
	protected TypedValue value_to_add;
129

  
130
	protected Concordance concordance;
131

  
132
	private Corpus corpus;
133

  
134
	private ConcordanceAnnotations annotations;
135
	
136
	public ConcordanceAnnotationExtension() {
137
		
138
	}
139

  
140
	@Override
141
	public String getName() {
142
		return "Annotation";
143
	}
144

  
145
	@Override
146
	public boolean installWidgets() throws Exception {
147
		
148
		final Composite controlArea = editor.getNavigationArea();
149
		final Composite navigationArea = editor.getNavigationArea();
150
		TableViewer viewer = editor.getLineTableViewer();
151
		
152
		// CONTROLS
153
		if (KRAnnotationEngine.getKnowledgeRepositoryNames(corpus).size() > 0) { // shown only if corpus has a registered KR
154

  
155
			annotationButton = new Button(navigationArea, SWT.PUSH);
156
			annotationButton.setToolTipText(ConcordanceUIMessages.ConcordancesEditor_75);
157
			annotationButton.setImage(IImageKeys.getImage(IImageKeys.PENCIL));
158
			annotationButton.addSelectionListener(new SelectionListener() {
159
				@Override
160
				public void widgetSelected(SelectionEvent e) {
161
					if (annotationArea != null) {
162
						AnnotationManager am = KRAnnotationEngine.getAnnotationManager(corpus);
163
						if (am != null && am.hasChanges()) {
164
							// && MessageDialog.openConfirm(e.display.getActiveShell(), "Confirm annotation save", "Saving annotation will close this editor. Are you sure you want to save annotations right now ?")
165
							JobHandler saveJob = SaveAnnotations.save(corpus.getMainCorpus());
166
							if (saveJob.getResult() == Status.CANCEL_STATUS) {
167
								// update editor corpus
168
								System.out.println("Fail to save annotations of the corpus."); //$NON-NLS-1$
169
							}
170
						}
171

  
172
					} else {
173
						if (annotationArea == null) composeAnnotationArea(controlArea);
174
						if (annotationArea == null) return;
175

  
176
						annotationColumn.pack();
177
						annotationButton.setToolTipText(ConcordanceUIMessages.ConcordancesEditor_76);
178
						annotationButton.setImage(IImageKeys.getImage(IImageKeys.PENCIL_SAVE));
179

  
180
						annotSize = annotationArea.getSize();
181
						Point size = controlArea.getSize();
182
						controlArea.setSize(size.x, size.y + annotSize.y);
183
						GridData data = (GridData) annotationArea.getLayoutData();
184
						data.heightHint = annotSize.y;
185
						data.minimumHeight = annotSize.y;
186
						annotationArea.setSize(annotSize);
187
						controlArea.layout(true);
188
						controlArea.getParent().layout();
189

  
190
						if (concordance != null) {
191
							AnnotationType type = getSelectedAnnotationType();
192
							if (type != null) {
193
								annotations.setViewAnnotation(type);
194
								annotations.setAnnotationOverlap(true);
195
								editor.fillDisplayArea();
196
							}
197
						}
198

  
199
						CorporaView.refreshObject(corpus);
200
					}
201
				}
202

  
203
				@Override
204
				public void widgetDefaultSelected(SelectionEvent e) {	}
205
			});
206
		}
207
		
208
		// RESULT
209
		
210
		int position = 3;
211
		TableViewerColumn annotationColumnViewer = new TableViewerColumn(viewer, SWT.CENTER, position);
212
		annotationColumn = annotationColumnViewer.getColumn();
213
		annotationColumn.setText(ConcordanceUIMessages.ConcordancesEditor_15);
214
		annotationColumn.setToolTipText(ConcordanceUIMessages.ConcordancesEditor_17);
215
		annotationColumn.setAlignment(SWT.CENTER);
216
		
217
		annotationColumn.addControlListener(new ConcordanceColumnSizeControlListener(annotationColumn));
218
		annotationColumnViewer.setLabelProvider(new ColumnLabelProvider() {
219
			@Override
220
			public String getText(Object element) {
221
				Line line = (Line)element;
222
				AnnotationLine annotationLine = annotations.getAnnotationLine(line);
223
				if (annotationLine.getAnnotation()!=null) {
224
					String value = annotationLine.getAnnotationValue().getStandardName();
225
					Annotation a = annotationLine.getAnnotation();
226
					if (value == null) value = a.getValue();
227
									
228
					if (a.getStart() < line.matchGetStart() - line.getLeftContextSize()) {
229
						value = "… "+value;
230
					}
231
					
232
					if (a.getEnd() > line.matchGetEnd() + line.getRightContextSize()) {
233
						value = value+" …";
234
					} 
235
					return value;
236
				} else {
237
					return "";
238
				}
239
			}
240
		});
241
		
242
		
243
		return true;
244
	}
245
	
246
	public boolean initialiseInput(ConcordanceEditor editor) throws Exception {
247
		this.editor = editor;
248
		concordance = editor.getConcordance();
249
		annotations = new ConcordanceAnnotations(concordance);
250
		corpus = concordance.getCorpus();
251
		
252
		Corpus corpus = editor.getCorpus();
253
		
254
		this.annotManager = KRAnnotationEngine.getAnnotationManager(corpus);
255
		
256
		return true;
257
		
258
	}
259
	
260
	private void composeAnnotationArea(final Composite parent){ 
261
		advanced_annotation_mode = "advanced".equals(TXMPreferences.getString(AnnotationPreferences.ANNOTATION_MODE, RCPPreferences.PREFERENCES_NODE));
262

  
263
		List<KnowledgeRepository> krs = InitializeKnowledgeRepository.get(corpus.getMainCorpus());
264
		typesList.clear();
265
		Log.warning("Corpus KRs: "+krs); //$NON-NLS-1$
266

  
267
		for (KnowledgeRepository kr : krs) {
268
			if (kr == null) continue;
269

  
270
			currentKnowledgeRepository = kr;
271
			Log.warning(" KR: "+kr); //$NON-NLS-1$
272
			List<AnnotationType> krtypes = kr.getAllAnnotationTypes();
273
			Log.warning("Advanced annotation mode ? "+advanced_annotation_mode);
274
			if (!advanced_annotation_mode) { // creates the "span" annotation type
275
				boolean createTagAnnotationType = true;
276
				for (AnnotationType type : krtypes) {
277
					if (type.getName().equals("span")) { //$NON-NLS-1$
278
						createTagAnnotationType = false;
279
					}
280
				}
281
				if (createTagAnnotationType) {
282
					tagAnnotationType = kr.addType("span", "span");//corresponds to an "undef" type //$NON-NLS-1$ //$NON-NLS-2$
283
					typesList.add(tagAnnotationType);
284
				} else {
285
					tagAnnotationType = kr.getType("span"); //$NON-NLS-1$
286
				}
287
			} else {
288
				typesList.addAll(krtypes);
289
			}
290
			Log.warning("Available annotation types: "+typesList);
291
			break;
292
		}
293

  
294
		if (currentKnowledgeRepository == null) {
295
			System.out.println("Error: no suitable KnowledgeRepository found");
296
			return;
297
		}
298

  
299
		annotationArea = new Composite(parent, SWT.NONE);
300
		annotationArea.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, false, false));
301

  
302
		GridLayout annotLayout = new GridLayout(12, false);
303
		annotLayout.verticalSpacing = 0;
304
		annotLayout.marginWidth = 0;
305
		annotLayout.marginHeight = 0;
306
		annotationArea.setLayout(annotLayout);
307

  
308
		//init values history
309
		for (AnnotationType type : typesList) {
310
			history.put(type, new ArrayList<TypedValue>());
311
		}
312

  
313
		addRemoveCombo = new Combo(annotationArea, SWT.READ_ONLY);
314
		String affectLabel = currentKnowledgeRepository.getString(editor.getLocale(), "ConcordancesEditor_22");
315
		if (affectLabel == null) affectLabel= ConcordanceUIMessages.ConcordancesEditor_22;
316
		String removeLabel = currentKnowledgeRepository.getString(editor.getLocale(), "ConcordancesEditor_24");
317
		if (removeLabel == null) removeLabel= ConcordanceUIMessages.ConcordancesEditor_24;
318
		String items[] = {affectLabel, removeLabel};
319
		addRemoveCombo.setItems(items);
320
		addRemoveCombo.select(0);
321
		addRemoveCombo.addSelectionListener(new SelectionListener() {
322
			@Override
323
			public void widgetSelected(SelectionEvent e) {
324
				if (addRemoveCombo.getSelectionIndex() == 0) { // add
325
					annotationValuesText.setEnabled(true);
326
					String withLabelText = currentKnowledgeRepository.getString(editor.getLocale(), "ConcordancesEditor_83");
327
					if (withLabelText == null) withLabelText= ConcordanceUIMessages.ConcordancesEditor_83;
328
					withLabel.setText(withLabelText);
329
					if (equalLabel != null) equalLabel.setText("=");
330
				} else { // remove
331
					annotationValuesText.setEnabled(false);
332
					withLabel.setText(""); //$NON-NLS-1$
333
					if (equalLabel != null) equalLabel.setText("");
334
				}
335
				withLabel.redraw();
336
				annotationArea.layout();
337
				updateAnnotationWidgetStates();
338
			}
339
			@Override
340
			public void widgetDefaultSelected(SelectionEvent e) { }
341
		});
342
		GridData gdata = new GridData(SWT.CENTER, SWT.CENTER, false, true);
343
		gdata.widthHint = 90;
344
		addRemoveCombo.setLayoutData(gdata);
345

  
346
		withLabel = new Label(annotationArea, SWT.NONE);
347
		String withLabelText = currentKnowledgeRepository.getString(editor.getLocale(), "ConcordancesEditor_83");
348
		if (withLabelText == null) withLabelText= ConcordanceUIMessages.ConcordancesEditor_83;
349
		withLabel.setText(withLabelText);
350
		withLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
351

  
352
		if (advanced_annotation_mode) {
353
			equalLabel = new Label(annotationArea, SWT.NONE);
354
			equalLabel.setText("="); //$NON-NLS-1$
355
			equalLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
356
		}
357

  
358
		//Display combo with list of Annotation Type 
359
		if (advanced_annotation_mode) {
360
			annotationTypesCombo = new ComboViewer(annotationArea, SWT.READ_ONLY);
361
			annotationTypesCombo.setContentProvider(new ArrayContentProvider());
362
			annotationTypesCombo.setLabelProvider(new SimpleLabelProvider() {
363
				@Override
364
				public String getColumnText(Object element, int columnIndex) {
365
					return ((AnnotationType)element).getName(); //$NON-NLS-1$
366
				}
367
				@Override
368
				public String getText(Object element) {
369
					return ((AnnotationType)element).getName(); //$NON-NLS-1$
370
				}
371
			});
372
			gdata = new GridData(SWT.CENTER, SWT.CENTER, false, true);
373
			gdata.widthHint = 200;
374

  
375
			annotationTypesCombo.getCombo().setLayoutData(gdata);
376
			annotationTypesCombo.setInput(typesList);
377
			if (advanced_annotation_mode.equals(AnnotationUIMessages.AnnotationPreferences_2)) {
378
				annotationTypesCombo.getCombo().select(0);
379
			}
380

  
381
			annotationTypesCombo.addSelectionChangedListener(new ISelectionChangedListener() {
382
				@Override
383
				public void selectionChanged(SelectionChangedEvent event) {
384
					AnnotationType type = getSelectedAnnotationType();
385
					if (type == null) return;
386
					if (type.getSize() != AnnotationType.ValuesSize.LARGE) {
387
						try {
388
							KnowledgeRepository kr = KnowledgeRepositoryManager.getKnowledgeRepository(type.getKnowledgeRepository());
389
							typeValuesList = kr.getValues(type);
390
						} catch (Exception e) {
391
							// TODO Auto-generated catch block
392
							e.printStackTrace();
393
						}
394
					} else { // set history values
395
						typeValuesList = history.get(type);
396
					}
397

  
398
					annotationColumn.setText(type.getName());
399
					if (concordance != null) {
400
						annotations.setViewAnnotation(type);
401
						annotations.setAnnotationOverlap(true);
402
						editor.fillDisplayArea();
403
					}
404

  
405
					updateAnnotationWidgetStates();
406
				}
407
			});
408
		}
409

  
410
		if (advanced_annotation_mode) {
411
			if (currentKnowledgeRepository instanceof SQLKnowledgeRepository) {
412
				infosAnnotTypeSelectedLink = new Button(annotationArea, SWT.PUSH);
413
				infosAnnotTypeSelectedLink.setImage(IImageKeys.getImage(IImageKeys.ACTION_INFO));
414
				infosAnnotTypeSelectedLink.setEnabled(true);
415
				infosAnnotTypeSelectedLink.setLayoutData(new GridData(SWT.FILL,	SWT.CENTER, false, true));
416
				infosAnnotTypeSelectedLink.addSelectionListener(new SelectionListener() {
417
					@Override
418
					public void widgetSelected(SelectionEvent e) {
419
						AnnotationType type = getSelectedAnnotationType();
420
						if (type == null) return;
421

  
422
						String typedValueURL = type.getURL(); // may be null/empty
423
						if (typedValueURL != null && typedValueURL.length() > 0) {
424
							KnowledgeRepository kr = KnowledgeRepositoryManager.getKnowledgeRepository(type.getKnowledgeRepository());
425
							kr.getTypeURL(type);
426
						}
427
						if (typedValueURL != null && typedValueURL.length() > 0) {
428
							OpenBrowser.openfile(typedValueURL, new File(typedValueURL).getName());
429
						} else {
430
							IWorkbenchWindow window = editor.getSite().getWorkbenchWindow();
431
							try {
432
								OpenKRView.openView(window, type);
433
							} catch (PartInitException e1) {
434
								e1.printStackTrace();
435
							}
436
						}
437
					}
438

  
439
					@Override
440
					public void widgetDefaultSelected(SelectionEvent e) { }
441
				});
442
			}
443

  
444
			if (currentKnowledgeRepository instanceof LocalKnowledgeRepository) {
445
				addAnnotationTypeLink = new Button(annotationArea, SWT.PUSH);
446
				addAnnotationTypeLink.setToolTipText(ConcordanceUIMessages.ConcordancesEditor_115);
447
				addAnnotationTypeLink.setImage(IImageKeys.getImage(IImageKeys.ACTION_ADD));
448
				addAnnotationTypeLink.setEnabled(true);
449
				addAnnotationTypeLink.setLayoutData(new GridData(SWT.FILL,	SWT.CENTER, false, true));
450
				addAnnotationTypeLink.addSelectionListener(new SelectionListener() {
451
					@Override
452
					public void widgetSelected(SelectionEvent e) {
453
						if (currentKnowledgeRepository == null) return;
454
						if (!(currentKnowledgeRepository instanceof LocalKnowledgeRepository)) return;
455

  
456
						LocalKnowledgeRepository kr = (LocalKnowledgeRepository)currentKnowledgeRepository;
457

  
458
						InputDialog dialog = new InputDialog(e.widget.getDisplay().getActiveShell(), ConcordanceUIMessages.ConcordancesEditor_82, ConcordanceUIMessages.ConcordancesEditor_81, "", null); //$NON-NLS-3$
459
						if (dialog.open() == InputDialog.OK) {
460
							String name = dialog.getValue();
461
							if (name.trim().length() == 0) return;
462
							String baseid = AsciiUtils.buildId(name);
463
							String id = AsciiUtils.buildId(name);
464
							int c = 1;
465
							while (kr.getType(id) != null) {
466
								id = baseid+"_"+(c++); //$NON-NLS-1$
467
							}
468
							AnnotationType type = kr.addType(name, id, ""); //$NON-NLS-1$
469
							typesList.add(type);
470
							history.put(type, new ArrayList<TypedValue>());
471

  
472
							if (annotationTypesCombo != null) annotationTypesCombo.refresh();
473
							if (typesList.size() == 1) {
474
								if (annotationTypesCombo != null) annotationTypesCombo.getCombo().select(0);
475
								if (concordance != null) {
476
									annotations.setViewAnnotation(type);
477
									annotations.setAnnotationOverlap(true);
478
									editor.fillDisplayArea();
479
								}
480
								annotationArea.layout(true);
481
							} else {
482
								if (typesList.size() > 1) {
483
									if (annotationTypesCombo != null) {
484
										annotationTypesCombo.getCombo().select(typesList.size()-1);
485
									}
486
								}
487
							}
488
							KRView.refresh();
489
							updateAnnotationWidgetStates();
490
						} else {
491
							System.out.println("Creation aborted."); //$NON-NLS-1$
492
						}
493
					}
494

  
495
					@Override
496
					public void widgetDefaultSelected(SelectionEvent e) { }
497
				});
498
			}
499
		}
500
		if (advanced_annotation_mode) {
501
			Label valueLabel = new Label(annotationArea, SWT.NONE);
502
			String valueLabelText = currentKnowledgeRepository.getString(editor.getLocale(), "ConcordancesEditor_80");
503
			if (valueLabelText == null) {
504
				valueLabelText= ConcordanceUIMessages.ConcordancesEditor_80;
505
			}
506
			valueLabel.setText(valueLabelText);
507
			valueLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
508
		}
509

  
510
		annotationValuesText = new Text(annotationArea, SWT.BORDER);
511
		annotationValuesText.setToolTipText(ConcordanceUIMessages.ConcordancesEditor_32);
512
		GridData gdata2 = new GridData(SWT.FILL, SWT.CENTER, false, true);
513
		gdata2.widthHint = 200;
514
		annotationValuesText.setLayoutData(gdata2);
515
		annotationValuesText.addKeyListener(new KeyListener() {
516
			@Override
517
			public void keyReleased(KeyEvent e) {
518
				if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
519
					affectAnnotationToSelection(editor.getLineTableViewer().getSelection());
520
				}
521
			}
522

  
523
			@Override
524
			public void keyPressed(KeyEvent e) { }
525
		});
526

  
527
		if (currentKnowledgeRepository instanceof LocalKnowledgeRepository) {
528
			addTypedValueLink = new Button(annotationArea, SWT.PUSH);
529
			addTypedValueLink.setText("..."); //$NON-NLS-1$
530
			if (advanced_annotation_mode.equals(AnnotationUIMessages.AnnotationPreferences_2)) {
531
				addTypedValueLink.setToolTipText(ConcordanceUIMessages.ConcordancesEditor_79);
532
			} else {
533
				addTypedValueLink.setToolTipText(ConcordanceUIMessages.ConcordancesEditor_77);
534
			}
535
			addTypedValueLink.setEnabled(true);
536
			addTypedValueLink.setLayoutData(new GridData(SWT.FILL,	SWT.CENTER, false, true));
537
			addTypedValueLink.addSelectionListener(new SelectionListener() {
538
				@Override
539
				public void widgetSelected(SelectionEvent e) {
540
					if (currentKnowledgeRepository == null) return;
541
					if (!(currentKnowledgeRepository instanceof LocalKnowledgeRepository)) return;
542

  
543
					AnnotationType type = getSelectedAnnotationType();
544
					if (type == null) return;
545

  
546
					LocalKnowledgeRepository kr = (LocalKnowledgeRepository)currentKnowledgeRepository;
547

  
548
					ListDialog dialog = new ListDialog(e.widget.getDisplay().getActiveShell());
549
					if (advanced_annotation_mode.equals(AnnotationUIMessages.AnnotationPreferences_2)) {
550
						String title = currentKnowledgeRepository.getString(editor.getLocale(), "ConcordancesEditor_100");
551
						if (title == null) title = ConcordanceUIMessages.ConcordancesEditor_100;
552
						dialog.setTitle(ConcordanceUIMessages.bind(title, type.getName()));//+"valeurs de "+type.getName());
553
					} else {
554
						dialog.setTitle(ConcordanceUIMessages.ConcordancesEditor_99);//+"valeurs de "+type.getName());
555
					}
556
					dialog.setContentProvider(new ArrayContentProvider());
557
					dialog.setLabelProvider(new SimpleLabelProvider() {
558
						public String getColumnText(Object element, int columnIndex) {
559
							if (element instanceof TypedValue)
560
								return ((TypedValue)element).getId();
561
							return element.toString();
562
						}
563
					});
564
					List<TypedValue> values;
565
					try {
566
						values = kr.getValues(type);
567
					} catch (Exception e1) {
568
						e1.printStackTrace();
569
						return;
570
					}
571
					dialog.setInput(values);
572
					if (dialog.open() == InputDialog.OK) {
573
						TypedValue value = (TypedValue) dialog.getResult()[0];
574
						String name = value.getId();
575
						if (name.trim().length() == 0) return;
576

  
577
						annotationValuesText.setText(name);
578
					} else {
579

  
580
					}
581
				}
582

  
583
				@Override
584
				public void widgetDefaultSelected(SelectionEvent e) { }
585
			});
586
		}
587

  
588
		affectCombo = new Combo(annotationArea, SWT.READ_ONLY);
589
		affectCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true));
590
		String items2[] = {ConcordanceUIMessages.ConcordancesEditor_35, ConcordanceUIMessages.ConcordancesEditor_42, ConcordanceUIMessages.ConcordancesEditor_49};
591
		affectCombo.setItems(items2);
592
		affectCombo.select(0);
593
		gdata = new GridData(SWT.CENTER, SWT.CENTER, false, true);
594
		gdata.widthHint = 140;
595
		affectCombo.setLayoutData(gdata);
596

  
597
		affectAnnotationButton = new Button(annotationArea, SWT.PUSH);
598
		affectAnnotationButton.setText(ConcordanceUIMessages.ConcordancesEditor_85);
599
		affectAnnotationButton.setToolTipText(ConcordanceUIMessages.ConcordancesEditor_116);
600
		affectAnnotationButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true));
601
		affectAnnotationButton.addSelectionListener(new SelectionListener() {
602
			@Override
603
			public void widgetSelected(SelectionEvent e) {
604
				int isel = affectCombo.getSelectionIndex();
605
				if (isel == 0) { // selected line
606
					affectAnnotationToSelection(editor.getLineTableViewer().getSelection());
607
				} else if (isel == 1) { // page
608
					try {
609
						List<Match> matches = concordance.getMatches().subList(editor.getTopLine(), editor.getBottomLine()+1);
610
						affectMatchesToSelection(matches);
611
					} catch (CqiClientException e1) {
612
						Log.severe(ConcordanceUIMessages.ConcordancesEditor_53+e1);
613
						Log.printStackTrace(e1);
614
						return;
615
					}
616
				} else { // all
617
					try {
618
						List<Match> matches = concordance.getMatches();
619
						affectMatchesToSelection(matches);
620
					} catch (CqiClientException e1) {
621
						Log.severe(ConcordanceUIMessages.ConcordancesEditor_53+e1);
622
						Log.printStackTrace(e1);
623
						return;
624
					}
625
				}
626
			}
627

  
628
			@Override
629
			public void widgetDefaultSelected(SelectionEvent e) { }
630
		});
631
		Button cancelAnnotateButton = new Button(annotationArea, SWT.PUSH);
632
		cancelAnnotateButton.setText(ConcordanceUIMessages.ConcordancesEditor_101);
633
		cancelAnnotateButton.addSelectionListener(new SelectionListener() {
634
			@Override
635
			public void widgetSelected(SelectionEvent e) {
636
				//@TODO Closing the annotation tool bar 
637
				//System.out.println("must hide");
638
				annotationSize = annotationArea.getSize();
639
				annotationArea.dispose();
640
				annotationArea = null;
641
				parent.layout(true);
642
				parent.layout();
643
				annotationButton.setToolTipText(ConcordanceUIMessages.ConcordancesEditor_75);
644
				annotationButton.setImage(IImageKeys.getImage(IImageKeys.PENCIL));
645
				annotationColumn.setWidth(0);
646
				annotationColumn.setResizable(false);
647
			}
648

  
649
			@Override
650
			public void widgetDefaultSelected(SelectionEvent e) { }
651
		});
652

  
653
		updateAnnotationWidgetStates();
654
		
655
		parent.layout();
656
	}
657

  
658
	protected void affectAnnotationToSelection(ISelection selection) {
659

  
660
		final IStructuredSelection lineSelection = (IStructuredSelection) selection;
661
		List<Line> lines = lineSelection.toList();
662
		ArrayList<Match> matches = new ArrayList<Match>();
663
		for (Line l : lines) {
664
			matches.add(l.getMatch());
665
		}
666

  
667
		affectMatchesToSelection(matches);
668
	}
669

  
670
	protected AnnotationType getSelectedAnnotationType() {
671
		if (annotationTypesCombo != null) {
672
			IStructuredSelection isel = (IStructuredSelection)annotationTypesCombo.getSelection();
673
			if (isel.isEmpty()) {
674
				return null;
675
			}
676
			return (AnnotationType) isel.getFirstElement();
677
		} else { // simple mode
678
			return tagAnnotationType;
679
		}
680
	}
681

  
682
	protected void affectMatchesToSelection(final List<Match> matches) {
683
		final AnnotationType type = getSelectedAnnotationType();
684
		final String svalue = annotationValuesText.getText();
685
		final boolean doAffect = addRemoveCombo.getSelectionIndex() == 0; // add is default
686

  
687
		JobHandler job = new JobHandler(ConcordanceUIMessages.ConcordancesEditor_36, true) {
688
			@Override
689
			protected IStatus run(IProgressMonitor monitor) {
690
				this.runInit(monitor);
691
				try {
692
					if (doAffect) {
693
						affectAnnotationValues(matches, type, svalue, this);
694
					} else {
695
						deleteAnnotationValues(matches, type, this);
696
					}
697
				} catch(Exception e) {
698
					Log.severe(ConcordanceUIMessages.ConcordancesEditor_40+e);
699
					Log.printStackTrace(e);
700
					return Status.CANCEL_STATUS;
701
				} catch(ThreadDeath td) {
702
					System.out.println("Annotation canceled by user.");
703
					return Status.CANCEL_STATUS;
704
				}
705

  
706
				return Status.OK_STATUS;
707
			}
708
		};
709
		job.startJob(true);
710
	}
711

  
712
	public void updateAnnotationWidgetStates() {
713
		if (annotationArea == null) return; // :)
714

  
715
		if (addRemoveCombo.getSelectionIndex() == 0) { // add is default
716

  
717
			if (getSelectedAnnotationType() != null) {
718
				annotationValuesText.setEnabled(true);
719

  
720
				affectAnnotationButton.setEnabled(true);
721
				affectCombo.setEnabled(true);
722
			} else {
723
				annotationValuesText.setEnabled(false);
724

  
725
				affectAnnotationButton.setEnabled(false);
726
				affectCombo.setEnabled(false);
727
			}
728
		} else {
729
			annotationValuesText.setEnabled(false);
730

  
731
			if (getSelectedAnnotationType() != null) {
732
				affectAnnotationButton.setEnabled(true);
733
				affectCombo.setEnabled(true);
734
			} else {
735
				affectAnnotationButton.setEnabled(false);
736
				affectCombo.setEnabled(false);
737
			}
738
		}
739

  
740
		if (advanced_annotation_mode.equals(AnnotationUIMessages.AnnotationPreferences_2)) {
741
			if (infosAnnotTypeSelectedLink != null) {
742
				infosAnnotTypeSelectedLink.setEnabled(getSelectedAnnotationType() != null);
743
			}
744
		}
745
		if (addTypedValueLink != null) {
746
			addTypedValueLink.setEnabled(getSelectedAnnotationType() != null);
747
		}
748

  
749
		if (concordance == null) {
750
			affectAnnotationButton.setEnabled(false);
751
			affectCombo.setEnabled(false);
752
		}
753
	}
754

  
755
	protected void deleteAnnotationValues(List<Match> matches, AnnotationType type, JobHandler job) {
756
		if (concordance == null) {
757
			return;
758
		}
759
		
760
		if (matches.size() == 0) {
761
			System.out.println("No lines selected. Aborting."); //$NON-NLS-1$
762
			return;
763
		}
764
		
765
		if (type == null) {
766
			return;
767
		}
768

  
769
		if (concordance != null) {
770
			try {
771
				if (annotManager != null && annotManager.deleteAnnotations(type, matches, job)) {
772
					if (Log.getLevel().intValue() < Level.WARNING.intValue()) annotManager.checkData();
773
					concordance.reloadLines(editor.getTopLine(), editor.getBottomLine()+1);
774
				} else {
775
					return;
776
				}
777
			} catch (Exception e1) {
778
				System.out.println(ConcordanceUIMessages.ConcordancesEditor_54+e1);
779
				Log.printStackTrace(e1);
780
				return;
781
			}
782

  
783
			job.syncExec(new Runnable() {
784
				@Override
785
				public void run() {
786
					editor.fillDisplayArea();
787
					CorporaView.refreshObject(corpus);
788
				}
789
			});
790
		}
791
	}
792
	
793
	protected void affectAnnotationValues(final List<Match> matches,  final AnnotationType type, final String svalue, JobHandler job) {
794
		value_to_add = null; // reset
795
		if (concordance == null) {
796
			System.out.println("No concordance done. Aborting."); //$NON-NLS-1$
797
			return;
798
		}
799
		if (matches.size() == 0) {
800
			System.out.println("No line selected. Aborting."); //$NON-NLS-1$
801
			return;
802
		}
803
		if (type == null) return; // no type, no annotation
804

  
805
		job.syncExec(new Runnable() {
806
			@Override
807
			public void run() {
808
				if (matches.size() > NALERTAFFECTANNOTATIONS) {
809
					ConfirmDialog dialog = new ConfirmDialog(Display.getCurrent().getActiveShell(), 
810
							"confirm_annotate",  //$NON-NLS-1$
811
							ConcordanceUIMessages.ConcordancesEditor_86, 
812
							ConcordanceUIMessages.ConcordancesEditor_87+matches.size()+ConcordanceUIMessages.ConcordancesEditor_108);
813

  
814
					if (dialog.open() == ConfirmDialog.CANCEL) {
815
						System.out.println("Annotation aborted by user."); //$NON-NLS-1$
816
						matches.clear();
817
					}
818
				}
819
			}
820
		});
821

  
822
		// get value from combo text value
823
		Log.warning(ConcordanceUIMessages.ConcordancesEditor_57+svalue);
824
		final KnowledgeRepository kr = KnowledgeRepositoryManager.getKnowledgeRepository(type.getKnowledgeRepository());
825
		value_to_add = kr.getValue(type, svalue);
826

  
827

  
828
		if (value_to_add == null && kr instanceof LocalKnowledgeRepository) { // create or not the annotation is simple mode
829
			job.syncExec(new Runnable() {
830
				@Override
831
				public void run() {
832
					String title = kr.getString(editor.getLocale(), "ConcordancesEditor_110");
833
					String content = kr.getString(editor.getLocale(), "ConcordancesEditor_112");
834
					if (title == null ) title = ConcordanceUIMessages.ConcordancesEditor_110;
835
					if (content == null ) content = ConcordanceUIMessages.ConcordancesEditor_112;
836
					ConfirmDialog dialog = new ConfirmDialog(Display.getCurrent().getActiveShell(), 
837
							"create_value", //$NON-NLS-1$
838
							ConcordanceUIMessages.bind(title, svalue, type.getName()),  
839
							ConcordanceUIMessages.bind(content, svalue, type.getName()));
840
					if (dialog.open() == ConfirmDialog.CANCEL) {
841
						System.out.println("Annotation aborted by user."); //$NON-NLS-1$
842
						return;
843
					} else {
844
						value_to_add = kr.addValue(svalue, svalue, type.getId());
845
						KRView.refresh();
846
					}
847
				}
848
			});
849
		}
850

  
851
		if (value_to_add == null && kr instanceof LocalKnowledgeRepository) return; // canceled
852

  
853
		if (value_to_add == null) {	
854
			job.syncExec(new Runnable() {
855
				@Override
856
				public void run() {
857
					String mess = ConcordanceUIMessages.bind(ConcordanceUIMessages.ConcordancesEditor_58, svalue);
858
					System.out.println(mess);
859
					MessageDialog.openError(Display.getCurrent().getActiveShell(), "Annotation canceled", mess); //$NON-NLS-1$
860
				}
861
			});
862
			return;
863
		}
864

  
865
		Log.info(ConcordanceUIMessages.ConcordancesEditor_59+value_to_add+ConcordanceUIMessages.ConcordancesEditor_60+matches);
866

  
867
		// finally we can 'try' to create the annotations \o/
868
		try {
869
			HashMap<Match, List<Annotation>> existingAnnots = annotManager.createAnnotations(type, value_to_add, matches, job);
870

  
871
			// did we had problems ?
872
			if (existingAnnots!=null && existingAnnots.size() > 0) {
873
				String message = ConcordanceUIMessages.ConcordancesEditor_61+value_to_add.getStandardName()+
874
						ConcordanceUIMessages.ConcordancesEditor_62;
875
				for (Match m : existingAnnots.keySet()) {
876
					message += ConcordanceUIMessages.ConcordancesEditor_63+m+ConcordanceUIMessages.ConcordancesEditor_64;
877

  
878
					for (Annotation existingAnnot : existingAnnots.get(m)) {
879
						if (existingAnnot.getStart() < m.getStart()){
880
							message += ConcordanceUIMessages.ConcordancesEditor_65+existingAnnot.getType()+ConcordanceUIMessages.ConcordancesEditor_66+existingAnnot.getStart()+ConcordanceUIMessages.ConcordancesEditor_67+existingAnnot.getEnd()+ConcordanceUIMessages.ConcordancesEditor_68;
881
						} else {
882
							message += ConcordanceUIMessages.ConcordancesEditor_69+existingAnnot.getType()+ConcordanceUIMessages.ConcordancesEditor_70+existingAnnot.getStart()+ConcordanceUIMessages.ConcordancesEditor_71+existingAnnot.getEnd()+ConcordanceUIMessages.ConcordancesEditor_72;
883
						}
884
					}
885
				}
886
				final String final_message = message;
887
				job.syncExec(new Runnable() {
888
					@Override
889
					public void run() {
890
						MessageDialog.openInformation(editor.getSite().getShell(), ConcordanceUIMessages.ConcordancesEditor_73, final_message);
891
					}
892
				});
893
			}
894
			//				if (history != null && !history.get(type).contains(value_to_add))
895
			//					history.get(type).add(value_to_add);
896

  
897
			if (Log.getLevel().intValue() < Level.WARNING.intValue() && annotManager != null) annotManager.checkData();
898
			concordance.reloadLines(editor.getTopLine(), editor.getBottomLine()+1);
899
		} catch (Exception e1) {
900
			System.out.println(ConcordanceUIMessages.ConcordancesEditor_74+e1);
901
			Log.printStackTrace(e1);
902
			return;
903
		}
904

  
905
		job.syncExec(new Runnable() {
906
			@Override
907
			public void run() {
908
				editor.fillDisplayArea();
909
				CorporaView.refreshObject(corpus);
910
			}
911
		});
912

  
913
	}
914

  
915
	@Override
916
	public boolean notifyStartOfCompute() throws Exception {
917
		if (annotationArea != null) {
918
			AnnotationType type = getSelectedAnnotationType();
919
			if (type != null) {
920
				annotations.setViewAnnotation(type);
921
				annotations.setAnnotationOverlap(true);
922
			}
923
		}
924
		return true;
925
	}
926

  
927
	@Override
928
	public boolean notifyEndOfCompute() throws Exception {
929
		updateAnnotationWidgetStates();
930
		
931
		// update annotation column width
932
		if (annotationArea != null) {
933
			annotationColumn.pack();
934

  
935
			annotationColumn.setResizable(true);
936
		} else { 
937
			annotationColumn.setWidth(0);
938
		}
939
		
940
		return true;
941
	}
942
	
943

  
944
	public boolean isAnnotationEnable() {
945
		return annotationArea != null;
946
	}
947
}
tmp/org.txm.annotation.rcp/src/org/txm/annotation/rcp/concordance/ConcordanceAnnotations.java (revision 782)
1 1
package org.txm.annotation.rcp.concordance;
2 2

  
3
import java.io.IOException;
4 3
import java.util.ArrayList;
5
import java.util.Arrays;
6
import java.util.Collections;
7 4
import java.util.HashMap;
8 5
import java.util.List;
9 6

  
......
14 11
import org.txm.annotation.core.repository.TypedValue;
15 12
import org.txm.concordance.core.functions.Concordance;
16 13
import org.txm.concordance.core.functions.Line;
17
import org.txm.concordance.core.messages.ConcordanceMessages;
18
import org.txm.searchengine.cqp.clientExceptions.CqiClientException;
19 14
import org.txm.searchengine.cqp.corpus.query.Match;
20
import org.txm.searchengine.cqp.serverException.CqiServerError;
21 15
import org.txm.utils.logger.Log;
22 16

  
23 17
public class ConcordanceAnnotations {
......
27 21
	private AnnotationType viewAnnotation;
28 22
	
29 23
	public ConcordanceAnnotations(Concordance concordance) {
30
		
24
		this.concordance = concordance;
31 25
	}
32 26
	
33 27
	private boolean overlapAnnotation;
......
42 36
		return overlapAnnotation;
43 37
	}
44 38
	
45
	public List<AnnotationLine> getLines(int from, int to) throws CqiClientException, IOException, CqiServerError {
39
	public List<AnnotationLine> getLines(List<Line> lines2, int from, int to) throws Exception {
46 40

  
47 41
		concordanceLines = concordance.getLines(from, to);
42
		
48 43
		ArrayList<Match> matches = new ArrayList<Match>();
49 44
		for (Line line : concordanceLines) matches.add(line.getMatch());
50 45
		
......
57 52
			lines.add(aline);
58 53
		}
59 54
		
60
		return lines; // TODO find a way to add 
55
		return lines; // TODO find a way to add with lazy method
61 56
	}
62 57
	
63 58
	private String findAnnotationForLine(Match match, List<Annotation> annots, boolean overlap) {
......
174 169
		return annotsList;
175 170
	}
176 171

  
177
	public List<Annotation> getAnnotationForMatches(List<Match> subsetMatch, boolean overlapAnnotation) {
172
	public List<Annotation> getAnnotationForMatches(List<Match> subsetMatch, boolean overlapAnnotation) throws Exception {
178 173
		
179 174
		AnnotationManager am = KRAnnotationEngine.getAnnotationManager(concordance.getCorpus());
180 175
		
181 176
		//List<Annotation> allAnnotations = null;
182
		List<Annotation> annotationsPerMatch = null;;
177
		List<Annotation> annotationsPerMatch = new ArrayList<Annotation>();
183 178
		if (viewAnnotation != null && am != null) {
184 179
			try {
185 180
				annotationsPerMatch = am.getAnnotationsForMatches(viewAnnotation, subsetMatch, overlapAnnotation);
......
205 200
	 * @return the AnnotationLine pointed by 'i'. May return null if i is wrong.
206 201
	 */
207 202
	public AnnotationLine getAnnotationLine(Line line) {
203
		if (concordanceLines == null) return null;
208 204
		int i = concordanceLines.indexOf(line);
209 205
		if (i < 0) return null;
210 206
		return getAnnotationLine(i);
......
217 213
	 * @return the AnnotationLine pointed by 'i'. May return null if i is wrong.
218 214
	 */
219 215
	public AnnotationLine getAnnotationLine(int i) {
216
		if (lines == null) return null;
217
		if (lines.size() == 0) return null;
220 218
		if (lines.size() < i) return null;
221 219
		if (i < 0) return null;
222 220
		
tmp/org.txm.annotation.rcp/src/org/txm/annotation/rcp/concordance/KRConcordanceAnnotationExtension.java (revision 782)
1
package org.txm.annotation.rcp.concordance;
2

  
3
import java.io.File;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Vector;
8
import java.util.logging.Level;
9

  
10
import org.eclipse.core.runtime.IProgressMonitor;
11
import org.eclipse.core.runtime.IStatus;
12
import org.eclipse.core.runtime.Status;
13
import org.eclipse.jface.dialogs.InputDialog;
14
import org.eclipse.jface.dialogs.MessageDialog;
15
import org.eclipse.jface.viewers.ArrayContentProvider;
16
import org.eclipse.jface.viewers.ColumnLabelProvider;
17
import org.eclipse.jface.viewers.ComboViewer;
18
import org.eclipse.jface.viewers.ISelection;
19
import org.eclipse.jface.viewers.ISelectionChangedListener;
20
import org.eclipse.jface.viewers.IStructuredSelection;
21
import org.eclipse.jface.viewers.SelectionChangedEvent;
22
import org.eclipse.jface.viewers.TableViewer;
23
import org.eclipse.jface.viewers.TableViewerColumn;
24
import org.eclipse.swt.SWT;
25
import org.eclipse.swt.events.KeyEvent;
26
import org.eclipse.swt.events.KeyListener;
27
import org.eclipse.swt.events.SelectionEvent;
28
import org.eclipse.swt.events.SelectionListener;
29
import org.eclipse.swt.graphics.Point;
30
import org.eclipse.swt.layout.GridData;
31
import org.eclipse.swt.layout.GridLayout;
32
import org.eclipse.swt.widgets.Button;
33
import org.eclipse.swt.widgets.Combo;
34
import org.eclipse.swt.widgets.Composite;
35
import org.eclipse.swt.widgets.Display;
36
import org.eclipse.swt.widgets.Group;
37
import org.eclipse.swt.widgets.Label;
38
import org.eclipse.swt.widgets.TableColumn;
39
import org.eclipse.swt.widgets.Text;
40
import org.eclipse.ui.IWorkbenchWindow;
41
import org.eclipse.ui.PartInitException;
42
import org.eclipse.ui.dialogs.ListDialog;
43
import org.txm.annotation.core.Annotation;
44
import org.txm.annotation.core.AnnotationManager;
45
import org.txm.annotation.core.KRAnnotationEngine;
46
import org.txm.annotation.core.preferences.AnnotationPreferences;
47
import org.txm.annotation.core.repository.AnnotationType;
48
import org.txm.annotation.core.repository.KnowledgeRepository;
49
import org.txm.annotation.core.repository.KnowledgeRepositoryManager;
50
import org.txm.annotation.core.repository.LocalKnowledgeRepository;
51
import org.txm.annotation.core.repository.SQLKnowledgeRepository;
52
import org.txm.annotation.core.repository.TypedValue;
53
import org.txm.annotation.rcp.commands.InitializeKnowledgeRepository;
54
import org.txm.annotation.rcp.commands.SaveAnnotations;
55
import org.txm.annotation.rcp.commands.krview.OpenKRView;
56
import org.txm.annotation.rcp.messages.AnnotationUIMessages;
57
import org.txm.annotation.rcp.views.knowledgerepositories.KRView;
58
import org.txm.concordance.core.functions.Concordance;
59
import org.txm.concordance.core.functions.Line;
60
import org.txm.concordance.rcp.editors.ConcordanceEditor;
61
import org.txm.concordance.rcp.editors.ConcordanceEditor.ConcordanceColumnSizeControlListener;
62
import org.txm.concordance.rcp.editors.ConcordanceEditorExtension;
63
import org.txm.concordance.rcp.messages.ConcordanceUIMessages;
64
import org.txm.core.preferences.TXMPreferences;
65
import org.txm.rcp.IImageKeys;
66
import org.txm.rcp.commands.OpenBrowser;
67
import org.txm.rcp.editors.TXMEditorToolBar.OpenCloseButton;
68
import org.txm.rcp.preferences.RCPPreferences;
69
import org.txm.rcp.swt.GLComposite;
70
import org.txm.rcp.swt.dialog.ConfirmDialog;
71
import org.txm.rcp.swt.provider.SimpleLabelProvider;
72
import org.txm.rcp.utils.JobHandler;
73
import org.txm.rcp.views.corpora.CorporaView;
74
import org.txm.searchengine.cqp.clientExceptions.CqiClientException;
75
import org.txm.searchengine.cqp.corpus.Corpus;
76
import org.txm.searchengine.cqp.corpus.query.Match;
77
import org.txm.utils.AsciiUtils;
78
import org.txm.utils.logger.Log;
79

  
80
public class KRConcordanceAnnotationExtension extends ConcordanceEditorExtension {
81

  
82
	/**
83
	 * the limit number of annotation when a confirm dialog box is shown
84
	 */
85
	protected static final int NALERTAFFECTANNOTATIONS = 100;
86
	public static final String EMPTYTEXT = "";
87

  
88
	/** The annotation service */
89
	protected AnnotationManager annotManager;
90

  
91
	//boolean annotation_expert_mode = TxmPreferences.getBoolean(AnnotationPreferencePage.MODE, false);
92

  
93
	/** The annotation area. */
94
	protected GLComposite annotationArea;
95

  
96
	protected Point annotationSize;
97

  
98
	/** The annot btn : starts the annotation */
99
	protected OpenCloseButton annotationButton;
100
	private Button addAnnotationTypeLink;
101

  
102
	protected Text annotationValuesText;
103
	protected ComboViewer annotationTypesCombo;
104

  
105
	protected List<TypedValue> typeValuesList;
106

  
107
	protected Point annotSize;
108

  
109
	protected TableColumn annotationColumn;
110

  
111
	protected Combo addRemoveCombo;
112
	protected Combo affectCombo;
113
	protected Button affectAnnotationButton;
114

  
115
	protected Button infosAnnotTypeSelectedLink;
116

  
117
	protected HashMap<AnnotationType, ArrayList<TypedValue>> history = new HashMap<AnnotationType, ArrayList<TypedValue>>();
118

  
119
	protected KnowledgeRepository currentKnowledgeRepository = null;
120
	protected Vector<AnnotationType> typesList = new Vector<AnnotationType>(); 
121

  
122
	protected Button addTypedValueLink;
123

  
124
	protected Label withLabel;
125

  
126
	protected AnnotationType tagAnnotationType; // the tag annotation type if annotation mode is simple
127

  
128
	protected Label equalLabel;
129
	protected Boolean advanced_annotation_mode = false;
130

  
131
	protected TypedValue value_to_add;
132

  
133
	protected Concordance concordance;
134

  
135
	private Corpus corpus;
136

  
137
	private ConcordanceAnnotations annotations = null;
138

  
139
	public KRConcordanceAnnotationExtension() {
140

  
141
	}
142

  
143
	@Override
144
	public String getName() {
145
		return "Annotation";
146
	}
147

  
148
	@Override
149
	public boolean installWidgets() throws Exception {
150

  
151
		List<String> krlist = KRAnnotationEngine.getKnowledgeRepositoryNames(corpus);
152
		// CONTROLS
153
		if (krlist.size() == 0) { // shown only if corpus has a registered KR
154
			return false;
155
		}
156
		
157
		final Group controlArea = editor.getTopToolbar().installGroup("annotation", "annotation tools",
158
				"platform:/plugin/org.txm.annotation.rcp/icons/functions/pencil.png", null, false);
159
		controlArea.setLayout(new GridLayout(1, true));
160
		//final Composite navigationArea = editor.getNavigationArea();
161
		TableViewer viewer = editor.getLineTableViewer();
162
		
163
		annotationButton = editor.getTopToolbar().getGroupButton("annotation");
164
		annotationButton.setToolTipText(ConcordanceUIMessages.ConcordancesEditor_75);
165
		annotationButton.setImage(IImageKeys.getImage(IImageKeys.PENCIL));
166
		annotationButton.addSelectionListener(new SelectionListener() {
167
			@Override
168
			public void widgetSelected(SelectionEvent e) {
169
				if (annotationArea != null) {
170
					try {
171
						AnnotationManager am = KRAnnotationEngine.getAnnotationManager(corpus);
172
						if (am != null && am.hasChanges()) {
173
							// && MessageDialog.openConfirm(e.display.getActiveShell(), "Confirm annotation save", "Saving annotation will close this editor. Are you sure you want to save annotations right now ?")
174
							JobHandler saveJob = SaveAnnotations.save(corpus.getMainCorpus());
175
							if (saveJob.getResult() == Status.CANCEL_STATUS) {
176
								// update editor corpus
177
								System.out.println("Fail to save annotations of the corpus."); //$NON-NLS-1$
178
							}
179
						}
180
					} catch(Exception ex) {
181
						ex.printStackTrace();
182
					}
183
				} else {
184
					if (annotationArea == null) {
185
						if (!composeAnnotationArea(controlArea)) {
186
							return;
187
						}
188
					}
189
					if (annotationArea == null) return;
190

  
191
					annotationColumn.pack();
192
					annotationButton.setToolTipText(ConcordanceUIMessages.ConcordancesEditor_76);
193
					annotationButton.setImage(IImageKeys.getImage(IImageKeys.PENCIL_SAVE));
194

  
195
					annotSize = annotationArea.getSize();
196
					Point size = controlArea.getSize();
197
					controlArea.setSize(size.x, size.y + annotSize.y);
198
					GridData data = (GridData) annotationArea.getLayoutData();
199
					data.heightHint = annotSize.y;
200
					data.minimumHeight = annotSize.y;
201
					annotationArea.setSize(annotSize);
202
					controlArea.layout(true);
203
					controlArea.getParent().layout();
204

  
205
					if (concordance != null) {
206
						AnnotationType type = getSelectedAnnotationType();
207
						if (type != null) {
208
							annotations.setViewAnnotation(type);
209
							annotations.setAnnotationOverlap(true);
210
							editor.fillDisplayArea();
211
						}
212
					}
213

  
214
					CorporaView.refreshObject(corpus);
215
				}
216
			}
217

  
218
			@Override
219
			public void widgetDefaultSelected(SelectionEvent e) {	}
220
		});
221

  
222

  
223
		// RESULT
224

  
225
		int position = 2; // after keyword column
226
		TableViewerColumn annotationColumnViewer = new TableViewerColumn(viewer, SWT.CENTER, position);
227
		annotationColumn = annotationColumnViewer.getColumn();
228
		annotationColumn.setText(ConcordanceUIMessages.ConcordancesEditor_15);
229
		annotationColumn.setToolTipText(ConcordanceUIMessages.ConcordancesEditor_17);
230
		annotationColumn.setAlignment(SWT.CENTER);
231

  
232
		annotationColumn.addControlListener(new ConcordanceColumnSizeControlListener(annotationColumn));
233
		annotationColumnViewer.setLabelProvider(new ColumnLabelProvider() {
234

  
235
			@Override
236
			public String getText(Object element) {
237
				Line line = (Line)element;
238
				AnnotationLine annotationLine = annotations.getAnnotationLine(line);
239
				if (annotationLine == null) return EMPTYTEXT ;
240
				if (annotationLine.getAnnotation()!=null) {
241
					String value = annotationLine.getAnnotationValue().getStandardName();
242
					Annotation a = annotationLine.getAnnotation();
243
					if (value == null) value = a.getValue();
244

  
245
					if (a.getStart() < line.matchGetStart() - line.getLeftContextSize()) {
246
						value = "… "+value;
247
					}
248

  
249
					if (a.getEnd() > line.matchGetEnd() + line.getRightContextSize()) {
250
						value = value+" …";
251
					} 
252
					return value;
253
				} else {
254
					return EMPTYTEXT;
255
				}
256
			}
257
		});
258
		
259

  
260
		return true;
261
	}
262

  
263
	public boolean initialiseInput(ConcordanceEditor editor) throws Exception {
264
		this.editor = editor;
265
		concordance = editor.getConcordance();
266
		annotations = new ConcordanceAnnotations(concordance);
267
		corpus = concordance.getCorpus();
268

  
269
		Corpus corpus = editor.getCorpus();
270

  
271
		this.annotManager = KRAnnotationEngine.getAnnotationManager(corpus);
272

  
273
		return true;
274

  
275
	}
276

  
277
	private boolean composeAnnotationArea(final Composite parent){ 
278
		advanced_annotation_mode = "advanced".equals(TXMPreferences.getString(AnnotationPreferences.ANNOTATION_MODE, RCPPreferences.PREFERENCES_NODE));
279

  
280
		List<KnowledgeRepository> krs = InitializeKnowledgeRepository.get(corpus.getMainCorpus());
281
		typesList.clear();
282
		Log.warning("Corpus KRs: "+krs); //$NON-NLS-1$
283

  
284
		for (KnowledgeRepository kr : krs) {
285
			if (kr == null) continue;
286

  
287
			currentKnowledgeRepository = kr;
288
			Log.warning(" KR: "+kr); //$NON-NLS-1$
289
			List<AnnotationType> krtypes = kr.getAllAnnotationTypes();
290
			Log.warning("Advanced annotation mode ? "+advanced_annotation_mode);
291
			if (!advanced_annotation_mode) { // creates the "span" annotation type
292
				boolean createTagAnnotationType = true;
293
				for (AnnotationType type : krtypes) {
294
					if (type.getName().equals("span")) { //$NON-NLS-1$
295
						createTagAnnotationType = false;
296
					}
297
				}
298
				if (createTagAnnotationType) {
299
					tagAnnotationType = kr.addType("span", "span");//corresponds to an "undef" type //$NON-NLS-1$ //$NON-NLS-2$
300
					typesList.add(tagAnnotationType);
301
				} else {
302
					tagAnnotationType = kr.getType("span"); //$NON-NLS-1$
303
				}
304
			} else {
305
				typesList.addAll(krtypes);
306
			}
307
			Log.warning("Available annotation types: "+typesList);
308
			break;
309
		}
310

  
311
		if (currentKnowledgeRepository == null) {
312
			System.out.println("Error: no suitable KnowledgeRepository found");
313
			return false;
314
		}
315

  
316
		annotationArea = new GLComposite(parent, SWT.NONE);
317
		annotationArea.getLayout().numColumns = 12;
318
		annotationArea.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, false, false));
319

  
320
		//init values history
321
		for (AnnotationType type : typesList) {
322
			history.put(type, new ArrayList<TypedValue>());
323
		}
324

  
325
		addRemoveCombo = new Combo(annotationArea, SWT.READ_ONLY);
326
		String affectLabel = currentKnowledgeRepository.getString(editor.getLocale(), "ConcordancesEditor_22");
327
		if (affectLabel == null) affectLabel= ConcordanceUIMessages.ConcordancesEditor_22;
328
		String removeLabel = currentKnowledgeRepository.getString(editor.getLocale(), "ConcordancesEditor_24");
329
		if (removeLabel == null) removeLabel= ConcordanceUIMessages.ConcordancesEditor_24;
330
		String items[] = {affectLabel, removeLabel};
331
		addRemoveCombo.setItems(items);
332
		addRemoveCombo.select(0);
333
		addRemoveCombo.addSelectionListener(new SelectionListener() {
334
			@Override
335
			public void widgetSelected(SelectionEvent e) {
336
				if (addRemoveCombo.getSelectionIndex() == 0) { // add
337
					annotationValuesText.setEnabled(true);
338
					String withLabelText = currentKnowledgeRepository.getString(editor.getLocale(), "ConcordancesEditor_83");
339
					if (withLabelText == null) withLabelText= ConcordanceUIMessages.ConcordancesEditor_83;
340
					withLabel.setText(withLabelText);
341
					if (equalLabel != null) equalLabel.setText("=");
342
				} else { // remove
343
					annotationValuesText.setEnabled(false);
344
					withLabel.setText(""); //$NON-NLS-1$
345
					if (equalLabel != null) equalLabel.setText("");
346
				}
347
				withLabel.redraw();
348
				annotationArea.layout();
349
				updateAnnotationWidgetStates();
350
			}
351
			@Override
352
			public void widgetDefaultSelected(SelectionEvent e) { }
... Ce différentiel a été tronqué car il excède la taille maximale pouvant être affichée.

Formats disponibles : Unified diff