1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.selectionview;
\r
14 import java.util.Arrays;
\r
15 import java.util.Collection;
\r
16 import java.util.Collections;
\r
17 import java.util.Iterator;
\r
18 import java.util.LinkedList;
\r
19 import java.util.Set;
\r
20 import java.util.TreeSet;
\r
21 import java.util.function.Consumer;
\r
23 import org.eclipse.jface.viewers.ISelection;
\r
24 import org.eclipse.swt.SWT;
\r
25 import org.eclipse.swt.widgets.Composite;
\r
26 import org.eclipse.ui.IWorkbenchPartSite;
\r
27 import org.simantics.utils.ObjectUtils;
\r
31 * See PropertyPage for more information on what can be extended or
\r
32 * reimplemented from the superclasses. In addition to those, subclasses may
\r
33 * extend or reimplement the following methods:
\r
35 * <li><code>getContexts</code> - reimplement to define what browse contexts
\r
36 * this property page should be initialized with. The same result can also be
\r
37 * achieved by using the
\r
38 * {@link StandardPropertyPage#StandardPropertyPage(IWorkbenchPartSite, Set)}
\r
43 * @author Tuukka Lehtonen
\r
45 * @see StandardProperties
\r
47 public class StandardPropertyPage extends PropertyPage {
\r
49 private final Set<String> contexts;
\r
50 private StandardProperties tabs;
\r
53 * For updating the view title when the active tab changes.
\r
55 //private transient ISelection lastSelection;
\r
56 private transient Consumer<String> lastTitleCallback;
\r
59 * Initializes a new standard property page for the specified workbench part
\r
60 * without loading the page configuration from any extensible contexts.
\r
62 * @param site the workbench part site that contains this page or
\r
63 * <code>null</code> if there is no site, i.e. the page is within a
\r
64 * dialog or a plain shell.
\r
66 public StandardPropertyPage(IWorkbenchPartSite site) {
\r
67 this(site, new TreeSet<String>());
\r
71 * Initializes a new standard property page for the specified workbench part
\r
72 * and specifies which contexts should be searched for
\r
73 * {@link SelectionProcessor} and {@link PropertyTabContributor} bindings to
\r
74 * initialize the page.
\r
76 * @param site the workbench part site that contains this page or
\r
77 * <code>null</code> if there is no site, i.e. the page is within a
\r
78 * dialog or a plain shell.
\r
79 * @param contexts the contexts to search bindings for to initialize the
\r
82 public StandardPropertyPage(IWorkbenchPartSite site, Set<String> contexts) {
\r
84 this.contexts = contexts;
\r
87 protected SelectionProcessor<?, ?> getSelectionProcessor() {
\r
88 return new StandardSelectionProcessor();
\r
91 protected Set<String> getContexts() {
\r
95 @SuppressWarnings("unchecked")
\r
97 protected void createPageControls(Composite parent) {
\r
98 SelectionProcessor<?, ?> sp = getSelectionProcessor();
\r
99 @SuppressWarnings("rawtypes")
\r
100 Collection sps = null;
\r
102 sps = Collections.singleton(sp);
\r
104 tabs = new StandardProperties(sourceSite, getSite(), parent, SWT.NONE, getContexts(), sps) {
\r
106 protected void activeTabChanged(TabChangeEvent event) {
\r
107 StandardPropertyPage.this.activeTabChanged(event);
\r
110 protected ComparableTabContributor selectInitialTab(Collection<Object> selectionContents, Collection<ComparableTabContributor> contributions) {
\r
111 return StandardPropertyPage.this.selectInitialTab(selectionContents, contributions);
\r
114 protected int getTabFolderStyle() {
\r
115 return StandardPropertyPage.this.getTabFolderStyle();
\r
119 tab.createControl(tabs, getSessionContext());
\r
120 getSite().setSelectionProvider(tabs.getSelectionProvider());
\r
122 fillToolBar(getSite().getActionBars().getToolBarManager());
\r
123 fillDropDownMenu(getSite().getActionBars().getMenuManager());
\r
127 public void updatePartName(ISelection forSelection, Consumer<String> updateCallback) {
\r
130 updateCallback.accept("Selection");
\r
134 //this.lastSelection = forSelection;
\r
135 this.lastTitleCallback = updateCallback;
\r
136 IPropertyTab tab = tabs.getActiveTab();
\r
137 //System.out.println("updatePartName(" + forSelection + ", " + updateCallback + "): " + tab);
\r
138 if (tab instanceof IPropertyTab2) {
\r
139 //System.out.println("invoking tab.updatePartName for tab " + tab);
\r
140 // TabbedPropertyTable implementation reacts to events with several subsequent runnables.
\r
141 // This causes the part name update to be run before the input is set. Hence we have to set the input here.
\r
142 // TLe: this is broken since there is no guarantee that forSelection
\r
143 // is the proper input for this property tab whose input may well be
\r
144 // a transformed version of the original workbench selection.
\r
145 //((IPropertyTab2) tab).setInput(getSessionContext(), forSelection, false);
\r
146 ((IPropertyTab2) tab).updatePartName(updateCallback);
\r
148 //System.out.println("using default updatePartName for tab " + tab);
\r
149 super.updatePartName(forSelection, updateCallback);
\r
156 protected int getTabFolderStyle() {
\r
157 return SWT.BOTTOM | SWT.FLAT;
\r
161 * A simple tab label based implementation for restoring the last selected
\r
162 * tab as the initial tab selection when the property page contents are
\r
163 * reset. It statically stores the last selected tab names for
\r
164 * {@value #MAX_SELECTED_TABS_REMEMBERED} last different ordered tab name
\r
167 * @param selectionContents
\r
168 * @param contributions
\r
169 * @return the previously selected tab contribution for a matching list of tabs
\r
171 protected ComparableTabContributor selectInitialTab(Collection<Object> selectionContents, Collection<ComparableTabContributor> contributions) {
\r
172 tabSelection = TabSelection.make(contributions);
\r
173 for (Iterator<TabSelection> it = previouslySelectedTabs.iterator(); it.hasNext();) {
\r
174 TabSelection t = it.next();
\r
175 if (t.equals(tabSelection)) {
\r
177 // Move selection to front of list.
\r
179 previouslySelectedTabs.addFirst(t);
\r
180 for (ComparableTabContributor contrib : contributions) {
\r
181 if (ObjectUtils.objectEquals(contrib.getLabel(), t.selected)) {
\r
185 // selection must have been null, select first tab anyway.
\r
189 tabSelection.setSelected(contributions.iterator().next().getLabel());
\r
190 previouslySelectedTabs.addFirst(tabSelection);
\r
191 while (previouslySelectedTabs.size() > MAX_SELECTED_TABS_REMEMBERED)
\r
192 previouslySelectedTabs.removeLast();
\r
196 protected void activeTabChanged(TabChangeEvent event) {
\r
197 //System.out.println("active tab changed: " + event);
\r
198 if (tabSelection != null)
\r
199 tabSelection.setSelected(event.getNewTabLabel());
\r
200 IPropertyTab tab = event.getNewTab();
\r
201 if (tab instanceof IPropertyTab2) {
\r
202 //System.out.println("invoking tab.updatePartName for tab " + tab);
\r
203 if(lastTitleCallback != null)
\r
204 ((IPropertyTab2) tab).updatePartName(lastTitleCallback);
\r
209 * The maximum number of previous initial tab selections to store. See
\r
210 * {@link #previouslySelectedTabs} and {@link #tabSelection}.
\r
212 private static final int MAX_SELECTED_TABS_REMEMBERED = 20;
\r
215 * An LRU managed list of previously existing tab selections. The list is
\r
216 * static but only used from the SWT display thread so no synchronization is
\r
219 private static LinkedList<TabSelection> previouslySelectedTabs = new LinkedList<TabSelection>();
\r
222 * The selection of tabs this property page is currently showing. Also
\r
223 * contains the currently selected tab name, which is updated by
\r
224 * {@link #activeTabChanged(TabChangeEvent)} and
\r
225 * {@link #selectInitialTab(Collection, Collection)}.
\r
227 private TabSelection tabSelection;
\r
230 * A class for representing a set of tabs by their labels and a marking the previously selected one.
\r
232 static class TabSelection {
\r
234 private final String[] tabLabels;
\r
235 private String selected;
\r
237 public static TabSelection make(Collection<ComparableTabContributor> contribs) {
\r
238 String[] labels = new String[contribs.size()];
\r
240 for (ComparableTabContributor contrib : contribs)
\r
241 labels[i++] = contrib.getLabel();
\r
242 return new TabSelection(labels);
\r
245 public TabSelection(String... tabLabels) {
\r
246 this.tabLabels = tabLabels;
\r
249 public String getSelected() {
\r
254 * Ensures that the selected tab is among the set of different tab
\r
255 * labels. Otherwise the set request is ignored.
\r
259 public void setSelected(String label) {
\r
260 for (String l : tabLabels) {
\r
261 if (l.equals(label)) {
\r
262 this.selected = label;
\r
269 public int hashCode() {
\r
270 return Arrays.hashCode(tabLabels);
\r
274 public boolean equals(Object obj) {
\r
279 if (getClass() != obj.getClass())
\r
281 TabSelection other = (TabSelection) obj;
\r
282 return Arrays.equals(tabLabels, other.tabLabels);
\r
286 public String toString() {
\r
287 return getClass().getName() + "[" + Arrays.toString(tabLabels) + " - " + selected + "]";
\r