1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor;
14 import java.util.Collections;
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;
28 public class DiagramViewerLoadJob extends DatabaseJob {
30 private static final boolean PROFILE = false;
31 private DiagramViewer viewer;
33 public DiagramViewerLoadJob(DiagramViewer viewer) {
34 super("Load Diagram");
40 protected IStatus run(final IProgressMonitor monitor) {
41 final SubMonitor mon = SubMonitor.convert(monitor, "Loading Diagram", 200);
44 Object task = BEGIN("DV.loadDiagram");
45 final IDiagram diagram = viewer.loadDiagram(mon.newChild(100), viewer.diagramResource);
47 return Status.CANCEL_STATUS;
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));
56 // Wait for load completion in AWT thread
57 ThreadUtils.syncExec(viewer.canvasContext.getThreadAccess(), new Runnable() {
60 setThread(viewer.canvasContext.getThreadAccess().getThread());
61 mon.setTaskName("Finalize Diagram Loading");
64 Object task = BEGIN("DV.beforeSetDiagram");
65 viewer.beforeSetDiagram(diagram);
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);
79 viewer.selectionProvider.fireSelection(Collections.emptyList());
81 // Zoom to fit if no previous view transform is available
82 task = BEGIN("DV.scheduleZoomToFit");
83 viewer.scheduleZoomToFit(diagram);
87 task = BEGIN("DV.onCreated");
93 task = BEGIN("DV.applyEditorState");
94 mon.subTask("Apply editor state");
95 viewer.applyEditorState(viewer.editorState, viewer.canvasContext);
99 task = BEGIN("DV.activateUiContexts");
100 viewer.contextUtil.inContextThread(new Runnable() {
103 if (!viewer.disposed)
104 viewer.activateUiContexts(viewer.contextUtil);
109 } catch (Throwable t) {
115 return Status.OK_STATUS;
116 } catch (CancelTransactionException e) {
119 return new Status(IStatus.CANCEL, Activator.PLUGIN_ID, "Diagram loading was cancelled.", e);
120 } catch (Throwable t) {
123 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Diagram loading failed, see exception for details.", t);
127 protected static Object BEGIN(String name) {
129 //return ThreadLog.BEGIN(name);
134 protected static void END(Object task) {
136 //((Task) task).end();