1 /*******************************************************************************
2 * Copyright (c) 2011 Association for Decentralized Information Management in
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.modeling.ui.property.svg;
14 import java.util.Collection;
16 import org.eclipse.jface.dialogs.IInputValidator;
17 import org.eclipse.jface.layout.GridDataFactory;
18 import org.eclipse.jface.resource.ColorDescriptor;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.graphics.RGB;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.ui.IWorkbenchSite;
25 import org.simantics.browsing.ui.swt.widgets.DefaultColorProvider;
26 import org.simantics.browsing.ui.swt.widgets.Label;
27 import org.simantics.browsing.ui.swt.widgets.TrackedText;
28 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.management.ISessionContext;
33 import org.simantics.diagram.stubs.DiagramResource;
34 import org.simantics.selectionview.ComparableTabContributor;
35 import org.simantics.selectionview.ConfigurationComposite;
36 import org.simantics.selectionview.TabContribution;
37 import org.simantics.ui.selection.WorkbenchSelectionUtils;
40 * @author Tuukka Lehtonen
42 public class SVGElementComposite extends ConfigurationComposite {
44 public static TabContribution<Object> svgTabContribution() {
45 return new TabContribution<Object>() {
48 public boolean accept(ReadGraph graph, Object input) throws DatabaseException {
49 Resource res = WorkbenchSelectionUtils.getPossibleResource(input);
50 if(res == null) return false;
51 return graph.isInstanceOf(res, DiagramResource.getInstance(graph).SVGElement);
55 public void contribute(ReadGraph graph, Object selection, Collection<ComparableTabContributor> result)
56 throws DatabaseException {
57 Resource res = WorkbenchSelectionUtils.getPossibleResource(selection);
59 if(graph.isInstanceOf(res, DiagramResource.getInstance(graph).SVGElement)) {
60 result.add(make(res, 100.0, "SVG"));
67 public static ComparableTabContributor make(Resource input, double priority, String tabLabel) {
68 return make(new SVGInput(input), priority, tabLabel);
71 public static ComparableTabContributor make(SVGInput input, double priority, String tabLabel) {
72 return new ComparableTabContributor(new SVGElementComposite(), priority, input, tabLabel);
75 public void create(final Composite body, IWorkbenchSite site, final ISessionContext context, final WidgetSupport support) {
77 final Display display = body.getDisplay();
79 body.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
81 Label expressionHeader = new Label(body, support, 0);
82 expressionHeader.setText("SVG markup");
83 expressionHeader.setFont(smallFont);
84 expressionHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
85 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).align(SWT.CENTER, SWT.CENTER).applyTo(expressionHeader.getWidget());
87 TrackedText expression = new TrackedText(body, support, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
88 expression.setSelectAllOnStartEdit(false);
89 expression.setColorProvider(new DefaultColorProvider(expression.getResourceManager()) {
91 private final ColorDescriptor inactiveColor = ColorDescriptor.createFrom(new RGB(220, 220, 255));
94 public Color getInactiveBackground() {
95 return resourceManager.createColor(inactiveColor);
99 expression.setTextFactory(new SVGTextFactory());
100 expression.addModifyListener(new SVGModifier());
101 expression.setInputValidator(new IInputValidator() {
103 public String isValid(String document) {
105 XMLPrettyPrinter.parseDocument(document);
107 } catch (Exception e) {
108 return e.getMessage();
112 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression.getWidget());