From 9bdb03b150af2a9a67ec649f69105514f865f2df Mon Sep 17 00:00:00 2001 From: Patrick Plitzner Date: Thu, 17 Mar 2016 16:23:05 +0100 Subject: [PATCH] Sort description element sources #3185 --- .../DescriptionElementSourceSection.java | 155 +++++++++++++----- 1 file changed, 111 insertions(+), 44 deletions(-) diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/DescriptionElementSourceSection.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/DescriptionElementSourceSection.java index 1546ffa8e..708236672 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/DescriptionElementSourceSection.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/DescriptionElementSourceSection.java @@ -3,71 +3,138 @@ */ package eu.etaxonomy.taxeditor.ui.section.description; +import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; import eu.etaxonomy.cdm.api.conversation.ConversationHolder; import eu.etaxonomy.cdm.model.common.OriginalSourceType; import eu.etaxonomy.cdm.model.description.DescriptionElementBase; import eu.etaxonomy.cdm.model.description.DescriptionElementSource; +import eu.etaxonomy.cdm.model.reference.Reference; import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory; import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement; import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection; /** - *

DescriptionElementSourceSection class.

+ *

+ * DescriptionElementSourceSection class. + *

* * @author n.hoffmann * @created Nov 17, 2009 * @version 1.0 */ -public class DescriptionElementSourceSection extends AbstractEntityCollectionSection{ +public class DescriptionElementSourceSection extends + AbstractEntityCollectionSection { - /** - *

Constructor for DescriptionElementSourceSection.

- * - * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object. - * @param style a int. - * @param cdmFormFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object. - * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object. - */ - public DescriptionElementSourceSection(CdmFormFactory cdmFormFactory, ConversationHolder conversation, ICdmFormElement parentElement, int style){ - super(cdmFormFactory, conversation, parentElement, "References", style); - } + /** + *

+ * Constructor for DescriptionElementSourceSection. + *

+ * + * @param parentElement + * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} + * object. + * @param style + * a int. + * @param cdmFormFactory + * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} + * object. + * @param conversation + * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} + * object. + */ + public DescriptionElementSourceSection(CdmFormFactory cdmFormFactory, ConversationHolder conversation, + ICdmFormElement parentElement, int style) { + super(cdmFormFactory, conversation, parentElement, "References", style); + } - /** {@inheritDoc} */ - @Override - public DescriptionElementSource createNewElement() { - return DescriptionElementSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource); - } + /** {@inheritDoc} */ + @Override + public DescriptionElementSource createNewElement() { + return DescriptionElementSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource); + } - /** {@inheritDoc} */ - @Override - public Collection getCollection( - DescriptionElementBase entity) { - return entity.getSources(); - } + /** {@inheritDoc} */ + @Override + public Collection getCollection(DescriptionElementBase entity) { + List sources = new ArrayList(entity.getSources()); + Collections.sort(sources, new Comparator() { - /** {@inheritDoc} */ - @Override - public String getEmptyString() { - return "No references yet."; - } + @Override + public int compare(DescriptionElementSource o1, DescriptionElementSource o2) { + int id1 = o1.getId(); + int id2 = o2.getId(); + OriginalSourceType type1 = o1.getType(); + OriginalSourceType type2 = o2.getType(); + Reference citation1 = o1.getCitation(); + Reference citation2 = o2.getCitation(); - /** {@inheritDoc} */ - @Override - protected String getTooltipString() { - return "Create a new reference"; - } + // the newly created should always be on top + if (id1 == 0) { + return -1; + } + if (id2 == 0) { + return 1; + } - /** {@inheritDoc} */ - @Override - public void addElement(DescriptionElementSource element) { - getEntity().addSource(element); - } + // sort by type (Primary taxonomic > Primary Media > others + // alphabetically by reference title cache) + if (type1 != null && type1.equals(OriginalSourceType.PrimaryTaxonomicSource)) { + return -1; + } + if (type1 != null && type1.equals(OriginalSourceType.PrimaryMediaSource) + && type2!=null && !type2.equals(OriginalSourceType.PrimaryTaxonomicSource)) { + return -1; + } + if (type2 != null && type2.equals(OriginalSourceType.PrimaryTaxonomicSource)) { + return 1; + } + if (type2 != null && type2.equals(OriginalSourceType.PrimaryMediaSource) + && type1!=null && !type1.equals(OriginalSourceType.PrimaryTaxonomicSource)) { + return 1; + } - /** {@inheritDoc} */ - @Override - public void removeElement(DescriptionElementSource element) { - getEntity().removeSource(element); - } + //sort by citation title cache + if(citation1==null){ + return -1; + } + if(citation2==null){ + return 1; + } + if(citation1!=null && citation2!=null){ + return citation1.getTitleCache().compareTo(citation2.getTitleCache()); + } + return 0; + } + }); + return sources; + } + + /** {@inheritDoc} */ + @Override + public String getEmptyString() { + return "No references yet."; + } + + /** {@inheritDoc} */ + @Override + protected String getTooltipString() { + return "Create a new reference"; + } + + /** {@inheritDoc} */ + @Override + public void addElement(DescriptionElementSource element) { + getEntity().addSource(element); + } + + /** {@inheritDoc} */ + @Override + public void removeElement(DescriptionElementSource element) { + getEntity().removeSource(element); + } } -- 2.34.1