Project

General

Profile

Download (10.5 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.TypeDesignationStatusComparator;
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
                    if(!result.isOk()){
123
                        MessagingUtils.warningDialog(Messages.TypeDesignationSection_DUPLICATE_FAILED, this, result.getExceptions().toString());
124
                    }
125
                    StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
126
                    internalUpdateSection(false);
127
                }
128
            }
129
        };
130
        cloneAction.setImageDescriptor(new ImageDescriptor() {
131

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

    
139
        toolBarManager.add(cloneAction);
140
	}
141

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

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

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

    
165
	@Override
166
	public Comparator<TypeDesignationBase> getComparator() {
167
        return new Comparator<TypeDesignationBase>(){
168

    
169
            //TODO: this is only a workaround to avoid a cdmlib hotfix this needs to be reverted for next release!!
170
            private TypeDesignationStatusComparator statusComparator = new TypeDesignationStatusComparator();
171

    
172
            @SuppressWarnings("unchecked")
173
            @Override
174
            public int compare(TypeDesignationBase o1, TypeDesignationBase o2) {
175

    
176
                if (o1 == null && o2 == null){
177
                    return 0;
178
                }
179
                if(o1==null){
180
                    return 1;
181
                }
182
                if(o2==null){
183
                    return -1;
184
                }
185
                int result = 0;
186
                if(o1.getTypeStatus()==null ){
187
                    if (o2.getTypeStatus() != null){
188
                        return 1;
189
                    }
190
                }
191
                if (o2.getTypeStatus() == null){
192
                    return -1;
193
                }
194
                if(result == 0 && (o1.getTypeStatus() == null || o2.getTypeStatus() == null)){
195
                    return o1.getUuid().compareTo(o2.getUuid());
196
                }
197
                return statusComparator.compare(o1.getTypeStatus(), o2.getTypeStatus()) ;
198
            }
199
        };
200
	}
201

    
202
	@Override
203
	public String getEmptyString() {
204
		return Messages.TypeDesignationSection_NO_TYPES_YET;
205
	}
206

    
207
	@Override
208
	protected String getTooltipString() {
209
		return Messages.TypeDesignationSection_ADD_TYPE;
210
	}
211

    
212
	@Override
213
	public void removeElement(TypeDesignationBase element) {
214
	    DetailsPartE4 detailsView = AbstractUtility.getDetailsView(getFormFactory().getContext().get((EPartService.class)));
215
	    if(detailsView!=null
216
	            && detailsView.getSelectionProvidingPart().getObject() instanceof IE4SavablePart
217
	            && StoreUtil.promptCheckIsDirty(((IE4SavablePart)detailsView.getSelectionProvidingPart().getObject()))){
218
	        return;
219
	    }
220
		boolean removeTypeDesignationFromAllTypifiedNames = PreferencesUtil.getBooleanValue(IPreferenceKeys.ADD_TYPES_TO_ALL_NAMES);
221
		TaxonName entity = removeTypeDesignationFromAllTypifiedNames ? null : getEntity();
222
		if(!element.isPersited() && entity!=null){
223
            entity.removeTypeDesignation(element);
224
		}
225
		else if(detailsView!=null
226
		        && detailsView.getSelectionProvidingPart().getObject() instanceof ITaxonEditor
227
		        && detailsView.getSelectionProvidingPart().getObject() instanceof IPostOperationEnabled) {
228
            DeleteTypeDesignationOperation operation = new DeleteTypeDesignationOperation(
229
                    "Remove type designation",
230
                    IOperationHistory.GLOBAL_UNDO_CONTEXT,
231
                    entity,
232
                    element,
233
                    (IPostOperationEnabled) detailsView.getSelectionProvidingPart().getObject(),
234
                    null);
235
            ((ITaxonEditor) detailsView.getSelectionProvidingPart().getObject()).addOperation(operation);
236
            if(entity!=null){
237
                entity.removeTypeDesignation(element);
238
            }
239
		}
240
		updateToolbar();
241
	}
242

    
243
	@Override
244
    public void setTaxonBase(TaxonBase entity) {
245
		this.taxonBase = entity;
246
		TaxonName name = HibernateProxyHelper.deproxy(entity.getName());
247
		setEntity(name);
248
	}
249

    
250
	private boolean isSpecimenType(){
251
		Rank rank = getEntity() == null ? null : getEntity().getRank();
252
		if(rank == null){
253
			return false;
254
		}
255
		return rank.isSpecies() || rank.isInfraSpecific();
256
	}
257

    
258
	@Override
259
	public TaxonBase getTaxonBase() {
260
		return taxonBase;
261
	}
262

    
263
    @Override
264
    public TypeDesignationBase addExisting() {
265
        return null;
266
    }
267

    
268
    @Override
269
    public boolean allowAddExisting() {
270
        return false;
271
    }
272
}
(21-21/21)