]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.desktop.ui/src/org/simantics/desktop/ui/internal/NewModel.java
Merge remote-tracking branch 'origin/svn'
[simantics/platform.git] / bundles / org.simantics.desktop.ui / src / org / simantics / desktop / ui / internal / NewModel.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.desktop.ui.internal;\r
13 \r
14  import org.eclipse.core.commands.AbstractHandler;\r
15 import org.eclipse.core.commands.ExecutionEvent;\r
16 import org.eclipse.core.commands.ExecutionException;\r
17 import org.eclipse.core.runtime.IProgressMonitor;\r
18 import org.eclipse.core.runtime.IStatus;\r
19 import org.eclipse.core.runtime.Status;\r
20 import org.eclipse.core.runtime.jobs.Job;\r
21 import org.simantics.DatabaseJob;\r
22 import org.simantics.Simantics;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.db.WriteGraph;\r
25 import org.simantics.db.common.request.WriteRequest;\r
26 import org.simantics.db.exception.DatabaseException;\r
27 import org.simantics.db.layer0.request.Configuration;\r
28 import org.simantics.modeling.ModelingUtils;\r
29 import org.simantics.platform.ui.PlatformUIResource;\r
30 import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;\r
31 import org.simantics.spreadsheet.util.SpreadsheetUtils;\r
32 \r
33 public class NewModel extends AbstractHandler {\r
34 \r
35     public static Resource execute(WriteGraph graph) throws DatabaseException {\r
36         PlatformUIResource PLATFORM = PlatformUIResource.getInstance(graph);\r
37         return execute(graph, PLATFORM.Model, null);\r
38     }\r
39 \r
40     @Override\r
41     public Object execute(ExecutionEvent event) throws ExecutionException {\r
42         Job job = new DatabaseJob("Creating Model") {\r
43             @Override\r
44             protected IStatus run(IProgressMonitor monitor) {\r
45                 try {\r
46                     Simantics.sync(new WriteRequest() {\r
47                         @Override\r
48                         public void perform(WriteGraph graph) throws DatabaseException {\r
49                             execute(graph);\r
50                         }\r
51                     });\r
52                     return Status.OK_STATUS;\r
53                 } catch (DatabaseException e) {\r
54                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);\r
55                 }\r
56             }\r
57         };\r
58         job.setUser(true);\r
59         job.schedule();\r
60         return null;\r
61     }\r
62 \r
63     public static Resource execute(WriteGraph graph, Resource type, String name) throws DatabaseException {\r
64         Resource model = ModelingUtils.createModel(graph, type, name);\r
65         Resource conf = graph.syncRequest(new Configuration(model));\r
66 \r
67         ModelingUtils.addSCLMainToModel(graph, model, "SCLMain", "");\r
68         ModelingUtils.createLocalLibrary(graph, model, "Library");\r
69 \r
70         Resource book = SpreadsheetGraphUtils.createBook(graph, conf, "Book1");\r
71         SpreadsheetUtils.createSheet(graph, book, "Sheet1", new String[] { }, new int[] { 50 });\r
72         SpreadsheetUtils.createSheet(graph, book, "Sheet2", new String[] { }, new int[] { 50 });\r
73         SpreadsheetUtils.createSheet(graph, book, "Sheet3", new String[] { }, new int[] { 50 });\r
74 \r
75         return model;\r
76     }\r
77 \r
78 }\r