1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.selectionview;
14 import java.util.function.Consumer;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.jface.action.IToolBarManager;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.ISelectionProvider;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.swt.widgets.Listener;
27 import org.eclipse.ui.IPartListener2;
28 import org.eclipse.ui.IWorkbenchPart;
29 import org.eclipse.ui.IWorkbenchPartReference;
30 import org.eclipse.ui.IWorkbenchPartSite;
31 import org.eclipse.ui.part.IPageSite;
32 import org.simantics.Simantics;
33 import org.simantics.browsing.ui.common.IPageBookViewPagePartInit;
34 import org.simantics.browsing.ui.common.views.IFilterAreaProvider;
35 import org.simantics.browsing.ui.swt.PartNameListener;
36 import org.simantics.browsing.ui.swt.PropertyPageUtil;
37 import org.simantics.db.ReadGraph;
38 import org.simantics.db.Session;
39 import org.simantics.db.exception.DatabaseException;
40 import org.simantics.db.management.ISessionContext;
41 import org.simantics.db.management.ISessionContextChangedListener;
42 import org.simantics.db.management.ISessionContextProvider;
43 import org.simantics.db.management.SessionContextChangedEvent;
44 import org.simantics.db.request.Read;
45 import org.simantics.ui.workbench.IPropertyPage;
49 * Subclasses may extend or reimplement the following methods as required:
51 * <li><code>createPageControls</code> - to create the page's controls</li>
52 * <li><code>fillToolBar</code> - to add actions to the page's toolbar</li>
53 * <li><code>fillDropDownMenu</code> - to add actions to the page's drop-down menu</li>
54 * <li><code>getControl</code> - to retrieve the page's control</li>
55 * <li><code>setFocus</code> - implement to accept focus</li>
56 * <li><code>sourceSelectionChanged</code> - puts the incoming ISelection into use in this page</li>
57 * <li><code>sourcePartClosed</code> - cleans up the page controls after a current selection source part has been closed</li>
58 * <li><code>dispose</code> - extend to provide additional cleanup</li>
59 * <li><code>setActionBars</code> - reimplement to make contributions</li>
60 * <li><code>makeContributions</code> - this method exists to support previous versions</li>
61 * <li><code>setActionBars</code> - this method exists to support previous versions</li>
62 * <li><code>init</code> - extend to provide additional setup for the view before any controls are created</li>
63 * <li><code>sessionContextChanged</code> - reimplement to take actions when the source database session changes</li>
67 * @author Tuukka Lehtonen
69 public class PropertyPage extends AbstractPropertyPage implements IPropertyPage, IAdaptable, IPartListener2, IPageBookViewPagePartInit {
71 protected static final int MAX_SELECTION_LENGTH_TO_SHOW = PropertyPageUtil.MAX_SELECTION_LENGTH_TO_SHOW;
73 protected ISessionContext sessionContext;
75 protected IPropertyTab tab;
77 protected ISelection latestSelection = StructuredSelection.EMPTY;
79 protected IWorkbenchPart propertyPageView;
81 protected boolean visible = true;
84 * @param site the workbench part site that contains this page or
85 * <code>null</code> if there is no site, i.e. the page is within a
86 * dialog or a plain shell.
88 public PropertyPage(IWorkbenchPartSite site) {
93 public void init(IPageSite pageSite) {
98 getSite().getPage().addPartListener(this);
103 public void initPart(IWorkbenchPart part) {
104 propertyPageView = part;
108 public void dispose() {
110 if(getSite() != null)
111 getSite().getPage().removePartListener(this);
113 // Stop listening for title changes.
114 if (currentPartNameListener != null) {
115 currentPartNameListener.dispose();
116 currentPartNameListener = null;
119 ISessionContextProvider contextProvider = getSessionContextProvider();
120 if (contextProvider != null)
121 contextProvider.removeContextChangedListener(contextChangeListener);
123 //System.out.println("dispose " + this);
124 // Must invoke dispose before nullifying table since super will dispose the widget.
127 sessionContext = null;
130 protected ISessionContextProvider getSessionContextProvider() {
131 return Simantics.getSessionContextProvider();
134 protected ISessionContext getSessionContext() {
135 return sessionContext;
138 protected Session getSession() {
139 if (sessionContext == null)
140 throw new IllegalStateException("null session context");
141 return sessionContext.getSession();
145 public final void createControl(Composite parent) {
146 createPageControls(parent);
148 // Attach to current session context provider to keep the UI intact even
149 // when the current UI session changes.
150 ISessionContextProvider contextProvider = getSessionContextProvider();
151 contextProvider.addContextChangedListener(contextChangeListener);
152 setSessionContext(contextProvider.getSessionContext());
156 * Override to customize the UI component created on this page.
160 protected void createPageControls(Composite parent) {
161 PropertyTable pt = new PropertyTable(sourceSite, parent, SWT.NONE);
163 tab.createControl(pt, getSessionContext());
164 tab.getControl().addListener(SWT.Dispose, new Listener() {
166 public void handleEvent(Event event) {
167 PropertyPage.this.dispose();
170 ISelectionProvider pv = pt.getSelectionProvider();
171 getSite().setSelectionProvider(pv);
173 fillToolBar(getSite().getActionBars().getToolBarManager());
174 fillDropDownMenu(getSite().getActionBars().getMenuManager());
177 protected void fillToolBar(IToolBarManager manager) {
180 protected void fillDropDownMenu(IMenuManager manager) {
186 protected final void setSessionContext(ISessionContext newContext) {
187 // ISessionContext oldContext = this.sessionContext;
188 this.sessionContext = newContext;
189 // System.out.println("PropertyPage.setSessionContext: " + oldContext + " -> " + newContext);
192 protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {
194 public void sessionContextChanged(SessionContextChangedEvent event) {
195 setSessionContext(event.getNewValue());
200 public Control getControl() {
201 return (tab != null) ? tab.getControl() : null;
205 public ISelection getSelection() {
206 if (tab != null && !tab.isDisposed()) {
207 ISelectionProvider provider = tab.getSelectionProvider();
208 if (provider != null)
209 return provider.getSelection();
215 * Sets focus to a part in the page.
216 * @see org.eclipse.ui.part.Page#setFocus()
219 public void setFocus() {
220 if (tab != null && !tab.isDisposed()) {
226 protected void sourcePartClosed(IWorkbenchPart part) {
227 if (tab != null && !tab.isDisposed()) {
228 tab.setInput(getSessionContext(), StructuredSelection.EMPTY, false);
233 protected void sourceSelectionChanged(ISelection selection) {
234 if (tab != null && !tab.isDisposed()) {
235 latestSelection = selection;
237 tab.setInput(getSessionContext(), selection, false);
241 protected void refresh() {
242 if (tab != null && !tab.isDisposed()) {
243 tab.setInput(getSessionContext(), latestSelection, true);
247 protected PartNameListener currentPartNameListener = null;
250 public void updatePartName(final ISelection forSelection, Consumer<String> updateCallback) {
253 updateCallback.accept("Selection");
257 PartNameListener oldListener = currentPartNameListener;
258 PartNameListener newListener = new PartNameListener(updateCallback);
259 currentPartNameListener = newListener;
260 if (oldListener != null)
261 oldListener.dispose();
263 if (sessionContext != null) {
264 sessionContext.getSession().asyncRequest(new Read<String>() {
266 public String perform(ReadGraph graph) throws DatabaseException {
267 return computeTitle(graph, forSelection);
273 protected String computeTitle(ReadGraph graph, ISelection forSelection) throws DatabaseException {
274 return PropertyPageUtil.computeTitle(graph, forSelection);
277 @SuppressWarnings("unchecked")
279 public <T> T getAdapter(Class<T> adapter) {
280 if (adapter == IFilterAreaProvider.class)
286 public void partActivated(IWorkbenchPartReference partRef) {
290 public void partBroughtToTop(IWorkbenchPartReference partRef) {
294 public void partClosed(IWorkbenchPartReference partRef) {
298 public void partDeactivated(IWorkbenchPartReference partRef) {
302 public void partOpened(IWorkbenchPartReference partRef) {
306 public void partInputChanged(IWorkbenchPartReference partRef) {
310 public void partHidden(IWorkbenchPartReference partRef) {
311 IWorkbenchPart part = partRef.getPart(false);
312 if (propertyPageView.equals(part)) {
318 public void partVisible(IWorkbenchPartReference partRef) {
319 IWorkbenchPart part = partRef.getPart(false);
320 if (propertyPageView.equals(part)) {
322 sourceSelectionChanged(latestSelection);
323 //System.err.println("[" + this + "]selection view was shown");