]> gerrit.simantics Code Review - simantics/district.git/blob
5ae0c0d0fdfbcaa6fd2cc9496b71b3e57184374d
[simantics/district.git] /
1 package org.simantics.district.network.ui.contributions;
2
3 import javax.inject.Named;
4
5 import org.eclipse.core.runtime.IProgressMonitor;
6 import org.eclipse.core.runtime.IStatus;
7 import org.eclipse.core.runtime.Status;
8 import org.eclipse.core.runtime.jobs.Job;
9 import org.eclipse.e4.core.di.annotations.CanExecute;
10 import org.eclipse.e4.core.di.annotations.Execute;
11 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
12 import org.eclipse.e4.ui.services.IServiceConstants;
13 import org.eclipse.ui.IEditorPart;
14 import org.simantics.DatabaseJob;
15 import org.simantics.Simantics;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.common.request.WriteRequest;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.district.network.ui.internal.Activator;
20 import org.simantics.ui.workbench.IResourceEditorInput;
21 import org.simantics.ui.workbench.e4.E4WorkbenchUtils;
22
23 public class RunMappingHandler {
24
25     @CanExecute
26     public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) {
27         IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
28         return activeEditorPart != null;
29     }
30     
31     @Execute
32     public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) {
33         Job job = new DatabaseJob("Map network to Apros") {
34             
35             @Override
36             protected IStatus run(IProgressMonitor monitor) {
37                 try {
38                     IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
39                     IResourceEditorInput input = (IResourceEditorInput) activeEditorPart.getEditorInput();
40                     Simantics.getSession().syncRequest(new WriteRequest() {
41                         
42                         @Override
43                         public void perform(WriteGraph graph) throws DatabaseException {
44                             Object result = Simantics.applySCL("Apros/District/DistrictMapping", "mapNetworkToAprosProgress", graph, monitor, input.getResource());
45                         }
46                     });
47                 } catch (DatabaseException e) {
48                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);
49                 }
50                 return Status.OK_STATUS;
51             }
52         };
53         job.setUser(true);
54         job.schedule();
55     }
56 }