]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewer.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / ComponentTypeViewer.java
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 java.util.ArrayList;
15
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;
50
51 public class ComponentTypeViewer {
52
53     ComponentTypeViewerData data;
54     
55     ResourceManager resourceManager;
56     
57     ArrayList<ComponentTypeViewerSection> sections = new ArrayList<ComponentTypeViewerSection>(3);
58
59     public ComponentTypeViewer(Composite parent, Resource _componentType, String formTitle) {
60         // Form
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));
67         {
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);
74         }
75         
76         // Data
77         data = new ComponentTypeViewerData(tk, _componentType, form);
78
79         // Sections
80         
81         sections.add(new ConfigurationPropertiesSection(data));
82         sections.add(new DerivedPropertiesSection(data));
83
84         BundleContext bundleContext = Activator.getContext();
85         try {
86             ArrayList<ComponentTypeViewerSectionFactory> factories = 
87                     Simantics.getSession().syncRequest(new Read<ArrayList<ComponentTypeViewerSectionFactory>>() {
88                         @Override
89                         public ArrayList<ComponentTypeViewerSectionFactory> perform(
90                                 ReadGraph graph) throws DatabaseException {
91                             ArrayList<ComponentTypeViewerSectionFactory> factories =
92                                     new ArrayList<ComponentTypeViewerSectionFactory>(3);
93                             try {
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);
101                                     }
102                             } catch (InvalidSyntaxException e) {
103                                 e.printStackTrace();
104                             }
105                             return factories;
106                         }
107                     });
108             for(ComponentTypeViewerSectionFactory factory : factories)
109                 sections.add(factory.create(data));
110         } catch(DatabaseException e) {
111             e.printStackTrace();
112         }
113         
114         sections.sort((a,b) -> { return Double.compare(a.getPriority(), b.getPriority()); });
115         
116         // Sashes
117         
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));
122             {
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);
127                 data.height = 10;
128                 sash.setLayoutData(data);
129             }
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);
137                     data.height = 10;
138                     sash.setLayoutData(data);
139                     form.getBody().layout(true);
140                 }
141             });
142             sashes[i] = sash;
143         }
144         for(int i=0;i<sections.size();++i) {
145             {
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);
153             }
154         }
155
156         // Listening
157         createGraphListener();
158     }
159     
160     private void createGraphListener() {
161         Simantics.getSession().asyncRequest(new ComponentTypePropertiesResultRequest(this.data.componentType, this.sections), new Listener<ComponentTypePropertiesResult>() {
162             @Override
163             public void execute(final ComponentTypePropertiesResult result) {  
164                 SWTUtils.asyncExec(data.form.getDisplay(), new Runnable() {
165                     @Override
166                     public void run() {
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()]);
170
171                         for(ComponentTypeViewerSection section : sections)
172                             section.update(result);
173                         setReadOnly(result.isImmutable());
174                     }
175                 });
176             }
177
178             @Override
179             public void exception(Throwable t) {
180                 ErrorLogger.defaultLogError(t);
181             }
182
183             @Override
184             public boolean isDisposed() {
185                 return data.form.isDisposed();
186             }
187
188         });
189     }
190
191     public void setFocus() {
192         data.form.setFocus();
193     }
194
195     /**
196      * @param readOnly
197      * @thread SWT
198      */
199     private void setReadOnly(boolean readOnly) {
200         if (readOnly == data.readOnly)
201             return;
202         data.readOnly = readOnly;
203         for(ComponentTypeViewerSection section : sections)
204             section.setReadOnly(readOnly);
205         IMessageManager mm = data.form.getMessageManager();
206         if (readOnly)
207             mm.addMessage("readonly", Messages.ComponentTypeViewer_OpenedInReadOnly, null, IMessageProvider.INFORMATION); //$NON-NLS-1$
208         else
209             mm.removeMessage("readonly"); //$NON-NLS-1$
210     }
211
212 }