]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/svg/SVGElementComposite.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / property / svg / SVGElementComposite.java
1 /*******************************************************************************
2  * Copyright (c) 2011 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.ui.property.svg;
13
14 import java.util.Collection;
15
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;
38
39 /**
40  * @author Tuukka Lehtonen
41  */
42 public class SVGElementComposite extends ConfigurationComposite {
43
44         public static TabContribution<Object> svgTabContribution() {
45                 return new TabContribution<Object>() {
46
47                         @Override
48                         public boolean accept(ReadGraph graph, Object input) throws DatabaseException {
49                                 Resource res = WorkbenchSelectionUtils.getPossibleResourceFromSelection(graph, input);
50                                 if(res == null) return false;
51                                 return graph.isInstanceOf(res, DiagramResource.getInstance(graph).SVGElement);
52                         }
53
54                         @Override
55                         public void contribute(ReadGraph graph, Object selection, Collection<ComparableTabContributor> result)
56                                         throws DatabaseException {
57                                 Resource res = WorkbenchSelectionUtils.getPossibleResourceFromSelection(graph, selection);
58                                 if(res != null) {
59                                         if(graph.isInstanceOf(res, DiagramResource.getInstance(graph).SVGElement)) {
60                                                 result.add(make(res, 100.0, "SVG"));
61                                         }
62                                 }
63                         }
64                 };
65         }
66         
67     public static ComparableTabContributor make(Resource input, double priority, String tabLabel) {
68         return make(new SVGInput(input), priority, tabLabel);
69     }
70
71     public static ComparableTabContributor make(SVGInput input, double priority, String tabLabel) {
72         return new ComparableTabContributor(new SVGElementComposite(), priority, input, tabLabel);
73     }
74
75     public void create(final Composite body, IWorkbenchSite site, final ISessionContext context, final WidgetSupport support) {
76
77         final Display display = body.getDisplay();
78
79         body.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
80
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());
86
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()) {
90
91             private final ColorDescriptor inactiveColor = ColorDescriptor.createFrom(new RGB(220, 220, 255));
92
93             @Override
94             public Color getInactiveBackground() {
95                 return resourceManager.createColor(inactiveColor);
96             }
97
98         });
99         expression.setTextFactory(new SVGTextFactory());
100         expression.addModifyListener(new SVGModifier());
101         expression.setInputValidator(new IInputValidator() {
102             @Override
103             public String isValid(String document) {
104                 try {
105                     XMLPrettyPrinter.parseDocument(document);
106                     return null;
107                 } catch (Exception e) {
108                     return e.getMessage();
109                 }
110             }
111         });
112         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression.getWidget());
113
114     }
115
116 }