]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.selectionview/src/org/simantics/selectionview/PropertyPage.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / PropertyPage.java
index a21378eef4aa3bb055409f6bfb2331056742e410..eb4caaebaf0b014e4de0ae124377a8658620172a 100644 (file)
-/*******************************************************************************\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.function.Consumer;\r
-\r
-import org.eclipse.core.runtime.IAdaptable;\r
-import org.eclipse.jface.action.IMenuManager;\r
-import org.eclipse.jface.action.IToolBarManager;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.jface.viewers.ISelectionProvider;\r
-import org.eclipse.jface.viewers.StructuredSelection;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Event;\r
-import org.eclipse.swt.widgets.Listener;\r
-import org.eclipse.ui.IPartListener2;\r
-import org.eclipse.ui.IWorkbenchPart;\r
-import org.eclipse.ui.IWorkbenchPartReference;\r
-import org.eclipse.ui.IWorkbenchPartSite;\r
-import org.eclipse.ui.part.IPageSite;\r
-import org.simantics.Simantics;\r
-import org.simantics.browsing.ui.common.IPageBookViewPagePartInit;\r
-import org.simantics.browsing.ui.common.views.IFilterAreaProvider;\r
-import org.simantics.browsing.ui.swt.PartNameListener;\r
-import org.simantics.browsing.ui.swt.PropertyPageUtil;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.management.ISessionContext;\r
-import org.simantics.db.management.ISessionContextChangedListener;\r
-import org.simantics.db.management.ISessionContextProvider;\r
-import org.simantics.db.management.SessionContextChangedEvent;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.ui.workbench.IPropertyPage;\r
-\r
-/**\r
- * <p>\r
- * Subclasses may extend or reimplement the following methods as required:\r
- * <ul>\r
- *   <li><code>createPageControls</code> - to create the page's controls</li>\r
- *   <li><code>fillToolBar</code> - to add actions to the page's toolbar</li>\r
- *   <li><code>fillDropDownMenu</code> - to add actions to the page's drop-down menu</li>\r
- *   <li><code>getControl</code> - to retrieve the page's control</li>\r
- *   <li><code>setFocus</code> - implement to accept focus</li>\r
- *   <li><code>sourceSelectionChanged</code> - puts the incoming ISelection into use in this page</li>\r
- *   <li><code>sourcePartClosed</code> - cleans up the page controls after a current selection source part has been closed</li>\r
- *   <li><code>dispose</code> - extend to provide additional cleanup</li>\r
- *   <li><code>setActionBars</code> - reimplement to make contributions</li>\r
- *   <li><code>makeContributions</code> - this method exists to support previous versions</li>\r
- *   <li><code>setActionBars</code> - this method exists to support previous versions</li>\r
- *   <li><code>init</code> - extend to provide additional setup for the view before any controls are created</li>\r
- *   <li><code>sessionContextChanged</code> - reimplement to take actions when the source database session changes</li>\r
- * </ul>\r
- * </p>\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public class PropertyPage extends AbstractPropertyPage implements IPropertyPage, IAdaptable, IPartListener2, IPageBookViewPagePartInit {\r
-\r
-    protected static final int MAX_SELECTION_LENGTH_TO_SHOW = PropertyPageUtil.MAX_SELECTION_LENGTH_TO_SHOW;\r
-\r
-    protected ISessionContext  sessionContext;\r
-\r
-    protected IPropertyTab     tab;\r
-\r
-    protected ISelection       latestSelection = StructuredSelection.EMPTY;\r
-\r
-    protected IWorkbenchPart   propertyPageView;\r
-    \r
-    protected boolean visible = true;\r
-\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 PropertyPage(IWorkbenchPartSite site) {\r
-       super(site);\r
-    }\r
-\r
-    @Override\r
-    public void init(IPageSite pageSite) {\r
-       \r
-       super.init(pageSite);\r
-       \r
-       if(getSite() != null)\r
-               getSite().getPage().addPartListener(this);\r
-       \r
-    }\r
-    \r
-    @Override\r
-    public void initPart(IWorkbenchPart part) {\r
-       propertyPageView = part;\r
-    }\r
-    \r
-    @Override\r
-    public void dispose() {\r
-\r
-       if(getSite() != null)\r
-               getSite().getPage().removePartListener(this);\r
-       \r
-        // Stop listening for title changes.\r
-        if (currentPartNameListener != null) {\r
-            currentPartNameListener.dispose();\r
-            currentPartNameListener = null;\r
-        }\r
-\r
-        ISessionContextProvider contextProvider = getSessionContextProvider();\r
-        if (contextProvider != null)\r
-            contextProvider.removeContextChangedListener(contextChangeListener);\r
-\r
-        //System.out.println("dispose " + this);\r
-        // Must invoke dispose before nullifying table since super will dispose the widget.\r
-        super.dispose();\r
-        tab = null;\r
-        sessionContext = null;\r
-    }\r
-\r
-    protected ISessionContextProvider getSessionContextProvider() {\r
-        return Simantics.getSessionContextProvider();\r
-    }\r
-\r
-    protected ISessionContext getSessionContext() {\r
-        return sessionContext;\r
-    }\r
-\r
-    protected Session getSession() {\r
-        if (sessionContext == null)\r
-            throw new IllegalStateException("null session context");\r
-        return sessionContext.getSession();\r
-    }\r
-\r
-    @Override\r
-    public final void createControl(Composite parent) {\r
-        createPageControls(parent);\r
-\r
-        // Attach to current session context provider to keep the UI intact even\r
-        // when the current UI session changes.\r
-        ISessionContextProvider contextProvider = getSessionContextProvider();\r
-        contextProvider.addContextChangedListener(contextChangeListener);\r
-        setSessionContext(contextProvider.getSessionContext());\r
-    }\r
-\r
-    /**\r
-     * Override to customize the UI component created on this page.\r
-     * \r
-     * @param parent\r
-     */\r
-    protected void createPageControls(Composite parent) {\r
-        PropertyTable pt = new PropertyTable(sourceSite, parent, SWT.NONE);\r
-        tab = pt;\r
-        tab.createControl(pt, getSessionContext());\r
-        tab.getControl().addListener(SWT.Dispose, new Listener() {\r
-            @Override\r
-            public void handleEvent(Event event) {\r
-                PropertyPage.this.dispose();\r
-            }\r
-        });\r
-        ISelectionProvider pv = pt.getSelectionProvider();\r
-        getSite().setSelectionProvider(pv);\r
-\r
-        fillToolBar(getSite().getActionBars().getToolBarManager());\r
-        fillDropDownMenu(getSite().getActionBars().getMenuManager());\r
-    }\r
-\r
-    protected void fillToolBar(IToolBarManager manager) {\r
-    }\r
-\r
-    protected void fillDropDownMenu(IMenuManager manager) {\r
-    }\r
-\r
-    /**\r
-     * @param newContext\r
-     */\r
-    protected final void setSessionContext(ISessionContext newContext) {\r
-//        ISessionContext oldContext = this.sessionContext;\r
-        this.sessionContext = newContext;\r
-//        System.out.println("PropertyPage.setSessionContext: " + oldContext + " -> " + newContext);\r
-    }\r
-\r
-    protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {\r
-        @Override\r
-        public void sessionContextChanged(SessionContextChangedEvent event) {\r
-            setSessionContext(event.getNewValue());\r
-        }\r
-    };\r
-\r
-    @Override\r
-    public Control getControl() {\r
-        return (tab != null) ? tab.getControl() : null;\r
-    }\r
-\r
-    @Override\r
-    public ISelection getSelection() {\r
-        if (tab != null && !tab.isDisposed()) {\r
-            ISelectionProvider provider = tab.getSelectionProvider();\r
-            if (provider != null)\r
-                return provider.getSelection();\r
-        }\r
-        return null;\r
-    }\r
-\r
-    /**\r
-     * Sets focus to a part in the page.\r
-     * @see org.eclipse.ui.part.Page#setFocus()\r
-     */\r
-    @Override\r
-    public void setFocus() {\r
-        if (tab != null && !tab.isDisposed()) {\r
-            tab.requestFocus();\r
-        }\r
-    }\r
-\r
-    @Override\r
-    protected void sourcePartClosed(IWorkbenchPart part) {\r
-        if (tab != null && !tab.isDisposed()) {\r
-            tab.setInput(getSessionContext(), StructuredSelection.EMPTY, false);\r
-        }\r
-    }\r
-\r
-    @Override\r
-    protected void sourceSelectionChanged(ISelection selection) {\r
-        if (tab != null && !tab.isDisposed()) {\r
-            latestSelection = selection;\r
-            if(visible)\r
-                tab.setInput(getSessionContext(), selection, false);\r
-        }\r
-    }\r
-\r
-    protected void refresh() {\r
-        if (tab != null && !tab.isDisposed()) {\r
-            tab.setInput(getSessionContext(), latestSelection, true);\r
-        }\r
-    }\r
-\r
-    protected PartNameListener currentPartNameListener = null;\r
-\r
-    @Override\r
-    public void updatePartName(final ISelection forSelection, Consumer<String> updateCallback) {\r
-       \r
-       if(!visible) {\r
-               updateCallback.accept("Selection");\r
-               return;\r
-       }\r
-       \r
-        PartNameListener oldListener = currentPartNameListener;\r
-        PartNameListener newListener = new PartNameListener(updateCallback);\r
-        currentPartNameListener = newListener;\r
-        if (oldListener != null)\r
-            oldListener.dispose();\r
-\r
-        if (sessionContext != null) {\r
-            sessionContext.getSession().asyncRequest(new Read<String>() {\r
-                @Override\r
-                public String perform(ReadGraph graph) throws DatabaseException {\r
-                    return computeTitle(graph, forSelection);\r
-                }\r
-            }, newListener);\r
-        }\r
-    }\r
-\r
-    protected String computeTitle(ReadGraph graph, ISelection forSelection) throws DatabaseException {\r
-        return PropertyPageUtil.computeTitle(graph, forSelection);\r
-    }\r
-\r
-    @SuppressWarnings("unchecked")\r
-    @Override\r
-    public <T> T getAdapter(Class<T> adapter) {\r
-        if (adapter == IFilterAreaProvider.class)\r
-            return (T) tab;\r
-        return null;\r
-    }\r
-\r
-       @Override\r
-       public void partActivated(IWorkbenchPartReference partRef) {\r
-       }\r
-\r
-       @Override\r
-       public void partBroughtToTop(IWorkbenchPartReference partRef) {\r
-       }\r
-\r
-       @Override\r
-       public void partClosed(IWorkbenchPartReference partRef) {\r
-       }\r
-\r
-       @Override\r
-       public void partDeactivated(IWorkbenchPartReference partRef) {\r
-       }\r
-\r
-       @Override\r
-       public void partOpened(IWorkbenchPartReference partRef) {\r
-       }\r
-\r
-       @Override\r
-       public void partInputChanged(IWorkbenchPartReference partRef) {\r
-       }\r
-       \r
-    @Override\r
-    public void partHidden(IWorkbenchPartReference partRef) {\r
-        IWorkbenchPart part = partRef.getPart(false);\r
-        if (propertyPageView.equals(part)) {\r
-               visible = false;\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public void partVisible(IWorkbenchPartReference partRef) {\r
-        IWorkbenchPart part = partRef.getPart(false);\r
-        if (propertyPageView.equals(part)) {\r
-               visible = true;\r
-               sourceSelectionChanged(latestSelection);\r
-               //System.err.println("[" + this + "]selection view was shown");\r
-        }\r
-    }\r
-       \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.selectionview;
+
+import java.util.function.Consumer;
+
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.IToolBarManager;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.ui.IPartListener2;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.IWorkbenchPartReference;
+import org.eclipse.ui.IWorkbenchPartSite;
+import org.eclipse.ui.part.IPageSite;
+import org.simantics.Simantics;
+import org.simantics.browsing.ui.common.IPageBookViewPagePartInit;
+import org.simantics.browsing.ui.common.views.IFilterAreaProvider;
+import org.simantics.browsing.ui.swt.PartNameListener;
+import org.simantics.browsing.ui.swt.PropertyPageUtil;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Session;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.db.management.ISessionContextChangedListener;
+import org.simantics.db.management.ISessionContextProvider;
+import org.simantics.db.management.SessionContextChangedEvent;
+import org.simantics.db.request.Read;
+import org.simantics.ui.workbench.IPropertyPage;
+
+/**
+ * <p>
+ * Subclasses may extend or reimplement the following methods as required:
+ * <ul>
+ *   <li><code>createPageControls</code> - to create the page's controls</li>
+ *   <li><code>fillToolBar</code> - to add actions to the page's toolbar</li>
+ *   <li><code>fillDropDownMenu</code> - to add actions to the page's drop-down menu</li>
+ *   <li><code>getControl</code> - to retrieve the page's control</li>
+ *   <li><code>setFocus</code> - implement to accept focus</li>
+ *   <li><code>sourceSelectionChanged</code> - puts the incoming ISelection into use in this page</li>
+ *   <li><code>sourcePartClosed</code> - cleans up the page controls after a current selection source part has been closed</li>
+ *   <li><code>dispose</code> - extend to provide additional cleanup</li>
+ *   <li><code>setActionBars</code> - reimplement to make contributions</li>
+ *   <li><code>makeContributions</code> - this method exists to support previous versions</li>
+ *   <li><code>setActionBars</code> - this method exists to support previous versions</li>
+ *   <li><code>init</code> - extend to provide additional setup for the view before any controls are created</li>
+ *   <li><code>sessionContextChanged</code> - reimplement to take actions when the source database session changes</li>
+ * </ul>
+ * </p>
+ * 
+ * @author Tuukka Lehtonen
+ */
+public class PropertyPage extends AbstractPropertyPage implements IPropertyPage, IAdaptable, IPartListener2, IPageBookViewPagePartInit {
+
+    protected static final int MAX_SELECTION_LENGTH_TO_SHOW = PropertyPageUtil.MAX_SELECTION_LENGTH_TO_SHOW;
+
+    protected ISessionContext  sessionContext;
+
+    protected IPropertyTab     tab;
+
+    protected ISelection       latestSelection = StructuredSelection.EMPTY;
+
+    protected IWorkbenchPart   propertyPageView;
+    
+    protected boolean visible = true;
+
+    /**
+     * @param site the workbench part site that contains this page or
+     *        <code>null</code> if there is no site, i.e. the page is within a
+     *        dialog or a plain shell.
+     */
+    public PropertyPage(IWorkbenchPartSite site) {
+       super(site);
+    }
+
+    @Override
+    public void init(IPageSite pageSite) {
+       
+       super.init(pageSite);
+       
+       if(getSite() != null)
+               getSite().getPage().addPartListener(this);
+       
+    }
+    
+    @Override
+    public void initPart(IWorkbenchPart part) {
+       propertyPageView = part;
+    }
+    
+    @Override
+    public void dispose() {
+
+       if(getSite() != null)
+               getSite().getPage().removePartListener(this);
+       
+        // Stop listening for title changes.
+        if (currentPartNameListener != null) {
+            currentPartNameListener.dispose();
+            currentPartNameListener = null;
+        }
+
+        ISessionContextProvider contextProvider = getSessionContextProvider();
+        if (contextProvider != null)
+            contextProvider.removeContextChangedListener(contextChangeListener);
+
+        //System.out.println("dispose " + this);
+        // Must invoke dispose before nullifying table since super will dispose the widget.
+        super.dispose();
+        tab = null;
+        sessionContext = null;
+    }
+
+    protected ISessionContextProvider getSessionContextProvider() {
+        return Simantics.getSessionContextProvider();
+    }
+
+    protected ISessionContext getSessionContext() {
+        return sessionContext;
+    }
+
+    protected Session getSession() {
+        if (sessionContext == null)
+            throw new IllegalStateException("null session context");
+        return sessionContext.getSession();
+    }
+
+    @Override
+    public final void createControl(Composite parent) {
+        createPageControls(parent);
+
+        // Attach to current session context provider to keep the UI intact even
+        // when the current UI session changes.
+        ISessionContextProvider contextProvider = getSessionContextProvider();
+        contextProvider.addContextChangedListener(contextChangeListener);
+        setSessionContext(contextProvider.getSessionContext());
+    }
+
+    /**
+     * Override to customize the UI component created on this page.
+     * 
+     * @param parent
+     */
+    protected void createPageControls(Composite parent) {
+        PropertyTable pt = new PropertyTable(sourceSite, parent, SWT.NONE);
+        tab = pt;
+        tab.createControl(pt, getSessionContext());
+        tab.getControl().addListener(SWT.Dispose, new Listener() {
+            @Override
+            public void handleEvent(Event event) {
+                PropertyPage.this.dispose();
+            }
+        });
+        ISelectionProvider pv = pt.getSelectionProvider();
+        getSite().setSelectionProvider(pv);
+
+        fillToolBar(getSite().getActionBars().getToolBarManager());
+        fillDropDownMenu(getSite().getActionBars().getMenuManager());
+    }
+
+    protected void fillToolBar(IToolBarManager manager) {
+    }
+
+    protected void fillDropDownMenu(IMenuManager manager) {
+    }
+
+    /**
+     * @param newContext
+     */
+    protected final void setSessionContext(ISessionContext newContext) {
+//        ISessionContext oldContext = this.sessionContext;
+        this.sessionContext = newContext;
+//        System.out.println("PropertyPage.setSessionContext: " + oldContext + " -> " + newContext);
+    }
+
+    protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {
+        @Override
+        public void sessionContextChanged(SessionContextChangedEvent event) {
+            setSessionContext(event.getNewValue());
+        }
+    };
+
+    @Override
+    public Control getControl() {
+        return (tab != null) ? tab.getControl() : null;
+    }
+
+    @Override
+    public ISelection getSelection() {
+        if (tab != null && !tab.isDisposed()) {
+            ISelectionProvider provider = tab.getSelectionProvider();
+            if (provider != null)
+                return provider.getSelection();
+        }
+        return null;
+    }
+
+    /**
+     * Sets focus to a part in the page.
+     * @see org.eclipse.ui.part.Page#setFocus()
+     */
+    @Override
+    public void setFocus() {
+        if (tab != null && !tab.isDisposed()) {
+            tab.requestFocus();
+        }
+    }
+
+    @Override
+    protected void sourcePartClosed(IWorkbenchPart part) {
+        if (tab != null && !tab.isDisposed()) {
+            tab.setInput(getSessionContext(), StructuredSelection.EMPTY, false);
+        }
+    }
+
+    @Override
+    protected void sourceSelectionChanged(ISelection selection) {
+        if (tab != null && !tab.isDisposed()) {
+            latestSelection = selection;
+            if(visible)
+                tab.setInput(getSessionContext(), selection, false);
+        }
+    }
+
+    protected void refresh() {
+        if (tab != null && !tab.isDisposed()) {
+            tab.setInput(getSessionContext(), latestSelection, true);
+        }
+    }
+
+    protected PartNameListener currentPartNameListener = null;
+
+    @Override
+    public void updatePartName(final ISelection forSelection, Consumer<String> updateCallback) {
+       
+       if(!visible) {
+               updateCallback.accept("Selection");
+               return;
+       }
+       
+        PartNameListener oldListener = currentPartNameListener;
+        PartNameListener newListener = new PartNameListener(updateCallback);
+        currentPartNameListener = newListener;
+        if (oldListener != null)
+            oldListener.dispose();
+
+        if (sessionContext != null) {
+            sessionContext.getSession().asyncRequest(new Read<String>() {
+                @Override
+                public String perform(ReadGraph graph) throws DatabaseException {
+                    return computeTitle(graph, forSelection);
+                }
+            }, newListener);
+        }
+    }
+
+    protected String computeTitle(ReadGraph graph, ISelection forSelection) throws DatabaseException {
+        return PropertyPageUtil.computeTitle(graph, forSelection);
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public <T> T getAdapter(Class<T> adapter) {
+        if (adapter == IFilterAreaProvider.class)
+            return (T) tab;
+        return null;
+    }
+
+       @Override
+       public void partActivated(IWorkbenchPartReference partRef) {
+       }
+
+       @Override
+       public void partBroughtToTop(IWorkbenchPartReference partRef) {
+       }
+
+       @Override
+       public void partClosed(IWorkbenchPartReference partRef) {
+       }
+
+       @Override
+       public void partDeactivated(IWorkbenchPartReference partRef) {
+       }
+
+       @Override
+       public void partOpened(IWorkbenchPartReference partRef) {
+       }
+
+       @Override
+       public void partInputChanged(IWorkbenchPartReference partRef) {
+       }
+       
+    @Override
+    public void partHidden(IWorkbenchPartReference partRef) {
+        IWorkbenchPart part = partRef.getPart(false);
+        if (propertyPageView.equals(part)) {
+               visible = false;
+        }
+    }
+
+    @Override
+    public void partVisible(IWorkbenchPartReference partRef) {
+        IWorkbenchPart part = partRef.getPart(false);
+        if (propertyPageView.equals(part)) {
+               visible = true;
+               sourceSelectionChanged(latestSelection);
+               //System.err.println("[" + this + "]selection view was shown");
+        }
+    }
+       
+}