Project

General

Profile

Download (15.4 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy 
4
 * http://www.e-taxonomy.eu
5
 * 
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9

    
10
package eu.etaxonomy.taxeditor.singlesource.editor.name.container;
11

    
12
import java.util.Iterator;
13

    
14
import javax.naming.OperationNotSupportedException;
15

    
16
import org.eclipse.core.commands.operations.IUndoContext;
17
import org.eclipse.core.runtime.Assert;
18
import org.eclipse.jface.text.Document;
19
import org.eclipse.jface.text.IUndoManager;
20
import org.eclipse.jface.text.IUndoManagerExtension;
21
import org.eclipse.jface.text.Position;
22
import org.eclipse.jface.text.TextViewerUndoManager;
23
import org.eclipse.jface.text.source.Annotation;
24
import org.eclipse.jface.text.source.AnnotationModel;
25
import org.eclipse.jface.text.source.AnnotationPainter;
26
import org.eclipse.jface.text.source.IAnnotationAccess;
27
import org.eclipse.jface.text.source.IVerticalRuler;
28
import org.eclipse.jface.text.source.SourceViewer;
29
import org.eclipse.swt.SWT;
30
import org.eclipse.swt.events.FocusListener;
31
import org.eclipse.swt.events.ModifyListener;
32
import org.eclipse.swt.events.MouseListener;
33
import org.eclipse.swt.events.VerifyListener;
34
import org.eclipse.swt.graphics.Color;
35
import org.eclipse.swt.graphics.Font;
36
import org.eclipse.swt.graphics.Image;
37
import org.eclipse.swt.widgets.Canvas;
38
import org.eclipse.swt.widgets.Composite;
39
import org.eclipse.swt.widgets.Control;
40
import org.eclipse.swt.widgets.Layout;
41
import org.eclipse.swt.widgets.Menu;
42
import org.eclipse.swt.widgets.Widget;
43
import org.eclipse.ui.IActionBars;
44
import org.eclipse.ui.IEditorSite;
45
import org.eclipse.ui.actions.ActionFactory;
46
import org.eclipse.ui.forms.widgets.TableWrapData;
47
import org.eclipse.ui.forms.widgets.TableWrapLayout;
48
import org.eclipse.ui.operations.OperationHistoryActionHandler;
49
import org.eclipse.ui.operations.RedoActionHandler;
50
import org.eclipse.ui.operations.UndoActionHandler;
51
import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;
52
import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
53

    
54
import eu.etaxonomy.taxeditor.singlesource.editor.ViewerConfiguration;
55
import eu.etaxonomy.taxeditor.singlesource.editor.name.container.EditorAnnotationFacade.EditorAnnotationType;
56
import eu.etaxonomy.taxeditor.singlesource.org.eclipse.jface.text.IPosition;
57
import eu.etaxonomy.taxeditor.singlesource.org.eclipse.jface.text.PositionFacade;
58
import eu.etaxonomy.taxeditor.singlesource.org.eclipse.swt.widgets.DisplayProxy;
59

    
60
/**
61
 * SourceViewer implementation called by NameComposite.
62
 *
63
 * @author p.ciardelli
64
 * @created 27.05.2008
65
 * @version 1.0
66
 */
67
public class NameViewerFacadeImpl extends SourceViewer implements INameViewer {
68
		
69
	private final IVerticalRuler ruler;
70
	private final AnnotationModel annotationModel;
71

    
72
	/**
73
	 * <p>Constructor for NameViewer.</p>
74
	 *
75
	 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
76
	 */
77
	public NameViewerFacadeImpl(Composite parent) {
78
		super(parent, new RulerWithIcon(RULER_WIDTH), SWT.WRAP | SWT.MULTI | SWT.RESIZE);
79
											
80
		this.ruler = getVerticalRuler();
81
				
82
		Canvas canvas = (Canvas) getControl();
83
		TableWrapLayout layout = (TableWrapLayout) canvas.getLayout();
84
		layout.topMargin = 0;
85
		layout.rightMargin = 0;
86
		layout.bottomMargin = 0;
87
		layout.leftMargin = 0;
88
		layout.verticalSpacing = 10;
89
		layout.horizontalSpacing = 0;
90
		canvas.setLayout(layout);
91
		
92
		
93
		TableWrapData twdata = new TableWrapData(TableWrapData.FILL_GRAB);
94
		getTextWidget().setLayoutData(twdata);
95
		getTextWidget().setLineSpacing(5);
96
		
97
		// Default implementation adds ruler after the text widget, then 
98
		//	uses a custom layout to display it before the text widget
99
		try {
100
			getVerticalRulerControl().moveAbove(getTextWidget());
101
		} catch (OperationNotSupportedException e) {
102
		}
103

    
104
		annotationModel = new AnnotationModel();
105
		this.setDocument(new Document(""), annotationModel);
106
		
107
		createAnnotationPainter();
108

    
109
		this.configure(new ViewerConfiguration());		
110
		
111
//		setDecorationSupport();
112
//		DocumentUndoManagerRegistry.connect(this.getDocument());
113
//		IDocumentUndoManager docUndoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(this.getDocument());
114
		
115
		/**
116
		getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.UNDO.getId(), new UndoActionHandler(getSite(), undoContext));
117
		getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.REDO.getId(), new RedoActionHandler(getSite(), undoContext));		
118
		**/
119
	}
120
	
121
	/**
122
	 * <p>setBackground</p>
123
	 *
124
	 * @param color a {@link org.eclipse.swt.graphics.Color} object.
125
	 */
126
	public void setBackground(Color color) {
127
		
128
		// Set background color of ruler
129
		try {
130
			getVerticalRulerControl().setBackground(color);
131
		} catch (OperationNotSupportedException e) {
132
		}
133

    
134
		// Set background color of text widget
135
		getTextWidget().setBackground(color);
136
		
137
		// Set background color of strip between ruler and textWidget
138
		getTextWidget().getParent().setBackground(color); 
139
	}
140
	
141
	/**
142
	 * <p>setIcon</p>
143
	 *
144
	 * @param icon a {@link org.eclipse.swt.graphics.Image} object.
145
	 */
146
	public void setIcon(Image icon) {
147
		if (ruler instanceof RulerWithIcon) {
148
			((RulerWithIcon) ruler).setIcon(icon);
149
		} else {
150
			throw new IllegalStateException("Viewer's IVerticalRuler is not an instance of RulerWithIcon.");
151
		}
152
	}
153
	
154
	/* (non-Javadoc)
155
	 * @see org.eclipse.jface.text.source.SourceViewer#createLayout()
156
	 */
157
	/**
158
	 * <p>createLayout</p>
159
	 *
160
	 * @return a {@link org.eclipse.swt.widgets.Layout} object.
161
	 */
162
	@Override
163
	protected Layout createLayout() {
164
		TableWrapLayout layout = new TableWrapLayout();
165
		layout.numColumns = 2;
166
		layout.verticalSpacing = 0;
167
		return layout;
168
	}
169
		
170
	private void createAnnotationPainter(){
171
		// Annotations section
172
		IAnnotationAccess fAnnotationAccess = new DefaultMarkerAnnotationAccess();
173
		
174
		// To paint the annotations
175
		AnnotationPainter annotationPainter = new AnnotationPainter(this, fAnnotationAccess);
176

    
177
		// Default SquigglesStrategy doesn't recognize line wraps
178
		annotationPainter.addDrawingStrategy(LineWrapSquigglesStrategy.ID, new LineWrapSquigglesStrategy());
179
		
180
		// Add ability to paint red squigglies
181
		annotationPainter.addAnnotationType(EditorAnnotationType.ERROR.name(), LineWrapSquigglesStrategy.ID);
182
		annotationPainter.setAnnotationTypeColor(EditorAnnotationType.ERROR.image, 
183
				new Color(DisplayProxy.getDefault(), EditorAnnotationType.ERROR.color));
184

    
185
		// Add ability to paint yellow squigglies
186
		annotationPainter.addAnnotationType(EditorAnnotationType.WARNING.name(), LineWrapSquigglesStrategy.ID);
187
		annotationPainter.setAnnotationTypeColor(EditorAnnotationType.WARNING.image, 
188
				new Color(DisplayProxy.getDefault(), EditorAnnotationType.WARNING.color));
189
		
190
		this.addPainter(annotationPainter);
191
	}
192

    
193

    
194
	/**
195
	 * <p>clearAnnotations</p>
196
	 */
197
	public void clearAnnotations() throws OperationNotSupportedException {
198
		Iterator<Annotation> annotations = this.getAnnotationModel().getAnnotationIterator();
199
		while (annotations.hasNext()) {
200
			Annotation annotation = annotations.next(); 
201
			this.getAnnotationModel().removeAnnotation(annotation);
202
		}
203
	}
204
	
205
	/**
206
	 * If <code>name.hasProblem()</code> is <code>true</code>, underlines section
207
	 * of text bounded by <code>name.getProblemStarts()</code> and
208
	 * <code>name.getProblemEnds()</code>.
209
	 *
210
	 * @param name a {@link eu.etaxonomy.cdm.model.name.TaxonNameBase} object.
211
	 */
212
//	public void setShowParsingErrors(TaxonNameBase<?, ?> name) {
213
//				
214
//		String text = this.getTextWidget().getText();
215
//						
216
//		if (name.hasProblem() && text.length() > 0) {
217
//			int start = name.getProblemStarts();
218
//			int length = name.getProblemEnds() - start;
219
//			
220
//			if (start == -1 || name.getProblemEnds() == -1) {
221
//				return;
222
//			}
223
//			
224
//			// Don't let squigglies try to draw beyond the end of the text
225
//			if (text.length() < start + length) {
226
//				length = text.length() - start;
227
//			}
228
//			
229
//			for (ParserProblem problem : name.getParsingProblems()) {
230
//				
231
//				String type = null;
232
//				if (problem.isWarning()) {
233
//					type = EditorAnnotation.WARNING_TYPE;
234
//				}
235
//				if (problem.isError()) {
236
//					type = EditorAnnotation.ERROR_TYPE;
237
//				}
238
//				if (type == null) {
239
//					continue;
240
//				}
241
//				this.getAnnotationModel().addAnnotation(
242
//						new EditorAnnotation(type, 0, problem.getMessage()), 
243
//						new Position(start, length));
244
//			}
245
//			
246
//		}
247
//	}
248
	
249
	@Override
250
	public void addAnnotation(IEditorAnnotation annotation, IPosition position) throws OperationNotSupportedException {
251
		if(position == null){
252
			position = new PositionFacade(0, 0);
253
		}
254
		if (position instanceof Position){
255
			this.getAnnotationModel().addAnnotation((Annotation)annotation, (Position)position);
256
		}else{
257
			throw new OperationNotSupportedException("type of argument 'position' must be org.eclipse.jface.text.Position.");
258
		}
259
	}
260

    
261
	/**
262
	 * <p>setText</p>
263
	 *
264
	 * @param text a {@link java.lang.String} object.
265
	 */
266
	public void setText(String text) {
267
		if (text == null) {
268
			text = "";
269
		}
270
		try {
271
			Assert.isNotNull(text);
272
			// TODO figure out why getTextWidget() returns null!
273
			if (this.getTextWidget() == null) {
274
				return;
275
			}
276
			Assert.isNotNull(this.getTextWidget());
277
			this.getTextWidget().setText(text);
278
		} catch (RuntimeException e) {
279
			throw e;
280
		}
281
	}
282
	
283
	/**
284
	 * <p>setMenu</p>
285
	 *
286
	 * @param menu a {@link org.eclipse.swt.widgets.Menu} object.
287
	 */
288
	public void setMenu(Menu menu) {
289
		try {
290
			getVerticalRulerControl().setMenu(menu);
291
		} catch (OperationNotSupportedException e) {
292
		}
293
		getTextWidget().setMenu(menu);
294
	}
295

    
296
	/**
297
	 * <p>setCursorToEOL</p>
298
	 */
299
	public void setCursorToEOL() throws OperationNotSupportedException {
300
		getTextWidget().setCaretOffset(getTextWidget().getText().length());
301
		getTextWidget().setFocus();
302
	}
303

    
304
	
305
	/**
306
	 * <p>getCursorPosition</p>
307
	 *
308
	 * @return a int.
309
	 */
310
	public int getCursorPosition() throws OperationNotSupportedException {
311
		return getTextWidget().getCaretOffset();
312
	}
313
	
314
	/**
315
	 * <p>setCursorPosition</p>
316
	 *
317
	 * @param offset a int.
318
	 */
319
	public void setCursorPosition(int offset) throws OperationNotSupportedException {
320
		try{
321
			getTextWidget().setCaretOffset(offset);
322
		}catch(IllegalArgumentException e){
323
			// do nothing
324
		}
325
	}
326

    
327
	/**
328
	 * <p>createUndoSupport</p>
329
	 *
330
	 * @param editorSite a {@link org.eclipse.ui.IEditorSite} object.
331
	 */
332
	public void createUndoSupport(IEditorSite editorSite) throws OperationNotSupportedException {
333

    
334
		IUndoManager undoManager = new TextViewerUndoManager(25);
335
		this.setUndoManager(undoManager);
336
		undoManager.connect(this);
337
		
338
//		IUndoContext workbenchUndoContext = UiUtil.getWorkbenchUndoContext();
339
		
340
		IUndoContext workbenchUndoContext = ((IUndoManagerExtension)undoManager).getUndoContext();
341
		
342
		OperationHistoryActionHandler undoAction = new UndoActionHandler(editorSite, workbenchUndoContext);
343
//		undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO);
344
		undoAction.setActionDefinitionId(ActionFactory.UNDO.getId());
345
		
346
		// Create the redo action.
347
		OperationHistoryActionHandler redoAction = new RedoActionHandler(editorSite, workbenchUndoContext);
348
//		redoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO);
349
		undoAction.setActionDefinitionId(ActionFactory.REDO.getId());
350

    
351
		IActionBars actionBars = editorSite.getActionBars();
352
		if (actionBars != null) {
353
//				actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.UNDO, undoAction);
354
//				actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.REDO, redoAction);
355
			actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
356
			actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
357
			
358
		}		
359
		
360
//		actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
361
//		actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
362
	}
363
	
364
	/**
365
	 * <p>createUndoSupport_</p>
366
	 *
367
	 * @param editorSite a {@link org.eclipse.ui.IEditorSite} object.
368
	 */
369
	public void createUndoSupport_(IEditorSite editorSite) throws OperationNotSupportedException {
370
		IUndoManager undoManager = new TextViewerUndoManager(25);
371
		this.setUndoManager(undoManager);
372
		undoManager.connect(this);
373
		IUndoContext undoContext;
374
		if (undoManager instanceof IUndoManagerExtension) {
375
			undoContext = ((IUndoManagerExtension)undoManager).getUndoContext();
376
		
377
			OperationHistoryActionHandler undoAction = new UndoActionHandler(editorSite, undoContext);
378
//			PlatformUI.getWorkbench().getHelpSystem().setHelp(undoAction, IAbstractTextEditorHelpContextIds.UNDO_ACTION);
379
			undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO);
380
			
381
			// Create the redo action.
382
			OperationHistoryActionHandler redoAction = new RedoActionHandler(editorSite, undoContext);
383
//			PlatformUI.getWorkbench().getHelpSystem().setHelp(redoAction, IAbstractTextEditorHelpContextIds.REDO_ACTION);
384
			redoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO);
385

    
386

    
387
			IActionBars actionBars = editorSite.getActionBars();
388
			if (actionBars != null) {
389
				actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.UNDO, undoAction);
390
				actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.REDO, redoAction);
391
				
392
			}
393
		}
394
	}
395

    
396
	@Override
397
	public void setLayoutData(Object layoutData) {
398
		getControl().setLayoutData(layoutData);
399
	}
400

    
401
	@Override
402
	public void setForeground(Color color) {
403
		getTextWidget().setForeground(color);
404
	}
405

    
406
	@Override
407
	public boolean setFocus() {
408
		return getTextWidget().setFocus();
409
	}
410

    
411
	@Override
412
	public void setFont(Font font) {
413
		getTextWidget().setFont(font);
414
	}
415

    
416
	@Override
417
	public String getText() {
418
		return getTextWidget().getText();
419
	}
420

    
421
	@Override
422
	public void addFocusListener(FocusListener listener) {
423
		getTextWidget().addFocusListener(listener);
424
	}
425

    
426
	@Override
427
	public void removeFocusListener(FocusListener listener) {
428
		getTextWidget().removeFocusListener(listener);
429
	}
430

    
431
	@Override
432
	public void addMouseListener(MouseListener listener) {
433
		try {
434
			getVerticalRulerControl().addMouseListener(listener);
435
		} catch (OperationNotSupportedException e) {
436
		}
437
		getTextWidget().addMouseListener(listener);
438
	}
439

    
440
	@Override
441
	public void removeMouseListener(MouseListener listener) {
442
		try {
443
			getVerticalRulerControl().removeMouseListener(listener);
444
		} catch (OperationNotSupportedException e) {
445
		}
446
		getTextWidget().removeMouseListener(listener);
447
	}
448

    
449
	@Override
450
	public void addModifyListener(ModifyListener listener) {
451
		getTextWidget().addModifyListener(listener);
452
	}
453

    
454
	@Override
455
	public void removeModifyListener(ModifyListener listener) {
456
		getTextWidget().removeModifyListener(listener);
457
	}
458

    
459
	@Override
460
	public void addVerifyListener(VerifyListener listener) {
461
		getTextWidget().addVerifyListener(listener);
462
	}
463

    
464
	@Override
465
	public void removeVerifyListener(VerifyListener listener) {
466
		getTextWidget().removeVerifyListener(listener);
467
	}
468

    
469
	@Override
470
	public Control getVerticalRulerControl() throws OperationNotSupportedException {
471
		return ruler.getControl();
472
	}
473

    
474
	@Override
475
	public void setEnabled(boolean enabled) {
476
		getTextWidget().setEnabled(enabled);
477
	}
478

    
479
	@Override
480
	public Control getDraggableControl() {
481
		try {
482
			return getVerticalRulerControl();
483
		} catch (OperationNotSupportedException e) {
484
			return null;
485
		}
486
	}
487

    
488
	@Override
489
	public Widget getStyledText() throws OperationNotSupportedException {
490
		return getTextWidget();
491
	}
492

    
493
	@Override
494
	public String getDocumentAsString() throws OperationNotSupportedException {
495
		return getDocument().get();
496
	}
497

    
498
	@Override
499
	public void setDocumentAsString(String text) throws OperationNotSupportedException {
500
		getDocument().set(text);
501
	}
502

    
503
	@Override
504
	public int getDocumentLength() throws OperationNotSupportedException {
505
		return getDocument().getLength();
506
	}
507

    
508
}
509

    
(5-5/6)