2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.definedterm
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
17 import org
.eclipse
.jface
.viewers
.TreeNodeContentProvider
;
19 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
20 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermDto
;
21 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermVocabularyDto
;
22 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
30 public class TermDtoContentProvider
extends TreeNodeContentProvider
{
32 private Map
<TermVocabularyDto
, Collection
<TermDto
>> vocabularyToChildTermMap
= new HashMap
<>();
35 public Object
[] getElements(Object inputElement
) {
36 Collection
<TermVocabularyDto
> inputElements
= (Collection
<TermVocabularyDto
>) inputElement
;
37 return inputElements
.toArray();
41 public Object
[] getChildren(Object parentElement
) {
42 Collection
<TermDto
> children
= new HashSet
<>();
43 if(parentElement
instanceof TermVocabularyDto
){
44 children
.addAll(getChildTerms((TermVocabularyDto
)parentElement
));
45 } else if(parentElement
instanceof TermDto
){
46 if(((TermDto
) parentElement
).getIncludes()!=null){
47 children
.addAll(((TermDto
) parentElement
).getIncludes());
49 if(((TermDto
) parentElement
).getGeneralizationOf()!=null){
50 children
.addAll(((TermDto
) parentElement
).getGeneralizationOf());
53 return children
.toArray();
57 public Object
getParent(Object element
) {
58 if(element
instanceof TermDto
){
59 TermDto termDto
= (TermDto
) element
;
60 TermDto partOfDto
= termDto
.getPartOfDto();
64 TermDto kindOfDto
= termDto
.getKindOfDto();
68 TermVocabularyDto vocabularyDto
= termDto
.getVocabularyDto();
69 if(vocabularyDto
!=null){
72 //parent is the vocabulary
73 return new TermVocabularyDto(termDto
.getVocabularyUuid(), null, termDto
.getTermType());
80 public boolean hasChildren(Object element
) {
81 if(element
instanceof TermVocabularyDto
){
82 return getChildren(element
).length
> 0;
84 if (getChildren(element
) != null){
85 return getChildren(element
).length
> 0;
90 public Collection
<?
extends TermDto
> getChildTerms(TermVocabularyDto voc
) {
91 Collection
<TermDto
> children
= vocabularyToChildTermMap
.get(voc
);
93 children
= new ArrayList
<>(CdmStore
.getService(IVocabularyService
.class).getCompleteTermHierarchy(voc
));
94 vocabularyToChildTermMap
.put(voc
, children
);
99 public void removeVocabularyFromCache(TermVocabularyDto voc
){
100 vocabularyToChildTermMap
.remove(voc
);