1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.selectionview;
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;
20 import java.util.TreeSet;
21 import java.util.function.Consumer;
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;
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:
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)}
43 * @author Tuukka Lehtonen
45 * @see StandardProperties
47 public class StandardPropertyPage extends PropertyPage {
49 private final Set<String> contexts;
50 private StandardProperties tabs;
53 * For updating the view title when the active tab changes.
55 //private transient ISelection lastSelection;
56 private transient Consumer<String> lastTitleCallback;
59 * Initializes a new standard property page for the specified workbench part
60 * without loading the page configuration from any extensible contexts.
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.
66 public StandardPropertyPage(IWorkbenchPartSite site) {
67 this(site, new TreeSet<String>());
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.
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
82 public StandardPropertyPage(IWorkbenchPartSite site, Set<String> contexts) {
84 this.contexts = contexts;
87 protected SelectionProcessor<?, ?> getSelectionProcessor() {
88 return new StandardSelectionProcessor();
91 protected Set<String> getContexts() {
95 @SuppressWarnings("unchecked")
97 protected void createPageControls(Composite parent) {
98 SelectionProcessor<?, ?> sp = getSelectionProcessor();
99 @SuppressWarnings("rawtypes")
100 Collection sps = null;
102 sps = Collections.singleton(sp);
104 tabs = new StandardProperties(sourceSite, getSite(), parent, SWT.NONE, getContexts(), sps) {
106 protected void activeTabChanged(TabChangeEvent event) {
107 StandardPropertyPage.this.activeTabChanged(event);
110 protected ComparableTabContributor selectInitialTab(Collection<Object> selectionContents, Collection<ComparableTabContributor> contributions) {
111 return StandardPropertyPage.this.selectInitialTab(selectionContents, contributions);
114 protected int getTabFolderStyle() {
115 return StandardPropertyPage.this.getTabFolderStyle();
119 tab.createControl(tabs, getSessionContext());
120 getSite().setSelectionProvider(tabs.getSelectionProvider());
122 fillToolBar(getSite().getActionBars().getToolBarManager());
123 fillDropDownMenu(getSite().getActionBars().getMenuManager());
127 public void updatePartName(ISelection forSelection, Consumer<String> updateCallback) {
130 updateCallback.accept("Selection");
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);
148 //System.out.println("using default updatePartName for tab " + tab);
149 super.updatePartName(forSelection, updateCallback);
156 protected int getTabFolderStyle() {
157 return SWT.BOTTOM | SWT.FLAT;
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
167 * @param selectionContents
168 * @param contributions
169 * @return the previously selected tab contribution for a matching list of tabs
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)) {
177 // Move selection to front of list.
179 previouslySelectedTabs.addFirst(t);
180 for (ComparableTabContributor contrib : contributions) {
181 if (ObjectUtils.objectEquals(contrib.getLabel(), t.selected)) {
185 // selection must have been null, select first tab anyway.
189 tabSelection.setSelected(contributions.iterator().next().getLabel());
190 previouslySelectedTabs.addFirst(tabSelection);
191 while (previouslySelectedTabs.size() > MAX_SELECTED_TABS_REMEMBERED)
192 previouslySelectedTabs.removeLast();
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);
209 * The maximum number of previous initial tab selections to store. See
210 * {@link #previouslySelectedTabs} and {@link #tabSelection}.
212 private static final int MAX_SELECTED_TABS_REMEMBERED = 20;
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
219 private static LinkedList<TabSelection> previouslySelectedTabs = new LinkedList<TabSelection>();
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)}.
227 private TabSelection tabSelection;
230 * A class for representing a set of tabs by their labels and a marking the previously selected one.
232 static class TabSelection {
234 private final String[] tabLabels;
235 private String selected;
237 public static TabSelection make(Collection<ComparableTabContributor> contribs) {
238 String[] labels = new String[contribs.size()];
240 for (ComparableTabContributor contrib : contribs)
241 labels[i++] = contrib.getLabel();
242 return new TabSelection(labels);
245 public TabSelection(String... tabLabels) {
246 this.tabLabels = tabLabels;
249 public String getSelected() {
254 * Ensures that the selected tab is among the set of different tab
255 * labels. Otherwise the set request is ignored.
259 public void setSelected(String label) {
260 for (String l : tabLabels) {
261 if (l.equals(label)) {
262 this.selected = label;
269 public int hashCode() {
270 return Arrays.hashCode(tabLabels);
274 public boolean equals(Object obj) {
279 if (getClass() != obj.getClass())
281 TabSelection other = (TabSelection) obj;
282 return Arrays.equals(tabLabels, other.tabLabels);
286 public String toString() {
287 return getClass().getName() + "[" + Arrays.toString(tabLabels) + " - " + selected + "]";