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