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;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class DiagramViewerLoadJob extends DatabaseJob {
32 private static final Logger LOGGER = LoggerFactory.getLogger(DiagramViewerLoadJob.class);
34 private static final boolean PROFILE = false;
35 private DiagramViewer viewer;
37 public DiagramViewerLoadJob(DiagramViewer viewer) {
38 super(Messages.DiagramViewerLoadJob_LoadDiagram);
44 protected IStatus run(final IProgressMonitor monitor) {
45 final SubMonitor mon = SubMonitor.convert(monitor, Messages.DiagramViewerLoadJob_MonitorLoadingDiagram, 200);
48 Object task = BEGIN("DV.loadDiagram"); //$NON-NLS-1$
49 final IDiagram diagram = viewer.loadDiagram(mon.newChild(100), viewer.diagramResource);
51 return Status.CANCEL_STATUS;
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));
60 // Wait for load completion in AWT thread
61 ThreadUtils.syncExec(viewer.canvasContext.getThreadAccess(), new Runnable() {
64 setThread(viewer.canvasContext.getThreadAccess().getThread());
65 mon.setTaskName(Messages.DiagramViewerLoadJob_MonitorFinalizeDiagramLoading);
68 Object task = BEGIN("DV.beforeSetDiagram"); //$NON-NLS-1$
69 viewer.beforeSetDiagram(diagram);
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);
83 viewer.selectionProvider.fireSelection(Collections.emptyList());
85 // Zoom to fit if no previous view transform is available
86 task = BEGIN("DV.scheduleZoomToFit"); //$NON-NLS-1$
87 viewer.scheduleZoomToFit(diagram);
91 task = BEGIN("DV.onCreated"); //$NON-NLS-1$
92 mon.subTask(""); //$NON-NLS-1$
97 task = BEGIN("DV.applyEditorState"); //$NON-NLS-1$
98 mon.subTask(Messages.DiagramViewerLoadJob_ApplyEditorState);
99 viewer.applyEditorState(viewer.editorState, viewer.canvasContext);
103 task = BEGIN("DV.activateUiContexts"); //$NON-NLS-1$
104 viewer.contextUtil.inContextThread(new Runnable() {
107 if (!viewer.disposed)
108 viewer.activateUiContexts(viewer.contextUtil);
113 } catch (Throwable t) {
115 LOGGER.error("Failed to complete loading of diagram {} in the canvas thread", viewer.diagramResource, t);
120 return Status.OK_STATUS;
121 } catch (CancelTransactionException e) {
124 return new Status(IStatus.CANCEL, Activator.PLUGIN_ID, Messages.DiagramViewerLoadJob_ActivatorDiagramLoadingCancelled, e);
125 } catch (Throwable t) {
128 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.DiagramViewerLoadJob_ActivatorDiagramLoadingFailed, t);
132 protected static Object BEGIN(String name) {
134 //return ThreadLog.BEGIN(name);
139 protected static void END(Object task) {
141 //((Task) task).end();