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