Project

General

Profile

Download (10.3 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
import org.eclipse.ui.PlatformUI;
27
import org.eclipse.ui.commands.ICommandService;
28

    
29
import eu.etaxonomy.cdm.api.service.dto.DescriptionBaseDto;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.common.ICdmBase;
32
import eu.etaxonomy.cdm.model.name.NomenclaturalSource;
33
import eu.etaxonomy.cdm.model.name.TaxonName;
34
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
35
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
36
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
37
import eu.etaxonomy.cdm.persistence.dto.ReferencingObjectDto;
38
import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
39
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
40
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
41
import eu.etaxonomy.taxeditor.model.MessagingUtils;
42
import eu.etaxonomy.taxeditor.store.CdmStore;
43

    
44
/**
45
 * Scans eu.etaxonomy.taxeditor.store.cdmViewer extension point.
46
 *
47
 * @author pplitzner
48
 * @date Jul 7, 2015
49
 */
50
public class CdmViewerUtil {
51

    
52
    /**
53
     * Returns a map of available commands to open the given input. Keys are the
54
     * command IDs and values are their string representations.
55
     *
56
     * @param input
57
     *            the object which should be handled by the available commands
58
     * @return a key-value map of available commands and their string
59
     *         representation
60
     */
61
    public static Map<Command, String> getAvailableViewers(List<UuidAndTitleCache<? extends ICdmBase>> input, ECommandService commandService,
62
            EHandlerService handlerService){
63

    
64
        Map<Command, String> commandViewerNameMap = new HashMap<>();
65

    
66
        Set<Class<?>> inputClasses = new HashSet<>();
67
        if(input!=null){
68
            input.stream()
69
                .filter(uuid->uuid.getType()!= null)
70
                .forEach(uuid->inputClasses.add(uuid.getType()));
71

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

    
97
        return commandViewerNameMap;
98
    }
99

    
100
    /**
101
     * Returns a map of available commands to open the given input.
102
     * Keys are the command IDs and values are their string representations.
103
     *
104
     * @param input
105
     *            the object which should be handled by the available commands
106
     * @return a key-value map of available commands and their string
107
     *         representation
108
     */
109
    public static  Map<Command, String> getAvailableViewers(Object input){
110
                Map<Command, String> commandViewerNameMap = new HashMap<>();
111

    
112
        if(input!=null){
113
            //for generic UuidAndTitleCache objects try to load the object
114
            if (input instanceof UuidAndTitleCache){
115
                UuidAndTitleCache<? extends CdmBase> uuidAndTitleCache = (UuidAndTitleCache<? extends CdmBase>)input;
116
                input = CdmStore.getCommonService().find(uuidAndTitleCache.getType(), uuidAndTitleCache.getUuid());
117
            }
118
            //for tree nodes get the value resp. the object of the node
119
            else if (input instanceof TreeNode){
120
                TreeNode treeNode = (TreeNode)input;
121
                input = treeNode.getValue();
122
            }
123
            if(input==null){
124
                return commandViewerNameMap;
125
            }
126
            if (input instanceof NomenclaturalSource){
127
                input = ((NomenclaturalSource)input).getSourcedName();
128
            }
129

    
130
            IExtensionRegistry reg = Platform.getExtensionRegistry();
131
            IConfigurationElement[] extensions = reg
132
                    .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer"); //$NON-NLS-1$
133
            for (IConfigurationElement configElement : extensions) {
134
                if(configElement.getName().equals("viewCommandMapping")){ //$NON-NLS-1$
135
                    try {
136
                        String commandId = configElement.getAttribute("commandId"); //$NON-NLS-1$
137
                        String viewerName = configElement.getAttribute("viewerName"); //$NON-NLS-1$
138
                        Class<?> selectionClass = Class.forName(configElement.getAttribute("selection")); //$NON-NLS-1$
139
                        if(selectionClass.isAssignableFrom(input.getClass())){
140
                            ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class);
141
                            Command command = commandService.getCommand(commandId);
142
                            if(command.isEnabled()){
143
                                commandViewerNameMap.put(command, viewerName);
144
                            }
145
                        }
146
                    } catch (ClassNotFoundException e) {
147
                        MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e); //$NON-NLS-1$
148
                    }
149
                }
150
            }
151
        }
152
        return commandViewerNameMap;
153
    }
154

    
155

    
156
    /**
157
     * Trys to create a List of {@link UuidAndTitleCache} from the selection.<BR>
158
     * Selections which are not recognized are not included in the list.<BR>
159
     * As a minimum each returned element contains the type and the uuid.
160
     * If available also the label and the CdmBase entity is added (later by using the subclass {@link DtoWithEntity}.
161
     * Label creation is not forced (TODO maybe in future we should have a parameter forceLabelCreation)
162
     * @param
163
     * @return list of {@link UuidAndTitleCache}
164
     */
165
    public static List<UuidAndTitleCache<? extends ICdmBase>> transformSelectionToUuidAndTitleCacheList(Object selection) {
166

    
167
        //make it all a list
168
        List<Object> multipleSelection = new ArrayList<>();
169
        if(selection instanceof IStructuredSelection){
170
            IStructuredSelection structuredSelection = ((IStructuredSelection) selection);
171
            ((List<?>)structuredSelection.toList()).forEach(o->multipleSelection.add(o));
172
        }else if(selection.getClass().isArray()){
173
            for (Object o : ((Object[])selection)){
174
                multipleSelection.add(o);
175
            }
176
        }else if(selection instanceof List){
177
            multipleSelection.addAll((List<?>)selection);
178
        }else{
179
            multipleSelection.add(selection);
180
        }
181

    
182
        //filter all as uuidIdAndTitleCache
183
        List<UuidAndTitleCache<? extends ICdmBase>> uuidTypes = new ArrayList<>();
184

    
185
        for (Object o : multipleSelection){
186
            if (o instanceof FeatureNodeContainer){
187
                continue ;
188
            }
189
            if(o instanceof TreeNode){
190
                o = ((TreeNode)o).getValue();
191
            }
192
            o = CdmBase.deproxy(o);
193

    
194
            UuidAndTitleCache<? extends ICdmBase> uuidAndTitleCache;
195
            if (o instanceof CdmBase){
196
                CdmBase cdmBase = (CdmBase)o;
197
                uuidAndTitleCache = new DtoWithEntity<>(cdmBase);
198
            }else if (o instanceof ReferencingObjectDto){
199
                ReferencingObjectDto dto = (ReferencingObjectDto)o;
200
                uuidAndTitleCache = dto.getOpenInTarget() != null ? dto.getOpenInTarget() : dto;
201
            }else if (o instanceof UuidAndTitleCache){
202
                uuidAndTitleCache = (UuidAndTitleCache<?>)o;
203
            }else if (o instanceof TermNodeDto){
204
                uuidAndTitleCache = new UuidAndTitleCache<>(DefinedTermBase.class, ((TermNodeDto)o).getTerm().getUuid(), null, ((TermNodeDto)o).getTerm().getTitleCache());
205
            }else if (o instanceof DescriptionBaseDto){
206
            	if ( ((DescriptionBaseDto)o).getSpecimenDto() != null){
207
            		uuidAndTitleCache = new UuidAndTitleCache<>(SpecimenOrObservationBase.class, ((DescriptionBaseDto)o).getSpecimenDto().getUuid(), null, ((DescriptionBaseDto)o).getTitleCache());
208
            	}else if (((DescriptionBaseDto)o).getTaxonDto() != null){
209
            		uuidAndTitleCache = new UuidAndTitleCache<>(TaxonBase.class, ((DescriptionBaseDto)o).getTaxonDto().getUuid(), null, ((DescriptionBaseDto)o).getTitleCache());
210
            	}else {
211
            		uuidAndTitleCache = new UuidAndTitleCache<>(TaxonName.class, ((DescriptionBaseDto)o).getNameDto().getUuid(), null, ((DescriptionBaseDto)o).getTitleCache());
212
            	}
213
            }else{
214
                continue;
215
            }
216
            uuidTypes.add(uuidAndTitleCache);
217
        }
218
        return uuidTypes;
219
    }
220
}
(3-3/4)