]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/StandardPropertyPage.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / StandardPropertyPage.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.Arrays;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.Iterator;
18 import java.util.LinkedList;
19 import java.util.Set;
20 import java.util.TreeSet;
21 import java.util.function.Consumer;
22
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.ui.IWorkbenchPartSite;
27 import org.simantics.utils.ObjectUtils;
28
29 /**
30  * <p>
31  * See PropertyPage for more information on what can be extended or
32  * reimplemented from the superclasses. In addition to those, subclasses may
33  * extend or reimplement the following methods:
34  * <ul>
35  * <li><code>getContexts</code> - reimplement to define what browse contexts
36  * this property page should be initialized with. The same result can also be
37  * achieved by using the
38  * {@link StandardPropertyPage#StandardPropertyPage(IWorkbenchPartSite, Set)}
39  * constructor.</li>
40  * </ul>
41  * </p>
42  * 
43  * @author Tuukka Lehtonen
44  * 
45  * @see StandardProperties
46  */
47 public class StandardPropertyPage extends PropertyPage {
48
49     private final Set<String>          contexts;
50     private StandardProperties         tabs;
51
52     /**
53      * For updating the view title when the active tab changes.
54      */
55     //private transient ISelection       lastSelection;
56     private transient Consumer<String> lastTitleCallback;
57
58     /**
59      * Initializes a new standard property page for the specified workbench part
60      * without loading the page configuration from any extensible contexts.
61      * 
62      * @param site the workbench part site that contains this page or
63      *        <code>null</code> if there is no site, i.e. the page is within a
64      *        dialog or a plain shell.
65      */
66     public StandardPropertyPage(IWorkbenchPartSite site) {
67         this(site, new TreeSet<String>());
68     }
69
70     /**
71      * Initializes a new standard property page for the specified workbench part
72      * and specifies which contexts should be searched for
73      * {@link SelectionProcessor} and {@link PropertyTabContributor} bindings to
74      * initialize the page.
75      * 
76      * @param site the workbench part site that contains this page or
77      *        <code>null</code> if there is no site, i.e. the page is within a
78      *        dialog or a plain shell.
79      * @param contexts the contexts to search bindings for to initialize the
80      *        property page
81      */
82     public StandardPropertyPage(IWorkbenchPartSite site, Set<String> contexts) {
83         super(site);
84         this.contexts = contexts;
85     }
86
87     protected SelectionProcessor<?, ?> getSelectionProcessor() {
88         return new StandardSelectionProcessor();
89     }
90
91     protected Set<String> getContexts() {
92         return contexts;
93     }
94
95     @SuppressWarnings("unchecked")
96     @Override
97     protected void createPageControls(Composite parent) {
98         SelectionProcessor<?, ?> sp = getSelectionProcessor();
99         @SuppressWarnings("rawtypes")
100         Collection sps = null;
101         if (sp != null)
102             sps = Collections.singleton(sp);
103
104         tabs = new StandardProperties(sourceSite, getSite(), parent, SWT.NONE, getContexts(), sps) {
105             @Override
106             protected void activeTabChanged(TabChangeEvent event) {
107                 StandardPropertyPage.this.activeTabChanged(event);
108             }
109             @Override
110             protected ComparableTabContributor selectInitialTab(Collection<Object> selectionContents, Collection<ComparableTabContributor> contributions) {
111                 return StandardPropertyPage.this.selectInitialTab(selectionContents, contributions);
112             }
113             @Override
114             protected int getTabFolderStyle() {
115                 return StandardPropertyPage.this.getTabFolderStyle();
116             }
117         };
118         tab = tabs;
119         tab.createControl(tabs, getSessionContext());
120         getSite().setSelectionProvider(tabs.getSelectionProvider());
121
122         fillToolBar(getSite().getActionBars().getToolBarManager());
123         fillDropDownMenu(getSite().getActionBars().getMenuManager());
124     }
125
126     @Override
127     public void updatePartName(ISelection forSelection, Consumer<String> updateCallback) {
128         
129         if(!visible) {
130                 updateCallback.accept("Selection");
131                 return;
132         }
133         
134         //this.lastSelection = forSelection;
135         this.lastTitleCallback = updateCallback;
136         IPropertyTab tab = tabs.getActiveTab();
137         //System.out.println("updatePartName(" + forSelection + ", " + updateCallback + "): " + tab);
138         if (tab instanceof IPropertyTab2) {
139             //System.out.println("invoking tab.updatePartName for tab " + tab);
140                 // TabbedPropertyTable implementation reacts to events with several subsequent runnables.
141                 // This causes the part name update to be run before the input is set. Hence we have to set the input here.
142                 // TLe: this is broken since there is no guarantee that forSelection
143                 // is the proper input for this property tab whose input may well be
144                 // a transformed version of the original workbench selection.
145                 //((IPropertyTab2) tab).setInput(getSessionContext(), forSelection, false);
146             ((IPropertyTab2) tab).updatePartName(updateCallback);
147         } else {
148             //System.out.println("using default updatePartName for tab " + tab);
149             super.updatePartName(forSelection, updateCallback);
150         }
151     }
152
153     /**
154      * @return
155      */
156     protected int getTabFolderStyle() {
157         return SWT.BOTTOM | SWT.FLAT;
158     }
159
160     /**
161      * A simple tab label based implementation for restoring the last selected
162      * tab as the initial tab selection when the property page contents are
163      * reset. It statically stores the last selected tab names for
164      * {@value #MAX_SELECTED_TABS_REMEMBERED} last different ordered tab name
165      * combinations.
166      * 
167      * @param selectionContents
168      * @param contributions
169      * @return the previously selected tab contribution for a matching list of tabs
170      */
171     protected ComparableTabContributor selectInitialTab(Collection<Object> selectionContents, Collection<ComparableTabContributor> contributions) {
172         tabSelection = TabSelection.make(contributions);
173         for (Iterator<TabSelection> it = previouslySelectedTabs.iterator(); it.hasNext();) {
174             TabSelection t = it.next();
175             if (t.equals(tabSelection)) {
176                 tabSelection = t;
177                 // Move selection to front of list.
178                 it.remove();
179                 previouslySelectedTabs.addFirst(t);
180                 for (ComparableTabContributor contrib : contributions) {
181                     if (ObjectUtils.objectEquals(contrib.getLabel(), t.selected)) {
182                         return contrib;
183                     }
184                 }
185                 // selection must have been null, select first tab anyway. 
186                 return null;
187             }
188         }
189         tabSelection.setSelected(contributions.iterator().next().getLabel());
190         previouslySelectedTabs.addFirst(tabSelection);
191         while (previouslySelectedTabs.size() > MAX_SELECTED_TABS_REMEMBERED)
192             previouslySelectedTabs.removeLast();
193         return null;
194     }
195
196     protected void activeTabChanged(TabChangeEvent event) {
197         //System.out.println("active tab changed: " + event);
198         if (tabSelection != null)
199             tabSelection.setSelected(event.getNewTabLabel());
200         IPropertyTab tab = event.getNewTab();
201         if (tab instanceof IPropertyTab2) {
202             //System.out.println("invoking tab.updatePartName for tab " + tab);
203                 if(lastTitleCallback != null)
204                         ((IPropertyTab2) tab).updatePartName(lastTitleCallback);
205         }
206     }
207
208     /**
209      * The maximum number of previous initial tab selections to store. See
210      * {@link #previouslySelectedTabs} and {@link #tabSelection}.
211      */
212     private static final int                MAX_SELECTED_TABS_REMEMBERED = 20;
213
214     /**
215      * An LRU managed list of previously existing tab selections. The list is
216      * static but only used from the SWT display thread so no synchronization is
217      * necessary.
218      */
219     private static LinkedList<TabSelection> previouslySelectedTabs       = new LinkedList<TabSelection>();
220
221     /**
222      * The selection of tabs this property page is currently showing. Also
223      * contains the currently selected tab name, which is updated by
224      * {@link #activeTabChanged(TabChangeEvent)} and
225      * {@link #selectInitialTab(Collection, Collection)}.
226      */
227     private TabSelection                    tabSelection;
228
229     /**
230      * A class for representing a set of tabs by their labels and a marking the previously selected one. 
231      */
232     static class TabSelection {
233
234         private final String[] tabLabels;
235         private String         selected;
236
237         public static TabSelection make(Collection<ComparableTabContributor> contribs) {
238             String[] labels = new String[contribs.size()];
239             int i = 0;
240             for (ComparableTabContributor contrib : contribs)
241                 labels[i++] = contrib.getLabel();
242             return new TabSelection(labels);
243         }
244
245         public TabSelection(String... tabLabels) {
246             this.tabLabels = tabLabels;
247         }
248
249         public String getSelected() {
250             return selected;
251         }
252
253         /**
254          * Ensures that the selected tab is among the set of different tab
255          * labels. Otherwise the set request is ignored.
256          * 
257          * @param label
258          */
259         public void setSelected(String label) {
260             for (String l : tabLabels) {
261                 if (l.equals(label)) {
262                     this.selected = label;
263                     break;
264                 }
265             }
266         }
267
268         @Override
269         public int hashCode() {
270             return Arrays.hashCode(tabLabels);
271         }
272
273         @Override
274         public boolean equals(Object obj) {
275             if (this == obj)
276                 return true;
277             if (obj == null)
278                 return false;
279             if (getClass() != obj.getClass())
280                 return false;
281             TabSelection other = (TabSelection) obj;
282             return Arrays.equals(tabLabels, other.tabLabels);
283         }
284
285         @Override
286         public String toString() {
287             return getClass().getName() + "[" + Arrays.toString(tabLabels) + " - " + selected + "]";
288         }
289
290     }
291
292 }