]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/OpenSheetAdapter.java
Merge "Catch Throwable instead of Exception in SCL AsyncUtils"
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / OpenSheetAdapter.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor;
13
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;
32
33 public class OpenSheetAdapter extends AbstractResourceEditorAdapter {
34     private static final Logger LOGGER = LoggerFactory.getLogger(OpenSheetAdapter.class);
35
36     private static final String EDITOR_ID = "org.simantics.spreadsheet.ui.editor2"; //$NON-NLS-1$
37
38     public OpenSheetAdapter() {
39         super(Messages.OpenSheetAdapter_SpreadsheetEditor, Activator.COMPOSITE_ICON);
40     }
41
42     protected String getEditorId() {
43         return EDITOR_ID;
44     }
45
46     @Override
47     public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
48         return g.isInstanceOf(r, SpreadsheetResource.getInstance(g).Book);
49     }
50
51     @Override
52     public void openEditor(final Resource r) throws Exception {
53         Simantics.getSession().asyncRequest(new ReadRequest() {
54             @Override
55             public void run(ReadGraph g) throws DatabaseException {
56
57                 Variable variable = Variables.getVariable(g, r);
58                 final Resource model = g.syncRequest(new PossibleIndexRoot(r));
59                 final RVI rvi = variable.getPossibleRVI(g);
60                 
61                 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
62                     @Override
63                     public void run() {
64                         try {
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$
69                         }
70                     }
71                 });
72             }
73         });
74     }
75
76 }