- added missing class filter to cdmViewer popup dialog
[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.ArrayList;
13 import java.util.Collection;
14
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;
35
36 import eu.etaxonomy.taxeditor.model.MessagingUtils;
37
38 /**
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.
41 *
42 * @author pplitzner
43 * @date Feb 23, 2015
44 *
45 */
46 public class CdmViewerChooser extends PopupDialog implements ISelectionChangedListener, ILabelProvider{
47
48 private Object input;
49 private Collection<IConfigurationElement> partConfigurationElements;
50
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");
54 }
55
56 public CdmViewerChooser(Shell parent, int shellStyle, boolean takeFocusOnOpen, boolean persistSize,
57 boolean persistLocation, boolean showDialogMenu, boolean showPersistActions, String titleText,
58 String infoText) {
59 super(parent, shellStyle, takeFocusOnOpen, persistSize, persistLocation, showDialogMenu, showPersistActions,
60 titleText, infoText);
61 }
62
63 /**
64 * Opens a popup dialog with all possible viewers for the given input.
65 * @param input the input for which the viewers are listed
66 */
67 public void chooseViewer(Object input){
68 this.input = input;
69 partConfigurationElements = new ArrayList<>();
70
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);
81 }
82 }
83 }
84 }
85 this.open();
86 }
87
88 /* (non-Javadoc)
89 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
90 */
91 @Override
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);
98 return parent;
99 }
100
101 /* (non-Javadoc)
102 * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
103 */
104 @Override
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");
112 try {
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);
117 this.close();
118 }
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);
123 }
124 }
125 }
126 }
127
128 /* (non-Javadoc)
129 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
130 */
131 @Override
132 public String getText(Object element) {
133 String text = null;
134 if(element instanceof IConfigurationElement){
135 IConfigurationElement configElement = (IConfigurationElement)element;
136 text = configElement.getAttribute("name");
137 if(text==null){
138 text = configElement.getAttribute("class");
139 }
140 }
141 return text;
142 }
143
144 /* (non-Javadoc)
145 * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
146 */
147 @Override
148 public void addListener(ILabelProviderListener listener) {
149 // TODO Auto-generated method stub
150
151 }
152
153 /* (non-Javadoc)
154 * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
155 */
156 @Override
157 public void dispose() {
158 // TODO Auto-generated method stub
159
160 }
161
162 /* (non-Javadoc)
163 * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
164 */
165 @Override
166 public boolean isLabelProperty(Object element, String property) {
167 // TODO Auto-generated method stub
168 return false;
169 }
170
171 /* (non-Javadoc)
172 * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
173 */
174 @Override
175 public void removeListener(ILabelProviderListener listener) {
176 // TODO Auto-generated method stub
177
178 }
179
180 /* (non-Javadoc)
181 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
182 */
183 @Override
184 public Image getImage(Object element) {
185 // TODO Auto-generated method stub
186 return null;
187 }
188
189 }