/******************************************************************************* * Copyright (c) 2012 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.modeling.ui.diagramEditor; import java.util.Collections; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.SubMonitor; import org.simantics.DatabaseJob; import org.simantics.db.exception.CancelTransactionException; import org.simantics.g2d.diagram.DiagramHints; import org.simantics.g2d.diagram.IDiagram; import org.simantics.modeling.ui.Activator; import org.simantics.utils.DataContainer; import org.simantics.utils.threads.ThreadUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DiagramViewerLoadJob extends DatabaseJob { private static final Logger LOGGER = LoggerFactory.getLogger(DiagramViewerLoadJob.class); private static final boolean PROFILE = false; private DiagramViewer viewer; public DiagramViewerLoadJob(DiagramViewer viewer) { super(Messages.DiagramViewerLoadJob_LoadDiagram); setUser(true); this.viewer = viewer; } @Override protected IStatus run(final IProgressMonitor monitor) { final SubMonitor mon = SubMonitor.convert(monitor, Messages.DiagramViewerLoadJob_MonitorLoadingDiagram, 200); try { Object task = BEGIN("DV.loadDiagram"); //$NON-NLS-1$ final IDiagram diagram = viewer.loadDiagram(mon.newChild(100), viewer.diagramResource); if (diagram == null) return Status.CANCEL_STATUS; END(task); // Start an activation for the input resource. // This will activate mapping if necessary. task = BEGIN("DV.performActivation"); //$NON-NLS-1$ viewer.performActivation(mon.newChild(50)); END(task); // Wait for load completion in AWT thread ThreadUtils.syncExec(viewer.canvasContext.getThreadAccess(), new Runnable() { @Override public void run() { setThread(viewer.canvasContext.getThreadAccess().getThread()); mon.setTaskName(Messages.DiagramViewerLoadJob_MonitorFinalizeDiagramLoading); try { Object task = BEGIN("DV.beforeSetDiagram"); //$NON-NLS-1$ viewer.beforeSetDiagram(diagram); mon.worked(10); END(task); task = BEGIN("DV.setDiagramHint"); //$NON-NLS-1$ mon.subTask(Messages.DiagramViewerLoadJob_SetDiagram); DataContainer diagramContainer = viewer.sourceDiagramContainer; if (diagramContainer != null) diagramContainer.set( diagram ); viewer.sourceDiagram = diagram; viewer.canvasContext.getDefaultHintContext().setHint(DiagramHints.KEY_DIAGRAM, diagram); mon.worked(10); END(task); viewer.selectionProvider.fireSelection(Collections.emptyList()); // Zoom to fit if no previous view transform is available task = BEGIN("DV.scheduleZoomToFit"); //$NON-NLS-1$ viewer.scheduleZoomToFit(diagram); mon.worked(10); END(task); task = BEGIN("DV.onCreated"); //$NON-NLS-1$ mon.subTask(""); //$NON-NLS-1$ viewer.onCreated(); mon.worked(10); END(task); task = BEGIN("DV.applyEditorState"); //$NON-NLS-1$ mon.subTask(Messages.DiagramViewerLoadJob_ApplyEditorState); viewer.applyEditorState(viewer.editorState, viewer.canvasContext); mon.worked(10); END(task); task = BEGIN("DV.activateUiContexts"); //$NON-NLS-1$ viewer.contextUtil.inContextThread(new Runnable() { @Override public void run() { if (!viewer.disposed) viewer.activateUiContexts(viewer.contextUtil); viewer = null; } }); END(task); } catch (Throwable t) { viewer = null; LOGGER.error("Failed to complete loading of diagram {} in the canvas thread", viewer.diagramResource, t); } } }); return Status.OK_STATUS; } catch (CancelTransactionException e) { monitor.done(); viewer = null; return new Status(IStatus.CANCEL, Activator.PLUGIN_ID, Messages.DiagramViewerLoadJob_ActivatorDiagramLoadingCancelled, e); } catch (Throwable t) { monitor.done(); viewer = null; return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.DiagramViewerLoadJob_ActivatorDiagramLoadingFailed, t); } } protected static Object BEGIN(String name) { if (PROFILE) { //return ThreadLog.BEGIN(name); } return null; } protected static void END(Object task) { if (PROFILE) { //((Task) task).end(); } } }