]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/ui/NewChart.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / NewChart.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.eclipse.core.runtime.preferences.IEclipsePreferences;
18 import org.eclipse.core.runtime.preferences.InstanceScope;
19 import org.simantics.Simantics;
20 import org.simantics.charts.Activator;
21 import org.simantics.charts.ontology.ChartResource;
22 import org.simantics.charts.preference.ChartPreferences;
23 import org.simantics.charts.preference.ChartTimeWindowTemplate;
24 import org.simantics.databoard.Bindings;
25 import org.simantics.db.Resource;
26 import org.simantics.db.WriteGraph;
27 import org.simantics.db.common.CommentMetadata;
28 import org.simantics.db.common.request.WriteResultRequest;
29 import org.simantics.db.common.utils.NameUtils;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.layer0.adapter.ActionFactory;
32 import org.simantics.layer0.Layer0;
33 import org.simantics.operation.Layer0X;
34 import org.simantics.trend.configuration.YAxisMode;
35
36 /**
37  * @author Tuukka Lehtonen
38  */
39 public class NewChart implements ActionFactory {
40
41     @Override
42     public Runnable create(Object target) {
43         if(!(target instanceof Resource))
44             return null;
45         final Resource parent = (Resource)target;
46
47         return new Runnable() {
48             @Override
49             public void run() {
50                 try {
51                     createChart(parent);
52                 } catch (DatabaseException e) {
53                     Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to create chart.", e));
54                 }
55             }
56         };
57     }
58
59     public static Resource createChart (final Resource target) throws DatabaseException {
60
61         return Simantics.getSession().syncRequest(new WriteResultRequest<Resource>() {
62                 @Override
63             public Resource perform(WriteGraph g) throws DatabaseException {
64                     g.markUndoPoint();
65                 Layer0 l0 = Layer0.getInstance(g);
66                 Layer0X L0X = Layer0X.getInstance(g);
67                 ChartResource wr = ChartResource.getInstance(g);
68                 String freshName = NameUtils.findFreshName(g, "Chart", target);
69                 Resource chart = g.newResource();
70                 g.claim(chart, l0.InstanceOf, null, wr.TimeSeriesChart);
71                 g.claimLiteral(chart, l0.HasName, freshName, Bindings.STRING);
72                 g.claim(chart, l0.PartOf, target);
73
74                 IEclipsePreferences pn = InstanceScope.INSTANCE.getNode( "org.simantics.charts" );
75
76                 // Template
77                 {
78                     String templateId = pn.get(ChartPreferences.P_TIMEWINDOW_TEMPLATE, ChartPreferences.DEFAULT_TIMEWINDOW_TEMPLATE);
79                     ChartTimeWindowTemplate template = ChartTimeWindowTemplate.getTemplate( templateId );
80                     if (template != null) {
81                         Resource r = g.getResource( template.uri );
82                         g.claim(chart, L0X.ObtainsProperty1, null, r);
83                     }
84                 }
85
86                 // Axis Mode
87                 {
88                     String sam = pn.get(ChartPreferences.P_AXISMODE, ChartPreferences.DEFAULT_AXISMODE);
89                     YAxisMode am = YAxisMode.valueOf( sam );
90                     Resource ram = null;
91                     if ( am == YAxisMode.SingleAxis ) ram = wr.YAxisMode_SingleAxis;
92                     if ( am == YAxisMode.MultiAxis ) ram = wr.YAxisMode_MultiAxis;
93                     if ( ram != null ) {
94                         g.claim(chart, wr.Chart_YAxisMode, ram);
95                     }
96                 }
97                 
98                 CommentMetadata cm = g.getMetadata(CommentMetadata.class);
99                 g.addMetadata(cm.add("Created new chart named " + freshName + ", resource " + chart));
100                 
101                 return chart;
102             }
103         });
104     }
105
106 }