3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.view
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collection
;
15 import org
.eclipse
.core
.runtime
.CoreException
;
16 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
17 import org
.eclipse
.core
.runtime
.IExtensionRegistry
;
18 import org
.eclipse
.core
.runtime
.InvalidRegistryObjectException
;
19 import org
.eclipse
.core
.runtime
.Platform
;
20 import org
.eclipse
.jface
.dialogs
.PopupDialog
;
21 import org
.eclipse
.jface
.viewers
.ArrayContentProvider
;
22 import org
.eclipse
.jface
.viewers
.ILabelProvider
;
23 import org
.eclipse
.jface
.viewers
.ILabelProviderListener
;
24 import org
.eclipse
.jface
.viewers
.ISelection
;
25 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
26 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
27 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
28 import org
.eclipse
.jface
.viewers
.TableViewer
;
29 import org
.eclipse
.swt
.SWT
;
30 import org
.eclipse
.swt
.graphics
.Image
;
31 import org
.eclipse
.swt
.widgets
.Composite
;
32 import org
.eclipse
.swt
.widgets
.Control
;
33 import org
.eclipse
.swt
.widgets
.Shell
;
34 import org
.eclipse
.swt
.widgets
.Table
;
36 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
39 * This class provides the possibility to choose from a list of possible viewers
40 * which can be opened for a given input in a popup dialog.
46 public class CdmViewerChooser
extends PopupDialog
implements ISelectionChangedListener
, ILabelProvider
{
49 private Collection
<IConfigurationElement
> partConfigurationElements
;
51 public CdmViewerChooser(Shell parentShell
) {
52 this(parentShell
, SWT
.RESIZE
| SWT
.ON_TOP
, true, false, false, false, false, "Open in ...",
53 "Clicking will open the selected viewer");
56 public CdmViewerChooser(Shell parent
, int shellStyle
, boolean takeFocusOnOpen
, boolean persistSize
,
57 boolean persistLocation
, boolean showDialogMenu
, boolean showPersistActions
, String titleText
,
59 super(parent
, shellStyle
, takeFocusOnOpen
, persistSize
, persistLocation
, showDialogMenu
, showPersistActions
,
64 * Opens a popup dialog with all possible viewers for the given input.
65 * @param input the input for which the viewers are listed
67 public void chooseViewer(Object input
){
69 partConfigurationElements
= new ArrayList
<>();
71 IExtensionRegistry reg
= Platform
.getExtensionRegistry();
72 IConfigurationElement
[] extensions
= reg
73 .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer");
74 for (IConfigurationElement e
: extensions
) {
75 for (IConfigurationElement inputConfigurationElement
: e
.getChildren("input")) {
76 String inputClass
= inputConfigurationElement
.getAttribute("class");
77 if(inputClass
.equals(input
.getClass().toString())){
78 System
.out
.println("input class: " + inputClass
);
79 for (IConfigurationElement partConfigurationElement
: inputConfigurationElement
.getChildren("part")) {
80 partConfigurationElements
.add(partConfigurationElement
);
89 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
92 protected Control
createDialogArea(Composite parent
) {
93 TableViewer viewer
= new TableViewer(new Table(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
94 viewer
.setContentProvider(new ArrayContentProvider());
95 viewer
.setLabelProvider(this);
96 viewer
.addSelectionChangedListener(this);
97 viewer
.setInput(partConfigurationElements
);
102 * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
105 public void selectionChanged(SelectionChangedEvent event
) {
106 ISelection selection
= event
.getSelection();
107 if(selection
instanceof IStructuredSelection
){
108 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
109 if(firstElement
instanceof IConfigurationElement
){
110 IConfigurationElement configElement
= (IConfigurationElement
)firstElement
;
111 String viewerClass
= configElement
.getAttribute("class");
113 //get the grand parent (this is the cdmViewer)
114 Object o
= ((IConfigurationElement
)((IConfigurationElement
)configElement
.getParent()).getParent()).createExecutableExtension("class");
115 if(o
instanceof ICdmViewer
){
116 ((ICdmViewer
) o
).show(input
, viewerClass
);
119 } catch (InvalidRegistryObjectException e
) {
120 MessagingUtils
.error(CdmViewerChooser
.class, "Could not load cdmViewer extension", e
);
121 } catch (CoreException e
) {
122 MessagingUtils
.error(CdmViewerChooser
.class, "Could not load cdmViewer extension", e
);
129 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
132 public String
getText(Object element
) {
134 if(element
instanceof IConfigurationElement
){
135 IConfigurationElement configElement
= (IConfigurationElement
)element
;
136 text
= configElement
.getAttribute("name");
138 text
= configElement
.getAttribute("class");
145 * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
148 public void addListener(ILabelProviderListener listener
) {
149 // TODO Auto-generated method stub
154 * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
157 public void dispose() {
158 // TODO Auto-generated method stub
163 * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
166 public boolean isLabelProperty(Object element
, String property
) {
167 // TODO Auto-generated method stub
172 * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
175 public void removeListener(ILabelProviderListener listener
) {
176 // TODO Auto-generated method stub
181 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
184 public Image
getImage(Object element
) {
185 // TODO Auto-generated method stub