--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.componentTypeEditor;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.jface.resource.JFaceResources;\r
+import org.eclipse.jface.resource.LocalResourceManager;\r
+import org.eclipse.jface.resource.ResourceManager;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.DisposeEvent;\r
+import org.eclipse.swt.events.DisposeListener;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.layout.FillLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.ui.forms.widgets.Form;\r
+import org.eclipse.ui.forms.widgets.FormToolkit;\r
+import org.eclipse.ui.forms.widgets.Section;\r
+import org.simantics.Simantics;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.ResourceRead;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ui.Activator;\r
+import org.simantics.scl.ui.editor.SCLTextEditorNew;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.SWTUtils;\r
+\r
+public class SCLModuleViewer {\r
+\r
+ ResourceManager resourceManager;\r
+\r
+ Resource componentType;\r
+ \r
+ SCLTextEditorNew code;\r
+\r
+ Form form;\r
+ FormToolkit tk;\r
+\r
+ public SCLModuleViewer(Composite parent, Resource _componentType, String formTitle) {\r
+ this.componentType = _componentType;\r
+\r
+ tk = new FormToolkit(parent.getDisplay());\r
+ form = tk.createForm(parent);\r
+ tk.decorateFormHeading(form);\r
+\r
+ resourceManager = new LocalResourceManager(JFaceResources.getResources(), form);\r
+ form.setText(formTitle);\r
+ form.setImage(resourceManager.createImage(Activator.COMPONENT_TYPE_ICON));\r
+\r
+ GridLayoutFactory.fillDefaults().margins(10, 10).applyTo(form.getBody());\r
+\r
+ // Table\r
+\r
+ Section codeSection = tk.createSection(form.getBody(), Section.TITLE_BAR | Section.EXPANDED);\r
+ codeSection.setLayout(new FillLayout());\r
+ GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(codeSection);\r
+ codeSection.setText("Code");\r
+ \r
+ Composite codeSectionBody = tk.createComposite(codeSection);\r
+ GridLayoutFactory.fillDefaults().numColumns(1).applyTo(codeSectionBody);\r
+ codeSection.setClient(codeSectionBody);\r
+\r
+ Composite codeButtons = tk.createComposite(codeSectionBody);\r
+ GridDataFactory.fillDefaults().applyTo(codeButtons);\r
+ GridLayoutFactory.fillDefaults().applyTo(codeButtons);\r
+\r
+ Button applyChanges = tk.createButton(codeButtons, "Apply changes", SWT.PUSH);\r
+\r
+ code = new SCLTextEditorNew(codeSectionBody, 0);\r
+ \r
+ GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(code);\r
+ \r
+\r
+ GridDataFactory.fillDefaults().applyTo(applyChanges);\r
+\r
+ applyChanges.addSelectionListener(new SelectionAdapter() {\r
+ @Override\r
+ public void widgetSelected(SelectionEvent e) {\r
+\r
+ final String text = code.getContent(); \r
+ \r
+ Simantics.getSession().async(new WriteRequest() {\r
+ @Override\r
+ public void perform(WriteGraph graph)\r
+ throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ graph.claimLiteral(componentType, L0.SCLModule_definition, text, Bindings.STRING);\r
+ }\r
+ });\r
+ \r
+ }\r
+ });\r
+ \r
+// Section section = tk.createSection(form.getBody(), Section.TITLE_BAR | Section.EXPANDED);\r
+// section.setLayout(new FillLayout());\r
+// GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(section);\r
+// section.setText("Problems");\r
+ \r
+ Simantics.getSession().asyncRequest(new ResourceRead<String>(componentType) {\r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ return graph.getPossibleRelatedValue(resource, L0.SCLModule_definition, Bindings.STRING);\r
+ }\r
+ }, new Listener<String>() {\r
+ @Override\r
+ public void execute(final String text) {\r
+ SWTUtils.asyncExec(code, new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ \r
+ if (code.isDisposed())\r
+ return;\r
+ \r
+ code.setContent(text, null);\r
+ \r
+ }\r
+ });\r
+ }\r
+\r
+ @Override\r
+ public void exception(Throwable t) {\r
+ ErrorLogger.defaultLogError(t);\r
+ }\r
+\r
+ @Override\r
+ public boolean isDisposed() {\r
+ return code.isDisposed();\r
+ }\r
+\r
+ }); \r
+\r
+ // Disposing\r
+\r
+ code.addDisposeListener(new DisposeListener() {\r
+ @Override\r
+ public void widgetDisposed(DisposeEvent e) {\r
+ tk.dispose();\r
+ }\r
+ });\r
+\r
+ }\r
+\r
+ protected Datatype getPossibleDatatype(ReadGraph graph, Resource literal) throws DatabaseException {\r
+ Binding binding = Bindings.getBindingUnchecked(Datatype.class);\r
+ for (Resource dataTypeResource : graph.getObjects(literal, Layer0.getInstance(graph).HasDataType)) {\r
+ Datatype dt = graph.getPossibleValue(dataTypeResource, binding);\r
+ if (dt != null)\r
+ return dt;\r
+ }\r
+ return null;\r
+ }\r
+\r
+ public void setFocus() {\r
+ if(code != null && !code.isDisposed())\r
+ code.setFocus();\r
+ }\r
+\r
+}\r