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(Messages.DiagramViewerLoadJob_LoadDiagram);
40 protected IStatus run(final IProgressMonitor monitor) {
41 final SubMonitor mon = SubMonitor.convert(monitor, Messages.DiagramViewerLoadJob_MonitorLoadingDiagram, 200);
44 Object task = BEGIN("DV.loadDiagram"); //$NON-NLS-1$
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"); //$NON-NLS-1$
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(Messages.DiagramViewerLoadJob_MonitorFinalizeDiagramLoading);
64 Object task = BEGIN("DV.beforeSetDiagram"); //$NON-NLS-1$
65 viewer.beforeSetDiagram(diagram);
69 task = BEGIN("DV.setDiagramHint"); //$NON-NLS-1$
70 mon.subTask(Messages.DiagramViewerLoadJob_SetDiagram);
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"); //$NON-NLS-1$
83 viewer.scheduleZoomToFit(diagram);
87 task = BEGIN("DV.onCreated"); //$NON-NLS-1$
88 mon.subTask(""); //$NON-NLS-1$
93 task = BEGIN("DV.applyEditorState"); //$NON-NLS-1$
94 mon.subTask(Messages.DiagramViewerLoadJob_ApplyEditorState);
95 viewer.applyEditorState(viewer.editorState, viewer.canvasContext);
99 task = BEGIN("DV.activateUiContexts"); //$NON-NLS-1$
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, Messages.DiagramViewerLoadJob_ActivatorDiagramLoadingCancelled, e);
120 } catch (Throwable t) {
123 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.DiagramViewerLoadJob_ActivatorDiagramLoadingFailed, t);
127 protected static Object BEGIN(String name) {
129 //return ThreadLog.BEGIN(name);
134 protected static void END(Object task) {
136 //((Task) task).end();