]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.desktop.ui/src/org/simantics/desktop/ui/internal/NewModel.java
Fixed multiple issues causing dangling references to discarded queries
[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.eclipse.osgi.util.NLS;
22 import org.simantics.DatabaseJob;
23 import org.simantics.Simantics;
24 import org.simantics.db.Resource;
25 import org.simantics.db.WriteGraph;
26 import org.simantics.db.common.request.WriteRequest;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.layer0.request.Configuration;
29 import org.simantics.modeling.ModelingUtils;
30 import org.simantics.platform.ui.PlatformUIResource;
31 import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;
32 import org.simantics.spreadsheet.util.SpreadsheetUtils;
33
34 public class NewModel extends AbstractHandler {
35
36     public static Resource execute(WriteGraph graph) throws DatabaseException {
37         PlatformUIResource PLATFORM = PlatformUIResource.getInstance(graph);
38         return execute(graph, PLATFORM.Model, null);
39     }
40
41     @Override
42     public Object execute(ExecutionEvent event) throws ExecutionException {
43         Job job = new DatabaseJob(Messages.NewModel_NewModel) {  
44             @Override
45             protected IStatus run(IProgressMonitor monitor) {
46                 try {
47                     Simantics.sync(new WriteRequest() {
48                         @Override
49                         public void perform(WriteGraph graph) throws DatabaseException {
50                             execute(graph);
51                         }
52                     });
53                     return Status.OK_STATUS;
54                 } catch (DatabaseException e) {
55                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(Messages.NewModel_0_Failed, getName()), e);
56                 }
57             }
58         };
59         job.setUser(true);
60         job.schedule();
61         return null;
62     }
63
64     public static Resource execute(WriteGraph graph, Resource type, String name) throws DatabaseException {
65         Resource model = ModelingUtils.createModel(graph, type, name);
66         Resource conf = graph.syncRequest(new Configuration(model));
67
68         ModelingUtils.addSCLMainToModel(graph, model, "SCLMain", ""); //$NON-NLS-1$ //$NON-NLS-2$
69         ModelingUtils.createLocalLibrary(graph, model, Messages.NewModel_Library);
70
71         Resource book = SpreadsheetGraphUtils.createBook(graph, conf, Messages.NewModel_Book1);
72         SpreadsheetUtils.createSheet(graph, book, Messages.NewModel_Sheet1, new String[] { }, new int[] { 50 });
73         SpreadsheetUtils.createSheet(graph, book, Messages.NewModel_Sheet2, new String[] { }, new int[] { 50 });
74         SpreadsheetUtils.createSheet(graph, book, Messages.NewModel_Sheet3, new String[] { }, new int[] { 50 });
75
76         return model;
77     }
78
79 }