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