1 /*******************************************************************************
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - issue #4262
12 *******************************************************************************/
13 package org.simantics.charts.ui;
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;
37 * @author Tuukka Lehtonen
39 public class NewChart implements ActionFactory {
42 public Runnable create(Object target) {
43 if(!(target instanceof Resource))
45 final Resource parent = (Resource)target;
47 return new Runnable() {
52 } catch (DatabaseException e) {
53 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to create chart.", e));
59 public static Resource createChart (final Resource target) throws DatabaseException {
61 return Simantics.getSession().syncRequest(new WriteResultRequest<Resource>() {
63 public Resource perform(WriteGraph g) throws DatabaseException {
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);
74 IEclipsePreferences pn = InstanceScope.INSTANCE.getNode( "org.simantics.charts" );
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);
88 String sam = pn.get(ChartPreferences.P_AXISMODE, ChartPreferences.DEFAULT_AXISMODE);
89 YAxisMode am = YAxisMode.valueOf( sam );
91 if ( am == YAxisMode.SingleAxis ) ram = wr.YAxisMode_SingleAxis;
92 if ( am == YAxisMode.MultiAxis ) ram = wr.YAxisMode_MultiAxis;
94 g.claim(chart, wr.Chart_YAxisMode, ram);
98 CommentMetadata cm = g.getMetadata(CommentMetadata.class);
99 g.addMetadata(cm.add("Created new chart named " + freshName + ", resource " + chart));