Project

General

Profile

Download (6.85 KB) Statistics
| Branch: | Tag: | Revision:
1 3dba9fc4 Patric Plitzner
/**
2
 * Copyright (C) 2015 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
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.
8
 */
9
package eu.etaxonomy.taxeditor.view;
10
11 250453cf Patrick Plitzner
import java.util.HashMap;
12 b93817a3 Patric Plitzner
import java.util.Map;
13 250453cf Patrick Plitzner
import java.util.UUID;
14 3dba9fc4 Patric Plitzner
15 250453cf Patrick Plitzner
import org.eclipse.core.commands.Command;
16
import org.eclipse.core.commands.ParameterizedCommand;
17
import org.eclipse.core.commands.common.NotDefinedException;
18 3dba9fc4 Patric Plitzner
import org.eclipse.jface.dialogs.PopupDialog;
19
import org.eclipse.jface.viewers.ArrayContentProvider;
20 6a159a76 Patric Plitzner
import org.eclipse.jface.viewers.ILabelProvider;
21
import org.eclipse.jface.viewers.ILabelProviderListener;
22 3dba9fc4 Patric Plitzner
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 6a159a76 Patric Plitzner
import org.eclipse.swt.graphics.Image;
29 3dba9fc4 Patric Plitzner
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 250453cf Patrick Plitzner
import org.eclipse.ui.PlatformUI;
34
import org.eclipse.ui.handlers.IHandlerService;
35
36 f153f11a Patrick Plitzner
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
37
import eu.etaxonomy.cdm.api.service.ITaxonService;
38 250453cf Patrick Plitzner
import eu.etaxonomy.cdm.model.common.ICdmBase;
39 f153f11a Patrick Plitzner
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 250453cf Patrick Plitzner
import eu.etaxonomy.taxeditor.model.MessagingUtils;
44 f153f11a Patrick Plitzner
import eu.etaxonomy.taxeditor.store.CdmStore;
45 3dba9fc4 Patric Plitzner
46
/**
47 243636c2 Patrick Plitzner
 * 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.
49 6a159a76 Patric Plitzner
 *
50 3dba9fc4 Patric Plitzner
 * @author pplitzner
51
 * @date Feb 23, 2015
52
 *
53
 */
54 6a159a76 Patric Plitzner
public class CdmViewerChooser extends PopupDialog implements ISelectionChangedListener, ILabelProvider{
55 3dba9fc4 Patric Plitzner
56 95aceac2 Patrick Plitzner
    private Map<Command, String> nameViewerMap;
57 3dba9fc4 Patric Plitzner
    private Object input;
58
59
    public CdmViewerChooser(Shell parentShell) {
60 6a159a76 Patric Plitzner
        this(parentShell, SWT.RESIZE | SWT.ON_TOP, true, false, false, false, false, "Open in ...",
61
                "Clicking will open the selected viewer");
62 3dba9fc4 Patric Plitzner
    }
63
64
    public CdmViewerChooser(Shell parent, int shellStyle, boolean takeFocusOnOpen, boolean persistSize,
65
            boolean persistLocation, boolean showDialogMenu, boolean showPersistActions, String titleText,
66
            String infoText) {
67
        super(parent, shellStyle, takeFocusOnOpen, persistSize, persistLocation, showDialogMenu, showPersistActions,
68
                titleText, infoText);
69
    }
70
71 243636c2 Patrick Plitzner
    /**
72
     * Opens a popup dialog with all possible viewers for the given input.
73
     * @param input the input for which the viewers are listed
74
     */
75
    public void chooseViewer(Object input){
76
        this.input = input;
77 250453cf Patrick Plitzner
        this.nameViewerMap = CdmViewerUtil.getAvailableViewers(input);
78 243636c2 Patrick Plitzner
79 b93817a3 Patric Plitzner
        //if only one editor is available then open it
80
        if(nameViewerMap.size()==1){
81 95aceac2 Patrick Plitzner
            Command command = nameViewerMap.keySet().iterator().next();
82 ad56b4cd p.plitzner
            executeCommand(command, input);
83 b93817a3 Patric Plitzner
        }
84
        else{
85
            if(nameViewerMap.isEmpty()){
86
                this.setInfoText("No viewers registered for this input");
87 3dba9fc4 Patric Plitzner
            }
88 b93817a3 Patric Plitzner
            this.open();
89 3dba9fc4 Patric Plitzner
        }
90
    }
91
92 ad56b4cd p.plitzner
    private void executeCommand(Command command, Object input) {
93 f153f11a Patrick Plitzner
    	 //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());
99
            }
100
            else if(SpecimenOrObservationBase.class.isAssignableFrom(type)){
101
            	input = CdmStore.getService(IOccurrenceService.class).load(uuidAndTitleCache.getUuid());
102
            }
103
        }
104 250453cf Patrick Plitzner
        //set uuid parameter
105
        if(input instanceof ICdmBase){
106
            Map<String, UUID> params = new HashMap<String, UUID>();
107 ad56b4cd p.plitzner
            String commandId = command.getId();
108
			params.put(commandId+".uuid", ((ICdmBase) input).getUuid());
109 250453cf Patrick Plitzner
110 db093ff8 Patrick Plitzner
			if(command.isEnabled()) {
111
112
			    //build the parameterized command
113
			    ParameterizedCommand pc = ParameterizedCommand.generateCommand(command, params);
114 250453cf Patrick Plitzner
115
                IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
116
                try {
117 db093ff8 Patrick Plitzner
                    if(pc!=null){
118
                        handlerService.executeCommand(pc, null);
119
                    }
120
                    else{
121
                        handlerService.executeCommand(commandId, null);
122
                    }
123 250453cf Patrick Plitzner
                } catch (NotDefinedException nde) {
124
                    throw new RuntimeException("Could not find open command: " + commandId);
125
                } catch (Exception exception) {
126 62b85f1d Andreas Müller
                    MessagingUtils.error(getClass(), "An exception occurred while trying execute "+commandId, exception);
127 250453cf Patrick Plitzner
                }
128
            }
129
        }
130
    }
131
132 3dba9fc4 Patric Plitzner
    @Override
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 6a159a76 Patric Plitzner
        viewer.setLabelProvider(this);
137 3dba9fc4 Patric Plitzner
        viewer.addSelectionChangedListener(this);
138 b93817a3 Patric Plitzner
        viewer.setInput(nameViewerMap.keySet());
139 3dba9fc4 Patric Plitzner
        return parent;
140
    }
141
142
    @Override
143
    public void selectionChanged(SelectionChangedEvent event) {
144
        ISelection selection = event.getSelection();
145
        if(selection instanceof IStructuredSelection){
146
            Object firstElement = ((IStructuredSelection) selection).getFirstElement();
147 ad56b4cd p.plitzner
            if(firstElement instanceof Command && nameViewerMap.containsKey(firstElement)){
148
                executeCommand((Command) firstElement, this.input);
149 b93817a3 Patric Plitzner
                this.close();
150 3dba9fc4 Patric Plitzner
            }
151
        }
152
    }
153
154 6a159a76 Patric Plitzner
    @Override
155
    public String getText(Object element) {
156 250453cf Patrick Plitzner
        return nameViewerMap.get(element);
157 6a159a76 Patric Plitzner
    }
158
159
    @Override
160
    public void addListener(ILabelProviderListener listener) {
161
        // TODO Auto-generated method stub
162
163
    }
164
165
    @Override
166
    public void dispose() {
167
        // TODO Auto-generated method stub
168
169
    }
170
171
    @Override
172
    public boolean isLabelProperty(Object element, String property) {
173
        // TODO Auto-generated method stub
174
        return false;
175
    }
176
177
    @Override
178
    public void removeListener(ILabelProviderListener listener) {
179
        // TODO Auto-generated method stub
180
181
    }
182
183
    @Override
184
    public Image getImage(Object element) {
185
        // TODO Auto-generated method stub
186
        return null;
187
    }
188
189 3dba9fc4 Patric Plitzner
}