adapt master to develop
[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.model.description.MeasurementUnit;
16 import eu.etaxonomy.cdm.persistence.dto.FeatureDto;
17 import eu.etaxonomy.cdm.persistence.dto.TermDto;
18 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
19 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
20 import eu.etaxonomy.taxeditor.ui.section.occurrence.dna.AbstractUnboundEntityCollectionSection;
21
22 /**
23 * @author pplitzner
24 * @date Apr 27, 2015
25 */
26 public class MeasurementUnitDtoCollectionSection extends AbstractUnboundEntityCollectionSection<FeatureDto, TermDto> {
27
28 public MeasurementUnitDtoCollectionSection(CdmFormFactory formFactory,
29 ICdmFormElement parentElement, int style) {
30 super(formFactory, parentElement, "Recommended measurement units", style);
31 }
32
33 @Override
34 protected Collection<TermDto> getEntityCollection(FeatureDto entity) {
35 if (entity.getRecommendedMeasurementUnits() == null){
36 entity.setRecommendedMeasurementUnits(new HashSet<TermDto>());
37 }
38 return entity.getRecommendedMeasurementUnits();
39 }
40
41 @Override
42 public Comparator<TermDto> getComparator() {
43 return new Comparator<TermDto>() {
44 @Override
45 public int compare(TermDto o1, TermDto o2) {
46 if(o1==null){
47 return -1;
48 }
49 if(o2==null){
50 return 1;
51 }
52 int diff = o1.getTitleCache().compareTo(o2.getTitleCache());
53 if(diff==0){
54 diff = o1.getUuid().compareTo(o2.getUuid());
55 }
56 return diff;
57 }
58 };
59 }
60
61 @Override
62 public TermDto createNewElement() {
63 return TermDto.fromTerm(MeasurementUnit.NewInstance());
64 }
65
66 @Override
67 public void addElement(TermDto element) {
68 //never gets called
69 }
70
71 @Override
72 public void removeElement(TermDto element) {
73 if (element == null){
74 return;
75 }
76 TermDto remove = null;
77 for (TermDto unit: getEntity().getRecommendedMeasurementUnits()){
78 if (unit.getUuid().equals(element.getUuid())){
79 remove = unit;
80 break;
81 }
82 }
83 getEntity().getRecommendedMeasurementUnits().remove(remove);
84 }
85
86 @Override
87 public String getEmptyString() {
88 return "No recommended measurement units yet.";
89 }
90
91 @Override
92 protected String getTooltipString() {
93 return "Add a measurement unit";
94 }
95
96 @Override
97 public TermDto addExisting() {
98 // TODO Auto-generated method stub
99 return null;
100 }
101
102 @Override
103 public boolean allowAddExisting() {
104 // TODO Auto-generated method stub
105 return false;
106 }
107 }