2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.view
;
11 import java
.util
.HashMap
;
13 import java
.util
.UUID
;
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
;
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
;
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.
54 public class CdmViewerChooser
extends PopupDialog
implements ISelectionChangedListener
, ILabelProvider
{
56 private Map
<Command
, String
> nameViewerMap
;
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");
64 public CdmViewerChooser(Shell parent
, int shellStyle
, boolean takeFocusOnOpen
, boolean persistSize
,
65 boolean persistLocation
, boolean showDialogMenu
, boolean showPersistActions
, String titleText
,
67 super(parent
, shellStyle
, takeFocusOnOpen
, persistSize
, persistLocation
, showDialogMenu
, showPersistActions
,
72 * Opens a popup dialog with all possible viewers for the given input.
73 * @param input the input for which the viewers are listed
75 public void chooseViewer(Object input
){
77 this.nameViewerMap
= CdmViewerUtil
.getAvailableViewers(input
);
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
);
85 if(nameViewerMap
.isEmpty()){
86 this.setInfoText("No viewers registered for this input");
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());
100 else if(SpecimenOrObservationBase
.class.isAssignableFrom(type
)){
101 input
= CdmStore
.getService(IOccurrenceService
.class).load(uuidAndTitleCache
.getUuid());
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());
110 if(command
.isEnabled()) {
112 //build the parameterized command
113 ParameterizedCommand pc
= ParameterizedCommand
.generateCommand(command
, params
);
115 IHandlerService handlerService
= (IHandlerService
)PlatformUI
.getWorkbench().getService(IHandlerService
.class);
118 handlerService
.executeCommand(pc
, null);
121 handlerService
.executeCommand(commandId
, null);
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
);
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());
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
);
155 public String
getText(Object element
) {
156 return nameViewerMap
.get(element
);
160 public void addListener(ILabelProviderListener listener
) {
161 // TODO Auto-generated method stub
166 public void dispose() {
167 // TODO Auto-generated method stub
172 public boolean isLabelProperty(Object element
, String property
) {
173 // TODO Auto-generated method stub
178 public void removeListener(ILabelProviderListener listener
) {
179 // TODO Auto-generated method stub
184 public Image
getImage(Object element
) {
185 // TODO Auto-generated method stub