Project

General

Profile

Download (9.37 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.compare.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.TextualTypeDesignation;
35
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
36
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
37
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
38
import eu.etaxonomy.taxeditor.l10n.Messages;
39
import eu.etaxonomy.taxeditor.model.AbstractUtility;
40
import eu.etaxonomy.taxeditor.model.ImageResources;
41
import eu.etaxonomy.taxeditor.model.MessagingUtils;
42
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
43
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
44
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
45
import eu.etaxonomy.taxeditor.store.CdmStore;
46
import eu.etaxonomy.taxeditor.store.StoreUtil;
47
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
48
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
49
import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection;
50
import eu.etaxonomy.taxeditor.ui.section.ITaxonBaseDetailSection;
51
import eu.etaxonomy.taxeditor.ui.section.name.operation.DeleteTypeDesignationOperation;
52
import eu.etaxonomy.taxeditor.ui.section.name.type.CloneTypeWizard;
53
import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
54
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
55

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

    
62
	private TaxonBase<?> taxonBase;
63

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

    
69
	@Override
70
	protected void addAction(ToolBarManager toolBarManager) {
71

    
72
	    // add textual type designation button
73
	    Action addTextualTypeDesignationAction = new Action("Add", IAction.AS_PUSH_BUTTON){
74
	        @Override
75
	        public void run() {
76
	            TextualTypeDesignation textualTypeDesignation = TextualTypeDesignation.NewInstance();
77
	            addElement(textualTypeDesignation);
78
	            if(! getSection().isExpanded()) {
79
	                getSection().setExpanded(true);
80
	            }
81
	            internalUpdateSection(true);
82
	        }
83
	    };
84
	    addTextualTypeDesignationAction.setImageDescriptor(new ImageDescriptor() {
85

    
86
	        @Override
87
	        public ImageData getImageData() {
88
	            return ImageResources.getImage(ImageResources.ADD_TEXT).getImageData();
89
	        }
90
	    });
91
	    addTextualTypeDesignationAction.setToolTipText("Add textual type designation");
92
	    toolBarManager.add(addTextualTypeDesignationAction);
93

    
94
        // clone button
95
	    if(!isSpecimenType() ||
96
	            getEntity().getTypeDesignations().isEmpty()
97
	            || getEntity().getTypeDesignations().stream().noneMatch(designation->designation instanceof SpecimenTypeDesignation)){
98
	        return;
99
	    }
100
	    Action cloneAction = new Action(Messages.TypeDesignationSection_CREATE_DUPLICATE, IAction.AS_PUSH_BUTTON){
101
            @Override
102
            public void run() {
103
                DetailsPartE4 detailsView = AbstractUtility.getDetailsView(getFormFactory().getContext().get((EPartService.class)));
104
                if(detailsView!=null
105
                        && detailsView.getSelectionProvidingPart().getObject() instanceof IE4SavablePart
106
                        && StoreUtil.promptCheckIsDirty(((IE4SavablePart)detailsView.getSelectionProvidingPart().getObject()))){
107
                    return;
108
                }
109
                CloneTypeWizard wizard = new CloneTypeWizard(getEntity());
110
                WizardDialog dialog = new WizardDialog(getShell(), wizard);
111

    
112
                if (dialog.open() == IStatus.OK) {
113
                    SpecimenTypeDesignation baseTypeDesignation = HibernateProxyHelper.deproxy(wizard.getBaseTypeDesignation());
114
                    UpdateResult result = CdmStore.getService(INameService.class).cloneTypeDesignation(
115
                            wizard.getTaxonName().getUuid(),
116
                            baseTypeDesignation,
117
                            wizard.getAccessionNumber(),
118
                            wizard.getBarcode(),
119
                            wizard.getCatalogNumber(),
120
                            wizard.getCollection().getUuid(),
121
                            wizard.getTypeStatus(),
122
                            wizard.getStableIdentifier());
123
                    if(!result.isOk()){
124
                        MessagingUtils.warningDialog(Messages.TypeDesignationSection_DUPLICATE_FAILED, this, result.getExceptions().toString());
125
                    }
126
                    StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
127
                    internalUpdateSection(false);
128
                }
129
            }
130
        };
131
        cloneAction.setImageDescriptor(new ImageDescriptor() {
132

    
133
            @Override
134
            public ImageData getImageData() {
135
                return ImageResources.getImage(ImageResources.ADD_DOUBLE).getImageData();
136
            }
137
        });
138
        cloneAction.setToolTipText(Messages.TypeDesignationSection_CREATE_DUPLICATE);
139

    
140
        toolBarManager.add(cloneAction);
141
	}
142

    
143
	@Override
144
	public void addElement(TypeDesignationBase element) {
145
	    getEntity().addTypeDesignation(element, PreferencesUtil.getBooleanValue(IPreferenceKeys.ADD_TYPES_TO_ALL_NAMES));
146
		updateToolbar();
147
	}
148

    
149
	@Override
150
	public TypeDesignationBase createNewElement() {
151
		if(isSpecimenType()){
152
			return SpecimenTypeDesignation.NewInstance();
153
		}else{
154
			return NameTypeDesignation.NewInstance();
155
		}
156
	}
157

    
158
	@Override
159
	public Collection<TypeDesignationBase> getCollection(TaxonName entity) {
160
		if (entity == null){
161
			return null;
162
		}
163
		return entity.getTypeDesignations();
164
	}
165

    
166
	@Override
167
	public Comparator<TypeDesignationBase> getComparator() {
168
	    return new TypeDesignationComparator();
169
	}
170

    
171
	@Override
172
	public String getEmptyString() {
173
		return Messages.TypeDesignationSection_NO_TYPES_YET;
174
	}
175

    
176
	@Override
177
	protected String getTooltipString() {
178
		return Messages.TypeDesignationSection_ADD_TYPE;
179
	}
180

    
181
	@Override
182
	public void removeElement(TypeDesignationBase element) {
183
	    DetailsPartE4 detailsView = AbstractUtility.getDetailsView(getFormFactory().getContext().get((EPartService.class)));
184
	    if(detailsView!=null
185
	            && detailsView.getSelectionProvidingPart().getObject() instanceof IE4SavablePart
186
	            && StoreUtil.promptCheckIsDirty(((IE4SavablePart)detailsView.getSelectionProvidingPart().getObject()))){
187
	        return;
188
	    }
189
		boolean removeTypeDesignationFromAllTypifiedNames = PreferencesUtil.getBooleanValue(IPreferenceKeys.ADD_TYPES_TO_ALL_NAMES);
190
		TaxonName entity = removeTypeDesignationFromAllTypifiedNames ? null : getEntity();
191
		if(!element.isPersited() && entity!=null){
192
            entity.removeTypeDesignation(element);
193
		}
194
		else if(detailsView!=null
195
		        && detailsView.getSelectionProvidingPart().getObject() instanceof ITaxonEditor
196
		        && detailsView.getSelectionProvidingPart().getObject() instanceof IPostOperationEnabled) {
197
            DeleteTypeDesignationOperation operation = new DeleteTypeDesignationOperation(
198
                    "Remove type designation",
199
                    IOperationHistory.GLOBAL_UNDO_CONTEXT,
200
                    entity,
201
                    element,
202
                    (IPostOperationEnabled) detailsView.getSelectionProvidingPart().getObject(),
203
                    null);
204
            ((ITaxonEditor) detailsView.getSelectionProvidingPart().getObject()).addOperation(operation);
205
            if(entity!=null){
206
                entity.removeTypeDesignation(element);
207
            }
208
		}
209
		updateToolbar();
210
	}
211

    
212
	@Override
213
    public void setTaxonBase(TaxonBase entity) {
214
		this.taxonBase = entity;
215
		TaxonName name = HibernateProxyHelper.deproxy(entity.getName());
216
		setEntity(name);
217
	}
218

    
219
	private boolean isSpecimenType(){
220
		Rank rank = getEntity() == null ? null : getEntity().getRank();
221
		if(rank == null){
222
			return false;
223
		}
224
		return rank.isSpecies() || rank.isInfraSpecific();
225
	}
226

    
227
	@Override
228
	public TaxonBase getTaxonBase() {
229
		return taxonBase;
230
	}
231

    
232
    @Override
233
    public TypeDesignationBase addExisting() {
234
        return null;
235
    }
236

    
237
    @Override
238
    public boolean allowAddExisting() {
239
        return false;
240
    }
241
}
(21-21/21)