Merge branch 'release/4.0.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / CdmViewerChooser.java
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.handlers.IHandlerService;
36
37 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
38 import eu.etaxonomy.cdm.api.service.ITaxonService;
39 import eu.etaxonomy.cdm.model.common.ICdmBase;
40 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
41 import eu.etaxonomy.cdm.model.taxon.Synonym;
42 import eu.etaxonomy.cdm.model.taxon.Taxon;
43 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
44 import eu.etaxonomy.taxeditor.model.MessagingUtils;
45 import eu.etaxonomy.taxeditor.store.CdmStore;
46
47 /**
48 * This class opens a popup dialog and provides the possibility to choose from a
49 * list of possible viewers which can be opened for a given input.
50 *
51 * @author pplitzner
52 * @date Feb 23, 2015
53 *
54 */
55 public class CdmViewerChooser extends PopupDialog implements ISelectionChangedListener, ILabelProvider{
56
57 private Map<Command, String> nameViewerMap;
58 private Object input;
59
60 public CdmViewerChooser(Shell parentShell) {
61 this(parentShell, SWT.RESIZE | SWT.ON_TOP, true, false, false, false, false, "Open in ...",
62 "Clicking will open the selected viewer");
63 }
64
65 public CdmViewerChooser(Shell parent, int shellStyle, boolean takeFocusOnOpen, boolean persistSize,
66 boolean persistLocation, boolean showDialogMenu, boolean showPersistActions, String titleText,
67 String infoText) {
68 super(parent, shellStyle, takeFocusOnOpen, persistSize, persistLocation, showDialogMenu, showPersistActions,
69 titleText, infoText);
70 }
71
72 /**
73 * Opens a popup dialog with all possible viewers for the given input.
74 * @param input the input for which the viewers are listed
75 */
76 public void chooseViewer(Object input){
77 this.input = input;
78 this.nameViewerMap = CdmViewerUtil.getAvailableViewers(input);
79
80 //if only one editor is available then open it
81 if(nameViewerMap.size()==1){
82 Command command = nameViewerMap.keySet().iterator().next();
83 executeCommand(command, input);
84 }
85 else{
86 if(nameViewerMap.isEmpty()){
87 this.setInfoText("No viewers registered for this input");
88 }
89 this.open();
90 }
91 }
92
93 private void executeCommand(Command command, Object input) {
94 //for generic UuidAndTitleCache objects try to load the object
95 if (input instanceof UuidAndTitleCache){
96 UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache)input;
97 Class type = uuidAndTitleCache.getType();
98 if(type == Taxon.class || type == Synonym.class){
99 input = CdmStore.getService(ITaxonService.class).load(uuidAndTitleCache.getUuid());
100 }
101 else if(SpecimenOrObservationBase.class.isAssignableFrom(type)){
102 input = CdmStore.getService(IOccurrenceService.class).load(uuidAndTitleCache.getUuid());
103 }
104 }
105 //set uuid parameter
106 if(input instanceof ICdmBase){
107 Map<String, UUID> params = new HashMap<String, UUID>();
108 String commandId = command.getId();
109 params.put(commandId+".uuid", ((ICdmBase) input).getUuid());
110
111 if(command.isEnabled()) {
112
113 //build the parameterized command
114 ParameterizedCommand pc = ParameterizedCommand.generateCommand(command, params);
115
116 IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
117 try {
118 if(pc!=null){
119 handlerService.executeCommand(pc, null);
120 }
121 else{
122 handlerService.executeCommand(commandId, null);
123 }
124 } catch (NotDefinedException nde) {
125 throw new RuntimeException("Could not find open command: " + commandId);
126 } catch (Exception exception) {
127 MessagingUtils.error(getClass(), "An exception occured while trying execute "+commandId, exception);
128 }
129 }
130 }
131 }
132
133 @Override
134 protected Control createDialogArea(Composite parent) {
135 TableViewer viewer = new TableViewer(new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION));
136 viewer.setContentProvider(new ArrayContentProvider());
137 viewer.setLabelProvider(this);
138 viewer.addSelectionChangedListener(this);
139 viewer.setInput(nameViewerMap.keySet());
140 return parent;
141 }
142
143 @Override
144 public void selectionChanged(SelectionChangedEvent event) {
145 ISelection selection = event.getSelection();
146 if(selection instanceof IStructuredSelection){
147 Object firstElement = ((IStructuredSelection) selection).getFirstElement();
148 if(firstElement instanceof Command && nameViewerMap.containsKey(firstElement)){
149 executeCommand((Command) firstElement, this.input);
150 this.close();
151 }
152 }
153 }
154
155 @Override
156 public String getText(Object element) {
157 return nameViewerMap.get(element);
158 }
159
160 @Override
161 public void addListener(ILabelProviderListener listener) {
162 // TODO Auto-generated method stub
163
164 }
165
166 @Override
167 public void dispose() {
168 // TODO Auto-generated method stub
169
170 }
171
172 @Override
173 public boolean isLabelProperty(Object element, String property) {
174 // TODO Auto-generated method stub
175 return false;
176 }
177
178 @Override
179 public void removeListener(ILabelProviderListener listener) {
180 // TODO Auto-generated method stub
181
182 }
183
184 @Override
185 public Image getImage(Object element) {
186 // TODO Auto-generated method stub
187 return null;
188 }
189
190 }