]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/CompilePGraphsAction.java
eda0228f9049d08ab1fba7e8ea0e5983d603de48
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / CompilePGraphsAction.java
1 package org.simantics.modeling.ui.actions;
2
3 import java.io.IOException;
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.jface.dialogs.MessageDialog;
10 import org.eclipse.jface.layout.GridDataFactory;
11 import org.eclipse.jface.layout.GridLayoutFactory;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Control;
15 import org.eclipse.swt.widgets.Display;
16 import org.eclipse.swt.widgets.Shell;
17 import org.simantics.db.Resource;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.layer0.adapter.ActionFactory;
20 import org.simantics.graph.compiler.CompilationResult;
21 import org.simantics.ltk.Problem;
22 import org.simantics.modeling.CompilePGraphs;
23 import org.simantics.modeling.ui.Activator;
24 import org.simantics.utils.ui.ExceptionUtils;
25
26 /**
27  * @author Antti Villberg
28  */
29 public class CompilePGraphsAction implements ActionFactory {
30
31     @Override
32     public Runnable create(Object target) {
33         if (!(target instanceof Resource))
34             return null;
35         return () -> {
36             Job job = new Job("Compile PGraphs") {
37                 @Override
38                 protected IStatus run(IProgressMonitor monitor) {
39                     try {
40                         CompilePGraphs.compilePGraphs((Resource) target, new CompileUserAgent(), monitor);
41                         return Status.OK_STATUS;
42                     } catch (IOException | DatabaseException e) {
43                         ExceptionUtils.logAndShowError(e);
44                         return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage());
45                     } finally {
46                         monitor.done();
47                     }
48                 }
49             };
50             job.schedule();
51         };
52     }
53
54     public static class CompileUserAgent implements CompilePGraphs.UserAgent {
55         @Override
56         public void reportProblems(CompilationResult result) {
57             class ErrorMessageDialog extends MessageDialog {
58                 public ErrorMessageDialog(Shell shell) {
59                     super(shell, 
60                             "Unsatisfied dependencies", null, 
61                             "The following dependencies were missing. Please import the dependencies and try again.", 
62                             MessageDialog.ERROR, new String[] { "Continue" }, 0);
63                 }
64
65                 @Override
66                 protected Control createCustomArea(Composite composite) {
67                     GridLayoutFactory.fillDefaults().applyTo(composite);
68
69                     org.eclipse.swt.widgets.List list = new org.eclipse.swt.widgets.List(composite, SWT.BORDER | SWT.READ_ONLY);
70                     GridDataFactory.fillDefaults().grab(true, true).applyTo(list);
71                     for (Problem problem : result.getErrors())
72                         list.add(problem.getLocation() + ": " + problem.getDescription() + "\n");
73                     for (Problem problem : result.getWarnings())
74                         list.add(problem.getLocation() + ": " + problem.getDescription() + "\n");
75
76                     return composite;
77                 }
78             }
79
80             ErrorMessageDialog md = new ErrorMessageDialog(Display.getCurrent().getActiveShell());
81             md.open();
82         }
83     }
84
85 }