]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.selectionview/src/org/simantics/selectionview/StandardPropertyPage.java b/bundles/org.simantics.selectionview/src/org/simantics/selectionview/StandardPropertyPage.java
new file mode 100644 (file)
index 0000000..b534e64
--- /dev/null
@@ -0,0 +1,292 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.selectionview;\r
+\r
+import java.util.Arrays;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Iterator;\r
+import java.util.LinkedList;\r
+import java.util.Set;\r
+import java.util.TreeSet;\r
+import java.util.function.Consumer;\r
+\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.ui.IWorkbenchPartSite;\r
+import org.simantics.utils.ObjectUtils;\r
+\r
+/**\r
+ * <p>\r
+ * See PropertyPage for more information on what can be extended or\r
+ * reimplemented from the superclasses. In addition to those, subclasses may\r
+ * extend or reimplement the following methods:\r
+ * <ul>\r
+ * <li><code>getContexts</code> - reimplement to define what browse contexts\r
+ * this property page should be initialized with. The same result can also be\r
+ * achieved by using the\r
+ * {@link StandardPropertyPage#StandardPropertyPage(IWorkbenchPartSite, Set)}\r
+ * constructor.</li>\r
+ * </ul>\r
+ * </p>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ * \r
+ * @see StandardProperties\r
+ */\r
+public class StandardPropertyPage extends PropertyPage {\r
+\r
+    private final Set<String>          contexts;\r
+    private StandardProperties         tabs;\r
+\r
+    /**\r
+     * For updating the view title when the active tab changes.\r
+     */\r
+    //private transient ISelection       lastSelection;\r
+    private transient Consumer<String> lastTitleCallback;\r
+\r
+    /**\r
+     * Initializes a new standard property page for the specified workbench part\r
+     * without loading the page configuration from any extensible contexts.\r
+     * \r
+     * @param site the workbench part site that contains this page or\r
+     *        <code>null</code> if there is no site, i.e. the page is within a\r
+     *        dialog or a plain shell.\r
+     */\r
+    public StandardPropertyPage(IWorkbenchPartSite site) {\r
+        this(site, new TreeSet<String>());\r
+    }\r
+\r
+    /**\r
+     * Initializes a new standard property page for the specified workbench part\r
+     * and specifies which contexts should be searched for\r
+     * {@link SelectionProcessor} and {@link PropertyTabContributor} bindings to\r
+     * initialize the page.\r
+     * \r
+     * @param site the workbench part site that contains this page or\r
+     *        <code>null</code> if there is no site, i.e. the page is within a\r
+     *        dialog or a plain shell.\r
+     * @param contexts the contexts to search bindings for to initialize the\r
+     *        property page\r
+     */\r
+    public StandardPropertyPage(IWorkbenchPartSite site, Set<String> contexts) {\r
+        super(site);\r
+        this.contexts = contexts;\r
+    }\r
+\r
+    protected SelectionProcessor<?, ?> getSelectionProcessor() {\r
+        return new StandardSelectionProcessor();\r
+    }\r
+\r
+    protected Set<String> getContexts() {\r
+        return contexts;\r
+    }\r
+\r
+    @SuppressWarnings("unchecked")\r
+    @Override\r
+    protected void createPageControls(Composite parent) {\r
+        SelectionProcessor<?, ?> sp = getSelectionProcessor();\r
+        @SuppressWarnings("rawtypes")\r
+        Collection sps = null;\r
+        if (sp != null)\r
+            sps = Collections.singleton(sp);\r
+\r
+        tabs = new StandardProperties(sourceSite, getSite(), parent, SWT.NONE, getContexts(), sps) {\r
+            @Override\r
+            protected void activeTabChanged(TabChangeEvent event) {\r
+                StandardPropertyPage.this.activeTabChanged(event);\r
+            }\r
+            @Override\r
+            protected ComparableTabContributor selectInitialTab(Collection<Object> selectionContents, Collection<ComparableTabContributor> contributions) {\r
+                return StandardPropertyPage.this.selectInitialTab(selectionContents, contributions);\r
+            }\r
+            @Override\r
+            protected int getTabFolderStyle() {\r
+                return StandardPropertyPage.this.getTabFolderStyle();\r
+            }\r
+        };\r
+        tab = tabs;\r
+        tab.createControl(tabs, getSessionContext());\r
+        getSite().setSelectionProvider(tabs.getSelectionProvider());\r
+\r
+        fillToolBar(getSite().getActionBars().getToolBarManager());\r
+        fillDropDownMenu(getSite().getActionBars().getMenuManager());\r
+    }\r
+\r
+    @Override\r
+    public void updatePartName(ISelection forSelection, Consumer<String> updateCallback) {\r
+       \r
+       if(!visible) {\r
+               updateCallback.accept("Selection");\r
+               return;\r
+       }\r
+       \r
+        //this.lastSelection = forSelection;\r
+        this.lastTitleCallback = updateCallback;\r
+        IPropertyTab tab = tabs.getActiveTab();\r
+        //System.out.println("updatePartName(" + forSelection + ", " + updateCallback + "): " + tab);\r
+        if (tab instanceof IPropertyTab2) {\r
+            //System.out.println("invoking tab.updatePartName for tab " + tab);\r
+               // TabbedPropertyTable implementation reacts to events with several subsequent runnables.\r
+               // This causes the part name update to be run before the input is set. Hence we have to set the input here.\r
+               // TLe: this is broken since there is no guarantee that forSelection\r
+               // is the proper input for this property tab whose input may well be\r
+               // a transformed version of the original workbench selection.\r
+               //((IPropertyTab2) tab).setInput(getSessionContext(), forSelection, false);\r
+            ((IPropertyTab2) tab).updatePartName(updateCallback);\r
+        } else {\r
+            //System.out.println("using default updatePartName for tab " + tab);\r
+            super.updatePartName(forSelection, updateCallback);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * @return\r
+     */\r
+    protected int getTabFolderStyle() {\r
+        return SWT.BOTTOM | SWT.FLAT;\r
+    }\r
+\r
+    /**\r
+     * A simple tab label based implementation for restoring the last selected\r
+     * tab as the initial tab selection when the property page contents are\r
+     * reset. It statically stores the last selected tab names for\r
+     * {@value #MAX_SELECTED_TABS_REMEMBERED} last different ordered tab name\r
+     * combinations.\r
+     * \r
+     * @param selectionContents\r
+     * @param contributions\r
+     * @return the previously selected tab contribution for a matching list of tabs\r
+     */\r
+    protected ComparableTabContributor selectInitialTab(Collection<Object> selectionContents, Collection<ComparableTabContributor> contributions) {\r
+        tabSelection = TabSelection.make(contributions);\r
+        for (Iterator<TabSelection> it = previouslySelectedTabs.iterator(); it.hasNext();) {\r
+            TabSelection t = it.next();\r
+            if (t.equals(tabSelection)) {\r
+                tabSelection = t;\r
+                // Move selection to front of list.\r
+                it.remove();\r
+                previouslySelectedTabs.addFirst(t);\r
+                for (ComparableTabContributor contrib : contributions) {\r
+                    if (ObjectUtils.objectEquals(contrib.getLabel(), t.selected)) {\r
+                        return contrib;\r
+                    }\r
+                }\r
+                // selection must have been null, select first tab anyway. \r
+                return null;\r
+            }\r
+        }\r
+        tabSelection.setSelected(contributions.iterator().next().getLabel());\r
+        previouslySelectedTabs.addFirst(tabSelection);\r
+        while (previouslySelectedTabs.size() > MAX_SELECTED_TABS_REMEMBERED)\r
+            previouslySelectedTabs.removeLast();\r
+        return null;\r
+    }\r
+\r
+    protected void activeTabChanged(TabChangeEvent event) {\r
+        //System.out.println("active tab changed: " + event);\r
+        if (tabSelection != null)\r
+            tabSelection.setSelected(event.getNewTabLabel());\r
+        IPropertyTab tab = event.getNewTab();\r
+        if (tab instanceof IPropertyTab2) {\r
+            //System.out.println("invoking tab.updatePartName for tab " + tab);\r
+               if(lastTitleCallback != null)\r
+                       ((IPropertyTab2) tab).updatePartName(lastTitleCallback);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * The maximum number of previous initial tab selections to store. See\r
+     * {@link #previouslySelectedTabs} and {@link #tabSelection}.\r
+     */\r
+    private static final int                MAX_SELECTED_TABS_REMEMBERED = 20;\r
+\r
+    /**\r
+     * An LRU managed list of previously existing tab selections. The list is\r
+     * static but only used from the SWT display thread so no synchronization is\r
+     * necessary.\r
+     */\r
+    private static LinkedList<TabSelection> previouslySelectedTabs       = new LinkedList<TabSelection>();\r
+\r
+    /**\r
+     * The selection of tabs this property page is currently showing. Also\r
+     * contains the currently selected tab name, which is updated by\r
+     * {@link #activeTabChanged(TabChangeEvent)} and\r
+     * {@link #selectInitialTab(Collection, Collection)}.\r
+     */\r
+    private TabSelection                    tabSelection;\r
+\r
+    /**\r
+     * A class for representing a set of tabs by their labels and a marking the previously selected one. \r
+     */\r
+    static class TabSelection {\r
+\r
+        private final String[] tabLabels;\r
+        private String         selected;\r
+\r
+        public static TabSelection make(Collection<ComparableTabContributor> contribs) {\r
+            String[] labels = new String[contribs.size()];\r
+            int i = 0;\r
+            for (ComparableTabContributor contrib : contribs)\r
+                labels[i++] = contrib.getLabel();\r
+            return new TabSelection(labels);\r
+        }\r
+\r
+        public TabSelection(String... tabLabels) {\r
+            this.tabLabels = tabLabels;\r
+        }\r
+\r
+        public String getSelected() {\r
+            return selected;\r
+        }\r
+\r
+        /**\r
+         * Ensures that the selected tab is among the set of different tab\r
+         * labels. Otherwise the set request is ignored.\r
+         * \r
+         * @param label\r
+         */\r
+        public void setSelected(String label) {\r
+            for (String l : tabLabels) {\r
+                if (l.equals(label)) {\r
+                    this.selected = label;\r
+                    break;\r
+                }\r
+            }\r
+        }\r
+\r
+        @Override\r
+        public int hashCode() {\r
+            return Arrays.hashCode(tabLabels);\r
+        }\r
+\r
+        @Override\r
+        public boolean equals(Object obj) {\r
+            if (this == obj)\r
+                return true;\r
+            if (obj == null)\r
+                return false;\r
+            if (getClass() != obj.getClass())\r
+                return false;\r
+            TabSelection other = (TabSelection) obj;\r
+            return Arrays.equals(tabLabels, other.tabLabels);\r
+        }\r
+\r
+        @Override\r
+        public String toString() {\r
+            return getClass().getName() + "[" + Arrays.toString(tabLabels) + " - " + selected + "]";\r
+        }\r
+\r
+    }\r
+\r
+}\r