]> gerrit.simantics Code Review - simantics/platform.git/blob
dcadd4d9130cb872aca11a096066e46412f9a1fd
[simantics/platform.git] /
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 org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.SubMonitor;
18 import org.simantics.DatabaseJob;
19 import org.simantics.db.exception.CancelTransactionException;
20 import org.simantics.g2d.diagram.DiagramHints;
21 import org.simantics.g2d.diagram.IDiagram;
22 import org.simantics.modeling.ui.Activator;
23 import org.simantics.utils.DataContainer;
24 import org.simantics.utils.threads.ThreadUtils;
25
26 public class DiagramViewerLoadJob extends DatabaseJob {
27
28     private static final boolean PROFILE = false;
29     private DiagramViewer        viewer;
30
31     public DiagramViewerLoadJob(DiagramViewer viewer) {
32         super("Load Diagram");
33         setUser(true);
34         this.viewer = viewer;
35     }
36
37     @Override
38     protected IStatus run(final IProgressMonitor monitor) {
39         final SubMonitor mon = SubMonitor.convert(monitor, "Loading Diagram", 200);
40
41         try {
42             Object task = BEGIN("DV.loadDiagram");
43             final IDiagram diagram = viewer.loadDiagram(mon.newChild(100), viewer.diagramResource);
44             if (diagram == null)
45                 return Status.CANCEL_STATUS;
46             END(task);
47
48             // Start an activation for the input resource.
49             // This will activate mapping if necessary.
50             task = BEGIN("DV.performActivation");
51             viewer.performActivation(mon.newChild(50));
52             END(task);
53
54             // Wait for load completion in AWT thread
55             ThreadUtils.syncExec(viewer.canvasContext.getThreadAccess(), new Runnable() {
56                 @Override
57                 public void run() {
58                     setThread(viewer.canvasContext.getThreadAccess().getThread());
59                     mon.setTaskName("Finalize Diagram Loading");
60
61                     try {
62                         Object task = BEGIN("DV.beforeSetDiagram");
63                         viewer.beforeSetDiagram(diagram);
64                         mon.worked(10);
65                         END(task);
66
67                         task = BEGIN("DV.setDiagramHint");
68                         mon.subTask("Set Diagram");
69                         DataContainer<IDiagram> diagramContainer = viewer.sourceDiagramContainer;
70                         if (diagramContainer != null)
71                             diagramContainer.set( diagram );
72                         viewer.sourceDiagram = diagram;
73                         viewer.canvasContext.getDefaultHintContext().setHint(DiagramHints.KEY_DIAGRAM, diagram);
74                         mon.worked(10);
75                         END(task);
76
77                         // Zoom to fit if no previous view transform is available
78                         task = BEGIN("DV.scheduleZoomToFit");
79                         viewer.scheduleZoomToFit(diagram);
80                         mon.worked(10);
81                         END(task);
82
83                         task = BEGIN("DV.onCreated");
84                         mon.subTask("");
85                         viewer.onCreated();
86                         mon.worked(10);
87                         END(task);
88
89                         task = BEGIN("DV.applyEditorState");
90                         mon.subTask("Apply editor state");
91                         viewer.applyEditorState(viewer.editorState, viewer.canvasContext);
92                         mon.worked(10);
93                         END(task);
94
95                         task = BEGIN("DV.activateUiContexts");
96                         viewer.contextUtil.inContextThread(new Runnable() {
97                             @Override
98                             public void run() {
99                                 if (!viewer.disposed)
100                                     viewer.activateUiContexts(viewer.contextUtil);
101                                 viewer = null;
102                             }
103                         });
104                         END(task);
105                     } catch (Throwable t) {
106                         viewer = null;
107                     }
108                 }
109             });
110
111             return Status.OK_STATUS;
112         } catch (CancelTransactionException e) {
113             monitor.done();
114             viewer = null;
115             return new Status(IStatus.CANCEL, Activator.PLUGIN_ID, "Diagram loading was cancelled.", e);
116         } catch (Throwable t) {
117             monitor.done();
118             viewer = null;
119             return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Diagram loading failed, see exception for details.", t);
120         }
121     }
122
123     protected static Object BEGIN(String name) {
124         if (PROFILE) {
125             //return ThreadLog.BEGIN(name);
126         }
127         return null;
128     }
129
130     protected static void END(Object task) {
131         if (PROFILE) {
132             //((Task) task).end();
133         }
134     }
135 }