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