Project

General

Profile

Download (7.33 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.ui.section.name;
11

    
12
import java.util.Collection;
13
import java.util.Comparator;
14

    
15
import org.eclipse.core.commands.operations.IOperationHistory;
16
import org.eclipse.core.runtime.IStatus;
17
import org.eclipse.e4.ui.workbench.modeling.EPartService;
18
import org.eclipse.jface.action.Action;
19
import org.eclipse.jface.action.IAction;
20
import org.eclipse.jface.action.ToolBarManager;
21
import org.eclipse.jface.resource.ImageDescriptor;
22
import org.eclipse.jface.wizard.WizardDialog;
23
import org.eclipse.swt.graphics.ImageData;
24

    
25
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
26
import eu.etaxonomy.cdm.api.service.INameService;
27
import eu.etaxonomy.cdm.api.service.UpdateResult;
28
import eu.etaxonomy.cdm.api.service.name.TypeDesignationComparator;
29
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
30
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
31
import eu.etaxonomy.cdm.model.name.Rank;
32
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
33
import eu.etaxonomy.cdm.model.name.TaxonName;
34
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
35
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
36
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
37
import eu.etaxonomy.taxeditor.l10n.Messages;
38
import eu.etaxonomy.taxeditor.model.AbstractUtility;
39
import eu.etaxonomy.taxeditor.model.ImageResources;
40
import eu.etaxonomy.taxeditor.model.MessagingUtils;
41
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
42
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
43
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
44
import eu.etaxonomy.taxeditor.store.CdmStore;
45
import eu.etaxonomy.taxeditor.store.StoreUtil;
46
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
47
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
48
import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection;
49
import eu.etaxonomy.taxeditor.ui.section.ITaxonBaseDetailSection;
50
import eu.etaxonomy.taxeditor.ui.section.name.operation.DeleteTypeDesignationOperation;
51
import eu.etaxonomy.taxeditor.ui.section.name.type.CloneTypeWizard;
52
import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
53

    
54
/**
55
 * @author n.hoffmann
56
 * @created May 17, 2010
57
 */
58
public class TypeDesignationSection extends AbstractEntityCollectionSection<TaxonName, TypeDesignationBase> implements ITaxonBaseDetailSection {
59

    
60
	private TaxonBase taxonBase;
61

    
62
	public TypeDesignationSection(CdmFormFactory formFactory, ConversationHolder conversation,
63
			ICdmFormElement parentElement, int style) {
64
		super(formFactory, conversation, parentElement, Messages.TypeDesignationSection_TYPE_DESIGNATIONS, style);
65
	}
66

    
67
	@Override
68
	protected void addAction(ToolBarManager toolBarManager) {
69
	    if(!isSpecimenType() ||
70
	            getEntity().getTypeDesignations().isEmpty()
71
	            || getEntity().getTypeDesignations().stream().noneMatch(designation->designation instanceof SpecimenTypeDesignation)){
72
	        return;
73
	    }
74
	    Action addAction = new Action(Messages.TypeDesignationSection_CREATE_DUPLICATE, IAction.AS_PUSH_BUTTON){
75
            @Override
76
            public void run() {
77
                CloneTypeWizard wizard = new CloneTypeWizard(getEntity());
78
                WizardDialog dialog = new WizardDialog(getShell(), wizard);
79

    
80
                if (dialog.open() == IStatus.OK) {
81
                    SpecimenTypeDesignation baseTypeDesignation = HibernateProxyHelper.deproxy(wizard.getBaseTypeDesignation());
82
                    UpdateResult result = CdmStore.getService(INameService.class).cloneTypeDesignation(
83
                            wizard.getTaxonName().getUuid(),
84
                            baseTypeDesignation,
85
                            wizard.getAccessionNumber(),
86
                            wizard.getCollection().getUuid(),
87
                            wizard.getTypeStatus());
88
                    if(!result.isOk()){
89
                        MessagingUtils.warningDialog(Messages.TypeDesignationSection_DUPLICATE_FAILED, this, result.getExceptions().toString());
90
                    }
91
                    StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
92
                    internalUpdateSection(true);
93
                }
94
            }
95
        };
96
        addAction.setImageDescriptor(new ImageDescriptor() {
97

    
98
            @Override
99
            public ImageData getImageData() {
100
                return ImageResources.getImage(ImageResources.COPY_ICON).getImageData();
101
            }
102
        });
103
        addAction.setToolTipText(Messages.TypeDesignationSection_CREATE_DUPLICATE);
104

    
105
        toolBarManager.add(addAction);
106
	}
107

    
108
	@Override
109
	public void addElement(TypeDesignationBase element) {
110
		getEntity().addTypeDesignation(element, PreferencesUtil.getBooleanValue(IPreferenceKeys.ADD_TYPES_TO_ALL_NAMES));
111
		updateToolbar();
112
	}
113

    
114
	@Override
115
	public TypeDesignationBase createNewElement() {
116
		if(isSpecimenType()){
117
			return SpecimenTypeDesignation.NewInstance();
118
		}else{
119
			return NameTypeDesignation.NewInstance();
120
		}
121
	}
122

    
123
	@Override
124
	public Collection<TypeDesignationBase> getCollection(TaxonName entity) {
125
		if (entity == null){
126
			return null;
127
		}
128
		return entity.getTypeDesignations();
129
	}
130

    
131
	@Override
132
	public Comparator<TypeDesignationBase> getComparator() {
133
        return new TypeDesignationComparator();
134
	}
135

    
136
	@Override
137
	public String getEmptyString() {
138
		return Messages.TypeDesignationSection_NO_TYPES_YET;
139
	}
140

    
141
	@Override
142
	protected String getTooltipString() {
143
		return Messages.TypeDesignationSection_ADD_TYPE;
144
	}
145

    
146
	@Override
147
	public void removeElement(TypeDesignationBase element) {
148
		boolean removeTypeDesignationFromAllTypifiedNames = PreferencesUtil.getBooleanValue(IPreferenceKeys.ADD_TYPES_TO_ALL_NAMES);
149
		TaxonName entity = removeTypeDesignationFromAllTypifiedNames ? null : getEntity();
150
		DetailsPartE4 detailsView = AbstractUtility.getDetailsView(getFormFactory().getContext().get((EPartService.class)));
151
		if(detailsView!=null
152
		        && detailsView.getSelectionProvidingPart().getObject() instanceof ITaxonEditor
153
		        && detailsView.getSelectionProvidingPart().getObject() instanceof IPostOperationEnabled) {
154
            DeleteTypeDesignationOperation operation = new DeleteTypeDesignationOperation(
155
                    "Remove type designation",
156
                    IOperationHistory.GLOBAL_UNDO_CONTEXT,
157
                    entity,
158
                    element,
159
                    (IPostOperationEnabled) detailsView.getSelectionProvidingPart().getObject(),
160
                    null);
161
            ((ITaxonEditor) detailsView.getSelectionProvidingPart().getObject()).addOperation(operation);
162
		}
163
		if(entity!=null){
164
		    entity.removeTypeDesignation(element);
165
		}
166
		updateToolbar();
167
	}
168

    
169
	@Override
170
    public void setTaxonBase(TaxonBase entity) {
171
		this.taxonBase = entity;
172
		TaxonName name = HibernateProxyHelper.deproxy(entity.getName());
173
		setEntity(name);
174
	}
175

    
176
	private boolean isSpecimenType(){
177
		Rank rank = getEntity().getRank();
178
		if(rank==null){
179
			return false;
180
		}
181
		return rank.isSpecies() || rank.isInfraSpecific();
182
	}
183

    
184
	@Override
185
	public TaxonBase getTaxonBase() {
186
		return taxonBase;
187
	}
188

    
189
    @Override
190
    public TypeDesignationBase addExisting() {
191
        return null;
192
    }
193

    
194
    @Override
195
    public boolean allowAddExisting() {
196
        return false;
197
    }
198
}
(21-21/21)