]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramViewerLoadJob.java
Log throwables in DiagramViewerLoadJob instead of swallowing them
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / DiagramViewerLoadJob.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor;
13
14 import java.util.Collections;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.runtime.SubMonitor;
20 import org.simantics.DatabaseJob;
21 import org.simantics.db.exception.CancelTransactionException;
22 import org.simantics.g2d.diagram.DiagramHints;
23 import org.simantics.g2d.diagram.IDiagram;
24 import org.simantics.modeling.ui.Activator;
25 import org.simantics.utils.DataContainer;
26 import org.simantics.utils.threads.ThreadUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class DiagramViewerLoadJob extends DatabaseJob {
31
32     private static final Logger LOGGER = LoggerFactory.getLogger(DiagramViewerLoadJob.class);
33
34     private static final boolean PROFILE = false;
35     private DiagramViewer        viewer;
36
37     public DiagramViewerLoadJob(DiagramViewer viewer) {
38         super(Messages.DiagramViewerLoadJob_LoadDiagram);
39         setUser(true);
40         this.viewer = viewer;
41     }
42
43     @Override
44     protected IStatus run(final IProgressMonitor monitor) {
45         final SubMonitor mon = SubMonitor.convert(monitor, Messages.DiagramViewerLoadJob_MonitorLoadingDiagram, 200);
46
47         try {
48             Object task = BEGIN("DV.loadDiagram"); //$NON-NLS-1$
49             final IDiagram diagram = viewer.loadDiagram(mon.newChild(100), viewer.diagramResource);
50             if (diagram == null)
51                 return Status.CANCEL_STATUS;
52             END(task);
53
54             // Start an activation for the input resource.
55             // This will activate mapping if necessary.
56             task = BEGIN("DV.performActivation"); //$NON-NLS-1$
57             viewer.performActivation(mon.newChild(50));
58             END(task);
59
60             // Wait for load completion in AWT thread
61             ThreadUtils.syncExec(viewer.canvasContext.getThreadAccess(), new Runnable() {
62                 @Override
63                 public void run() {
64                     setThread(viewer.canvasContext.getThreadAccess().getThread());
65                     mon.setTaskName(Messages.DiagramViewerLoadJob_MonitorFinalizeDiagramLoading);
66
67                     try {
68                         Object task = BEGIN("DV.beforeSetDiagram"); //$NON-NLS-1$
69                         viewer.beforeSetDiagram(diagram);
70                         mon.worked(10);
71                         END(task);
72
73                         task = BEGIN("DV.setDiagramHint"); //$NON-NLS-1$
74                         mon.subTask(Messages.DiagramViewerLoadJob_SetDiagram);
75                         DataContainer<IDiagram> diagramContainer = viewer.sourceDiagramContainer;
76                         if (diagramContainer != null)
77                             diagramContainer.set( diagram );
78                         viewer.sourceDiagram = diagram;
79                         viewer.canvasContext.getDefaultHintContext().setHint(DiagramHints.KEY_DIAGRAM, diagram);
80                         mon.worked(10);
81                         END(task);
82
83                         viewer.selectionProvider.fireSelection(Collections.emptyList());
84                         
85                         // Zoom to fit if no previous view transform is available
86                         task = BEGIN("DV.scheduleZoomToFit"); //$NON-NLS-1$
87                         viewer.scheduleZoomToFit(diagram);
88                         mon.worked(10);
89                         END(task);
90
91                         task = BEGIN("DV.onCreated"); //$NON-NLS-1$
92                         mon.subTask(""); //$NON-NLS-1$
93                         viewer.onCreated();
94                         mon.worked(10);
95                         END(task);
96
97                         task = BEGIN("DV.applyEditorState"); //$NON-NLS-1$
98                         mon.subTask(Messages.DiagramViewerLoadJob_ApplyEditorState);
99                         viewer.applyEditorState(viewer.editorState, viewer.canvasContext);
100                         mon.worked(10);
101                         END(task);
102
103                         task = BEGIN("DV.activateUiContexts"); //$NON-NLS-1$
104                         viewer.contextUtil.inContextThread(new Runnable() {
105                             @Override
106                             public void run() {
107                                 if (!viewer.disposed)
108                                     viewer.activateUiContexts(viewer.contextUtil);
109                                 viewer = null;
110                             }
111                         });
112                         END(task);
113                     } catch (Throwable t) {
114                         viewer = null;
115                         LOGGER.error("Failed to complete loading of diagram {} in the canvas thread", viewer.diagramResource, t);
116                     }
117                 }
118             });
119
120             return Status.OK_STATUS;
121         } catch (CancelTransactionException e) {
122             monitor.done();
123             viewer = null;
124             return new Status(IStatus.CANCEL, Activator.PLUGIN_ID, Messages.DiagramViewerLoadJob_ActivatorDiagramLoadingCancelled, e);
125         } catch (Throwable t) {
126             monitor.done();
127             viewer = null;
128             return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.DiagramViewerLoadJob_ActivatorDiagramLoadingFailed, t);
129         }
130     }
131
132     protected static Object BEGIN(String name) {
133         if (PROFILE) {
134             //return ThreadLog.BEGIN(name);
135         }
136         return null;
137     }
138
139     protected static void END(Object task) {
140         if (PROFILE) {
141             //((Task) task).end();
142         }
143     }
144 }