]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/StandardPropertyPage.java
DefaultPasteHandler causes NPE if copied data is missing
[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     @Override
96     public void dispose() {
97         super.dispose();
98         tabs = null;
99     }
100
101     @SuppressWarnings("unchecked")
102     @Override
103     protected void createPageControls(Composite parent) {
104         SelectionProcessor<?, ?> sp = getSelectionProcessor();
105         @SuppressWarnings("rawtypes")
106         Collection sps = null;
107         if (sp != null)
108             sps = Collections.singleton(sp);
109
110         tabs = new StandardProperties(sourceSite, getSite(), parent, SWT.NONE, getContexts(), sps) {
111             @Override
112             protected void activeTabChanged(TabChangeEvent event) {
113                 StandardPropertyPage.this.activeTabChanged(event);
114             }
115             @Override
116             protected ComparableTabContributor selectInitialTab(Collection<Object> selectionContents, Collection<ComparableTabContributor> contributions) {
117                 return StandardPropertyPage.this.selectInitialTab(selectionContents, contributions);
118             }
119             @Override
120             protected int getTabFolderStyle() {
121                 return StandardPropertyPage.this.getTabFolderStyle();
122             }
123         };
124         tab = tabs;
125         tab.createControl(tabs, getSessionContext());
126         getSite().setSelectionProvider(tabs.getSelectionProvider());
127
128         fillToolBar(getSite().getActionBars().getToolBarManager());
129         fillDropDownMenu(getSite().getActionBars().getMenuManager());
130     }
131
132     @Override
133     public void updatePartName(ISelection forSelection, Consumer<String> updateCallback) {
134         
135         if(!visible) {
136                 updateCallback.accept("Selection");
137                 return;
138         }
139         
140         //this.lastSelection = forSelection;
141         this.lastTitleCallback = updateCallback;
142         IPropertyTab tab = tabs.getActiveTab();
143         //System.out.println("updatePartName(" + forSelection + ", " + updateCallback + "): " + tab);
144         if (tab instanceof IPropertyTab2) {
145             //System.out.println("invoking tab.updatePartName for tab " + tab);
146                 // TabbedPropertyTable implementation reacts to events with several subsequent runnables.
147                 // This causes the part name update to be run before the input is set. Hence we have to set the input here.
148                 // TLe: this is broken since there is no guarantee that forSelection
149                 // is the proper input for this property tab whose input may well be
150                 // a transformed version of the original workbench selection.
151                 //((IPropertyTab2) tab).setInput(getSessionContext(), forSelection, false);
152             ((IPropertyTab2) tab).updatePartName(updateCallback);
153         } else {
154             //System.out.println("using default updatePartName for tab " + tab);
155             super.updatePartName(forSelection, updateCallback);
156         }
157     }
158
159     /**
160      * @return
161      */
162     protected int getTabFolderStyle() {
163         return SWT.BOTTOM | SWT.FLAT;
164     }
165
166     /**
167      * A simple tab label based implementation for restoring the last selected
168      * tab as the initial tab selection when the property page contents are
169      * reset. It statically stores the last selected tab names for
170      * {@value #MAX_SELECTED_TABS_REMEMBERED} last different ordered tab name
171      * combinations.
172      * 
173      * @param selectionContents
174      * @param contributions
175      * @return the previously selected tab contribution for a matching list of tabs
176      */
177     protected ComparableTabContributor selectInitialTab(Collection<Object> selectionContents, Collection<ComparableTabContributor> contributions) {
178         tabSelection = TabSelection.make(contributions);
179         for (Iterator<TabSelection> it = previouslySelectedTabs.iterator(); it.hasNext();) {
180             TabSelection t = it.next();
181             if (t.equals(tabSelection)) {
182                 tabSelection = t;
183                 // Move selection to front of list.
184                 it.remove();
185                 previouslySelectedTabs.addFirst(t);
186                 for (ComparableTabContributor contrib : contributions) {
187                     if (ObjectUtils.objectEquals(contrib.getLabel(), t.selected)) {
188                         return contrib;
189                     }
190                 }
191                 // selection must have been null, select first tab anyway. 
192                 return null;
193             }
194         }
195         tabSelection.setSelected(contributions.iterator().next().getLabel());
196         previouslySelectedTabs.addFirst(tabSelection);
197         while (previouslySelectedTabs.size() > MAX_SELECTED_TABS_REMEMBERED)
198             previouslySelectedTabs.removeLast();
199         return null;
200     }
201
202     protected void activeTabChanged(TabChangeEvent event) {
203         //System.out.println("active tab changed: " + event);
204         if (tabSelection != null)
205             tabSelection.setSelected(event.getNewTabLabel());
206         IPropertyTab tab = event.getNewTab();
207         if (tab instanceof IPropertyTab2) {
208             //System.out.println("invoking tab.updatePartName for tab " + tab);
209                 if(lastTitleCallback != null)
210                         ((IPropertyTab2) tab).updatePartName(lastTitleCallback);
211         }
212     }
213
214     /**
215      * The maximum number of previous initial tab selections to store. See
216      * {@link #previouslySelectedTabs} and {@link #tabSelection}.
217      */
218     private static final int                MAX_SELECTED_TABS_REMEMBERED = 20;
219
220     /**
221      * An LRU managed list of previously existing tab selections. The list is
222      * static but only used from the SWT display thread so no synchronization is
223      * necessary.
224      */
225     private static LinkedList<TabSelection> previouslySelectedTabs       = new LinkedList<TabSelection>();
226
227     /**
228      * The selection of tabs this property page is currently showing. Also
229      * contains the currently selected tab name, which is updated by
230      * {@link #activeTabChanged(TabChangeEvent)} and
231      * {@link #selectInitialTab(Collection, Collection)}.
232      */
233     private TabSelection                    tabSelection;
234
235     /**
236      * A class for representing a set of tabs by their labels and a marking the previously selected one. 
237      */
238     static class TabSelection {
239
240         private final String[] tabLabels;
241         private String         selected;
242
243         public static TabSelection make(Collection<ComparableTabContributor> contribs) {
244             String[] labels = new String[contribs.size()];
245             int i = 0;
246             for (ComparableTabContributor contrib : contribs)
247                 labels[i++] = contrib.getLabel();
248             return new TabSelection(labels);
249         }
250
251         public TabSelection(String... tabLabels) {
252             this.tabLabels = tabLabels;
253         }
254
255         public String getSelected() {
256             return selected;
257         }
258
259         /**
260          * Ensures that the selected tab is among the set of different tab
261          * labels. Otherwise the set request is ignored.
262          * 
263          * @param label
264          */
265         public void setSelected(String label) {
266             for (String l : tabLabels) {
267                 if (l.equals(label)) {
268                     this.selected = label;
269                     break;
270                 }
271             }
272         }
273
274         @Override
275         public int hashCode() {
276             return Arrays.hashCode(tabLabels);
277         }
278
279         @Override
280         public boolean equals(Object obj) {
281             if (this == obj)
282                 return true;
283             if (obj == null)
284                 return false;
285             if (getClass() != obj.getClass())
286                 return false;
287             TabSelection other = (TabSelection) obj;
288             return Arrays.equals(tabLabels, other.tabLabels);
289         }
290
291         @Override
292         public String toString() {
293             return getClass().getName() + "[" + Arrays.toString(tabLabels) + " - " + selected + "]";
294         }
295
296     }
297
298 }