import java.util.HashMap;
import java.util.Map;
-import java.util.Map.Entry;
-
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IConfigurationElement;
-import org.eclipse.core.runtime.IExtensionRegistry;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Platform;
-import org.eclipse.core.runtime.Status;
+import java.util.UUID;
+
+import org.eclipse.core.commands.Command;
+import org.eclipse.core.commands.ParameterizedCommand;
+import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
-
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.handlers.IHandlerService;
+
+import eu.etaxonomy.cdm.api.service.IOccurrenceService;
+import eu.etaxonomy.cdm.api.service.ITaxonService;
+import eu.etaxonomy.cdm.model.common.ICdmBase;
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
+import eu.etaxonomy.cdm.model.taxon.Synonym;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
import eu.etaxonomy.taxeditor.model.MessagingUtils;
-import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
+import eu.etaxonomy.taxeditor.store.CdmStore;
/**
* This class opens a popup dialog and provides the possibility to choose from a
*/
public class CdmViewerChooser extends PopupDialog implements ISelectionChangedListener, ILabelProvider{
+ private Map<Command, String> nameViewerMap;
private Object input;
- private Map<Entry<Class<?>, String>, ICdmViewer> nameViewerMap;
public CdmViewerChooser(Shell parentShell) {
this(parentShell, SWT.RESIZE | SWT.ON_TOP, true, false, false, false, false, "Open in ...",
titleText, infoText);
}
- public static Map<Entry<Class<?>, String>, ICdmViewer> getNameViewerMap(Object input){
- Map<Entry<Class<?>, String>, ICdmViewer> nameViewerMap = new HashMap<Entry<Class<?>, String>, ICdmViewer>();
-
- IExtensionRegistry reg = Platform.getExtensionRegistry();
- IConfigurationElement[] extensions = reg
- .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer");
- for (IConfigurationElement configElement : extensions) {
- try {
- Object object = configElement.createExecutableExtension("class");
- if(object instanceof ICdmViewer){
- ICdmViewer cdmViewer = (ICdmViewer)object;
- Map<Class<?>, String> viewerClasses = cdmViewer.getViewerClasses(input);
- for (Entry<Class<?>, String> entry : viewerClasses.entrySet()) {
- nameViewerMap.put(entry, cdmViewer);
- }
- }
- else{
- MessagingUtils.error(CdmViewerChooser.class, new Status(IStatus.ERROR, TaxeditorStorePlugin.PLUGIN_ID, "Could not load cdmViewer extension"));
- }
- } catch (CoreException e) {
- MessagingUtils.error(CdmViewerChooser.class, "Could not load cdmViewer extension", e);
- }
- }
- return nameViewerMap;
- }
-
/**
* Opens a popup dialog with all possible viewers for the given input.
* @param input the input for which the viewers are listed
*/
public void chooseViewer(Object input){
this.input = input;
- this.nameViewerMap = getNameViewerMap(input);
+ this.nameViewerMap = CdmViewerUtil.getAvailableViewers(input);
//if only one editor is available then open it
if(nameViewerMap.size()==1){
- Entry<Class<?>, String> next = nameViewerMap.keySet().iterator().next();
- ICdmViewer cdmViewer = nameViewerMap.get(next);
- cdmViewer.show(input, next.getKey());
+ Command command = nameViewerMap.keySet().iterator().next();
+ executeCommand(command, input);
}
else{
if(nameViewerMap.isEmpty()){
}
}
+ private void executeCommand(Command command, Object input) {
+ //for generic UuidAndTitleCache objects try to load the object
+ if (input instanceof UuidAndTitleCache){
+ UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache)input;
+ Class type = uuidAndTitleCache.getType();
+ if(type == Taxon.class || type == Synonym.class){
+ input = CdmStore.getService(ITaxonService.class).load(uuidAndTitleCache.getUuid());
+ }
+ else if(SpecimenOrObservationBase.class.isAssignableFrom(type)){
+ input = CdmStore.getService(IOccurrenceService.class).load(uuidAndTitleCache.getUuid());
+ }
+ }
+ //set uuid parameter
+ if(input instanceof ICdmBase){
+ Map<String, UUID> params = new HashMap<String, UUID>();
+ String commandId = command.getId();
+ params.put(commandId+".uuid", ((ICdmBase) input).getUuid());
+
+ if(command.isEnabled()) {
+
+ //build the parameterized command
+ ParameterizedCommand pc = ParameterizedCommand.generateCommand(command, params);
+
+ IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
+ try {
+ if(pc!=null){
+ handlerService.executeCommand(pc, null);
+ }
+ else{
+ handlerService.executeCommand(commandId, null);
+ }
+ } catch (NotDefinedException nde) {
+ throw new RuntimeException("Could not find open command: " + commandId);
+ } catch (Exception exception) {
+ MessagingUtils.error(getClass(), "An exception occured while trying execute "+commandId, exception);
+ }
+ }
+ }
+ }
+
@Override
protected Control createDialogArea(Composite parent) {
TableViewer viewer = new TableViewer(new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION));
ISelection selection = event.getSelection();
if(selection instanceof IStructuredSelection){
Object firstElement = ((IStructuredSelection) selection).getFirstElement();
- if(nameViewerMap.containsKey(firstElement)){
- Entry<Class<?>, String> entry = (Entry<Class<?>, String>)firstElement;
- ICdmViewer cdmViewer = nameViewerMap.get(entry);
- cdmViewer.show(input, entry.getKey());
+ if(firstElement instanceof Command && nameViewerMap.containsKey(firstElement)){
+ executeCommand((Command) firstElement, this.input);
this.close();
}
}
@Override
public String getText(Object element) {
- String text = null;
- if(nameViewerMap.containsKey(element)){
- Entry<Class<?>, String> entry = (Entry<Class<?>, String>) element;
- text = entry.getValue();
- }
- return text;
+ return nameViewerMap.get(element);
}
@Override