Project

General

Profile

Download (6.05 KB) Statistics
| Branch: | Tag: | Revision:
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.view;
10

    
11
import java.util.ArrayList;
12
import java.util.HashMap;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Set;
17

    
18
import org.eclipse.core.commands.Command;
19
import org.eclipse.core.runtime.IConfigurationElement;
20
import org.eclipse.core.runtime.IExtensionRegistry;
21
import org.eclipse.core.runtime.Platform;
22
import org.eclipse.e4.core.commands.ECommandService;
23
import org.eclipse.e4.core.commands.EHandlerService;
24
import org.eclipse.jface.viewers.IStructuredSelection;
25
import org.eclipse.jface.viewers.TreeNode;
26

    
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.ICdmBase;
29
import eu.etaxonomy.cdm.persistence.dto.ReferencingObjectDto;
30
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
31
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
32
import eu.etaxonomy.taxeditor.model.MessagingUtils;
33

    
34
/**
35
 * Scans eu.etaxonomy.taxeditor.store.cdmViewer extension point.
36
 * @author pplitzner
37
 * @date Jul 7, 2015
38
 */
39
public class CdmViewerUtilE4 {
40

    
41
    /**
42
     * Returns a map of available commands to open the given input. Keys are the
43
     * command IDs and values are their string representations.
44
     *
45
     * @param input
46
     *            the object which should be handled by the available commands
47
     * @return a key-value map of available commands and their string
48
     *         representation
49
     */
50
    public static Map<Command, String> getAvailableViewers(List<UuidAndTitleCache<? extends ICdmBase>> input, ECommandService commandService,
51
            EHandlerService handlerService){
52

    
53
        Map<Command, String> commandViewerNameMap = new HashMap<>();
54

    
55
        Set<Class<?>> inputClasses = new HashSet<>();
56
        if(input!=null){
57
            input.stream()
58
                .filter(uuid->uuid.getType()!= null)
59
                .forEach(uuid->inputClasses.add(uuid.getType()));
60

    
61
            inputClasses.forEach(ic->{
62
                IExtensionRegistry reg = Platform.getExtensionRegistry();
63
                IConfigurationElement[] extensions = reg
64
                        .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer"); //$NON-NLS-1$
65
                for (IConfigurationElement configElement : extensions) {
66
                    try {
67
                        if(configElement.getName().equals("viewCommandMapping")){ //$NON-NLS-1$
68
                            String commandId = configElement.getAttribute("commandId"); //$NON-NLS-1$
69
                            String viewerName = configElement.getAttribute("viewerName"); //$NON-NLS-1$
70
                            Class<?> selectionClass = Class.forName(configElement.getAttribute("selection")); //$NON-NLS-1$
71
                            if(selectionClass!=null
72
                                    && commandId!=null
73
                                    && viewerName!=null
74
                                    && selectionClass.isAssignableFrom(ic)){
75
                                Command command = commandService.getCommand(commandId);
76
                                commandViewerNameMap.put(command, viewerName);
77
                            }
78
                        }
79
                    } catch (ClassNotFoundException e) {
80
                        MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e); //$NON-NLS-1$
81
                    }
82
                }
83
            });
84
        }
85

    
86
        return commandViewerNameMap;
87
    }
88

    
89
    /**
90
     * Trys to create a List of {@link UuidAndTitleCache} from the selection.<BR>
91
     * Selections which are not recognized are not included in the list.<BR>
92
     * As a minimum each returned element contains the type and the uuid.
93
     * If available also the label and the CdmBase entity is added (later by using the subclass {@link DtoWithEntity}.
94
     * Label creation is not forced (TODO maybe in future we should have a parameter forceLabelCreation)
95
     * @param
96
     * @return list of {@link UuidAndTitleCache}
97
     */
98
    public static List<UuidAndTitleCache<? extends ICdmBase>> transformSelectionToUuidAndTitleCacheList(Object selection) {
99

    
100
        //make it all a list
101
        List<Object> multipleSelection = new ArrayList<>();
102
        if(selection instanceof IStructuredSelection){
103
            IStructuredSelection structuredSelection = ((IStructuredSelection) selection);
104
            ((List<?>)structuredSelection.toList()).forEach(o->multipleSelection.add(o));
105
        }else if(selection.getClass().isArray()){
106
            for (Object o : ((Object[])selection)){
107
                multipleSelection.add(o);
108
            }
109
        }else if(selection instanceof List){
110
            multipleSelection.addAll((List<?>)selection);
111
        }else{
112
            multipleSelection.add(selection);
113
        }
114

    
115
        //filter all as uuidIdAndTitleCache
116
        List<UuidAndTitleCache<? extends ICdmBase>> uuidTypes = new ArrayList<>();
117

    
118
        for (Object o : multipleSelection){
119
            if (o instanceof FeatureNodeContainer){
120
                continue ;
121
            }
122
            if(o instanceof TreeNode){
123
                o = ((TreeNode)o).getValue();
124
            }
125
            o = CdmBase.deproxy(o);
126

    
127
            UuidAndTitleCache<? extends ICdmBase> uuidAndTitleCache;
128
            if (o instanceof CdmBase){
129
                CdmBase cdmBase = (CdmBase)o;
130
                uuidAndTitleCache = new DtoWithEntity<>(cdmBase);
131
            }else if (o instanceof ReferencingObjectDto){
132
                ReferencingObjectDto dto = (ReferencingObjectDto)o;
133
                uuidAndTitleCache = dto.getOpenInTarget() != null ? dto.getOpenInTarget() : dto;
134
            }else if (o instanceof UuidAndTitleCache){
135
                uuidAndTitleCache = (UuidAndTitleCache<CdmBase>)o;
136
            }else{
137
                continue;
138
            }
139
            uuidTypes.add(uuidAndTitleCache);
140
        }
141
        return uuidTypes;
142
    }
143
}
(4-4/5)