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.Map;
|
13
|
import java.util.Map.Entry;
|
14
|
|
15
|
import org.eclipse.jface.dialogs.PopupDialog;
|
16
|
import org.eclipse.jface.viewers.ArrayContentProvider;
|
17
|
import org.eclipse.jface.viewers.ILabelProvider;
|
18
|
import org.eclipse.jface.viewers.ILabelProviderListener;
|
19
|
import org.eclipse.jface.viewers.ISelection;
|
20
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
21
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
22
|
import org.eclipse.jface.viewers.SelectionChangedEvent;
|
23
|
import org.eclipse.jface.viewers.TableViewer;
|
24
|
import org.eclipse.swt.SWT;
|
25
|
import org.eclipse.swt.graphics.Image;
|
26
|
import org.eclipse.swt.widgets.Composite;
|
27
|
import org.eclipse.swt.widgets.Control;
|
28
|
import org.eclipse.swt.widgets.Shell;
|
29
|
import org.eclipse.swt.widgets.Table;
|
30
|
|
31
|
/**
|
32
|
* This class opens a popup dialog and provides the possibility to choose from a
|
33
|
* list of possible viewers which can be opened for a given input.
|
34
|
*
|
35
|
* @author pplitzner
|
36
|
* @date Feb 23, 2015
|
37
|
*
|
38
|
*/
|
39
|
public class CdmViewerChooser extends PopupDialog implements ISelectionChangedListener, ILabelProvider{
|
40
|
|
41
|
private Object input;
|
42
|
private Map<Entry<Class<?>, String>, ICdmViewer> nameViewerMap;
|
43
|
|
44
|
public CdmViewerChooser(Shell parentShell) {
|
45
|
this(parentShell, SWT.RESIZE | SWT.ON_TOP, true, false, false, false, false, "Open in ...",
|
46
|
"Clicking will open the selected viewer");
|
47
|
}
|
48
|
|
49
|
public CdmViewerChooser(Shell parent, int shellStyle, boolean takeFocusOnOpen, boolean persistSize,
|
50
|
boolean persistLocation, boolean showDialogMenu, boolean showPersistActions, String titleText,
|
51
|
String infoText) {
|
52
|
super(parent, shellStyle, takeFocusOnOpen, persistSize, persistLocation, showDialogMenu, showPersistActions,
|
53
|
titleText, infoText);
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Opens a popup dialog with all possible viewers for the given input.
|
58
|
* @param input the input for which the viewers are listed
|
59
|
*/
|
60
|
public void chooseViewer(Object input){
|
61
|
this.input = input;
|
62
|
this.nameViewerMap = CdmViewerUtil.getNameViewerMap(input);
|
63
|
|
64
|
//if only one editor is available then open it
|
65
|
if(nameViewerMap.size()==1){
|
66
|
Entry<Class<?>, String> next = nameViewerMap.keySet().iterator().next();
|
67
|
ICdmViewer cdmViewer = nameViewerMap.get(next);
|
68
|
cdmViewer.show(input, next.getKey());
|
69
|
}
|
70
|
else{
|
71
|
if(nameViewerMap.isEmpty()){
|
72
|
this.setInfoText("No viewers registered for this input");
|
73
|
}
|
74
|
this.open();
|
75
|
}
|
76
|
}
|
77
|
|
78
|
@Override
|
79
|
protected Control createDialogArea(Composite parent) {
|
80
|
TableViewer viewer = new TableViewer(new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION));
|
81
|
viewer.setContentProvider(new ArrayContentProvider());
|
82
|
viewer.setLabelProvider(this);
|
83
|
viewer.addSelectionChangedListener(this);
|
84
|
viewer.setInput(nameViewerMap.keySet());
|
85
|
return parent;
|
86
|
}
|
87
|
|
88
|
@Override
|
89
|
public void selectionChanged(SelectionChangedEvent event) {
|
90
|
ISelection selection = event.getSelection();
|
91
|
if(selection instanceof IStructuredSelection){
|
92
|
Object firstElement = ((IStructuredSelection) selection).getFirstElement();
|
93
|
if(nameViewerMap.containsKey(firstElement)){
|
94
|
Entry<Class<?>, String> entry = (Entry<Class<?>, String>)firstElement;
|
95
|
ICdmViewer cdmViewer = nameViewerMap.get(entry);
|
96
|
cdmViewer.show(input, entry.getKey());
|
97
|
this.close();
|
98
|
}
|
99
|
}
|
100
|
}
|
101
|
|
102
|
@Override
|
103
|
public String getText(Object element) {
|
104
|
String text = null;
|
105
|
if(nameViewerMap.containsKey(element)){
|
106
|
Entry<Class<?>, String> entry = (Entry<Class<?>, String>) element;
|
107
|
text = entry.getValue();
|
108
|
}
|
109
|
return text;
|
110
|
}
|
111
|
|
112
|
@Override
|
113
|
public void addListener(ILabelProviderListener listener) {
|
114
|
// TODO Auto-generated method stub
|
115
|
|
116
|
}
|
117
|
|
118
|
@Override
|
119
|
public void dispose() {
|
120
|
// TODO Auto-generated method stub
|
121
|
|
122
|
}
|
123
|
|
124
|
@Override
|
125
|
public boolean isLabelProperty(Object element, String property) {
|
126
|
// TODO Auto-generated method stub
|
127
|
return false;
|
128
|
}
|
129
|
|
130
|
@Override
|
131
|
public void removeListener(ILabelProviderListener listener) {
|
132
|
// TODO Auto-generated method stub
|
133
|
|
134
|
}
|
135
|
|
136
|
@Override
|
137
|
public Image getImage(Object element) {
|
138
|
// TODO Auto-generated method stub
|
139
|
return null;
|
140
|
}
|
141
|
|
142
|
}
|