]> gerrit.simantics Code Review - simantics/platform.git/blob
739b66327f5b15f7fd204940d6b9bf9e000d67d3
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2012 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.modeling.template2d.ui.actions;
13
14 import java.util.Arrays;
15
16 import org.simantics.databoard.Bindings;
17 import org.simantics.datatypes.DatatypeResource;
18 import org.simantics.datatypes.literal.Font;
19 import org.simantics.datatypes.literal.RGB;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.CommentMetadata;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.common.utils.ListUtils;
25 import org.simantics.db.common.utils.NameUtils;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.adapter.ActionFactory;
28 import org.simantics.diagram.stubs.DiagramResource;
29 import org.simantics.diagram.stubs.G2DResource;
30 import org.simantics.layer0.Layer0;
31 import org.simantics.scenegraph.ontology.ScenegraphResources;
32 import org.simantics.ui.SimanticsUI;
33
34 public class NewMonitor implements ActionFactory {
35
36     @Override
37     public Runnable create(Object target) {
38         if(!(target instanceof Resource))
39             return null;
40         final Resource parent = (Resource)target;
41
42         return new Runnable() {
43             @Override
44             public void run() {
45                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
46                     @Override
47                     public void perform(WriteGraph g) throws DatabaseException {
48                         g.markUndoPoint();
49                         createNewMonitor(g, parent);
50                     }
51                 });
52             }
53         };
54     }
55     
56     public static Resource createNewMonitor(WriteGraph g, Resource parent) throws DatabaseException {
57
58         Layer0 L0 = Layer0.getInstance(g);
59         DiagramResource DIA = DiagramResource.getInstance(g);
60         DatatypeResource DATA = DatatypeResource.getInstance(g);
61         G2DResource G2D = G2DResource.getInstance(g);
62         ScenegraphResources SG = ScenegraphResources.getInstance(g);
63
64         Resource monitor = g.newResource();
65         g.claim(monitor, L0.InstanceOf, null, DIA.Scenegraph_Monitor);
66         String name = NameUtils.findFreshName(g, "Monitor", parent, L0.ConsistsOf);
67         g.addLiteral(monitor, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);
68         g.addLiteral(monitor, DIA.Scenegraph_Monitor_reference, DIA.Scenegraph_Monitor_reference_Inverse, "#HasLabel", Bindings.STRING);
69         g.addLiteral(monitor, DIA.Scenegraph_AbstractText_width, DIA.Scenegraph_AbstractText_width_Inverse, 0.0f, Bindings.FLOAT);
70         g.addLiteral(monitor, DIA.Scenegraph_AbstractText_horizontalAlignment, DIA.Scenegraph_AbstractText_horizontalAlignment_Inverse, L0.Byte, (byte) 0, Bindings.BYTE);
71         g.addLiteral(monitor, DIA.Scenegraph_AbstractText_verticalAlignment, DIA.Scenegraph_AbstractText_verticalAlignment_Inverse, L0.Byte, (byte) 0, Bindings.BYTE);
72         //g.claim(monitor, DIA.Scenegraph_AbstractText_horizontalAlignment, DIA.Scenegraph_AbstractText_horizontalAlignment_Inverse, DIA.Scenegraph_AbstractText_HAlignment_Left);
73         g.addLiteral(monitor, DIA.Scenegraph_AbstractText_color, DIA.Scenegraph_AbstractText_color_Inverse, DATA.RGB_Integer, new RGB.Integer(0,0,0), Bindings.getBindingUnchecked(RGB.Integer.class));
74         g.addLiteral(monitor, DIA.Scenegraph_AbstractText_font, DIA.Scenegraph_AbstractText_font_Inverse, DATA.Font, new Font("Arial", 7, "Normal"), Bindings.getBindingUnchecked(Font.class));
75         g.addLiteral(monitor, DIA.Scenegraph_AbstractText_transform, DIA.Scenegraph_AbstractText_transform_Inverse, G2D.Transform, new Double[] { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 }, Bindings.getBindingUnchecked(Double[].class));
76         g.claim(monitor, L0.PartOf, parent);
77         Resource list = g.getPossibleObject(parent, SG.Node_children);
78         if (list != null)
79                 ListUtils.insertBack(g, list, Arrays.asList(monitor));
80         
81         CommentMetadata cm = g.getMetadata(CommentMetadata.class);
82         g.addMetadata(cm.add("Create New Monitor."));
83         
84         return monitor;
85     }
86 }