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
.commands
.ICommandService
;
36 import org
.eclipse
.ui
.handlers
.IHandlerService
;
38 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
39 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
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.
49 public class CdmViewerChooser
extends PopupDialog
implements ISelectionChangedListener
, ILabelProvider
{
51 private Map
<String
, String
> nameViewerMap
;
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");
59 public CdmViewerChooser(Shell parent
, int shellStyle
, boolean takeFocusOnOpen
, boolean persistSize
,
60 boolean persistLocation
, boolean showDialogMenu
, boolean showPersistActions
, String titleText
,
62 super(parent
, shellStyle
, takeFocusOnOpen
, persistSize
, persistLocation
, showDialogMenu
, showPersistActions
,
67 * Opens a popup dialog with all possible viewers for the given input.
68 * @param input the input for which the viewers are listed
70 public void chooseViewer(Object input
){
72 this.nameViewerMap
= CdmViewerUtil
.getAvailableViewers(input
);
74 //if only one editor is available then open it
75 if(nameViewerMap
.size()==1){
76 String commandId
= nameViewerMap
.keySet().iterator().next();
77 executeCommand(commandId
, input
);
80 if(nameViewerMap
.isEmpty()){
81 this.setInfoText("No viewers registered for this input");
87 private void executeCommand(String commandId
, Object input
) {
88 ICommandService commandService
= (ICommandService
)PlatformUI
.getWorkbench().getService(ICommandService
.class);
89 //get the command from plugin.xml
90 Command command
= commandService
.getCommand(commandId
);
93 if(input
instanceof ICdmBase
){
94 Map
<String
, UUID
> params
= new HashMap
<String
, UUID
>();
95 params
.put(commandId
+".uuid", ((ICdmBase
) input
).getUuid());
97 //build the parameterized command
98 ParameterizedCommand pc
= ParameterizedCommand
.generateCommand(command
, params
);
100 if(command
.isEnabled()) {
101 IHandlerService handlerService
= (IHandlerService
)PlatformUI
.getWorkbench().getService(IHandlerService
.class);
103 handlerService
.executeCommand(pc
, null);
104 } catch (NotDefinedException nde
) {
105 throw new RuntimeException("Could not find open command: " + commandId
);
106 } catch (Exception exception
) {
107 MessagingUtils
.error(getClass(), "An exception occured while trying execute "+commandId
, exception
);
114 protected Control
createDialogArea(Composite parent
) {
115 TableViewer viewer
= new TableViewer(new Table(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
116 viewer
.setContentProvider(new ArrayContentProvider());
117 viewer
.setLabelProvider(this);
118 viewer
.addSelectionChangedListener(this);
119 viewer
.setInput(nameViewerMap
.keySet());
124 public void selectionChanged(SelectionChangedEvent event
) {
125 ISelection selection
= event
.getSelection();
126 if(selection
instanceof IStructuredSelection
){
127 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
128 if(firstElement
instanceof String
&& nameViewerMap
.containsKey(firstElement
)){
129 executeCommand((String
) firstElement
, this.input
);
136 public String
getText(Object element
) {
137 return nameViewerMap
.get(element
);
141 public void addListener(ILabelProviderListener listener
) {
142 // TODO Auto-generated method stub
147 public void dispose() {
148 // TODO Auto-generated method stub
153 public boolean isLabelProperty(Object element
, String property
) {
154 // TODO Auto-generated method stub
159 public void removeListener(ILabelProviderListener listener
) {
160 // TODO Auto-generated method stub
165 public Image
getImage(Object element
) {
166 // TODO Auto-generated method stub