1 /*******************************************************************************
2 * Copyright (c) 2012 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.componentTypeEditor;
14 import org.eclipse.jface.layout.GridDataFactory;
15 import org.eclipse.jface.layout.GridLayoutFactory;
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.jface.resource.LocalResourceManager;
18 import org.eclipse.jface.resource.ResourceManager;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.DisposeEvent;
21 import org.eclipse.swt.events.DisposeListener;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.layout.FillLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.ui.forms.widgets.Form;
28 import org.eclipse.ui.forms.widgets.FormToolkit;
29 import org.eclipse.ui.forms.widgets.Section;
30 import org.simantics.Simantics;
31 import org.simantics.databoard.Bindings;
32 import org.simantics.databoard.binding.Binding;
33 import org.simantics.databoard.type.Datatype;
34 import org.simantics.db.ReadGraph;
35 import org.simantics.db.Resource;
36 import org.simantics.db.WriteGraph;
37 import org.simantics.db.common.request.ResourceRead;
38 import org.simantics.db.common.request.WriteRequest;
39 import org.simantics.db.exception.DatabaseException;
40 import org.simantics.db.procedure.Listener;
41 import org.simantics.layer0.Layer0;
42 import org.simantics.modeling.ui.Activator;
43 import org.simantics.scl.ui.editor.SCLTextEditorNew;
44 import org.simantics.utils.ui.ErrorLogger;
45 import org.simantics.utils.ui.SWTUtils;
47 public class SCLModuleViewer {
49 ResourceManager resourceManager;
51 Resource componentType;
53 SCLTextEditorNew code;
58 public SCLModuleViewer(Composite parent, Resource _componentType, String formTitle) {
59 this.componentType = _componentType;
61 tk = new FormToolkit(parent.getDisplay());
62 form = tk.createForm(parent);
63 tk.decorateFormHeading(form);
65 resourceManager = new LocalResourceManager(JFaceResources.getResources(), form);
66 form.setText(formTitle);
67 form.setImage(resourceManager.createImage(Activator.COMPONENT_TYPE_ICON));
69 GridLayoutFactory.fillDefaults().margins(10, 10).applyTo(form.getBody());
73 Section codeSection = tk.createSection(form.getBody(), Section.TITLE_BAR | Section.EXPANDED);
74 codeSection.setLayout(new FillLayout());
75 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(codeSection);
76 codeSection.setText("Code");
78 Composite codeSectionBody = tk.createComposite(codeSection);
79 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(codeSectionBody);
80 codeSection.setClient(codeSectionBody);
82 Composite codeButtons = tk.createComposite(codeSectionBody);
83 GridDataFactory.fillDefaults().applyTo(codeButtons);
84 GridLayoutFactory.fillDefaults().applyTo(codeButtons);
86 Button applyChanges = tk.createButton(codeButtons, "Apply changes", SWT.PUSH);
88 code = new SCLTextEditorNew(codeSectionBody, 0);
90 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(code);
93 GridDataFactory.fillDefaults().applyTo(applyChanges);
95 applyChanges.addSelectionListener(new SelectionAdapter() {
97 public void widgetSelected(SelectionEvent e) {
99 final String text = code.getContent();
101 Simantics.getSession().async(new WriteRequest() {
103 public void perform(WriteGraph graph)
104 throws DatabaseException {
105 Layer0 L0 = Layer0.getInstance(graph);
106 graph.claimLiteral(componentType, L0.SCLModule_definition, text, Bindings.STRING);
113 // Section section = tk.createSection(form.getBody(), Section.TITLE_BAR | Section.EXPANDED);
114 // section.setLayout(new FillLayout());
115 // GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(section);
116 // section.setText("Problems");
118 Simantics.getSession().asyncRequest(new ResourceRead<String>(componentType) {
120 public String perform(ReadGraph graph) throws DatabaseException {
121 Layer0 L0 = Layer0.getInstance(graph);
122 return graph.getPossibleRelatedValue(resource, L0.SCLModule_definition, Bindings.STRING);
124 }, new Listener<String>() {
126 public void execute(final String text) {
127 SWTUtils.asyncExec(code, new Runnable() {
131 if (code.isDisposed())
134 code.setContent(text, null);
141 public void exception(Throwable t) {
142 ErrorLogger.defaultLogError(t);
146 public boolean isDisposed() {
147 return code.isDisposed();
154 code.addDisposeListener(new DisposeListener() {
156 public void widgetDisposed(DisposeEvent e) {
163 protected Datatype getPossibleDatatype(ReadGraph graph, Resource literal) throws DatabaseException {
164 Binding binding = Bindings.getBindingUnchecked(Datatype.class);
165 for (Resource dataTypeResource : graph.getObjects(literal, Layer0.getInstance(graph).HasDataType)) {
166 Datatype dt = graph.getPossibleValue(dataTypeResource, binding);
173 public void setFocus() {
174 if(code != null && !code.isDisposed())