Project

General

Profile

Revision 65514c8c

ID65514c8c58f8f5a3db4228ddeb59a8071d5ed90c
Parent 033ef209
Child ae55584a

Added by Patrick Plitzner almost 3 years ago

ref #6595 further migration of name editor

  • Add context menu
  • fix selection propagation
  • fix saving

View differences:

eu.etaxonomy.taxeditor.editor/fragment.e4xmi
123 123
        <children xsi:type="menu:HandledMenuItem" xmi:id="_KT8rYIZ5EeeOc9DZcjNN7g" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel44" label="%command.label.44" command="_Ob4xIIZ5EeeOc9DZcjNN7g"/>
124 124
      </menus>
125 125
    </elements>
126
    <elements xsi:type="basic:PartDescriptor" xmi:id="_tLceMIl4Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4" label="Taxon Name Editor" iconURI="platform:/plugin/eu.etaxonomy.taxeditor.editor/icons/edit_16x16.gif" tooltip="" allowMultiple="true" category="" closeable="true" dirtyable="true" contributionURI="bundleclass://eu.etaxonomy.taxeditor.editor/eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4">
127
      <tags>View</tags>
128
      <tags>nonRestore</tags>
129
      <handlers xmi:id="_tLceQIl4Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.nameEditor.handler.SaveHandler" contributionURI="bundleclass://eu.etaxonomy.taxeditor.workbench/eu.etaxonomy.taxeditor.workbench.SaveHandler" command="_majwMEaQEeeXMc6kSYO7Xg"/>
130
      <menus xsi:type="menu:PopupMenu" xmi:id="_tLceQYl4Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.popupmenu.nameeditor">
131
        <children xsi:type="menu:Menu" xmi:id="_R6buoIl5Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.nameEditor.menu.new" label="%menu.label">
132
          <children xsi:type="menu:HandledMenuItem" xmi:id="_FjdDgIl8Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel9" label="%command.label.9"/>
133
          <children xsi:type="menu:HandledMenuItem" xmi:id="_Gsr8cIl8Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel10" label="%command.label.10"/>
134
          <children xsi:type="menu:HandledMenuItem" xmi:id="_I6FoIIl8Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel11" label="%command.label.11"/>
135
          <children xsi:type="menu:MenuSeparator" xmi:id="_LGspoIl8Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.menuseparator.16"/>
136
          <children xsi:type="menu:HandledMenuItem" xmi:id="_MCsX0Il8Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel49" label="%command.label.49"/>
137
        </children>
138
        <children xsi:type="menu:Menu" xmi:id="_ZXIJsIl5Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.nameEditor.menu.changeTo" label="%menu.label.0">
139
          <children xsi:type="menu:HandledMenuItem" xmi:id="_Nt_cAIl8Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel12" label="%command.label.12"/>
140
          <children xsi:type="menu:HandledMenuItem" xmi:id="_POj24Il8Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel13" label="%command.label.13"/>
141
          <children xsi:type="menu:HandledMenuItem" xmi:id="_QbC4IIl8Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel14" label="%command.label.14"/>
142
        </children>
143
        <children xsi:type="menu:MenuSeparator" xmi:id="_a0anEIl5Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.menuseparator.13"/>
144
        <children xsi:type="menu:HandledMenuItem" xmi:id="_bu3dIIl5Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel15" label="%command.label.15"/>
145
        <children xsi:type="menu:HandledMenuItem" xmi:id="_dVXg4Il5Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel16" label="%command.label.16"/>
146
        <children xsi:type="menu:MenuSeparator" xmi:id="_e7Jy8Il5Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.menuseparator.14"/>
147
        <children xsi:type="menu:HandledMenuItem" xmi:id="_fkhgEIl5Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel17" label="%command.label.17"/>
148
        <children xsi:type="menu:HandledMenuItem" xmi:id="_hdfz4Il5Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel57" label="%command.label.57"/>
149
        <children xsi:type="menu:HandledMenuItem" xmi:id="_iw4HUIl5Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel56" label="%command.label.56"/>
150
        <children xsi:type="menu:MenuSeparator" xmi:id="_kS2QYIl5Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.menuseparator.15"/>
151
        <children xsi:type="menu:HandledMenuItem" xmi:id="_lF1MgIl5Eeeox7omhvOBWg" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel18" label="%command.label.18"/>
152
      </menus>
153
    </elements>
126 154
  </fragments>
127 155
  <fragments xsi:type="fragment:StringModelFragment" xmi:id="__mwtMDVpEee_b7RlBzTDRw" featurename="commands" parentElementId="xpath:/">
128 156
    <elements xsi:type="commands:Command" xmi:id="_BjF3ADVqEee_b7RlBzTDRw" elementId="eu.etaxonomy.taxeditor.editor.command.specimeneditor.create_field_unit" commandName="%command.commandname.1"/>
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/name/e4/TaxonNameEditorE4.java
20 20

  
21 21
import org.apache.commons.lang.StringUtils;
22 22
import org.eclipse.core.commands.operations.IUndoContext;
23
import org.eclipse.core.commands.operations.UndoContext;
23 24
import org.eclipse.core.runtime.IProgressMonitor;
24 25
import org.eclipse.core.runtime.OperationCanceledException;
25 26
import org.eclipse.e4.ui.di.Focus;
......
27 28
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
28 29
import org.eclipse.e4.ui.services.EMenuService;
29 30
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
30
import org.eclipse.jface.action.MenuManager;
31
import org.eclipse.jface.dialogs.MessageDialog;
31 32
import org.eclipse.jface.viewers.ISelection;
32
import org.eclipse.jface.viewers.ISelectionChangedListener;
33 33
import org.eclipse.jface.viewers.ISelectionProvider;
34 34
import org.eclipse.jface.viewers.StructuredSelection;
35 35
import org.eclipse.swt.graphics.Color;
36 36
import org.eclipse.swt.widgets.Composite;
37
import org.eclipse.swt.widgets.Menu;
38 37
import org.eclipse.ui.ISelectionListener;
39
import org.eclipse.ui.ISelectionService;
40 38
import org.eclipse.ui.IWorkbenchPart;
41 39
import org.eclipse.ui.IWorkbenchPartReference;
42 40
import org.eclipse.ui.PartInitException;
......
58 56
import eu.etaxonomy.taxeditor.editor.ISecuredEditor;
59 57
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
60 58
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
61
import eu.etaxonomy.taxeditor.editor.name.container.AbstractGroupedContainer;
62 59
import eu.etaxonomy.taxeditor.editor.name.e4.container.AbstractGroupE4;
63 60
import eu.etaxonomy.taxeditor.editor.name.e4.container.AbstractGroupedContainerE4;
64 61
import eu.etaxonomy.taxeditor.editor.name.e4.container.AcceptedGroupE4;
......
70 67
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
71 68
import eu.etaxonomy.taxeditor.model.IPartChangeListener;
72 69
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
70
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
71
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
72
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
73 73
import eu.etaxonomy.taxeditor.model.TaxeditorPartService;
74 74
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
75 75
import eu.etaxonomy.taxeditor.preference.Resources;
76 76
import eu.etaxonomy.taxeditor.security.RequiredPermissions;
77
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
78 77
import eu.etaxonomy.taxeditor.store.CdmStore;
79 78

  
80 79
/**
......
83 82
 * @date Aug 24, 2017
84 83
 *
85 84
 */
86
public class TaxonNameEditorE4 implements
87
		IConversationEnabled, IDirtyMarkable,
88
		IPartContentHasDetails, IPartChangeListener,
89
		ISelectionListener, ISecuredEditor, IPostOperationEnabled {
85
public class TaxonNameEditorE4 implements IConversationEnabled, IDirtyMarkable, IPartContentHasDetails,
86
        IPartContentHasSupplementalData, IPartContentHasMedia, IPartContentHasFactualData, IPartChangeListener,
87
        ISelectionListener, ISecuredEditor, IPostOperationEnabled {
90 88

  
91 89
	private Taxon taxon;
92 90

  
......
99 97

  
100 98
	private ConversationHolder conversation;
101 99

  
102
	private MenuManager menuManager;
103
	private Menu menu;
104

  
105 100
	private AcceptedGroupE4 acceptedGroup;
106 101
	private List<HomotypicalSynonymGroupE4> heterotypicSynonymGroups = new ArrayList<>();
107 102
	private MisappliedGroupE4 misappliedGroup;
......
109 104
	//FIXME E4 migrate dnd
110 105
//	private DropTarget target;
111 106

  
112
	private ISelectionService selectionService;
113

  
114 107
	private TaxonBase objectAffectedByLastOperation;
115 108

  
116
	private ICdmEntitySession cdmEntitySession;
109
	@Inject
110
	private EMenuService menuService;
117 111

  
118 112
	@Inject
119 113
	private ESelectionService selService;
......
121 115
	@Inject
122 116
	private MDirtyable dirty;
123 117

  
124
	private ISelectionChangedListener selectionChangedListener;
125

  
126 118
    private TaxonEditorInputE4 input;
127 119

  
120
    private UndoContext undoContext;
121

  
128 122
	@Inject
129 123
	public TaxonNameEditorE4() {
124
	    undoContext = new UndoContext();
130 125
	}
131 126

  
132 127

  
133 128
	@PostConstruct
134
    public void createPartControl(Composite parent, EMenuService menuService) {
129
    public void createPartControl(Composite parent) {
135 130
        if (CdmStore.isActive()){
136 131
            if(conversation == null){
137 132
                conversation = CdmStore.createConversation();
......
143 138

  
144 139
        createManagedForm(parent);
145 140

  
146
		//propagate selection
147
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
148
        //FIXME E4 migrate
149
//        viewer.addSelectionChangedListener(selectionChangedListener);
150

  
151
        //create context menu
152
        menuService.registerContextMenu(managedForm, "eu.etaxonomy.taxeditor.editor.popupmenu.nameeditor");
153

  
154 141
		TaxeditorPartService.getInstance().addListener(
155 142
				TaxeditorPartService.PART_ACTIVATED, this);
156 143

  
......
169 156

  
170 157
			@Override
171 158
			public boolean setInput(Object input) {
172
				if (input instanceof AbstractGroupedContainer) {
173
					selection = ((AbstractGroupedContainer) input).getData();
159
				if (input instanceof AbstractGroupedContainerE4) {
160
					selection = ((AbstractGroupedContainerE4) input).getData();
174 161

  
175
			        selectionChangedListener = (event -> selService.setSelection(new StructuredSelection(selection)));
162
			        selService.setSelection(new StructuredSelection(selection));
176 163
				}else if(input == null){
177 164
					selection = null;
178
                    selectionChangedListener = (event -> selService.setSelection(new StructuredSelection()));
165
                    selService.setSelection(new StructuredSelection());
179 166
				}
180 167

  
181 168

  
......
200 187
		parent.setLayout(layout);
201 188
		parent.setBackground(AbstractUtility
202 189
				.getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
190

  
191
        //FIXME E4 migrate
192
//        viewer.addSelectionChangedListener(selectionChangedListener);
193

  
203 194
	}
204 195

  
205 196
	public void createOrUpdateNameComposites() {
......
223 214

  
224 215
	@Focus
225 216
	public void setFocus() {
217
	    //make sure to bind again if maybe in another view the conversation was unbound
218
        if(conversation!=null && !conversation.isBound()){
219
            conversation.bind();
220
        }
226 221
	    if(input!=null){
227 222
	        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
228 223
	        if (getSelectedContainer() == null) {
......
298 293
		return managedForm;
299 294
	}
300 295

  
301
	public Menu getMenu() {
302
		if (menu == null || menu.isDisposed()) {
303
			// Creating the menu because it was either not initialised or
304
			// disposed while refreshing the editor
305
			menu = menuManager.createContextMenu(parent);
306
		}
307
		return menu;
308
	}
309 296

  
310 297
	/**
311 298
	 * <p>
......
341 328
	@Persist
342 329
	public void doSave(IProgressMonitor monitor) {
343 330

  
344
		monitor.beginTask(Messages.TaxonNameEditor_SAVING_NAMES, getGroupedContainers().size());
331
	    monitor.beginTask(Messages.TaxonNameEditor_SAVING_NAMES, getGroupedContainers().size());
332
	    if (!conversation.isBound()) {
333
	        conversation.bind();
334
	    }
335
	    monitor.worked(1);
345 336

  
346
		try {
347
			// check for empty names
348
			for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
337
	    // check for empty names
338
	    if (checkForEmptyNames()) {
339
	        MessageDialog.openWarning(AbstractUtility.getShell(), Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED,
340
	                Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED_MESSAGE);
341
	        return;
342
	    }
343
	    for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
349 344

  
350
				monitor.subTask(Messages.TaxonNameEditor_SAVING_COMPOSITES
351
						+ container.getTaxonBase().getTitleCache());
352
				container.persistName();
345
	        monitor.subTask(Messages.TaxonNameEditor_SAVING_COMPOSITES
346
	                + container.getTaxonBase().getTitleCache());
347
	        container.persistName();
353 348

  
354
				// In case the progress monitor was canceled throw an exception.
355
				if (monitor.isCanceled()) {
356
					throw new OperationCanceledException();
357
				}
349
	        // In case the progress monitor was canceled throw an exception.
350
	        if (monitor.isCanceled()) {
351
	            throw new OperationCanceledException();
352
	        }
353
	        // commit the conversation and start a new transaction immediately
354
	        input.merge();
358 355

  
359
				// Otherwise declare this step as done.
360
				monitor.worked(1);
361
			}
362
		} finally {
356
	        conversation.commit(true);
363 357

  
364
			// Stop the progress monitor.
365
			monitor.done();
366
		}
358
	        // Otherwise declare this step as done.
359
	        monitor.worked(1);
360

  
361
	        dirty.setDirty(false);
362
	    }
367 363

  
364
	    // Stop the progress monitor.
365
	    monitor.done();
368 366
	}
369 367

  
370 368
	public void init(TaxonEditorInputE4 input)
......
467 465

  
468 466
	@PreDestroy
469 467
	public void dispose() {
470
		conversation.unregisterForDataStoreChanges(this);
468
        if(conversation!=null){
469
            conversation.unregisterForDataStoreChanges(this);
470
            conversation.close();
471
        }
471 472
	}
472 473

  
473 474
	/** {@inheritDoc} */
......
614 615

  
615 616

  
616 617
    public IUndoContext getUndoContext() {
617
        //FIXME E4 is this needed
618
        return null;
618
        return undoContext;
619 619
    }
620 620

  
621 621
    public Composite getControl(){
622 622
        return managedForm.getForm().getBody();
623 623
    }
624 624

  
625
    public EMenuService getMenuService() {
626
        return menuService;
627
    }
628

  
629
    public ESelectionService getSelectionService() {
630
        return selService;
631
    }
632

  
633

  
634
    /**
635
     * {@inheritDoc}
636
     */
637
    @Override
638
    public boolean canAttachMedia() {
639
        return true;
640
    }
641

  
625 642
}
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/name/e4/container/AbstractGroupE4.java
20 20
import org.eclipse.swt.graphics.Color;
21 21
import org.eclipse.swt.widgets.Composite;
22 22
import org.eclipse.swt.widgets.Display;
23
import org.eclipse.swt.widgets.Menu;
24 23
import org.eclipse.ui.forms.widgets.TableWrapData;
25 24
import org.eclipse.ui.forms.widgets.TableWrapLayout;
26 25

  
......
176 175
		}
177 176
	}
178 177

  
179
	public void setMenu(Menu menu) {
180
		getControl().setMenu(menu);
181
		for(AbstractGroupedContainerE4 container : getGroupedContainers()){
182
			container.setMenu(menu);
183
		}
184

  
185
	}
186

  
187 178
	protected void emptyGroup(){
188 179
		for(AbstractGroupedContainerE4 container : groupedContainers){
189 180
			container.dispose();
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/name/e4/container/AbstractGroupedContainerE4.java
15 15

  
16 16
import org.apache.commons.lang.StringUtils;
17 17
import org.eclipse.core.runtime.Assert;
18
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
18 19
import org.eclipse.jface.dialogs.Dialog;
19 20
import org.eclipse.jface.text.IDocument;
20 21
import org.eclipse.jface.text.Position;
22
import org.eclipse.jface.viewers.ISelectionChangedListener;
21 23
import org.eclipse.jface.window.DefaultToolTip;
22 24
import org.eclipse.swt.custom.StyledText;
23 25
import org.eclipse.swt.dnd.DND;
......
40 42
import org.eclipse.swt.widgets.Control;
41 43
import org.eclipse.swt.widgets.Display;
42 44
import org.eclipse.swt.widgets.Label;
43
import org.eclipse.swt.widgets.Menu;
44 45
import org.eclipse.ui.forms.IFormPart;
45 46
import org.eclipse.ui.forms.IManagedForm;
46 47
import org.eclipse.ui.forms.widgets.TableWrapData;
......
118 119
	private Color backgroundColor;
119 120
	private boolean isDirty;
120 121

  
122
    private ISelectionChangedListener selectionChangedListener;
123

  
121 124
	public AbstractGroupedContainerE4(T taxonBase) {
122 125
		setData(taxonBase);
123 126
		parseHandler = ParseHandler.NewInstance(taxonBase.getName());
......
130 133
		createLineWrapSupport();
131 134
		createLineBreakListener();
132 135

  
133
		//FIXME E4 this has to be done in fragment
134
//		setMenu(getEditor().getMenu());
136
		setMenu();
135 137

  
136 138
		setDraggableControl(new Control[] { getControl(),
137 139
				getNameViewer().getRulerControl() });
......
653 655
		return isDirty;
654 656
	}
655 657

  
656
	public void setMenu(Menu menu) {
657
		control.setMenu(menu);
658

  
659
		getNameViewer().setMenu(menu);
658
	public void setMenu() {
659
        getEditor().getMenuService().registerContextMenu(getNameViewer().getTextWidget(), "eu.etaxonomy.taxeditor.editor.popupmenu.nameeditor");
660 660
	}
661 661

  
662 662
	private Control[] draggableControls;
......
856 856
	@Override
857 857
	public void dispose() {
858 858
		if (getControl() != null) {
859
			setMenu(null);
859
			setMenu();
860 860
			getControl().dispose();
861 861
		}
862 862
	}
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/name/e4/container/NameViewerE4.java
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.editor.name.e4.container;
11

  
12
import java.util.Iterator;
13

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

  
47
import eu.etaxonomy.taxeditor.editor.ViewerConfiguration;
48
import eu.etaxonomy.taxeditor.editor.name.container.EditorAnnotation;
49
import eu.etaxonomy.taxeditor.editor.name.container.EditorAnnotation.EditorAnnotationType;
50
import eu.etaxonomy.taxeditor.editor.name.container.LineWrapSquigglesStrategy;
51
import eu.etaxonomy.taxeditor.editor.name.container.RulerWithIcon;
52
import eu.etaxonomy.taxeditor.model.AbstractUtility;
53
import eu.etaxonomy.taxeditor.preference.Resources;
54

  
55
/**
56
 *
57
 * @author pplitzner
58
 * @since Aug 25, 2017
59
 *
60
 */
61
public class NameViewerE4 extends SourceViewer {
62

  
63
	public static final int RULER_WIDTH = 16;
64

  
65
	private final IVerticalRuler ruler;
66
	private final AnnotationModel annotationModel;
67

  
68
	public NameViewerE4(Composite parent) {
69
		super(parent, new RulerWithIcon(RULER_WIDTH), SWT.WRAP | SWT.MULTI | SWT.RESIZE);
70

  
71
		this.ruler = getVerticalRuler();
72

  
73
		setBackground(AbstractUtility.getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
74

  
75
		Canvas canvas = (Canvas) getControl();
76
		TableWrapLayout layout = (TableWrapLayout) canvas.getLayout();
77
		layout.topMargin = 0;
78
		layout.rightMargin = 0;
79
		layout.bottomMargin = 0;
80
		layout.leftMargin = 0;
81
		layout.verticalSpacing = 10;
82
		layout.horizontalSpacing = 0;
83

  
84
		canvas.setLayout(layout);
85

  
86
		// Lay out the viewer's widgets
87
		TableWrapData twdata = new TableWrapData(TableWrapData.FILL_GRAB);
88
		getControl().setLayoutData(twdata);
89

  
90
		twdata = new TableWrapData(TableWrapData.FILL_GRAB);
91
		getTextWidget().setLayoutData(twdata);
92
		getTextWidget().setLineSpacing(5);
93

  
94
		// Default implementation adds ruler after the text widget, then
95
		//	uses a custom layout to display it before the text widget
96
		ruler.getControl().moveAbove(getTextWidget());
97

  
98
		annotationModel = new AnnotationModel();
99
		this.setDocument(new Document(""), annotationModel); //$NON-NLS-1$
100

  
101
		createAnnotationPainter();
102

  
103
		this.configure(new ViewerConfiguration());
104

  
105
	}
106

  
107
	public void setBackground(Color color) {
108

  
109
		// Set background color of ruler
110
		ruler.getControl().setBackground(color);
111

  
112
		// Set background color of text widget
113
		getTextWidget().setBackground(color);
114

  
115
		// Set background color of strip between ruler and textWidget
116
		getTextWidget().getParent().setBackground(color);
117
	}
118

  
119
	public Control getRulerControl() {
120
		return ruler.getControl();
121
	}
122

  
123
	public void setIcon(Image icon) {
124
		if (ruler instanceof RulerWithIcon) {
125
			((RulerWithIcon) ruler).setIcon(icon);
126
		} else {
127
			throw new IllegalStateException("Viewer's IVerticalRuler is not an instance of RulerWithIcon."); //$NON-NLS-1$
128
		}
129
	}
130

  
131
	@Override
132
	protected Layout createLayout() {
133
		TableWrapLayout layout = new TableWrapLayout();
134
		layout.numColumns = 2;
135
		layout.verticalSpacing = 0;
136
		return layout;
137
	}
138

  
139
	private void createAnnotationPainter(){
140
		// Annotations section
141
		IAnnotationAccess fAnnotationAccess = new DefaultMarkerAnnotationAccess();
142

  
143
		// To paint the annotations
144
		AnnotationPainter annotationPainter = new AnnotationPainter(this, fAnnotationAccess);
145

  
146
		// Default SquigglesStrategy doesn't recognize line wraps
147
		annotationPainter.addDrawingStrategy(LineWrapSquigglesStrategy.ID, new LineWrapSquigglesStrategy());
148

  
149
		// Add ability to paint red squigglies
150
		annotationPainter.addAnnotationType(EditorAnnotationType.ERROR.name(), LineWrapSquigglesStrategy.ID);
151
		annotationPainter.setAnnotationTypeColor(EditorAnnotationType.ERROR.image,
152
				new Color(Display.getDefault(), EditorAnnotationType.ERROR.color));
153

  
154
		// Add ability to paint yellow squigglies
155
		annotationPainter.addAnnotationType(EditorAnnotationType.WARNING.name(), LineWrapSquigglesStrategy.ID);
156
		annotationPainter.setAnnotationTypeColor(EditorAnnotationType.WARNING.image,
157
				new Color(Display.getDefault(), EditorAnnotationType.WARNING.color));
158

  
159
		this.addPainter(annotationPainter);
160
	}
161

  
162

  
163
	public void clearAnnotations() {
164
		Iterator<Annotation> annotations = this.getAnnotationModel().getAnnotationIterator();
165
		while (annotations.hasNext()) {
166
			Annotation annotation = annotations.next();
167
			this.getAnnotationModel().removeAnnotation(annotation);
168
		}
169
	}
170

  
171
	public void addAnnotation(EditorAnnotation annotation){
172
		addAnnotation(annotation, null);
173
	}
174

  
175
	public void addAnnotation(EditorAnnotation annotation, Position position){
176
		if(position == null){
177
			position = new Position(0, 0);
178
		}
179

  
180
		this.getAnnotationModel().addAnnotation(annotation, position);
181
	}
182

  
183
	public void setText(String text) {
184
		if (text == null) {
185
			text = ""; //$NON-NLS-1$
186
		}
187
		try {
188
			Assert.isNotNull(text);
189
			// TODO figure out why getTextWidget() returns null!
190
			if (this.getTextWidget() == null) {
191
				return;
192
			}
193
			Assert.isNotNull(this.getTextWidget());
194
			this.getTextWidget().setText(text);
195
		} catch (RuntimeException e) {
196
			throw e;
197
		}
198
	}
199

  
200
	public void setMenu(Menu menu) {
201
		getRulerControl().setMenu(menu);
202
		getTextWidget().setMenu(menu);
203
	}
204

  
205
	public void setCursorToEOL() {
206
		getTextWidget().setCaretOffset(getTextWidget().getText().length());
207
		getTextWidget().setFocus();
208
	}
209

  
210
	public int getCursorPosition(){
211
		return getTextWidget().getCaretOffset();
212
	}
213

  
214
	public void setCursorPosition(int offset){
215
		try{
216
			getTextWidget().setCaretOffset(offset);
217
		}catch(IllegalArgumentException e){
218
			// do nothing
219
		}
220
	}
221

  
222
	public void createUndoSupport(IEditorSite editorSite) {
223

  
224
		IUndoManager undoManager = new TextViewerUndoManager(25);
225
		this.setUndoManager(undoManager);
226
		undoManager.connect(this);
227

  
228
		IUndoContext workbenchUndoContext = ((IUndoManagerExtension)undoManager).getUndoContext();
229

  
230
		OperationHistoryActionHandler undoAction = new UndoActionHandler(editorSite, workbenchUndoContext);
231
		undoAction.setActionDefinitionId(ActionFactory.UNDO.getId());
232

  
233
		// Create the redo action.
234
		OperationHistoryActionHandler redoAction = new RedoActionHandler(editorSite, workbenchUndoContext);
235
		undoAction.setActionDefinitionId(ActionFactory.REDO.getId());
236

  
237
		IActionBars actionBars = editorSite.getActionBars();
238
		if (actionBars != null) {
239
			actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
240
			actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
241

  
242
		}
243

  
244
	}
245

  
246
	public void createUndoSupport_(IEditorSite editorSite) {
247
		IUndoManager undoManager = new TextViewerUndoManager(25);
248
		this.setUndoManager(undoManager);
249
		undoManager.connect(this);
250
		IUndoContext undoContext;
251
		if (undoManager instanceof IUndoManagerExtension) {
252
			undoContext = ((IUndoManagerExtension)undoManager).getUndoContext();
253

  
254
			OperationHistoryActionHandler undoAction = new UndoActionHandler(editorSite, undoContext);
255
			undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO);
256

  
257
			// Create the redo action.
258
			OperationHistoryActionHandler redoAction = new RedoActionHandler(editorSite, undoContext);
259
			redoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO);
260

  
261
			IActionBars actionBars = editorSite.getActionBars();
262
			if (actionBars != null) {
263
				actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.UNDO, undoAction);
264
				actionBars.setGlobalActionHandler(IWorkbenchActionDefinitionIds.REDO, redoAction);
265

  
266
			}
267
		}
268
	}
269

  
270
}
271

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)