Merge branch 'hotfix/3.12.3' into develop
[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.commands.ICommandService;
36 import org.eclipse.ui.handlers.IHandlerService;
37
38 import eu.etaxonomy.cdm.model.common.ICdmBase;
39 import eu.etaxonomy.taxeditor.model.MessagingUtils;
40
41 /**
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.
44 *
45 * @author pplitzner
46 * @date Feb 23, 2015
47 *
48 */
49 public class CdmViewerChooser extends PopupDialog implements ISelectionChangedListener, ILabelProvider{
50
51 private Map<String, String> nameViewerMap;
52 private Object input;
53
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");
57 }
58
59 public CdmViewerChooser(Shell parent, int shellStyle, boolean takeFocusOnOpen, boolean persistSize,
60 boolean persistLocation, boolean showDialogMenu, boolean showPersistActions, String titleText,
61 String infoText) {
62 super(parent, shellStyle, takeFocusOnOpen, persistSize, persistLocation, showDialogMenu, showPersistActions,
63 titleText, infoText);
64 }
65
66 /**
67 * Opens a popup dialog with all possible viewers for the given input.
68 * @param input the input for which the viewers are listed
69 */
70 public void chooseViewer(Object input){
71 this.input = input;
72 this.nameViewerMap = CdmViewerUtil.getAvailableViewers(input);
73
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);
78 }
79 else{
80 if(nameViewerMap.isEmpty()){
81 this.setInfoText("No viewers registered for this input");
82 }
83 this.open();
84 }
85 }
86
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);
91
92 //set uuid parameter
93 if(input instanceof ICdmBase){
94 Map<String, UUID> params = new HashMap<String, UUID>();
95 params.put(commandId+".uuid", ((ICdmBase) input).getUuid());
96
97 //build the parameterized command
98 ParameterizedCommand pc = ParameterizedCommand.generateCommand(command, params);
99
100 if(command.isEnabled()) {
101 IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
102 try {
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);
108 }
109 }
110 }
111 }
112
113 @Override
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());
120 return parent;
121 }
122
123 @Override
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);
130 this.close();
131 }
132 }
133 }
134
135 @Override
136 public String getText(Object element) {
137 return nameViewerMap.get(element);
138 }
139
140 @Override
141 public void addListener(ILabelProviderListener listener) {
142 // TODO Auto-generated method stub
143
144 }
145
146 @Override
147 public void dispose() {
148 // TODO Auto-generated method stub
149
150 }
151
152 @Override
153 public boolean isLabelProperty(Object element, String property) {
154 // TODO Auto-generated method stub
155 return false;
156 }
157
158 @Override
159 public void removeListener(ILabelProviderListener listener) {
160 // TODO Auto-generated method stub
161
162 }
163
164 @Override
165 public Image getImage(Object element) {
166 // TODO Auto-generated method stub
167 return null;
168 }
169
170 }