ref #8263 Cache child terms in content provider
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 8 May 2019 14:36:22 +0000 (16:36 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 8 May 2019 14:42:30 +0000 (16:42 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/editor/definedterm/TermDtoContentProvider.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/editor/definedterm/e4/DefinedTermEditorE4.java

index 6b32392..bb77dd5 100644 (file)
@@ -8,8 +8,11 @@
 */
 package eu.etaxonomy.taxeditor.editor.definedterm;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 
 import org.eclipse.jface.viewers.TreeNodeContentProvider;
 
@@ -26,7 +29,9 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
  */
 public class TermDtoContentProvider extends TreeNodeContentProvider {
 
-       @Override
+    private Map<TermVocabularyDto, Collection<TermDto>> vocabularyToChildTermMap = new HashMap<>();
+
+    @Override
        public Object[] getElements(Object inputElement) {
                Collection<TermVocabularyDto> inputElements = (Collection<TermVocabularyDto>) inputElement;
                return inputElements.toArray();
@@ -36,7 +41,7 @@ public class TermDtoContentProvider extends TreeNodeContentProvider {
        public Object[] getChildren(Object parentElement) {
            Collection<TermDto> children = new HashSet<>();
            if(parentElement instanceof TermVocabularyDto){
-               children.addAll(CdmStore.getService(IVocabularyService.class).getCompleteTermHierarchy((TermVocabularyDto)parentElement));
+               children.addAll(getChildTerms((TermVocabularyDto)parentElement));
            } else if(parentElement instanceof TermDto){
                    if(((TermDto) parentElement).getIncludes()!=null){
                        children.addAll(((TermDto) parentElement).getIncludes());
@@ -82,4 +87,17 @@ public class TermDtoContentProvider extends TreeNodeContentProvider {
                return false;
        }
 
+    public Collection<? extends TermDto> getChildTerms(TermVocabularyDto voc) {
+        Collection<TermDto> children = vocabularyToChildTermMap.get(voc);
+        if(children==null){
+            children = new ArrayList<>(CdmStore.getService(IVocabularyService.class).getCompleteTermHierarchy(voc));
+            vocabularyToChildTermMap.put(voc, children);
+        }
+        return children;
+    }
+
+    public void removeVocabularyFromCache(TermVocabularyDto voc){
+        vocabularyToChildTermMap.remove(voc);
+    }
+
 }
index 1b68f88..5961f5a 100644 (file)
@@ -96,6 +96,8 @@ IPartContentHasDetails, IPartContentHasSupplementalData, IE4SavablePart, IContex
 
     private Set<TermBase> changedTerms = new HashSet<>();
 
+    private TermDtoContentProvider contentProvider;
+
        @Inject
        public DefinedTermEditorE4() {
            CdmStore.getContextManager().addContextListener(this);
@@ -120,7 +122,8 @@ IPartContentHasDetails, IPartContentHasSupplementalData, IE4SavablePart, IContex
            parent.setLayout(layout);
            viewer = new TreeViewer(new Tree(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.MULTI));
            viewer.getControl().setLayoutData(LayoutConstants.FILL());
-           viewer.setContentProvider(new TermDtoContentProvider());
+           contentProvider = new TermDtoContentProvider();
+        viewer.setContentProvider(contentProvider);
            viewer.setLabelProvider(new TermDtoLabelProvider());
            viewer.setComparator(new DefinedTermSorter());
 
@@ -148,9 +151,11 @@ IPartContentHasDetails, IPartContentHasSupplementalData, IE4SavablePart, IContex
            if(objectAffectedByOperation instanceof Collection){
                for (Object o : (Collection<?>)objectAffectedByOperation) {
                 if(o instanceof TermVocabularyDto){
+                    contentProvider.removeVocabularyFromCache((TermVocabularyDto) o);
                     viewer.refresh(o);
                 }
                 else if(o instanceof TermDto){
+                    contentProvider.removeVocabularyFromCache(((TermDto) o).getVocabularyDto());
                     viewer.refresh(((TermDto) o).getVocabularyDto());
                     viewer.refresh(o);
                     itemsToSelect.add((TermDto) o);
@@ -158,12 +163,14 @@ IPartContentHasDetails, IPartContentHasSupplementalData, IE4SavablePart, IContex
             }
            }
            if(objectAffectedByOperation instanceof TermVocabularyDto){
+               contentProvider.removeVocabularyFromCache((TermVocabularyDto) objectAffectedByOperation);
                viewer.refresh();
                itemsToSelect.add((AbstractTermDto) objectAffectedByOperation);
            }
            else if(objectAffectedByOperation instanceof TermDto){
                TermDto termDto = (TermDto) objectAffectedByOperation;
             itemsToSelect.add(termDto);
+            contentProvider.removeVocabularyFromCache(termDto.getVocabularyDto());
                viewer.refresh(termDto.getVocabularyDto());
            }
            else{