]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/CompilePGraphsAction.java
Allow simulation stop handling in Simantics/Sequences
[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.graph.compiler.internal.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(Messages.CompilePGraphsAction_CompilePGraphs) {
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         
56         @Override
57         public void reportProblems(CompilationResult result) {
58                 Runnable runnable = () -> {
59                         class ErrorMessageDialog extends MessageDialog {
60                                 public ErrorMessageDialog(Shell shell) {
61                                         super(shell, 
62                                                         Messages.CompilePGraphsAction_ProblemsinOntologyDefinitionFile, null, 
63                                                         Messages.CompilePGraphsAction_FollowingIssuesFound, 
64                                                         MessageDialog.ERROR, new String[] { Messages.CompilePGraphsAction_Continue }, 0);
65                                 }
66
67                                 @Override
68                                 protected Control createCustomArea(Composite composite) {
69                                         GridLayoutFactory.fillDefaults().applyTo(composite);
70
71                                         org.eclipse.swt.widgets.List list = new org.eclipse.swt.widgets.List(composite, SWT.BORDER | SWT.READ_ONLY);
72                                         GridDataFactory.fillDefaults().grab(true, true).applyTo(list);
73                                         for (Problem problem : result.getErrors())
74                                                 list.add(problem.getLocation() + ": " + problem.getDescription() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
75                                         for (Problem problem : result.getWarnings())
76                                                 list.add(problem.getLocation() + ": " + problem.getDescription() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
77
78                                         return composite;
79                                 }
80                         }
81
82                         ErrorMessageDialog md = new ErrorMessageDialog(Display.getCurrent().getActiveShell());
83                         md.open();
84                 };              
85                 
86                 Display display = Display.getCurrent();
87                 if (display == null)
88                         display = Display.getDefault();
89                 display.asyncExec(runnable);
90         }
91
92     }
93
94 }