1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.ui.PartInitException;
15 import org.eclipse.ui.PlatformUI;
16 import org.simantics.Simantics;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.request.PossibleIndexRoot;
20 import org.simantics.db.common.request.ReadRequest;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.variable.RVI;
23 import org.simantics.db.layer0.variable.Variable;
24 import org.simantics.db.layer0.variable.Variables;
25 import org.simantics.modeling.ui.Activator;
26 import org.simantics.spreadsheet.resource.SpreadsheetResource;
27 import org.simantics.ui.workbench.ResourceEditorInput2;
28 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
29 import org.simantics.utils.ui.workbench.WorkbenchUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class OpenSheetAdapter extends AbstractResourceEditorAdapter {
34 private static final Logger LOGGER = LoggerFactory.getLogger(OpenSheetAdapter.class);
36 private static final String EDITOR_ID = "org.simantics.spreadsheet.ui.editor2"; //$NON-NLS-1$
38 public OpenSheetAdapter() {
39 super(Messages.OpenSheetAdapter_SpreadsheetEditor, Activator.COMPOSITE_ICON);
42 protected String getEditorId() {
47 public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
48 return g.isInstanceOf(r, SpreadsheetResource.getInstance(g).Book);
52 public void openEditor(final Resource r) throws Exception {
53 Simantics.getSession().asyncRequest(new ReadRequest() {
55 public void run(ReadGraph g) throws DatabaseException {
57 Variable variable = Variables.getVariable(g, r);
58 final Resource model = g.syncRequest(new PossibleIndexRoot(r));
59 final RVI rvi = variable.getPossibleRVI(g);
61 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
65 String editorId = getEditorId();
66 WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, r, model, rvi));
67 } catch (PartInitException e) {
68 LOGGER.error("Failed to open the spreadsheet editor.", e); //$NON-NLS-1$