03c7291bc31700d368b197100480b39f23ca0c76
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / operation / e4 / RemotingCdmHandlerE4.java
1 /**
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.operation.e4;
10
11 import javax.inject.Named;
12
13 import org.eclipse.core.commands.operations.AbstractOperation;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
16 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
17 import org.eclipse.e4.ui.services.IServiceConstants;
18 import org.eclipse.jface.dialogs.ErrorDialog;
19 import org.eclipse.jface.viewers.TreeSelection;
20 import org.eclipse.swt.widgets.Shell;
21
22 import eu.etaxonomy.taxeditor.model.AbstractUtility;
23 import eu.etaxonomy.taxeditor.model.MessagingUtils;
24
25 /**
26 * @author cmathew
27 * @date 16 Jun 2015
28 *
29 */
30 public abstract class RemotingCdmHandlerE4 {
31
32 private final String label;
33
34 public RemotingCdmHandlerE4(String label) {
35 this.label = label;
36 }
37
38 public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection,
39 @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
40 @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
41 MHandledMenuItem menuItem) {
42 IStatus allowStatus = allowOperations(selection, shell, activePart, menuItem);
43 if(allowStatus.isOK()) {
44 AbstractOperation op = prepareOperation(selection, shell, activePart, menuItem);
45 if(op != null) {
46 AbstractUtility.executeOperation(op, this);
47 }
48 } else if(allowStatus.getSeverity() == IStatus.ERROR ||
49 allowStatus.getSeverity() == IStatus.WARNING ||
50 allowStatus.getSeverity() == IStatus.INFO) {
51 MessagingUtils.warningDialog("Can not perform " + label, getTrigger(), allowStatus);
52 }
53 return;
54 }
55
56 public void postOperation(IStatus status) {
57
58 switch(status.getSeverity()) {
59 case IStatus.WARNING:
60 ErrorDialog.openError(null, "Operation successful but with warnings", null, status);
61 break;
62 case IStatus.ERROR:
63 ErrorDialog.openError(null, "Error executing operation", null, status);
64 break;
65 default:
66 }
67
68 onComplete();
69 }
70
71 public abstract IStatus allowOperations(TreeSelection selection,
72 Shell shell,
73 MPart activePart,
74 MHandledMenuItem menuItem);
75
76 public abstract AbstractOperation prepareOperation(TreeSelection selection,
77 Shell shell,
78 MPart activePart,
79 MHandledMenuItem menuItem);
80
81 public abstract void onComplete();
82
83 protected abstract Object getTrigger();
84
85 }