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 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;
26 public class DiagramViewerLoadJob extends DatabaseJob {
28 private static final boolean PROFILE = false;
29 private DiagramViewer viewer;
31 public DiagramViewerLoadJob(DiagramViewer viewer) {
32 super("Load Diagram");
38 protected IStatus run(final IProgressMonitor monitor) {
39 final SubMonitor mon = SubMonitor.convert(monitor, "Loading Diagram", 200);
42 Object task = BEGIN("DV.loadDiagram");
43 final IDiagram diagram = viewer.loadDiagram(mon.newChild(100), viewer.diagramResource);
45 return Status.CANCEL_STATUS;
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));
54 // Wait for load completion in AWT thread
55 ThreadUtils.syncExec(viewer.canvasContext.getThreadAccess(), new Runnable() {
58 setThread(viewer.canvasContext.getThreadAccess().getThread());
59 mon.setTaskName("Finalize Diagram Loading");
62 Object task = BEGIN("DV.beforeSetDiagram");
63 viewer.beforeSetDiagram(diagram);
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);
77 // Zoom to fit if no previous view transform is available
78 task = BEGIN("DV.scheduleZoomToFit");
79 viewer.scheduleZoomToFit(diagram);
83 task = BEGIN("DV.onCreated");
89 task = BEGIN("DV.applyEditorState");
90 mon.subTask("Apply editor state");
91 viewer.applyEditorState(viewer.editorState, viewer.canvasContext);
95 task = BEGIN("DV.activateUiContexts");
96 viewer.contextUtil.inContextThread(new Runnable() {
100 viewer.activateUiContexts(viewer.contextUtil);
105 } catch (Throwable t) {
111 return Status.OK_STATUS;
112 } catch (CancelTransactionException e) {
115 return new Status(IStatus.CANCEL, Activator.PLUGIN_ID, "Diagram loading was cancelled.", e);
116 } catch (Throwable t) {
119 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Diagram loading failed, see exception for details.", t);
123 protected static Object BEGIN(String name) {
125 //return ThreadLog.BEGIN(name);
130 protected static void END(Object task) {
132 //((Task) task).end();