]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/ui/NewChartGroup.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / NewChartGroup.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2013 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  *     Semantum Oy - issue #4262
12  *******************************************************************************/
13 package org.simantics.charts.ui;
14
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.simantics.Simantics;
18 import org.simantics.charts.Activator;
19 import org.simantics.charts.ontology.ChartResource;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.db.Resource;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.request.WriteResultRequest;
24 import org.simantics.db.common.utils.NameUtils;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.layer0.adapter.ActionFactory;
27 import org.simantics.layer0.Layer0;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public class NewChartGroup implements ActionFactory {
33
34     @Override
35     public Runnable create(Object target) {
36         if(!(target instanceof Resource))
37             return null;
38         final Resource parent = (Resource)target;
39
40         return new Runnable() {
41             @Override
42             public void run() {
43                 try {
44                     createChartGroup(parent);
45                 } catch (DatabaseException e) {
46                     Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to create chart group.", e));
47                 }
48             }
49         };
50     }
51
52     public static Resource createChartGroup (final Resource target) throws DatabaseException {
53
54         return Simantics.getSession().syncRequest(new WriteResultRequest<Resource>() {
55             @Override
56             public Resource perform(WriteGraph g) throws DatabaseException {
57                 g.markUndoPoint();
58                 Layer0 l0 = Layer0.getInstance(g);
59                 ChartResource wr = ChartResource.getInstance(g);
60                 String freshName = NameUtils.findFreshName(g, "Chart Group", target);
61                 Resource chart = g.newResource();
62                 g.claim(chart, l0.InstanceOf, null, wr.ChartGroup);
63                 g.claimLiteral(chart, l0.HasName, freshName, Bindings.STRING);
64                 g.claim(chart, l0.PartOf, target);
65                 return chart;
66             }
67         });
68
69     }
70
71 }