1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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 *******************************************************************************/
12 package org.simantics.diagram;
14 import java.awt.BasicStroke;
15 import java.awt.Color;
17 import java.awt.geom.AffineTransform;
18 import java.awt.geom.Rectangle2D;
21 import org.simantics.databoard.Bindings;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.WriteGraph;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.exception.DoesNotContainValueException;
27 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
28 import org.simantics.db.exception.NoSingleResultException;
29 import org.simantics.db.exception.ServiceException;
30 import org.simantics.diagram.stubs.DiagramResource;
31 import org.simantics.diagram.stubs.G2DResource;
32 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
33 import org.simantics.g2d.svg.LineCap;
34 import org.simantics.g2d.svg.LineJoin;
35 import org.simantics.layer0.Layer0;
36 import org.simantics.utils.strings.format.MetricsFormat;
40 * Utility class for handling G2D ontology concepts.
42 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
44 public class G2DUtils {
47 * Utility to wrap resource to a object
52 * @throws ServiceException
53 * @throws NoSingleResultException
54 * @throws DoesNotContainValueException
55 * @throws ManyObjectsForFunctionalRelationException
57 public static Object getObject(ReadGraph g, Resource resource) throws ServiceException, NoSingleResultException, DoesNotContainValueException, ManyObjectsForFunctionalRelationException {
58 G2DResource g2d = G2DResource.getInstance(g);
59 Set<Resource> types = g.getTypes(resource);
60 if (types.contains(g2d.Font)) {
61 return getFont(g, resource);
63 if (types.contains(g2d.Color)) {
64 return getColor(g, resource);
66 if (types.contains(g2d.Transform)) {
67 return getTransform(g, resource);
69 if (types.contains(g2d.Rectangle2D)) {
70 return getRectangle(g, resource);
73 return g.getPossibleValue(resource);
76 public static Font getFont(ReadGraph g, Resource fontResource) throws ServiceException, NoSingleResultException, DoesNotContainValueException, ManyObjectsForFunctionalRelationException {
77 G2DResource g2d = G2DResource.getInstance(g);
78 if (!g.isInstanceOf(fontResource, g2d.Font))
79 throw new IllegalArgumentException("Resource " + fontResource + " is not a font");
81 String name = g.getRelatedValue(fontResource, g2d.HasFontFamily);
82 int size = g.getRelatedValue(fontResource, g2d.HasFontSize);
83 int style = Font.PLAIN;
84 Resource styleResource = g.getPossibleObject(fontResource, g2d.HasFontStyle);
85 if (styleResource != null) {
86 if (styleResource.equals(g2d.FontStyle_bold_italic_style)) {
87 style = Font.BOLD | Font.ITALIC;
88 } else if (styleResource.equals(g2d.FontStyle_bold_font_style)) {
90 } else if (styleResource.equals(g2d.FontStyle_italic_font_style)) {
92 } else if (styleResource.equals(g2d.FontStyle_normal_font_style)) {
95 throw new IllegalArgumentException("Given font " + fontResource + " does not contain valid style. Current style is " + styleResource);
99 return new Font(name, style, size);
102 public static Color getColor(ReadGraph g, Resource colorResource) throws DoesNotContainValueException, ServiceException {
103 G2DResource g2d = G2DResource.getInstance(g);
104 if (!g.isInstanceOf(colorResource, g2d.Color))
105 throw new IllegalArgumentException("Resource " + colorResource + " is not a color");
107 float value[] = (float[])g.getValue(colorResource);
108 if (value.length != 4)
109 throw new IllegalArgumentException("Color " + colorResource + " does not have proper definition, expected 4 components, got " + value.length + " components");
111 return new Color(value[0], value[1], value[2], value[3]);
114 public static AffineTransform getTransform(ReadGraph g, Resource resource) throws DoesNotContainValueException, ServiceException {
115 G2DResource g2d = G2DResource.getInstance(g);
116 if (!g.isInstanceOf(resource, g2d.Transform))
117 throw new IllegalArgumentException("Resource " + resource + " is not a transform");
119 double values[] = (double[])g.getValue(resource);
120 if (values.length != 6)
121 throw new IllegalArgumentException("Transform " + resource + " does not have proper definition, expected 6 components, got " + values.length + " components");
123 return new AffineTransform(values);
126 public static Rectangle2D getRectangle(ReadGraph g, Resource resource) throws DoesNotContainValueException, ServiceException {
127 G2DResource g2d = G2DResource.getInstance(g);
128 if (!g.isInstanceOf(resource, g2d.Rectangle2D))
129 throw new IllegalArgumentException("Resource " + resource + " is not a rectangle");
131 double values[] = (double[])g.getValue(resource);
132 if (values.length != 4)
133 throw new IllegalArgumentException("Transform " + resource + " does not have proper definition, expected 4 components, got " + values.length + " components");
135 return new Rectangle2D.Double(values[0], values[1], values[2], values[3]);
138 public static Resource createFont(WriteGraph g, Font font) throws ServiceException, ManyObjectsForFunctionalRelationException {
139 Layer0 b = Layer0.getInstance(g);
140 G2DResource g2d = G2DResource.getInstance(g);
141 Resource fontResource = g.newResource();
142 g.claim(fontResource, b.InstanceOf, null, g2d.Font);
143 g.claimLiteral(fontResource, g2d.HasFontFamily, font.getFamily());
144 g.claimLiteral(fontResource, g2d.HasFontSize, font.getSize());
145 if (font.getStyle() == (Font.BOLD|Font.ITALIC)) {
146 g.claim(fontResource, g2d.HasFontStyle, g2d.FontStyle_bold_italic_style);
147 } else if (font.getStyle() == Font.BOLD) {
148 g.claim(fontResource, g2d.HasFontStyle, g2d.FontStyle_bold_font_style);
149 } else if (font.getStyle() == Font.ITALIC) {
150 g.claim(fontResource, g2d.HasFontStyle, g2d.FontStyle_italic_font_style);
152 g.claim(fontResource, g2d.HasFontStyle, g2d.FontStyle_normal_font_style);
158 public static Resource createColor(WriteGraph g, Color color) throws ServiceException {
159 Layer0 b = Layer0.getInstance(g);
160 G2DResource g2d = G2DResource.getInstance(g);
161 Resource colorResource = g.newResource();
162 g.claim(colorResource, b.InstanceOf, null, g2d.Color);
163 g.claimValue(colorResource, color.getRGBComponents(null));
164 return colorResource;
171 public static Resource createMetricsFormat(WriteGraph g, MetricsFormat format) throws ServiceException, ManyObjectsForFunctionalRelationException {
172 Layer0 b = Layer0.getInstance(g);
173 DiagramResource dr = DiagramResource.getInstance(g);
174 Resource formatResource = g.newResource();
175 g.claim(formatResource, b.InstanceOf, null, dr.Format);
176 g.claimLiteral(formatResource, b.HasName, format.getName());
177 g.claimLiteral(formatResource, dr.HasPattern, format.getPattern());
178 return formatResource;
181 public static MetricsFormat getMetricsFormat(ReadGraph g, Resource formatResource) throws NoSingleResultException, DoesNotContainValueException, ServiceException {
182 Layer0 l0 = Layer0.getInstance(g);
183 DiagramResource dr = DiagramResource.getInstance(g);
184 String name = g.getRelatedValue(formatResource, l0.HasName);
185 String pattern = g.getRelatedValue(formatResource, dr.HasPattern);
186 return new MetricsFormat(pattern,1.0,name);
189 public static final Float DEFAULT_DASH_OFFSET = 0f;
190 public static final Float DEFAULT_STROKE_WIDTH = 1f;
191 public static final Float DEFAULT_MITER_LIMIT = 10f;
193 public static BasicStroke getStroke(ReadGraph graph, Resource stroke) throws DatabaseException {
194 return getStroke(graph, stroke, DEFAULT_DASH_OFFSET, DEFAULT_STROKE_WIDTH, DEFAULT_MITER_LIMIT);
197 public static BasicStroke getStroke(ReadGraph graph, Resource stroke, Float defaultDashOffset, Float defaultStrokeWidth, Float defaultMiterLimit) throws DatabaseException {
201 G2DResource g2d = G2DResource.getInstance(graph);
202 float[] dashArray = DiagramGraphUtil.getPossibleRelatedValue(graph, stroke, g2d.HasDashArray, float[].class, null);
203 Float dashOffset = DiagramGraphUtil.getPossibleRelatedValue(graph, stroke, g2d.HasDashOffset, Float.class, defaultDashOffset);
204 Float strokeWidth = DiagramGraphUtil.getPossibleRelatedValue(graph, stroke, g2d.HasStrokeWidth, Float.class, defaultStrokeWidth);
205 Float miterLimit = DiagramGraphUtil.getPossibleRelatedValue(graph, stroke, g2d.HasMiterLimit, Float.class, defaultMiterLimit);
206 LineJoin lineJoin = DiagramGraphUtil.toLineJoin(g2d, graph.getPossibleObject(stroke, g2d.HasLineJoin));
207 LineCap lineCap = DiagramGraphUtil.toLineCap(g2d, graph.getPossibleObject(stroke, g2d.HasLineCap));
213 return new BasicStroke(strokeWidth, lineCap.ordinal(), lineJoin.ordinal(), miterLimit, dashArray, dashOffset);
216 public static Resource createStroke(WriteGraph graph, BasicStroke stroke) throws DatabaseException {
220 Layer0 L0 = Layer0.getInstance(graph);
221 G2DResource g2d = G2DResource.getInstance(graph);
222 Resource result = graph.newResource();
223 graph.claim(result, L0.InstanceOf, null, g2d.Stroke);
224 if (stroke.getDashArray() != null)
225 graph.claimLiteral(result, g2d.HasDashArray, L0.FloatArray, stroke.getDashArray(), Bindings.FLOAT_ARRAY);
226 graph.claimLiteral(result, g2d.HasDashOffset, L0.Float, stroke.getDashPhase(), Bindings.FLOAT);
227 graph.claimLiteral(result, g2d.HasStrokeWidth, L0.Float, stroke.getLineWidth(), Bindings.FLOAT);
228 graph.claimLiteral(result, g2d.HasMiterLimit, L0.Float, stroke.getMiterLimit(), Bindings.FLOAT);
229 graph.claim(result, g2d.HasLineJoin, null, DiagramGraphUtil.toLineJoin(g2d, LineJoin.values()[stroke.getLineJoin()]));
230 graph.claim(result, g2d.HasLineCap, null, DiagramGraphUtil.toLineCap(g2d, LineCap.values()[stroke.getEndCap()]));