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