1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.componentTypeEditor;
14 import java.util.ArrayList;
16 import org.eclipse.jface.dialogs.IMessageProvider;
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.jface.resource.LocalResourceManager;
19 import org.eclipse.jface.resource.ResourceManager;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.layout.FormAttachment;
24 import org.eclipse.swt.layout.FormData;
25 import org.eclipse.swt.layout.FormLayout;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Sash;
28 import org.eclipse.ui.forms.IMessageManager;
29 import org.eclipse.ui.forms.widgets.Form;
30 import org.eclipse.ui.forms.widgets.FormToolkit;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.framework.InvalidSyntaxException;
33 import org.osgi.framework.ServiceReference;
34 import org.simantics.Simantics;
35 import org.simantics.databoard.Bindings;
36 import org.simantics.databoard.binding.Binding;
37 import org.simantics.databoard.type.Datatype;
38 import org.simantics.db.ReadGraph;
39 import org.simantics.db.Resource;
40 import org.simantics.db.common.NamedResource;
41 import org.simantics.db.exception.DatabaseException;
42 import org.simantics.db.procedure.Listener;
43 import org.simantics.db.request.Read;
44 import org.simantics.layer0.Layer0;
45 import org.simantics.modeling.ui.Activator;
46 import org.simantics.modeling.utils.ComponentTypePropertiesResult;
47 import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
48 import org.simantics.utils.ui.ErrorLogger;
49 import org.simantics.utils.ui.SWTUtils;
51 public class ComponentTypeViewer {
53 ComponentTypeViewerData data;
55 ResourceManager resourceManager;
57 ArrayList<ComponentTypeViewerSection> sections = new ArrayList<ComponentTypeViewerSection>(3);
59 public ComponentTypeViewer(Composite parent, Resource _componentType, String formTitle) {
61 FormToolkit tk = new FormToolkit(parent.getDisplay());
62 Form form = tk.createForm(parent);
63 tk.decorateFormHeading(form);
64 resourceManager = new LocalResourceManager(JFaceResources.getResources(), form);
65 form.setText(formTitle);
66 form.setImage(resourceManager.createImage(Activator.COMPONENT_TYPE_ICON));
68 FormLayout layout = new FormLayout();
69 layout.marginBottom = 10;
70 layout.marginTop = 10;
71 layout.marginLeft = 10;
72 layout.marginRight = 10;
73 form.getBody().setLayout(layout);
77 data = new ComponentTypeViewerData(tk, _componentType, form);
81 sections.add(new ConfigurationPropertiesSection(data));
82 sections.add(new DerivedPropertiesSection(data));
84 BundleContext bundleContext = Activator.getContext();
86 ArrayList<ComponentTypeViewerSectionFactory> factories =
87 Simantics.getSession().syncRequest(new Read<ArrayList<ComponentTypeViewerSectionFactory>>() {
89 public ArrayList<ComponentTypeViewerSectionFactory> perform(
90 ReadGraph graph) throws DatabaseException {
91 ArrayList<ComponentTypeViewerSectionFactory> factories =
92 new ArrayList<ComponentTypeViewerSectionFactory>(3);
94 String className = ComponentTypeViewerSectionFactory.class.getName();
95 ServiceReference<?>[] references = bundleContext.getAllServiceReferences(className, null);
96 if(references != null)
97 for(ServiceReference<?> reference : references) {
98 ComponentTypeViewerSectionFactory factory = (ComponentTypeViewerSectionFactory)bundleContext.getService(reference);
99 if(factory.doesSupport(graph, _componentType))
100 factories.add(factory);
102 } catch (InvalidSyntaxException e) {
108 for(ComponentTypeViewerSectionFactory factory : factories)
109 sections.add(factory.create(data));
110 } catch(DatabaseException e) {
114 sections.sort((a,b) -> { return Double.compare(a.getPriority(), b.getPriority()); });
118 Sash[] sashes = new Sash[sections.size()-1];
119 for(int i=0;i<sections.size()-1;++i) {
120 Sash sash = new Sash(form.getBody(), SWT.HORIZONTAL);
121 sash.setBackground(sash.getDisplay().getSystemColor(SWT.COLOR_WHITE));
123 FormData data = new FormData();
124 data.top = new FormAttachment(100*(i+1)/sections.size(), 0);
125 data.left = new FormAttachment(0, 0);
126 data.right = new FormAttachment(100, 0);
128 sash.setLayoutData(data);
130 sash.addSelectionListener(new SelectionAdapter() {
131 public void widgetSelected(SelectionEvent e) {
132 sash.setBounds(e.x, e.y, e.width, e.height);
133 FormData data = new FormData();
134 data.top = new FormAttachment(0, e.y);
135 data.left = new FormAttachment(0, 0);
136 data.right = new FormAttachment(100, 0);
138 sash.setLayoutData(data);
139 form.getBody().layout(true);
144 for(int i=0;i<sections.size();++i) {
146 FormData formData = new FormData();
147 formData.top = i > 0 ? new FormAttachment(sashes[i-1]) : new FormAttachment(0, 0);
148 formData.left = new FormAttachment(0, 0);
149 formData.right = new FormAttachment(100, 0);
150 formData.bottom = i < sections.size()-1
151 ? new FormAttachment(sashes[i]) : new FormAttachment(100, 0);
152 sections.get(i).getSection().setLayoutData(formData);
157 createGraphListener();
160 private void createGraphListener() {
161 Simantics.getSession().asyncRequest(new ComponentTypePropertiesResultRequest(this.data.componentType, this.sections), new Listener<ComponentTypePropertiesResult>() {
163 public void execute(final ComponentTypePropertiesResult result) {
164 SWTUtils.asyncExec(data.form.getDisplay(), new Runnable() {
167 // Store loaded properties
168 data.properties = result.getProperties().toArray(new ComponentTypeViewerPropertyInfo[result.getProperties().size()]);
169 data.connectionPoints = result.getConnectionPoints().toArray(new NamedResource[result.getConnectionPoints().size()]);
171 for(ComponentTypeViewerSection section : sections)
172 section.update(result);
173 setReadOnly(result.isImmutable());
179 public void exception(Throwable t) {
180 ErrorLogger.defaultLogError(t);
184 public boolean isDisposed() {
185 return data.form.isDisposed();
191 public void setFocus() {
192 data.form.setFocus();
199 private void setReadOnly(boolean readOnly) {
200 if (readOnly == data.readOnly)
202 data.readOnly = readOnly;
203 for(ComponentTypeViewerSection section : sections)
204 section.setReadOnly(readOnly);
205 IMessageManager mm = data.form.getMessageManager();
207 mm.addMessage("readonly", Messages.ComponentTypeViewer_OpenedInReadOnly, null, IMessageProvider.INFORMATION); //$NON-NLS-1$
209 mm.removeMessage("readonly"); //$NON-NLS-1$