3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.view
;
12 import java
.util
.HashMap
;
14 import java
.util
.UUID
;
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
.handlers
.IHandlerService
;
37 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
38 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
41 * This class opens a popup dialog and provides the possibility to choose from a
42 * list of possible viewers which can be opened for a given input.
48 public class CdmViewerChooser
extends PopupDialog
implements ISelectionChangedListener
, ILabelProvider
{
50 private Map
<Command
, String
> nameViewerMap
;
53 public CdmViewerChooser(Shell parentShell
) {
54 this(parentShell
, SWT
.RESIZE
| SWT
.ON_TOP
, true, false, false, false, false, "Open in ...",
55 "Clicking will open the selected viewer");
58 public CdmViewerChooser(Shell parent
, int shellStyle
, boolean takeFocusOnOpen
, boolean persistSize
,
59 boolean persistLocation
, boolean showDialogMenu
, boolean showPersistActions
, String titleText
,
61 super(parent
, shellStyle
, takeFocusOnOpen
, persistSize
, persistLocation
, showDialogMenu
, showPersistActions
,
66 * Opens a popup dialog with all possible viewers for the given input.
67 * @param input the input for which the viewers are listed
69 public void chooseViewer(Object input
){
71 this.nameViewerMap
= CdmViewerUtil
.getAvailableViewers(input
);
73 //if only one editor is available then open it
74 if(nameViewerMap
.size()==1){
75 Command command
= nameViewerMap
.keySet().iterator().next();
76 executeCommand(command
, input
);
79 if(nameViewerMap
.isEmpty()){
80 this.setInfoText("No viewers registered for this input");
86 private void executeCommand(Command command
, Object input
) {
88 if(input
instanceof ICdmBase
){
89 Map
<String
, UUID
> params
= new HashMap
<String
, UUID
>();
90 String commandId
= command
.getId();
91 params
.put(commandId
+".uuid", ((ICdmBase
) input
).getUuid());
93 if(command
.isEnabled()) {
95 //build the parameterized command
96 ParameterizedCommand pc
= ParameterizedCommand
.generateCommand(command
, params
);
98 IHandlerService handlerService
= (IHandlerService
)PlatformUI
.getWorkbench().getService(IHandlerService
.class);
101 handlerService
.executeCommand(pc
, null);
104 handlerService
.executeCommand(commandId
, null);
106 } catch (NotDefinedException nde
) {
107 throw new RuntimeException("Could not find open command: " + commandId
);
108 } catch (Exception exception
) {
109 MessagingUtils
.error(getClass(), "An exception occured while trying execute "+commandId
, exception
);
116 protected Control
createDialogArea(Composite parent
) {
117 TableViewer viewer
= new TableViewer(new Table(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
118 viewer
.setContentProvider(new ArrayContentProvider());
119 viewer
.setLabelProvider(this);
120 viewer
.addSelectionChangedListener(this);
121 viewer
.setInput(nameViewerMap
.keySet());
126 public void selectionChanged(SelectionChangedEvent event
) {
127 ISelection selection
= event
.getSelection();
128 if(selection
instanceof IStructuredSelection
){
129 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
130 if(firstElement
instanceof Command
&& nameViewerMap
.containsKey(firstElement
)){
131 executeCommand((Command
) firstElement
, this.input
);
138 public String
getText(Object element
) {
139 return nameViewerMap
.get(element
);
143 public void addListener(ILabelProviderListener listener
) {
144 // TODO Auto-generated method stub
149 public void dispose() {
150 // TODO Auto-generated method stub
155 public boolean isLabelProperty(Object element
, String property
) {
156 // TODO Auto-generated method stub
161 public void removeListener(ILabelProviderListener listener
) {
162 // TODO Auto-generated method stub
167 public Image
getImage(Object element
) {
168 // TODO Auto-generated method stub