ref #8223: check success before try to open new taxon
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / e4 / handler / NewTaxonNodeHandlerE4.java
1 /**
2 * Copyright (C) 2007 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
10 package eu.etaxonomy.taxeditor.navigation.navigator.e4.handler;
11
12 import javax.inject.Named;
13
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.e4.core.di.annotations.CanExecute;
16 import org.eclipse.e4.core.di.annotations.Execute;
17 import org.eclipse.e4.ui.model.application.MApplication;
18 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
19 import org.eclipse.e4.ui.services.IServiceConstants;
20 import org.eclipse.e4.ui.workbench.modeling.EModelService;
21 import org.eclipse.e4.ui.workbench.modeling.EPartService;
22 import org.eclipse.jface.viewers.ITreeSelection;
23 import org.eclipse.jface.viewers.TreeSelection;
24 import org.eclipse.jface.wizard.WizardDialog;
25 import org.eclipse.swt.widgets.Shell;
26
27 import eu.etaxonomy.taxeditor.editor.EditorUtil;
28 import eu.etaxonomy.taxeditor.event.EventUtility;
29 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
30 import eu.etaxonomy.taxeditor.newWizard.IWizardPageListener;
31 import eu.etaxonomy.taxeditor.newWizard.NewTaxonNodeWizard;
32 import eu.etaxonomy.taxeditor.newWizard.NewTaxonNodeWizardDialog;
33
34 /**
35 *
36 * @author pplitzner
37 * @date 05.09.2017
38 *
39 */
40 public class NewTaxonNodeHandlerE4 {
41
42 @Execute
43 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
44 @Named(IServiceConstants.ACTIVE_SELECTION) ITreeSelection selection, EModelService modelService,
45 EPartService partService, MApplication application) throws Exception {
46
47 final NewTaxonNodeWizard wizard = new NewTaxonNodeWizard();
48 wizard.init(null, selection);
49 final WizardDialog dialog = new NewTaxonNodeWizardDialog(shell, wizard);
50 wizard.addWizardPageListener(new IWizardPageListener() {
51 @Override
52 public void close() {
53 wizard.performFinish();
54 dialog.close();
55 }
56 });
57 int status = dialog.open();
58
59 if(status == IStatus.OK && wizard.openInEditor() && wizard.isSuccess()){
60 EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, true);
61 if(wizard.openEmpty()){
62 EditorUtil.openEmptyE4(wizard.getParentTreeNode().getUuid());
63 }else if(wizard.getEntity() != null){
64 EditorUtil.openTaxonNodeE4(wizard.getEntity().getUuid(), modelService, partService, application);
65 }
66 }
67 }
68
69 @CanExecute
70 private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection,
71 MHandledMenuItem menuItem){
72 boolean canExecute = false;
73 canExecute = selection!=null && selection.size()==1;
74 menuItem.setVisible(canExecute);
75 return canExecute;
76 }
77 }