]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/PropertyTabContributorImpl.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / PropertyTabContributorImpl.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.selectionview;
13
14 import java.util.function.Consumer;
15
16 import org.eclipse.jface.layout.GridDataFactory;
17 import org.eclipse.jface.layout.GridLayoutFactory;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ISelectionProvider;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Event;
25 import org.eclipse.swt.widgets.Listener;
26 import org.eclipse.ui.IWorkbenchSite;
27 import org.simantics.Simantics;
28 import org.simantics.browsing.ui.common.ErrorLogger;
29 import org.simantics.browsing.ui.common.views.IFilterArea;
30 import org.simantics.browsing.ui.common.views.IFilterAreaProvider;
31 import org.simantics.browsing.ui.swt.PartNameListener;
32 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
33 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
34 import org.simantics.db.Disposable;
35 import org.simantics.db.common.procedure.adapter.ListenerSupport;
36 import org.simantics.db.management.ISessionContext;
37 import org.simantics.db.request.Read;
38
39 /**
40  * Important points of customization:
41  * <ul>
42  * <li>{@link #createControls(Composite, IWorkbenchSite, ISessionContext, WidgetSupport)}</li>
43  * <li>{@link #getSelectionProvider()}</li>
44  * <li>{@link #getPartNameReadRequest(ISelection)}</li>
45  * </ul>
46
47  * @author Antti Villberg
48  * @author Tuukka Lehtonen
49  * @see ModelledTabContributor ModelledTabContributor for graph-modelled tab contributions
50  */
51 public abstract class PropertyTabContributorImpl implements PropertyTabContributor, Disposable /*, ListenerSupport*/ {
52
53     // For ListenerSupport (supporting DB request listeners)
54 //    protected boolean    disposed = false;
55 //    protected ISelection input    = StructuredSelection.EMPTY;
56 //    private Object identity;
57
58     abstract public void createControls(Composite body, IWorkbenchSite site, final ISessionContext context, WidgetSupport support);
59
60     public PropertyTabContributorImpl() {
61     }
62
63     public IFilterArea getFilterArea() {
64         return null;
65     }
66
67     public void requestFocus() {
68     }
69
70     public ISelectionProvider getSelectionProvider() {
71         return null;
72     }
73
74     public String getPartName(ISelection forSelection) {
75         return null;
76     }
77     
78     public Read<String> getPartNameReadRequest(ISelection forSelection) {
79         return null;
80     }
81
82 //    public void updatePartName(ISelection forSelection, Callback<String> updateCallback) {
83 //        Read<String> read = getPartNameReadRequest(forSelection);
84 //        if (read == null) {
85 //            updateCallback.run("Override to control part name (PropertyTabContributorImpl.updatePartName)");
86 //        } else {
87 //            Simantics.getSession().asyncRequest(read, new PartNameListener(updateCallback, this));
88 //        }
89 //    }
90
91 //    public void updatePartName(Callback<String> updateCallback) {
92 //        updatePartName(input, updateCallback);
93 //    }
94
95 //    protected void dispose() {
96 //        this.disposed = true;
97 //    }
98
99 //    @Override
100 //    public void exception(Throwable t) {
101 //        ErrorLogger.defaultLogError(getClass().getSimpleName() + " received unexpected exception.", t);
102 //    }
103
104 //    @Override
105 //    public boolean isDisposed() {
106 //        return disposed;
107 //    }
108
109     public void createControl(Composite parent, final IWorkbenchSite site, final ISessionContext context, final WidgetSupportImpl support) {
110
111         class TabComposite extends Composite {
112             public TabComposite(Composite parent) {
113                 super(parent, 0);
114
115                 GridLayoutFactory.fillDefaults().applyTo(parent);
116                 GridDataFactory.fillDefaults().span(1, 1).grab(true, true).applyTo(this);
117
118                 Composite body = this;
119                 GridLayoutFactory.fillDefaults().spacing(0, 0).equalWidth(false).numColumns(1).applyTo(body);
120
121                 try {
122                     PropertyTabContributorImpl.this.createControls(body, site, context, support);
123                 } catch (Throwable t) {
124                     ErrorLogger.defaultLogError(t);
125                 }
126             }
127         }
128
129         final TabComposite tc = new TabComposite(parent);
130         tc.addListener(SWT.Dispose, new Listener() {
131                 public void handleEvent(Event e) {
132                         if(support instanceof Disposable)
133                                 ((Disposable)support).dispose();
134                         PropertyTabContributorImpl.this.dispose();
135             }
136         });
137         
138     }
139
140     @Override
141     public IPropertyTab create(Composite parent, IWorkbenchSite site, ISessionContext context, Object input) {
142         IPropertyTab tab = new Tab(site, parent);
143         tab.createControl((Composite) tab.getControl(), context);
144         return tab;
145     }
146
147     protected WidgetSupportImpl createSupport() {
148         return new WidgetSupportImpl();
149     }
150     
151     class Tab extends Composite implements IPropertyTab2, IFilterAreaProvider, ListenerSupport {
152
153         final IWorkbenchSite    site;
154         final WidgetSupportImpl support = createSupport();
155         protected ISelection input    = StructuredSelection.EMPTY;
156
157         public Tab(IWorkbenchSite site, Composite parent) {
158             super(parent, SWT.NONE);
159             this.site = site;
160         }
161
162         @Override
163         public void createControl(Composite parent, ISessionContext context) {
164             PropertyTabContributorImpl.this.createControl(parent, site, context, support);
165         }
166
167         @Override
168         public Control getControl() {
169             return this;
170         }
171
172         @Override
173         public boolean isDisposed() {
174             return super.isDisposed();
175         }
176
177         @Override
178         public void requestFocus() {
179             PropertyTabContributorImpl.this.requestFocus();
180         }
181
182         @Override
183         public void setInput(ISessionContext context, ISelection selection, boolean force) {
184             this.input = selection;
185             support.fireInput(context, selection);
186         }
187
188         @Override
189         public ISelectionProvider getSelectionProvider() {
190             return PropertyTabContributorImpl.this.getSelectionProvider();
191         }
192
193         @Override
194         public IFilterArea getFilterArea() {
195             return PropertyTabContributorImpl.this.getFilterArea();
196         }
197
198         @Override
199         public void updatePartName(Consumer<String> updateCallback) {
200                 String partName = PropertyTabContributorImpl.this.getPartName(input);
201                 if(partName != null) {
202                         updateCallback.accept(partName);
203                         return;
204                 }
205                 Read<String> read = getPartNameReadRequest(input);
206                 if (read == null) {
207                         updateCallback.accept("Override to control part name (PropertyTabContributorImpl.updatePartName)");
208                 } else {
209                         Simantics.getSession().asyncRequest(read, new PartNameListener(updateCallback, this));
210                 }
211             //PropertyTabContributorImpl.this.updatePartName(input, updateCallback);
212         }
213
214                 @Override
215                 public void exception(Throwable t) {
216                 ErrorLogger.defaultLogError(getClass().getSimpleName() + " received unexpected exception.", t);
217                 }
218
219     }
220     
221     @Override
222     public void dispose() {
223     }
224     
225 }