Project

General

Profile

Revision 4ff11da5

ID4ff11da52f34b700620b1817db3223c7d4b8974f
Parent faf2ca45
Child 033ef209

Added by pplitzner almost 3 years ago

ref #6595 Initial commit of migrated/refactored classes (not working)

View differences:

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/e4/MultiPageTaxonEditorDataChangeBehaviourE4.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.e4;
11

  
12
import java.util.Vector;
13

  
14
import org.apache.log4j.Logger;
15

  
16
import eu.etaxonomy.cdm.model.name.TaxonName;
17
import eu.etaxonomy.cdm.model.taxon.Synonym;
18
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
19
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeEvent;
20
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeEvent.EventType;
21
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
22
import eu.etaxonomy.taxeditor.model.AbstractDataChangeBehaviour;
23
import eu.etaxonomy.taxeditor.model.IDataChangeBehavior;
24

  
25
/**
26
 * Implementation if {@link eu.etaxonomy.taxeditor.store.model.IDataChangeBehaviour}
27
 * for the {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}.
28
 *
29
 * TODO make this class a bean and thus configurable by Spring
30
 *
31
 * @author pplitzner
32
 * @date Aug 24, 2017
33
 *
34
 */
35
public class MultiPageTaxonEditorDataChangeBehaviourE4 extends AbstractDataChangeBehaviour implements
36
		IDataChangeBehavior {
37
	private static final Logger logger = Logger
38
			.getLogger(MultiPageTaxonEditorDataChangeBehaviourE4.class);
39

  
40
	private MultiPageTaxonEditorE4 source;
41

  
42
	private TaxonEditorInputE4 input;
43

  
44
	public MultiPageTaxonEditorDataChangeBehaviourE4(
45
	        MultiPageTaxonEditorE4 multiPageTaxonEditor) {
46
		source = multiPageTaxonEditor;
47
	}
48

  
49
	/** {@inheritDoc} */
50
	@Override
51
    public void reactOnDataChange(CdmDataChangeMap changeEvents) {
52
		input = source.getEditorInput();
53

  
54
		// I don't know when this happens but ...
55
		if(input.getTaxon() == null) {
56
            return;
57
        }
58

  
59
		if(changeEvents.sizeByEventType(EventType.UPDATE) > 0){
60
			reactOnUpdate(changeEvents.getEvents(EventType.UPDATE));
61
		}
62
		if(changeEvents.sizeByEventType(EventType.INSERT) > 0){
63
			reactOnInsert(changeEvents.getEvents(EventType.INSERT));
64
		}
65
		if(changeEvents.sizeByEventType(EventType.DELETE) > 0){
66
			reactOnDelete(changeEvents.getEvents(EventType.DELETE));
67
		}
68
	}
69

  
70

  
71
	/** {@inheritDoc} */
72
	//we do not really need this anymore as closing the editors is
73
	//now run before the delete operation in DeleteHandler
74
	@Override
75
	public void reactOnDelete(Vector<CdmDataChangeEvent> events){
76
		for(CdmDataChangeEvent event : events){
77
			// close open editors
78
			if((event.getEntity() instanceof TaxonNode)
79
					&& input.getTaxonNode().equals(event.getEntity())){
80
				//EditorUtil.close(source);
81
				logger.debug("Closing open editor for deleted taxon."); //$NON-NLS-1$
82
			}
83
		}
84
	}
85

  
86
	/** {@inheritDoc} */
87
	@Override
88
	public void reactOnUpdate(Vector<CdmDataChangeEvent> events){
89

  
90
		for(CdmDataChangeEvent event : events){
91

  
92
			// update editor title
93
			if(event.getEntity() instanceof TaxonName){
94
				if(input.getTaxon().getName().equals(event.getEntity())){
95
					// set the name of the editor window
96
					source.setPartName();
97
					logger.debug("Updating title of taxons editor."); //$NON-NLS-1$
98
				}
99
			}
100
			// TODO other stuff to happen
101

  
102
		}
103
	}
104

  
105
	/** {@inheritDoc} */
106
	@Override
107
	public void reactOnInsert(Vector<CdmDataChangeEvent> events){
108
		boolean doRedraw = false;
109
		for(CdmDataChangeEvent event : events){
110
			// refresh on synonym changes
111
			if(input.getTaxon().hasSynonyms() && event.getEntity() instanceof Synonym){
112
				Synonym synonym = (Synonym) event.getEntity();
113

  
114
				if(synonym.isSynonymOf(input.getTaxon())){
115
					doRedraw = true;
116
					break;
117
				}
118
			}
119
		}
120
	}
121
}
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/e4/MultiPageTaxonEditorE4.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.e4;
11

  
12
import java.util.ArrayList;
13
import java.util.List;
14
import java.util.UUID;
15

  
16
import javax.annotation.PreDestroy;
17
import javax.inject.Inject;
18

  
19
import org.eclipse.core.commands.operations.IUndoContext;
20
import org.eclipse.core.commands.operations.UndoContext;
21
import org.eclipse.core.runtime.IProgressMonitor;
22
import org.eclipse.e4.ui.di.Focus;
23
import org.eclipse.e4.ui.di.Persist;
24
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
25
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
26
import org.eclipse.jface.dialogs.MessageDialog;
27
import org.eclipse.jface.viewers.ISelectionChangedListener;
28
import org.eclipse.ui.IEditorInput;
29
import org.eclipse.ui.IEditorPart;
30
import org.eclipse.ui.PartInitException;
31

  
32
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
33
import eu.etaxonomy.cdm.api.application.CdmChangeEvent;
34
import eu.etaxonomy.cdm.api.application.CdmChangeEvent.Action;
35
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
36
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
37
import eu.etaxonomy.cdm.model.common.CdmBase;
38
import eu.etaxonomy.cdm.model.name.TaxonName;
39
import eu.etaxonomy.cdm.model.taxon.Taxon;
40
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
41
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
42
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
43
import eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage;
44
import eu.etaxonomy.taxeditor.editor.ISecuredEditor;
45
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
46
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
47
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
48
import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
49
import eu.etaxonomy.taxeditor.editor.name.container.AbstractGroupedContainer;
50
import eu.etaxonomy.taxeditor.model.AbstractUtility;
51
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
52
import eu.etaxonomy.taxeditor.model.IDataChangeBehavior;
53
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
54
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
55
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
56
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
57
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
58
import eu.etaxonomy.taxeditor.model.MessagingUtils;
59
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
60

  
61
/**
62
 *
63
 * @author pplitzner
64
 * @date Aug 24, 2017
65
 *
66
 */
67
public class MultiPageTaxonEditorE4
68
        implements IPartContentHasFactualData, IConversationEnabled, IPostOperationEnabled, IDirtyMarkable,
69
        IPartContentHasDetails, IPartContentHasSupplementalData, ISecuredEditor, IPartContentHasMedia, ITaxonEditor {
70

  
71
    /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.taxon"{trunked}</code> */
72
    public static final String ID = "eu.etaxonomy.taxeditor.editor.taxon"; //$NON-NLS-1$
73

  
74
    private ConversationHolder conversation;
75
    private IDataChangeBehavior dataChangeBehavior;
76
    private IUndoContext undoContext;
77

  
78
    private TaxonEditorInputE4 input;
79

  
80
    @Inject
81
    private ESelectionService selService;
82

  
83
    @Inject
84
    private MDirtyable dirty;
85

  
86
    private ISelectionChangedListener selectionChangedListener;
87

  
88

  
89
    @Inject
90
    public MultiPageTaxonEditorE4() {
91
        undoContext = new UndoContext();
92

  
93
    }
94

  
95
    /** {@inheritDoc} */
96
    @PreDestroy
97
    public void dispose() {
98
        input.dispose();
99
        conversation.unregisterForDataStoreChanges(this);
100
        conversation.close();
101
    }
102

  
103
    /** {@inheritDoc} */
104
    @Override
105
    protected void addPages() {
106
        input = getEditorInput();
107
        conversation = input.getConversationHolder();
108
        conversation.registerForDataStoreChanges(this);
109

  
110
        try {
111
            addPage(Page.NAME.getIndex(), new TaxonNameEditor(this),
112
                    getEditorInput());
113

  
114
        } catch (PartInitException e) {
115
            MessagingUtils.error(getClass(), e);
116
        }
117
    }
118

  
119
    /**
120
     * {@inheritDoc}
121
     */
122
    @Override
123
    protected void pageChange(int newPageIndex) {
124
        if(getCurrentPage()==-1){
125
            return;
126
        }
127
        super.pageChange(newPageIndex);
128
    }
129

  
130
    /** {@inheritDoc} */
131
    @Persist
132
    public void doSave(IProgressMonitor monitor) {
133
        monitor.beginTask(Messages.MultiPageTaxonEditor_SAVING_EDITOR, 4);
134
        try {
135
            if (!conversation.isBound()) {
136
                conversation.bind();
137
            }
138
            monitor.worked(1);
139

  
140
            for (IEditorPart editorPage : getPages()) {
141
                if (editorPage instanceof TaxonNameEditor) {
142
                    if (((TaxonNameEditor) editorPage).checkForEmptyNames()) {
143
                        MessageDialog.openWarning(AbstractUtility.getShell(), Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED,
144
                                Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED_MESSAGE);
145
                        return;
146
                    }
147
                }
148

  
149
                editorPage.doSave(monitor);
150
                monitor.worked(1);
151
            }
152

  
153
            // commit the conversation and start a new transaction immediately
154

  
155
            input.merge();
156

  
157
            conversation.commit(true);
158
            CdmApplicationState.getCurrentDataChangeService()
159
            .fireChangeEvent(new CdmChangeEvent(Action.Update, input.getTaxonNode() , MultiPageTaxonEditorE4.class), true);
160
            monitor.worked(1);
161

  
162
            this.setDirty(false);
163
            monitor.worked(1);
164
        } catch (Exception e) {
165
            setFocus();
166
            MessagingUtils.operationDialog(this, e, TaxeditorEditorPlugin.PLUGIN_ID,Messages.MultiPageTaxonEditor_SAVING_TAXON, Messages.MultiPageTaxonEditor_SAVING_TAXON_MESSAGE);
167
            disableEditor(true);
168
        } finally {
169
            monitor.done();
170
        }
171
    }
172

  
173
    private void disableEditor(boolean isOnError) {
174
        for (IMultiPageTaxonEditorPage editorPage : getPages()) {
175
            if(isOnError){
176
                editorPage.setOnError();
177
            }else {
178
                editorPage.setDisabled();
179
            }
180
        }
181

  
182
        conversation.unregisterForDataStoreChanges(this);
183
        conversation.close();
184
        setDirty(false);
185
    }
186

  
187
    private void setDirty(boolean dirty) {
188
        this.dirty.setDirty(dirty);
189
    }
190

  
191
    public boolean isDirty() {
192
        return dirty.isDirty();
193
    }
194

  
195
    public void init(IEditorInput input)
196
            throws PartInitException {
197

  
198
        if (!(input instanceof TaxonEditorInputE4)) {
199
            throw new PartInitException(
200
                    Messages.MultiPageTaxonEditor_INVALID_INPUT);
201
        }
202

  
203
        this.input = (TaxonEditorInputE4) input;
204
        setPartName();
205
    }
206

  
207
    /**
208
     * Calls <code>MultiPageEditorPart.setPartName(String partName)</code> with
209
     * text appropriate to the state of the taxon: any taxon that has been saved
210
     * will by necessity have a name to display; a new taxon should display
211
     * "New taxon" in the editor tab.
212
     */
213
    protected void setPartName() {
214

  
215
        String partName = null;
216
        TaxonName name = getTaxon().getName();
217

  
218
        if (name != null) {
219
            partName = name.getTitleCache();
220
        }
221

  
222
        if (partName == null || partName.equals("")) { //$NON-NLS-1$
223
            partName = (Messages.MultiPageTaxonEditor_NEW_TAXON);
224
        }
225

  
226
        //FIXME E4 migrate
227
//        setPartName(partName);
228
    }
229

  
230
    /**
231
     * {@inheritDoc}
232
     *
233
     * Editor pages call this in their postOperation to notify the
234
     * MultiPageTaxonEditor of unsaved changes
235
     */
236
    @Override
237
    public void changed(Object element) {
238
        // setDirty(true);
239
        // if the attribute is null then do not set the dirty flag -> hotfix for the problem that for tasks done in service methods the changes are saved automatically
240
        if (element != null){
241
            setDirty(true);
242
        }
243

  
244
        if (element instanceof TaxonBase) {
245
            TaxonNameEditor page = (TaxonNameEditor) getPage(Page.NAME);
246
            AbstractGroupedContainer container = page.getContainer((TaxonBase) element);
247
            if (container != null) {
248
                container.refresh();
249
            }
250
        }
251
        if (element instanceof TaxonRelationship) {
252
            TaxonNameEditor page = (TaxonNameEditor) getPage(Page.NAME);
253
            AbstractGroupedContainer container = page.getContainer(((TaxonRelationship) element).getFromTaxon());
254
            if (container != null) {
255
                container.refresh();
256
            }
257
        }
258
        //refresh part title
259
        //TODO: refresh taxon node in taxon navigator
260
        setPartName();
261
    }
262

  
263
    @Override
264
    public void forceDirty() {
265
        changed(null);
266
    }
267

  
268
    /**
269
     * The accepted taxon that is the input for this editor
270
     *
271
     * @return the accepted taxon
272
     */
273
    @Override
274
    public Taxon getTaxon() {
275
        return input.getTaxon();
276
    }
277

  
278
    @Override
279
    public ConversationHolder getConversationHolder() {
280
        return conversation;
281
    }
282

  
283
    public void setConversationHolder(ConversationHolder conversation) {
284
        this.conversation = conversation;
285
    }
286

  
287
    public IUndoContext getUndoContext() {
288
        return undoContext;
289
    }
290

  
291
    public void setUndoContext(IUndoContext undoContext) {
292
        this.undoContext = undoContext;
293
    }
294

  
295
    /** {@inheritDoc} */
296
    @Focus
297
    public void setFocus() {
298
        // logger.warn("Setting focus to editor");
299
        // bind the conversation
300
        getConversationHolder().bind();
301
        input.bind();
302
        // pass focus to the active editor page
303
        //FIXME E4 migrate
304
//        getActiveEditor().setFocus();
305
    }
306

  
307
    /** {@inheritDoc} */
308
    @Override
309
    public void update(CdmDataChangeMap events) {
310
        if (dataChangeBehavior == null) {
311
            dataChangeBehavior = new MultiPageTaxonEditorDataChangeBehaviourE4(this);
312
        }
313

  
314
        DataChangeBridge.handleDataChange(events, dataChangeBehavior);
315
    }
316

  
317
    /** {@inheritDoc} */
318
    @Override
319
    public boolean postOperation(CdmBase objectAffectedByOperation) {
320
    		setDirty(true);
321

  
322
        for (IEditorPart editor : this.getPages()) {
323
            if (editor instanceof IPostOperationEnabled) {
324
                ((IPostOperationEnabled) editor).postOperation(objectAffectedByOperation);
325
            } else {
326
                MessagingUtils.warn(getClass(), Messages.MultiPageTaxonEditor_POST_OP_NOT_ENABLED + editor);
327
            }
328
        }
329
        MessagingUtils.warn(getClass(), Messages.MultiPageTaxonEditor_POST_OP_CALLED);
330

  
331
        return false;
332
    }
333

  
334
    /**
335
     * Returns an <code>IEditorPart</code> implementation by type
336
     *
337
     * @param page
338
     *            the page type
339
     * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
340
     *         object.
341
     */
342
    public IMultiPageTaxonEditorPage getPage(Page page) {
343
        for (IEditorPart editor : this.getPages()) {
344
            if (editor.getClass().equals(page.getClazz())) {
345
                return (IMultiPageTaxonEditorPage) editor;
346
            }
347
        }
348
        return null;
349
    }
350

  
351
    /**
352
     * Return a list of <code>AbstractTaxonEditor</code>s registered with this
353
     * <code>MultiPageTaxonEditor</code>.
354
     *
355
     * @return a {@link java.util.List} object.
356
     */
357
    public List<IMultiPageTaxonEditorPage> getPages() {
358
        ArrayList<IMultiPageTaxonEditorPage> editors = new ArrayList<IMultiPageTaxonEditorPage>();
359
        for (int i = 0; i < this.getPageCount(); i++) {
360

  
361
            editors.add((IMultiPageTaxonEditorPage) this.getEditor(i));
362
        }
363
        return editors;
364
    }
365

  
366
    /**
367
     * Refreshes a certain page of the MultipageTaxonEditor
368
     *
369
     * @param page
370
     *            a {@link eu.etaxonomy.taxeditor.editor.Page} object.
371
     * @return a boolean.
372
     */
373
    public boolean redraw(Page page) {
374
        return redraw(page, true);
375
    }
376

  
377
    /**
378
     * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
379
     * that page
380
     *
381
     * @param page
382
     *            a {@link eu.etaxonomy.taxeditor.editor.Page} object.
383
     * @param focus
384
     *            a boolean.
385
     * @return a boolean.
386
     */
387
    public boolean redraw(Page page, boolean focus) {
388
        IMultiPageTaxonEditorPage editorPage = getPage(page);
389
        return editorPage != null && editorPage.redraw(focus);
390
    }
391

  
392
    /**
393
     * <p>
394
     * onComplete
395
     * </p>
396
     *
397
     * @return a boolean.
398
     */
399
    @Override
400
    public boolean onComplete() {
401
        return false;
402
    }
403

  
404
    /**
405
     * Reloads the data for this
406
     */
407
    public void reload() {
408
        if (isDirty()) {
409
            MessagingUtils.warningDialog(Messages.MultiPageTaxonEditor_UNSAVED_DATA, getClass(), Messages.MultiPageTaxonEditor_UNSAVED_DATA_MESSAGE);
410
        } else {
411
            TaxonEditorInputE4 input = getEditorInput();
412

  
413
            UUID uuid = input.getTaxonNode().getUuid();
414

  
415
            conversation.clear();
416

  
417
            try {
418
                TaxonEditorInputE4 newInput = TaxonEditorInputE4.NewInstance(uuid);
419
                setInput(newInput);
420
                for (IMultiPageTaxonEditorPage editorPart : getPages()) {
421
                    editorPart.redraw();
422
                }
423
            } catch (Exception e) {
424
                MessagingUtils.messageDialog(Messages.MultiPageTaxonEditor_REFRESH_ERROR, getClass(), Messages.MultiPageTaxonEditor_REFRESH_ERROR_MESSAGE, e);
425
            }
426
        }
427
    }
428

  
429
    @Override
430
    public String toString() {
431
        return String.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput()); //$NON-NLS-1$
432
    }
433

  
434
    @Override
435
    public boolean permissionsSatisfied() {
436
        IEditorPart activeEditor = getActiveEditor();
437
        if(activeEditor != null && ISecuredEditor.class.isAssignableFrom(activeEditor.getClass())){
438
            return ((ISecuredEditor)activeEditor).permissionsSatisfied();
439
        }
440
        return true;
441
    }
442

  
443
    @Override
444
    public boolean canAttachMedia() {
445
        return true;
446
    }
447

  
448
    public TaxonEditorInputE4 getEditorInput() {
449
        return input;
450
    }
451

  
452
}
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/e4/TaxonEditorInputDataChangeBehaviourE4.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.e4;
11

  
12
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
13
import eu.etaxonomy.taxeditor.model.AbstractDataChangeBehaviour;
14

  
15
/**
16
 *
17
 * @author pplitzner
18
 * @date Aug 24, 2017
19
 *
20
 */
21
public class TaxonEditorInputDataChangeBehaviourE4 extends
22
		AbstractDataChangeBehaviour {
23

  
24
	private TaxonEditorInputE4 source;
25

  
26
	public TaxonEditorInputDataChangeBehaviourE4(TaxonEditorInputE4 taxonEditorInput) {
27
		source = taxonEditorInput;
28
	}
29

  
30
	/** {@inheritDoc} */
31
	@Override
32
	public void reactOnDataChange(CdmDataChangeMap events) {
33
		source.update(events);
34

  
35
	}
36
}
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/e4/TaxonEditorInputE4.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.e4;
11

  
12
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.UUID;
19

  
20
import org.eclipse.jface.resource.ImageDescriptor;
21
import org.eclipse.ui.IEditorInput;
22
import org.eclipse.ui.IMemento;
23
import org.eclipse.ui.IPersistableElement;
24

  
25
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
26
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
27
import eu.etaxonomy.cdm.api.service.IClassificationService;
28
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
29
import eu.etaxonomy.cdm.api.service.ITaxonService;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.name.TaxonName;
32
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
33
import eu.etaxonomy.cdm.model.taxon.Synonym;
34
import eu.etaxonomy.cdm.model.taxon.Taxon;
35
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
36
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
38
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
39
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
40
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
41
import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleTaxonNodesDialog;
42
import eu.etaxonomy.taxeditor.editor.TaxonEditorInputFactory;
43
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
44
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
45
import eu.etaxonomy.taxeditor.model.MessagingUtils;
46
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
47
import eu.etaxonomy.taxeditor.store.CdmStore;
48

  
49

  
50
/**
51
 *
52
 * @author pplitzner
53
 * @date Aug 24, 2017
54
 *
55
 */
56
public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IEditorInput, IConversationEnabled, IPersistableElement {
57

  
58
    private static final String INCORRECT_STATE = Messages.TaxonEditorInput_INCORRECT_STATE;
59

  
60
    private final ConversationHolder conversation;
61

  
62
    private TaxonNode taxonNode;
63

  
64
    private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior;
65

  
66
    private TaxonBase initiallySelectedTaxonBase;
67

  
68
    private enum CdmType {
69
        TAXON_NODE,
70
        TAXON_BASE,
71
        PARENT_TAXON_NODE
72
    }
73

  
74
    private TaxonEditorInputE4(UUID uuid, CdmType type) {
75
        super(true);
76
        this.conversation = CdmStore.createConversation();
77
        switch(type) {
78
        case PARENT_TAXON_NODE:
79
            initForParentTaxonNode(uuid);
80
            break;
81
        case TAXON_BASE:
82
            initForTaxonBase(uuid);
83
            break;
84
        case TAXON_NODE:
85
            initForTaxonNode(uuid);
86
            break;
87
        }
88
    }
89

  
90
    private void init(TaxonNode taxonNode) {
91
        this.taxonNode = taxonNode;
92
    }
93

  
94

  
95
    private void initForTaxonNode(UUID taxonNodeUuid) {
96

  
97

  
98
        TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, getTaxonNodePropertyPaths());
99

  
100
        if(taxonNode == null){
101
            MessagingUtils.warningDialog(Messages.TaxonEditorInput_NOT_IMPLEMENTED, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE);
102
        }
103
        init(taxonNode);
104

  
105
    }
106

  
107
    private void initForTaxonBase(UUID taxonBaseUuid) {
108
        TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
109
        if (taxonBase != null){
110
            if(taxonBase.isInstanceOf(Taxon.class)){
111
                Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
112

  
113
                if (taxon.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
114
                    // TODO get accepted taxon
115
                    MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
116

  
117
                    Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
118
                    Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
119
                    for(TaxonRelationship relation : relations){
120
                        if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
121
                            acceptedTaxa.add(relation.getToTaxon());
122
                        }
123
                    }
124
                    setInputForMultipleTaxa(conversation, acceptedTaxa);
125

  
126
                }else{
127
                    setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
128
                }
129
            }else if(taxonBase instanceof Synonym){
130
                Synonym synonym = (Synonym) taxonBase;
131

  
132
                Set<Taxon> taxa = new HashSet<>();
133
                Taxon taxon = synonym.getAcceptedTaxon();
134
                if (taxon != null){
135
                	taxa.add(taxon);
136
                }
137
                setInputForMultipleTaxa(conversation, taxa);
138
            }
139
        }
140
    }
141

  
142

  
143
    private void initForParentTaxonNode(UUID parentNodeUuid){
144

  
145

  
146
        TaxonName name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
147
        ITaxonTreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
148

  
149
        Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
150
        TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
151

  
152
        // add the new taxon to the editors persistence context
153
        UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode).getUuid();
154

  
155
        initForTaxonNode(newTaxonNodeUuid);
156
    }
157

  
158
    private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
159
        if(taxonNodes.size() == 1){
160
            TaxonNode taxonNode = taxonNodes.iterator().next();
161
            init(taxonNode);
162
        }else if(taxonNodes.size() > 1){
163
            TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
164
            if(taxonNode != null){
165
                init(taxonNode);
166
            }
167
        } else if (taxonNodes.size() == 0) {
168
            // this is an undesired state
169
            MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
170
        }
171
    }
172

  
173
    private void setInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
174
        if(taxa.size() == 1){
175
            Taxon taxon = taxa.iterator().next();
176
            Set<TaxonNode> nodes = taxon.getTaxonNodes();
177
            setInputForMultipleNodes(conversation, nodes);
178
        }else if(taxa.size() > 1){
179
            Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
180
            for ( Taxon taxon : taxa ){
181
                taxonNodes.addAll(taxon.getTaxonNodes());
182
            }
183
            setInputForMultipleNodes(conversation, taxonNodes);
184
        }else if(taxa.size() == 0){
185
            // this is an undesired state
186
            MessagingUtils.warningDialog(INCORRECT_STATE, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT);
187
        }
188
    }
189

  
190
    public static TaxonEditorInputE4 NewInstance(UUID taxonNodeUuid) throws Exception {
191
        return new TaxonEditorInputE4(taxonNodeUuid, CdmType.TAXON_NODE);
192

  
193
    }
194

  
195
    public static TaxonEditorInputE4 NewInstanceFromTaxonBase(UUID taxonBaseUuid){
196
        return new TaxonEditorInputE4(taxonBaseUuid, CdmType.TAXON_BASE);
197
    }
198

  
199

  
200
    public static TaxonEditorInputE4 NewEmptyInstance(UUID parentNodeUuid){
201
        return new TaxonEditorInputE4(parentNodeUuid, CdmType.PARENT_TAXON_NODE);
202
    }
203

  
204
    @Override
205
    public boolean exists() {
206
        return taxonNode != null;
207
    }
208

  
209
    @Override
210
    public ImageDescriptor getImageDescriptor() {
211
        return null;
212
    }
213

  
214
    @Override
215
    public String getName() {
216
        if(getTaxon() == null){
217
            return null;
218
        }
219
        TaxonName name = getTaxon().getName();
220
        if (name == null || name.getTitleCache() == null) {
221
            return Messages.TaxonEditorInput_NEW_TAXON;
222
        } else {
223
            return name.getTitleCache();
224
        }
225
    }
226

  
227
    @Override
228
    public IPersistableElement getPersistable() {
229
        return null;
230
    }
231

  
232
    @Override
233
    public String getToolTipText() {
234
        return getName();
235
    }
236

  
237
    /** {@inheritDoc} */
238
    @Override
239
    public Object getAdapter(Class adapter) {
240

  
241
        if (adapter == Taxon.class) {
242
            return getTaxon();
243
        }
244

  
245
        if (adapter == TaxonNode.class) {
246
            return taxonNode;
247
        }
248

  
249
        return null;
250
    }
251

  
252
    /**
253
     * {@inheritDoc}
254
     *
255
     * Overrides equals to ensure that a taxon can only be edited by
256
     * one editor at a time.
257
     */
258
    @Override
259
    public boolean equals(Object obj) {
260
        if (TaxonEditorInputE4.class.equals(obj.getClass())
261
                && getTaxon() != null
262
                && getTaxon().equals(((TaxonEditorInputE4) obj).getTaxon())) {
263
            if (((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase() != null){
264
                setInitiallySelectedTaxonBase(((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase());
265
            }
266
            return true;
267
        }
268
        return false;
269
    }
270

  
271
    public Taxon getTaxon(){
272
        Taxon taxon = CdmBase.deproxy(taxonNode.getTaxon(), Taxon.class);
273
        return taxon;
274
    }
275

  
276
    public TaxonNode getTaxonNode() {
277
        return taxonNode;
278
    }
279

  
280
    @Override
281
    public ConversationHolder getConversationHolder() {
282
        return conversation;
283
    }
284

  
285
    /** {@inheritDoc} */
286
    @Override
287
    public void update(CdmDataChangeMap events) {
288
        if(dataChangeBehavior == null){
289
            dataChangeBehavior = new TaxonEditorInputDataChangeBehaviourE4(this);
290
        }
291

  
292
        DataChangeBridge.handleDataChange(events, dataChangeBehavior);
293
    }
294

  
295
    @Override
296
    public String getFactoryId() {
297
        return TaxonEditorInputFactory.getFactoryId();
298
    }
299

  
300
    /** {@inheritDoc} */
301
    @Override
302
    public void saveState(IMemento memento) {
303
        TaxonEditorInputFactoryE4.saveState(memento, this);
304
    }
305

  
306

  
307
    public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
308
        this.initiallySelectedTaxonBase = taxonBase;
309
    }
310

  
311
    public TaxonBase getInitiallySelectedTaxonBase() {
312
        return initiallySelectedTaxonBase;
313
    }
314

  
315
    @Override
316
    public List<TaxonNode> getRootEntities() {
317
        return Arrays.asList(taxonNode);
318
    }
319

  
320
    @Override
321
    public void merge() {
322
       CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
323

  
324
    }
325

  
326
    @Override
327
    public Map<Object, List<String>> getPropertyPathsMap() {
328
        return null;
329
    }
330

  
331
    private List<String> getTaxonNodePropertyPaths() {
332
        List<String> taxonNodePropertyPaths = new ArrayList<String>();
333
        for(String propertyPath : getTaxonBasePropertyPaths()) {
334
            taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
335
        }
336
        return taxonNodePropertyPaths;
337
    }
338

  
339
    private List<String> getTaxonBasePropertyPaths() {
340
        List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
341
                "sec", //$NON-NLS-1$
342
                "createdBy", //$NON-NLS-1$
343
                "updatedBy", //$NON-NLS-1$
344
                "annotations", //$NON-NLS-1$
345
                "markers", //$NON-NLS-1$
346
                "credits", //$NON-NLS-1$
347
                "extensions", //$NON-NLS-1$
348
                "rights", //$NON-NLS-1$
349
                "sources", //$NON-NLS-1$
350
                "descriptions", //$NON-NLS-1$
351
                "relationsToThisTaxon", //$NON-NLS-1$
352
                "relationsFromThisTaxon", //$NON-NLS-1$
353
                "taxonNodes", //$NON-NLS-1$
354
                "descriptions.descriptionElements.feature", //$NON-NLS-1$
355
                "descriptions.descriptionElements.area", //$NON-NLS-1$
356
                "descriptions.descriptionElements.status", //$NON-NLS-1$
357
                "descriptions.markers", //$NON-NLS-1$
358
                "name.descriptions", //$NON-NLS-1$
359
                "name.typeDesignations", //$NON-NLS-1$
360
                "name.status", //$NON-NLS-1$
361
                "name.nomenclaturalReference.inReference", //$NON-NLS-1$
362
                "name.taxonBases.taxonNodes", //$NON-NLS-1$
363
                "name.relationsFromThisName", //$NON-NLS-1$
364
                "name.relationsToThisName", //$NON-NLS-1$
365
                "name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations.synonym.name.status", //$NON-NLS-1$
366
                "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
367
                "synonymRelations.synonym.name.status.type", //$NON-NLS-1$
368
                "synonymRelations.synonym.name.relationsToThisName.fromName", //$NON-NLS-1$
369
                "synonymRelations.synonym.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
370
                "synonymRelations.synonym.name.nomenclaturalReference.authorship", //$NON-NLS-1$
371
                "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations" //$NON-NLS-1$
372
        });
373

  
374
        return taxonBasePropertyPaths;
375
    }
376

  
377
}
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/e4/TaxonEditorInputFactoryE4.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.e4;
11

  
12
import java.util.UUID;
13

  
14
import org.eclipse.core.runtime.IAdaptable;
15
import org.eclipse.ui.IElementFactory;
16
import org.eclipse.ui.IMemento;
17

  
18
import eu.etaxonomy.cdm.api.service.IClassificationService;
19
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
20
import eu.etaxonomy.taxeditor.model.MessagingUtils;
21
import eu.etaxonomy.taxeditor.store.CdmStore;
22

  
23

  
24
/**
25
 *
26
 * @author pplitzner
27
 * @date Aug 24, 2017
28
 *
29
 */
30
public class TaxonEditorInputFactoryE4 implements IElementFactory {
31

  
32
    /**
33
     * Factory id. The workbench plug-in registers a factory by this name
34
     * with the "org.eclipse.ui.elementFactories" extension point.
35
     */
36
    private static final String ID_FACTORY = "eu.etaxonomy.taxeditor.editor.name.taxonEditorInputFactory"; //$NON-NLS-1$
37

  
38
    /**
39
     * Tag for the IFile.fullPath of the file resource.
40
     */
41
    private static final String TAXON_NODE_UUID = "uuid"; //$NON-NLS-1$
42

  
43
	/** {@inheritDoc} */
44
	@Override
45
    public IAdaptable createElement(IMemento memento) {
46
        // Get the uuid
47
        String taxonNodeUuid = memento.getString(TAXON_NODE_UUID);
48
        if(! CdmStore.isActive()){
49
        	return null;
50
        }
51
        if (taxonNodeUuid == null || CdmStore.getService(IClassificationService.class).getTaxonNodeByUuid(UUID.fromString(taxonNodeUuid)) == null) {
52
        	MessagingUtils.warn(this.getClass(), Messages.TaxonEditorInputFactory_NOT_FOUND_TAXON + taxonNodeUuid);
53
        	return null;
54
        }
55
        try {
56
			return TaxonEditorInputE4.NewInstance(UUID.fromString(taxonNodeUuid));
57
		} catch (Exception e) {
58
			MessagingUtils.warningDialog(Messages.TaxonEditorInputFactory_COULD_NOT_CREATE, TaxonEditorInputFactoryE4.class, e.getMessage());
59
		}
60
		return null;
61
	}
62

  
63
    /**
64
     * Returns the element factory id for this class.
65
     *
66
     * @return the element factory id
67
     */
68
    public static String getFactoryId() {
69
        return ID_FACTORY;
70
    }
71

  
72
    /**
73
     * Saves the state of the given file editor input into the given memento.
74
     *
75
     * @param memento the storage area for element state
76
     * @param input the file editor input
77
     */
78
    public static void saveState(IMemento memento, TaxonEditorInputE4 input) {
79
    	String uuid = input.getTaxonNode().getUuid().toString();
80
        memento.putString(TAXON_NODE_UUID, uuid);
81
    }
82
}
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/name/e4/IDropTargetableE4.java
1
/**
2
 *
3
 */
4
package eu.etaxonomy.taxeditor.editor.name.e4;
5

  
6
import org.eclipse.swt.widgets.Composite;
7

  
8
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
9

  
10
/**
11
 *
12
 * @author pplitzner
13
 * @date Aug 24, 2017
14
 *
15
 */
16
public interface IDropTargetableE4 extends IPostOperationEnabled {
17

  
18
	public Composite getControl();
19

  
20
	public TaxonNameEditorE4 getEditor();
21

  
22
	public void dragEntered();
23

  
24
	public void dragLeft();
25
}
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/name/e4/TaxonNameEditorE4.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;
11

  
12
import java.util.ArrayList;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16

  
17
import javax.annotation.PostConstruct;
18
import javax.annotation.PreDestroy;
19
import javax.inject.Inject;
20

  
21
import org.apache.commons.lang.StringUtils;
22
import org.eclipse.core.commands.operations.IUndoContext;
23
import org.eclipse.core.runtime.IProgressMonitor;
24
import org.eclipse.core.runtime.OperationCanceledException;
25
import org.eclipse.e4.ui.di.Focus;
26
import org.eclipse.e4.ui.di.Persist;
27
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
28
import org.eclipse.e4.ui.services.EMenuService;
29
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
30
import org.eclipse.jface.action.MenuManager;
31
import org.eclipse.jface.viewers.ISelection;
32
import org.eclipse.jface.viewers.ISelectionChangedListener;
33
import org.eclipse.jface.viewers.ISelectionProvider;
34
import org.eclipse.jface.viewers.StructuredSelection;
35
import org.eclipse.swt.graphics.Color;
36
import org.eclipse.swt.widgets.Composite;
37
import org.eclipse.swt.widgets.Menu;
38
import org.eclipse.ui.ISelectionListener;
39
import org.eclipse.ui.ISelectionService;
40
import org.eclipse.ui.IWorkbenchPart;
41
import org.eclipse.ui.IWorkbenchPartReference;
42
import org.eclipse.ui.PartInitException;
43
import org.eclipse.ui.PlatformUI;
44
import org.eclipse.ui.forms.ManagedForm;
45
import org.eclipse.ui.forms.widgets.FormToolkit;
46
import org.eclipse.ui.forms.widgets.ScrolledForm;
47
import org.eclipse.ui.forms.widgets.TableWrapLayout;
48

  
49
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
50
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
51
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
52
import eu.etaxonomy.cdm.model.common.CdmBase;
53
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
54
import eu.etaxonomy.cdm.model.taxon.Taxon;
55
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
56
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
57
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
58
import eu.etaxonomy.taxeditor.editor.ISecuredEditor;
59
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
60
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
61
import eu.etaxonomy.taxeditor.editor.name.container.AbstractGroupedContainer;
62
import eu.etaxonomy.taxeditor.editor.name.e4.container.AbstractGroupE4;
63
import eu.etaxonomy.taxeditor.editor.name.e4.container.AbstractGroupedContainerE4;
64
import eu.etaxonomy.taxeditor.editor.name.e4.container.AcceptedGroupE4;
65
import eu.etaxonomy.taxeditor.editor.name.e4.container.AcceptedNameContainerE4;
66
import eu.etaxonomy.taxeditor.editor.name.e4.container.ContainerFactoryE4;
67
import eu.etaxonomy.taxeditor.editor.name.e4.container.HomotypicalSynonymGroupE4;
68
import eu.etaxonomy.taxeditor.editor.name.e4.container.MisappliedGroupE4;
69
import eu.etaxonomy.taxeditor.model.AbstractUtility;
70
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
71
import eu.etaxonomy.taxeditor.model.IPartChangeListener;
72
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
73
import eu.etaxonomy.taxeditor.model.TaxeditorPartService;
74
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
75
import eu.etaxonomy.taxeditor.preference.Resources;
76
import eu.etaxonomy.taxeditor.security.RequiredPermissions;
77
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
78
import eu.etaxonomy.taxeditor.store.CdmStore;
79

  
80
/**
81
 *
82
 * @author pplitzner
83
 * @date Aug 24, 2017
84
 *
85
 */
86
public class TaxonNameEditorE4 implements
87
		IConversationEnabled, IDirtyMarkable,
88
		IPartContentHasDetails, IPartChangeListener,
89
		ISelectionListener, ISecuredEditor, IPostOperationEnabled {
90

  
91
	private Taxon taxon;
92

  
93
	private ManagedForm managedForm;
94
	private ScrolledForm scrolledForm;
95
	private Composite parent;
96
	private ISelectionProvider simpleSelectionProvider;
97

  
98
	private TaxonBase selection;
99

  
100
	private ConversationHolder conversation;
101

  
102
	private MenuManager menuManager;
103
	private Menu menu;
104

  
105
	private AcceptedGroupE4 acceptedGroup;
106
	private List<HomotypicalSynonymGroupE4> heterotypicSynonymGroups = new ArrayList<>();
107
	private MisappliedGroupE4 misappliedGroup;
108

  
109
	private ISelectionService selectionService;
110

  
111
	private TaxonBase objectAffectedByLastOperation;
112

  
113
	private ICdmEntitySession cdmEntitySession;
114

  
115
	@Inject
116
	private ESelectionService selService;
117

  
118
	@Inject
119
	private MDirtyable dirty;
120

  
121
	private ISelectionChangedListener selectionChangedListener;
122

  
123
    private TaxonEditorInputE4 input;
124

  
125
	@Inject
126
	public TaxonNameEditorE4() {
127
	}
128

  
129

  
130
	@PostConstruct
131
    public void createPartControl(Composite parent, EMenuService menuService) {
132
        if (CdmStore.isActive()){
133
            if(conversation == null){
134
                conversation = CdmStore.createConversation();
135
            }
136
        }
137
        else{
138
            return;
139
        }
140

  
141
		createManagedForm(parent);
142

  
143
		//propagate selection
144
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
145
        //FIXME E4 migrate
146
//        viewer.addSelectionChangedListener(selectionChangedListener);
147

  
148
        //create context menu
149
        menuService.registerContextMenu(managedForm, "eu.etaxonomy.taxeditor.editor.popupmenu.nameeditor");
150

  
151
		TaxeditorPartService.getInstance().addListener(
152
				TaxeditorPartService.PART_ACTIVATED, this);
153

  
154
	}
155

  
156
	protected void createManagedForm(Composite composite) {
157

  
158
		managedForm = new ManagedForm(composite) {
159

  
160
			@Override
161
			public void dirtyStateChanged() {
162
			    dirty.setDirty(true);
163
			    //FIXME E4 migrate
164
//				firePropertyChange(PROP_DIRTY);
165
			}
166

  
167
			@Override
168
			public boolean setInput(Object input) {
169
				if (input instanceof AbstractGroupedContainer) {
170
					selection = ((AbstractGroupedContainer) input).getData();
171

  
172
			        selectionChangedListener = (event -> selService.setSelection(new StructuredSelection(selection)));
173
				}else if(input == null){
174
					selection = null;
175
                    selectionChangedListener = (event -> selService.setSelection(new StructuredSelection()));
176
				}
177

  
178

  
179
				return super.setInput(input);
180
			}
181
		};
182

  
183
		scrolledForm = managedForm.getForm();
184
		parent = scrolledForm.getBody();
185

  
186
		parent.setData(taxon);
187

  
188
		TableWrapLayout layout = new TableWrapLayout();
189
		layout.leftMargin = 0;
190
		layout.rightMargin = 0;
191
		layout.topMargin = 0;
192
		layout.bottomMargin = 0;
193

  
194
		layout.verticalSpacing = 0;
195
		layout.horizontalSpacing = 0;
196

  
197
		parent.setLayout(layout);
198
		parent.setBackground(AbstractUtility
199
				.getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
200
	}
201

  
202
	public void createOrUpdateNameComposites() {
203
		ContainerFactoryE4.createOrUpdateAcceptedTaxonsHomotypicGroup(this);
204
		ContainerFactoryE4.createOrUpdateHeterotypicSynonymyGroups(this);
205
		ContainerFactoryE4.createOrUpdateMisapplicationsGroup(this);
206

  
207

  
208
		// Redraw composite
209
		managedForm.reflow(true);
210
	}
211

  
212
	public Taxon getTaxon() {
213
		return HibernateProxyHelper.deproxy(taxon);
214
	}
215

  
216
	public void setDirty() {
217
		managedForm.dirtyStateChanged();
218
	}
219

  
220
	@Focus
221
	public void setFocus() {
222
	    if(input!=null){
223
	        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
224
	        if (getSelectedContainer() == null) {
225
	            throw new IllegalStateException(
226
	                    Messages.TaxonNameEditor_THERE_SHOULD_ALWAYS_BE);
227
	        }
228
	        getSelectedContainer().setSelected();
229

  
230
	        // check permissions
231
	        boolean doEnable = permissionsSatisfied();
232
	        managedForm.getForm().setEnabled(doEnable);
233
	    }
234
	}
235

  
236
	@Override
237
	public boolean permissionsSatisfied() {
238
		TaxonNode taxonNode = input.getTaxonNode();
239
		boolean doEnable = CdmStore.currentAuthentiationHasPermission(taxonNode, RequiredPermissions.TAXONNODE_EDIT);
240
		return doEnable;
241
	}
242

  
243
	@Override
244
    public ConversationHolder getConversationHolder() {
245
		return conversation;
246
	}
247

  
248
	/** {@inheritDoc} */
249
	@Override
250
    public void update(CdmDataChangeMap events) {
251
		// redraw();
252
	}
253

  
254
	/**
255
	 * Redraws this editor return true on success
256
	 *
257
	 * @return a boolean.
258
	 */
259
    public boolean redraw() {
260
		return redraw(true);
261
	}
262

  
263
	/**
264
	 * {@inheritDoc}
265
	 *
266
	 * Redraws the editor controls
267
	 */
268
    public boolean redraw(boolean focus) {
269

  
270
		createOrUpdateNameComposites();
271

  
272
		if (focus) {
273
			setFocus();
274
		}
275

  
276
		return true;
277
	}
278

  
279
	@Override
280
    public boolean postOperation(CdmBase objectAffectedByOperation) {
281

  
282
		changed(objectAffectedByOperation);
283

  
284
		redraw(false);
285

  
286
		if (objectAffectedByOperation instanceof TaxonBase) {
287
			objectAffectedByLastOperation = (TaxonBase) objectAffectedByOperation;
288
		}
289

  
290
		return true;
291
	}
292

  
293
	public ManagedForm getManagedForm() {
294
		return managedForm;
295
	}
296

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

  
306
	/**
307
	 * <p>
308
	 * checkForEmptyNames
309
	 * </p>
310
	 *
311
	 * @return true if there are empty names
312
	 */
313
	public boolean checkForEmptyNames() {
314
		for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
315
			if (container.getName() == null
316
					|| StringUtils.isEmpty(container.getName().getTitleCache())) {
317
				return true;
318
			}
319
		}
320
		return false;
321
	}
322

  
323
	public Set<AbstractGroupedContainerE4> getEmptyContainers() {
324
		Set<AbstractGroupedContainerE4> containersWithEmptyNames = new HashSet<>();
325

  
326
		for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
327
			if (container.getName() == null
328
					|| StringUtils.isEmpty(container.getName().getTitleCache())) {
329
				containersWithEmptyNames.add(container);
330
			}
331
		}
332

  
333
		return containersWithEmptyNames;
334
	}
335

  
336
	/** {@inheritDoc} */
337
	@Persist
338
	public void doSave(IProgressMonitor monitor) {
339

  
340
		monitor.beginTask(Messages.TaxonNameEditor_SAVING_NAMES, getGroupedContainers().size());
341

  
342
		try {
343
			// check for empty names
344
			for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
345

  
346
				monitor.subTask(Messages.TaxonNameEditor_SAVING_COMPOSITES
347
						+ container.getTaxonBase().getTitleCache());
348
				container.persistName();
349

  
350
				// In case the progress monitor was canceled throw an exception.
351
				if (monitor.isCanceled()) {
352
					throw new OperationCanceledException();
353
				}
354

  
355
				// Otherwise declare this step as done.
356
				monitor.worked(1);
357
			}
358
		} finally {
359

  
360
			// Stop the progress monitor.
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)