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