]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.desktop.ui/src/org/simantics/desktop/ui/internal/NewModel.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.desktop.ui / src / org / simantics / desktop / ui / internal / NewModel.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * 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.desktop.ui.internal;
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.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.Job;
21 import org.simantics.DatabaseJob;
22 import org.simantics.Simantics;
23 import org.simantics.db.Resource;
24 import org.simantics.db.WriteGraph;
25 import org.simantics.db.common.request.WriteRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.request.Configuration;
28 import org.simantics.modeling.ModelingUtils;
29 import org.simantics.platform.ui.PlatformUIResource;
30 import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;
31 import org.simantics.spreadsheet.util.SpreadsheetUtils;
32
33 public class NewModel extends AbstractHandler {
34
35     public static Resource execute(WriteGraph graph) throws DatabaseException {
36         PlatformUIResource PLATFORM = PlatformUIResource.getInstance(graph);
37         return execute(graph, PLATFORM.Model, null);
38     }
39
40     @Override
41     public Object execute(ExecutionEvent event) throws ExecutionException {
42         Job job = new DatabaseJob("Creating Model") {
43             @Override
44             protected IStatus run(IProgressMonitor monitor) {
45                 try {
46                     Simantics.sync(new WriteRequest() {
47                         @Override
48                         public void perform(WriteGraph graph) throws DatabaseException {
49                             execute(graph);
50                         }
51                     });
52                     return Status.OK_STATUS;
53                 } catch (DatabaseException e) {
54                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);
55                 }
56             }
57         };
58         job.setUser(true);
59         job.schedule();
60         return null;
61     }
62
63     public static Resource execute(WriteGraph graph, Resource type, String name) throws DatabaseException {
64         Resource model = ModelingUtils.createModel(graph, type, name);
65         Resource conf = graph.syncRequest(new Configuration(model));
66
67         ModelingUtils.addSCLMainToModel(graph, model, "SCLMain", "");
68         ModelingUtils.createLocalLibrary(graph, model, "Library");
69
70         Resource book = SpreadsheetGraphUtils.createBook(graph, conf, "Book1");
71         SpreadsheetUtils.createSheet(graph, book, "Sheet1", new String[] { }, new int[] { 50 });
72         SpreadsheetUtils.createSheet(graph, book, "Sheet2", new String[] { }, new int[] { 50 });
73         SpreadsheetUtils.createSheet(graph, book, "Sheet3", new String[] { }, new int[] { 50 });
74
75         return model;
76     }
77
78 }