]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/SCLModuleViewer.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / SCLModuleViewer.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.componentTypeEditor;\r
13 \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
46 \r
47 public class SCLModuleViewer {\r
48 \r
49     ResourceManager resourceManager;\r
50 \r
51     Resource componentType;\r
52     \r
53     SCLTextEditorNew code;\r
54 \r
55     Form form;\r
56     FormToolkit tk;\r
57 \r
58     public SCLModuleViewer(Composite parent, Resource _componentType, String formTitle) {\r
59         this.componentType = _componentType;\r
60 \r
61         tk = new FormToolkit(parent.getDisplay());\r
62         form = tk.createForm(parent);\r
63         tk.decorateFormHeading(form);\r
64 \r
65         resourceManager = new LocalResourceManager(JFaceResources.getResources(), form);\r
66         form.setText(formTitle);\r
67         form.setImage(resourceManager.createImage(Activator.COMPONENT_TYPE_ICON));\r
68 \r
69         GridLayoutFactory.fillDefaults().margins(10, 10).applyTo(form.getBody());\r
70 \r
71         // Table\r
72 \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
77         \r
78         Composite codeSectionBody = tk.createComposite(codeSection);\r
79         GridLayoutFactory.fillDefaults().numColumns(1).applyTo(codeSectionBody);\r
80         codeSection.setClient(codeSectionBody);\r
81 \r
82         Composite codeButtons = tk.createComposite(codeSectionBody);\r
83         GridDataFactory.fillDefaults().applyTo(codeButtons);\r
84         GridLayoutFactory.fillDefaults().applyTo(codeButtons);\r
85 \r
86         Button applyChanges = tk.createButton(codeButtons, "Apply changes", SWT.PUSH);\r
87 \r
88         code = new SCLTextEditorNew(codeSectionBody, 0);\r
89         \r
90         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(code);\r
91         \r
92 \r
93         GridDataFactory.fillDefaults().applyTo(applyChanges);\r
94 \r
95         applyChanges.addSelectionListener(new SelectionAdapter() {\r
96             @Override\r
97             public void widgetSelected(SelectionEvent e) {\r
98 \r
99                 final String text = code.getContent(); \r
100                 \r
101                 Simantics.getSession().async(new WriteRequest() {\r
102                     @Override\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
107                     }\r
108                 });\r
109                 \r
110             }\r
111         });\r
112         \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
117         \r
118         Simantics.getSession().asyncRequest(new ResourceRead<String>(componentType) {\r
119             @Override\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
123             }\r
124         }, new Listener<String>() {\r
125             @Override\r
126             public void execute(final String text) {\r
127                 SWTUtils.asyncExec(code, new Runnable() {\r
128                     @Override\r
129                     public void run() {\r
130                         \r
131                         if (code.isDisposed())\r
132                             return;\r
133                         \r
134                         code.setContent(text, null);\r
135                         \r
136                     }\r
137                 });\r
138             }\r
139 \r
140             @Override\r
141             public void exception(Throwable t) {\r
142                 ErrorLogger.defaultLogError(t);\r
143             }\r
144 \r
145             @Override\r
146             public boolean isDisposed() {\r
147                 return code.isDisposed();\r
148             }\r
149 \r
150         });        \r
151 \r
152         // Disposing\r
153 \r
154         code.addDisposeListener(new DisposeListener() {\r
155             @Override\r
156             public void widgetDisposed(DisposeEvent e) {\r
157                 tk.dispose();\r
158             }\r
159         });\r
160 \r
161     }\r
162 \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
167             if (dt != null)\r
168                 return dt;\r
169         }\r
170         return null;\r
171     }\r
172 \r
173     public void setFocus() {\r
174         if(code != null && !code.isDisposed())\r
175             code.setFocus();\r
176     }\r
177 \r
178 }\r