Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / vocabulary / MeasurementUnitDtoCollectionSection.java
1 /**
2 * Copyright (C) 2015 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 package eu.etaxonomy.taxeditor.ui.section.vocabulary;
10
11 import java.util.Collection;
12 import java.util.Comparator;
13 import java.util.HashSet;
14
15 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
16 import eu.etaxonomy.cdm.model.description.MeasurementUnit;
17 import eu.etaxonomy.cdm.persistence.dto.FeatureDto;
18 import eu.etaxonomy.cdm.persistence.dto.TermDto;
19 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
20 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
21 import eu.etaxonomy.taxeditor.ui.section.occurrence.dna.AbstractUnboundEntityCollectionSection;
22
23 /**
24 * @author pplitzner
25 * @date Apr 27, 2015
26 *
27 */
28 public class MeasurementUnitDtoCollectionSection extends AbstractUnboundEntityCollectionSection<FeatureDto, TermDto> {
29
30 public MeasurementUnitDtoCollectionSection(CdmFormFactory formFactory,
31 ConversationHolder conversation, ICdmFormElement parentElement, int style) {
32 super(formFactory, conversation, parentElement, "Recommended measurement units", style);
33 }
34
35 /* (non-Javadoc)
36 * @see eu.etaxonomy.taxeditor.ui.section.occurrence.dna.AbstractNullEntityCollectionSection#getEntityCollection(java.lang.Object)
37 */
38 /** {@inheritDoc} */
39 @Override
40 protected Collection<TermDto> getEntityCollection(FeatureDto entity) {
41 if (entity.getRecommendedMeasurementUnits() == null){
42 entity.setRecommendedMeasurementUnits(new HashSet<TermDto>());
43 }
44 return entity.getRecommendedMeasurementUnits();
45 }
46
47 @Override
48 public Comparator<TermDto> getComparator() {
49 return new Comparator<TermDto>() {
50 @Override
51 public int compare(TermDto o1, TermDto o2) {
52 if(o1==null){
53 return -1;
54 }
55 if(o2==null){
56 return 1;
57 }
58 int diff = o1.getTitleCache().compareTo(o2.getTitleCache());
59 if(diff==0){
60 diff = o1.getUuid().compareTo(o2.getUuid());
61 }
62 return diff;
63 }
64 };
65 }
66
67 /* (non-Javadoc)
68 * @see eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection#createNewElement()
69 */
70 /** {@inheritDoc} */
71 @Override
72 public TermDto createNewElement() {
73 return TermDto.fromTerm(MeasurementUnit.NewInstance());
74 }
75
76 /* (non-Javadoc)
77 * @see eu.etaxonomy.taxeditor.section.AbstractEntityCollectionSection#addElement(eu.etaxonomy.cdm.model.common.IVersionableEntity)
78 */
79 /** {@inheritDoc} */
80 @Override
81 public void addElement(TermDto element) {
82 //never gets called
83 }
84
85 /* (non-Javadoc)
86 * @see eu.etaxonomy.taxeditor.section.AbstractEntityCollectionSection#removeElement(eu.etaxonomy.cdm.model.common.IVersionableEntity)
87 */
88 /** {@inheritDoc} */
89 @Override
90 public void removeElement(TermDto element) {
91 if (element == null){
92 return;
93 }
94 TermDto remove = null;
95 for (TermDto unit: getEntity().getRecommendedMeasurementUnits()){
96 if (unit.getUuid().equals(element.getUuid())){
97 remove = unit;
98 break;
99 }
100 }
101 getEntity().getRecommendedMeasurementUnits().remove(remove);
102 }
103
104 /* (non-Javadoc)
105 * @see eu.etaxonomy.taxeditor.section.AbstractEntityCollectionSection#getEmptyString()
106 */
107 /** {@inheritDoc} */
108 @Override
109 public String getEmptyString() {
110 return "No recommended measurement units yet.";
111 }
112
113 /* (non-Javadoc)
114 * @see eu.etaxonomy.taxeditor.section.AbstractEntityCollectionSection#getTooltipString()
115 */
116 /** {@inheritDoc} */
117 @Override
118 protected String getTooltipString() {
119 return "Add a measurement unit";
120 }
121
122 /**
123 * {@inheritDoc}
124 */
125 @Override
126 public TermDto addExisting() {
127 // TODO Auto-generated method stub
128 return null;
129 }
130
131 /**
132 * {@inheritDoc}
133 */
134 @Override
135 public boolean allowAddExisting() {
136 // TODO Auto-generated method stub
137 return false;
138 }
139
140 }