]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/StandardPropertyPage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / StandardPropertyPage.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.selectionview;\r
13 \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
22 \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
28 \r
29 /**\r
30  * <p>\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
34  * <ul>\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
39  * constructor.</li>\r
40  * </ul>\r
41  * </p>\r
42  * \r
43  * @author Tuukka Lehtonen\r
44  * \r
45  * @see StandardProperties\r
46  */\r
47 public class StandardPropertyPage extends PropertyPage {\r
48 \r
49     private final Set<String>          contexts;\r
50     private StandardProperties         tabs;\r
51 \r
52     /**\r
53      * For updating the view title when the active tab changes.\r
54      */\r
55     //private transient ISelection       lastSelection;\r
56     private transient Consumer<String> lastTitleCallback;\r
57 \r
58     /**\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
61      * \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
65      */\r
66     public StandardPropertyPage(IWorkbenchPartSite site) {\r
67         this(site, new TreeSet<String>());\r
68     }\r
69 \r
70     /**\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
75      * \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
80      *        property page\r
81      */\r
82     public StandardPropertyPage(IWorkbenchPartSite site, Set<String> contexts) {\r
83         super(site);\r
84         this.contexts = contexts;\r
85     }\r
86 \r
87     protected SelectionProcessor<?, ?> getSelectionProcessor() {\r
88         return new StandardSelectionProcessor();\r
89     }\r
90 \r
91     protected Set<String> getContexts() {\r
92         return contexts;\r
93     }\r
94 \r
95     @SuppressWarnings("unchecked")\r
96     @Override\r
97     protected void createPageControls(Composite parent) {\r
98         SelectionProcessor<?, ?> sp = getSelectionProcessor();\r
99         @SuppressWarnings("rawtypes")\r
100         Collection sps = null;\r
101         if (sp != null)\r
102             sps = Collections.singleton(sp);\r
103 \r
104         tabs = new StandardProperties(sourceSite, getSite(), parent, SWT.NONE, getContexts(), sps) {\r
105             @Override\r
106             protected void activeTabChanged(TabChangeEvent event) {\r
107                 StandardPropertyPage.this.activeTabChanged(event);\r
108             }\r
109             @Override\r
110             protected ComparableTabContributor selectInitialTab(Collection<Object> selectionContents, Collection<ComparableTabContributor> contributions) {\r
111                 return StandardPropertyPage.this.selectInitialTab(selectionContents, contributions);\r
112             }\r
113             @Override\r
114             protected int getTabFolderStyle() {\r
115                 return StandardPropertyPage.this.getTabFolderStyle();\r
116             }\r
117         };\r
118         tab = tabs;\r
119         tab.createControl(tabs, getSessionContext());\r
120         getSite().setSelectionProvider(tabs.getSelectionProvider());\r
121 \r
122         fillToolBar(getSite().getActionBars().getToolBarManager());\r
123         fillDropDownMenu(getSite().getActionBars().getMenuManager());\r
124     }\r
125 \r
126     @Override\r
127     public void updatePartName(ISelection forSelection, Consumer<String> updateCallback) {\r
128         \r
129         if(!visible) {\r
130                 updateCallback.accept("Selection");\r
131                 return;\r
132         }\r
133         \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
147         } else {\r
148             //System.out.println("using default updatePartName for tab " + tab);\r
149             super.updatePartName(forSelection, updateCallback);\r
150         }\r
151     }\r
152 \r
153     /**\r
154      * @return\r
155      */\r
156     protected int getTabFolderStyle() {\r
157         return SWT.BOTTOM | SWT.FLAT;\r
158     }\r
159 \r
160     /**\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
165      * combinations.\r
166      * \r
167      * @param selectionContents\r
168      * @param contributions\r
169      * @return the previously selected tab contribution for a matching list of tabs\r
170      */\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
176                 tabSelection = t;\r
177                 // Move selection to front of list.\r
178                 it.remove();\r
179                 previouslySelectedTabs.addFirst(t);\r
180                 for (ComparableTabContributor contrib : contributions) {\r
181                     if (ObjectUtils.objectEquals(contrib.getLabel(), t.selected)) {\r
182                         return contrib;\r
183                     }\r
184                 }\r
185                 // selection must have been null, select first tab anyway. \r
186                 return null;\r
187             }\r
188         }\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
193         return null;\r
194     }\r
195 \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
205         }\r
206     }\r
207 \r
208     /**\r
209      * The maximum number of previous initial tab selections to store. See\r
210      * {@link #previouslySelectedTabs} and {@link #tabSelection}.\r
211      */\r
212     private static final int                MAX_SELECTED_TABS_REMEMBERED = 20;\r
213 \r
214     /**\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
217      * necessary.\r
218      */\r
219     private static LinkedList<TabSelection> previouslySelectedTabs       = new LinkedList<TabSelection>();\r
220 \r
221     /**\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
226      */\r
227     private TabSelection                    tabSelection;\r
228 \r
229     /**\r
230      * A class for representing a set of tabs by their labels and a marking the previously selected one. \r
231      */\r
232     static class TabSelection {\r
233 \r
234         private final String[] tabLabels;\r
235         private String         selected;\r
236 \r
237         public static TabSelection make(Collection<ComparableTabContributor> contribs) {\r
238             String[] labels = new String[contribs.size()];\r
239             int i = 0;\r
240             for (ComparableTabContributor contrib : contribs)\r
241                 labels[i++] = contrib.getLabel();\r
242             return new TabSelection(labels);\r
243         }\r
244 \r
245         public TabSelection(String... tabLabels) {\r
246             this.tabLabels = tabLabels;\r
247         }\r
248 \r
249         public String getSelected() {\r
250             return selected;\r
251         }\r
252 \r
253         /**\r
254          * Ensures that the selected tab is among the set of different tab\r
255          * labels. Otherwise the set request is ignored.\r
256          * \r
257          * @param label\r
258          */\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
263                     break;\r
264                 }\r
265             }\r
266         }\r
267 \r
268         @Override\r
269         public int hashCode() {\r
270             return Arrays.hashCode(tabLabels);\r
271         }\r
272 \r
273         @Override\r
274         public boolean equals(Object obj) {\r
275             if (this == obj)\r
276                 return true;\r
277             if (obj == null)\r
278                 return false;\r
279             if (getClass() != obj.getClass())\r
280                 return false;\r
281             TabSelection other = (TabSelection) obj;\r
282             return Arrays.equals(tabLabels, other.tabLabels);\r
283         }\r
284 \r
285         @Override\r
286         public String toString() {\r
287             return getClass().getName() + "[" + Arrays.toString(tabLabels) + " - " + selected + "]";\r
288         }\r
289 \r
290     }\r
291 \r
292 }\r