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.HashMap;
|
12
|
import java.util.Map;
|
13
|
import java.util.UUID;
|
14
|
|
15
|
import org.eclipse.core.commands.Command;
|
16
|
import org.eclipse.core.commands.ParameterizedCommand;
|
17
|
import org.eclipse.core.commands.common.NotDefinedException;
|
18
|
import org.eclipse.jface.dialogs.PopupDialog;
|
19
|
import org.eclipse.jface.viewers.ArrayContentProvider;
|
20
|
import org.eclipse.jface.viewers.ILabelProvider;
|
21
|
import org.eclipse.jface.viewers.ILabelProviderListener;
|
22
|
import org.eclipse.jface.viewers.ISelection;
|
23
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
24
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
25
|
import org.eclipse.jface.viewers.SelectionChangedEvent;
|
26
|
import org.eclipse.jface.viewers.TableViewer;
|
27
|
import org.eclipse.swt.SWT;
|
28
|
import org.eclipse.swt.graphics.Image;
|
29
|
import org.eclipse.swt.widgets.Composite;
|
30
|
import org.eclipse.swt.widgets.Control;
|
31
|
import org.eclipse.swt.widgets.Shell;
|
32
|
import org.eclipse.swt.widgets.Table;
|
33
|
import org.eclipse.ui.PlatformUI;
|
34
|
import org.eclipse.ui.handlers.IHandlerService;
|
35
|
|
36
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
37
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
38
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
39
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
40
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
41
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
42
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
43
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
44
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
45
|
|
46
|
/**
|
47
|
* This class opens a popup dialog and provides the possibility to choose from a
|
48
|
* list of possible viewers which can be opened for a given input.
|
49
|
*
|
50
|
* @author pplitzner
|
51
|
* @date Feb 23, 2015
|
52
|
*
|
53
|
*/
|
54
|
public class CdmViewerChooser extends PopupDialog implements ISelectionChangedListener, ILabelProvider{
|
55
|
|
56
|
private Map<Command, String> nameViewerMap;
|
57
|
private Object input;
|
58
|
|
59
|
public CdmViewerChooser(Shell parentShell) {
|
60
|
this(parentShell, SWT.RESIZE | SWT.ON_TOP, true, false, false, false, false, "Open in ...",
|
61
|
"Clicking will open the selected viewer");
|
62
|
}
|
63
|
|
64
|
public CdmViewerChooser(Shell parent, int shellStyle, boolean takeFocusOnOpen, boolean persistSize,
|
65
|
boolean persistLocation, boolean showDialogMenu, boolean showPersistActions, String titleText,
|
66
|
String infoText) {
|
67
|
super(parent, shellStyle, takeFocusOnOpen, persistSize, persistLocation, showDialogMenu, showPersistActions,
|
68
|
titleText, infoText);
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* Opens a popup dialog with all possible viewers for the given input.
|
73
|
* @param input the input for which the viewers are listed
|
74
|
*/
|
75
|
public void chooseViewer(Object input){
|
76
|
this.input = input;
|
77
|
this.nameViewerMap = CdmViewerUtil.getAvailableViewers(input);
|
78
|
|
79
|
//if only one editor is available then open it
|
80
|
if(nameViewerMap.size()==1){
|
81
|
Command command = nameViewerMap.keySet().iterator().next();
|
82
|
executeCommand(command, input);
|
83
|
}
|
84
|
else{
|
85
|
if(nameViewerMap.isEmpty()){
|
86
|
this.setInfoText("No viewers registered for this input");
|
87
|
}
|
88
|
this.open();
|
89
|
}
|
90
|
}
|
91
|
|
92
|
private void executeCommand(Command command, Object input) {
|
93
|
//for generic UuidAndTitleCache objects try to load the object
|
94
|
if (input instanceof UuidAndTitleCache){
|
95
|
UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache)input;
|
96
|
Class type = uuidAndTitleCache.getType();
|
97
|
if(type == Taxon.class || type == Synonym.class){
|
98
|
input = CdmStore.getService(ITaxonService.class).load(uuidAndTitleCache.getUuid());
|
99
|
}
|
100
|
else if(SpecimenOrObservationBase.class.isAssignableFrom(type)){
|
101
|
input = CdmStore.getService(IOccurrenceService.class).load(uuidAndTitleCache.getUuid());
|
102
|
}
|
103
|
}
|
104
|
//set uuid parameter
|
105
|
if(input instanceof ICdmBase){
|
106
|
Map<String, UUID> params = new HashMap<String, UUID>();
|
107
|
String commandId = command.getId();
|
108
|
params.put(commandId+".uuid", ((ICdmBase) input).getUuid());
|
109
|
|
110
|
if(command.isEnabled()) {
|
111
|
|
112
|
//build the parameterized command
|
113
|
ParameterizedCommand pc = ParameterizedCommand.generateCommand(command, params);
|
114
|
|
115
|
IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
|
116
|
try {
|
117
|
if(pc!=null){
|
118
|
handlerService.executeCommand(pc, null);
|
119
|
}
|
120
|
else{
|
121
|
handlerService.executeCommand(commandId, null);
|
122
|
}
|
123
|
} catch (NotDefinedException nde) {
|
124
|
throw new RuntimeException("Could not find open command: " + commandId);
|
125
|
} catch (Exception exception) {
|
126
|
MessagingUtils.error(getClass(), "An exception occurred while trying execute "+commandId, exception);
|
127
|
}
|
128
|
}
|
129
|
}
|
130
|
}
|
131
|
|
132
|
@Override
|
133
|
protected Control createDialogArea(Composite parent) {
|
134
|
TableViewer viewer = new TableViewer(new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION));
|
135
|
viewer.setContentProvider(new ArrayContentProvider());
|
136
|
viewer.setLabelProvider(this);
|
137
|
viewer.addSelectionChangedListener(this);
|
138
|
viewer.setInput(nameViewerMap.keySet());
|
139
|
return parent;
|
140
|
}
|
141
|
|
142
|
@Override
|
143
|
public void selectionChanged(SelectionChangedEvent event) {
|
144
|
ISelection selection = event.getSelection();
|
145
|
if(selection instanceof IStructuredSelection){
|
146
|
Object firstElement = ((IStructuredSelection) selection).getFirstElement();
|
147
|
if(firstElement instanceof Command && nameViewerMap.containsKey(firstElement)){
|
148
|
executeCommand((Command) firstElement, this.input);
|
149
|
this.close();
|
150
|
}
|
151
|
}
|
152
|
}
|
153
|
|
154
|
@Override
|
155
|
public String getText(Object element) {
|
156
|
return nameViewerMap.get(element);
|
157
|
}
|
158
|
|
159
|
@Override
|
160
|
public void addListener(ILabelProviderListener listener) {
|
161
|
// TODO Auto-generated method stub
|
162
|
|
163
|
}
|
164
|
|
165
|
@Override
|
166
|
public void dispose() {
|
167
|
// TODO Auto-generated method stub
|
168
|
|
169
|
}
|
170
|
|
171
|
@Override
|
172
|
public boolean isLabelProperty(Object element, String property) {
|
173
|
// TODO Auto-generated method stub
|
174
|
return false;
|
175
|
}
|
176
|
|
177
|
@Override
|
178
|
public void removeListener(ILabelProviderListener listener) {
|
179
|
// TODO Auto-generated method stub
|
180
|
|
181
|
}
|
182
|
|
183
|
@Override
|
184
|
public Image getImage(Object element) {
|
185
|
// TODO Auto-generated method stub
|
186
|
return null;
|
187
|
}
|
188
|
|
189
|
}
|