]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/NewSpreadsheetHandler.java
Remove usage of deprecated SimanticsUI-methods
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / NewSpreadsheetHandler.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.modelBrowser.handlers;
13
14 import org.eclipse.core.commands.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.ui.handlers.HandlerUtil;
19 import org.simantics.Simantics;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.request.WriteRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.SelectionHints;
25 import org.simantics.db.layer0.adapter.InstanceFactory;
26 import org.simantics.simulation.ontology.SimulationResource;
27 import org.simantics.spreadsheet.resource.SpreadsheetResource;
28 import org.simantics.utils.datastructures.ArrayMap;
29 import org.simantics.utils.ui.ISelectionUtils;
30
31 public class NewSpreadsheetHandler extends AbstractHandler {
32
33     @Override
34     public Object execute(ExecutionEvent event) throws ExecutionException {
35         ISelection selection = HandlerUtil.getCurrentSelection(event);
36         final Resource container = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, Resource.class);
37         if (container == null)
38             return null;
39
40         Simantics.getSession().asyncRequest(new WriteRequest() {
41             @Override
42             public void perform(WriteGraph graph) throws DatabaseException {
43                 SimulationResource SIMU = SimulationResource.getInstance(graph);
44                 SpreadsheetResource ssr = SpreadsheetResource.getInstance(graph);
45                 Resource configuration = graph.getPossibleObject(container, SIMU.HasConfiguration);
46                 if (configuration == null)
47                     return;
48                 InstanceFactory sheetFactory = graph.adapt(ssr.Spreadsheet, InstanceFactory.class);
49                 graph.syncRequest(sheetFactory.create(graph, ArrayMap.keys("composite").values(configuration), ssr.ForCompositeTemplate));
50             }
51         });
52
53         return null;
54     }
55
56 }