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.browsing.ui.platform;
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.eclipse.jface.resource.JFaceResources;
16 import org.eclipse.jface.resource.LocalResourceManager;
17 import org.eclipse.jface.resource.ResourceManager;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.IMemento;
23 import org.eclipse.ui.IPropertyListener;
24 import org.eclipse.ui.ISelectionListener;
25 import org.eclipse.ui.IViewPart;
26 import org.eclipse.ui.IViewSite;
27 import org.eclipse.ui.IWorkbenchPage;
28 import org.eclipse.ui.IWorkbenchPart;
29 import org.eclipse.ui.PartInitException;
30 import org.eclipse.ui.contexts.IContextService;
31 import org.eclipse.ui.part.IContributedContentsView;
32 import org.eclipse.ui.part.IPage;
33 import org.eclipse.ui.part.PageBook;
34 import org.eclipse.ui.part.PageBookView;
35 import org.simantics.browsing.ui.swt.IVariablesPage;
36 import org.simantics.db.management.ISessionContextProvider;
37 import org.simantics.selectionview.PropertyPage;
38 import org.simantics.ui.SimanticsUI;
39 import org.simantics.ui.workbench.IPropertyPage;
40 import org.simantics.ui.workbench.ResourceInput;
41 import org.simantics.utils.ui.BundleUtils;
44 * This is a version of the standard eclipse <code>PropertySheet</code> view a
45 * graph database access twist. It presents a property view based the active
46 * workbench part and the active part's current selection.
49 * To get a property page for your own view or editor part you can do one of the
53 * <li>Implement getAdapter for your view or editor part as follows:
56 * Object getAdapter(Class c) {
57 * if (c == ISessionContextProvider.class)
58 * // Get ISessionContextProvider from somewhere, i.e. SimanticsUI
59 * if (c == IPropertyPage.class)
60 * return new PropertyPage(getSite(), this);
64 * This method also allows customization of the actual property page control
65 * that gets created. <code>PropertyPage</code> serves as a good starting
66 * point for your own version.</li>
67 * <li>Make the workbench part implement the marker interface
68 * <code>IStandardPropertyPage</code> which will make this view do the above
69 * automatically without implementing <code>getAdapter</code>.</li>
72 * @author Tuukka Lehtonen
74 * @see IStandardPropertyPage
78 public class VariablesPageView extends PageBookView implements ISelectionListener {
80 private static final String VARIABLES_VIEW_CONTEXT = "org.simantics.browsing.ui.variables";
82 private static final String PROP_PINNED = "pinned";
84 protected static final long SELECTION_CHANGE_THRESHOLD = 500;
86 private ISessionContextProvider contextProvider;
89 * The initial selection when the property sheet opens
91 private ISelection bootstrapSelection;
94 * A flag for indicating whether or not this view will only use the
95 * bootstrap selection and and IPropertyPage source instead of listening to
96 * the input constantly.
98 private final boolean bootstrapOnly = false;
100 private IMemento memento;
102 private boolean pinSelection = false;
104 private IWorkbenchPart lastPart;
105 private ISelection lastSelection;
107 private ResourceManager resourceManager;
109 private ImageDescriptor notPinned;
110 private ImageDescriptor pinned;
113 public void createPartControl(Composite parent) {
114 super.createPartControl(parent);
116 this.resourceManager = new LocalResourceManager(JFaceResources.getResources());
117 notPinned = BundleUtils.getImageDescriptorFromPlugin("org.simantics.browsing.ui.common", "icons/table_multiple.png");
118 pinned = BundleUtils.getImageDescriptorFromPlugin("org.simantics.browsing.ui.common", "icons/table_multiple_pinned.png");
120 IContextService cs = (IContextService) getSite().getService(IContextService.class);
121 cs.activateContext(VARIABLES_VIEW_CONTEXT);
127 * @see org.eclipse.ui.part.PageBookView#getAdapter(java.lang.Class)
129 @SuppressWarnings("unchecked")
131 public <T> T getAdapter(Class<T> adapter) {
132 if (adapter == IContributedContentsView.class) {
133 // This makes it possible to duplicate a PropertyPageView with another
134 // secondary ID and make it show the same property page that was showing
135 // in the original property page view.
136 return (T) new IContributedContentsView() {
138 public IWorkbenchPart getContributingPart() {
139 return getContributingEditor();
143 if (adapter == ISessionContextProvider.class)
144 return (T) contextProvider;
145 return super.getAdapter(adapter);
149 * Returns the editor which contributed the current
152 * @return the editor which contributed the current page
153 * or <code>null</code> if no editor contributed the current page
155 private IWorkbenchPart getContributingEditor() {
156 return getCurrentContributingPart();
160 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
163 public void init(IViewSite site, IMemento memento) throws PartInitException {
164 this.memento = memento;
171 * @see org.eclipse.ui.part.PageBookView#init(org.eclipse.ui.IViewSite)
174 public void init(IViewSite site) throws PartInitException {
175 String secondaryId = site.getSecondaryId();
176 if (secondaryId != null) {
177 ResourceInput input = ResourceInput.unmarshall(secondaryId);
179 //bootstrapOnly = true;
183 //System.out.println("PPV init: " + this);
186 // This prevents the Properties view from providing a selection to other
187 // workbench parts, thus making them lose their selections which is not
189 site.setSelectionProvider(null);
191 contextProvider = SimanticsUI.getSessionContextProvider();
193 if (!bootstrapOnly) {
194 site.getPage().addSelectionListener(immediateSelectionListener);
195 site.getPage().addPostSelectionListener(this);
200 public void saveState(IMemento memento) {
201 if (this.memento != null) {
202 memento.putMemento(this.memento);
207 * Method declared on IWorkbenchPart.
210 public void dispose() {
211 //System.out.println("PPV dispose: " + this);
212 // Dispose of this before nullifying contextProvider because this
213 // dispose may just need the context provider - at least PropertyPage
217 if (lastPart != null)
218 lastPart.removePropertyListener(partPropertyListener);
220 contextProvider = null;
222 // Remove ourselves as a workbench selection listener.
223 if (!bootstrapOnly) {
224 getSite().getPage().removePostSelectionListener(this);
225 getSite().getPage().removeSelectionListener(immediateSelectionListener);
228 if (resourceManager != null) {
229 resourceManager.dispose();
230 resourceManager = null;
235 protected IPage createDefaultPage(PageBook book) {
237 MessagePage page = new MessagePage();
239 page.createControl(book);
240 page.setMessage(Messages.PropertyPageView_noPropertiesAvailable);
244 VariablesPage page = new VariablesPage(getSite(), this);
245 page.setAdapter(this);
247 page.createControl(book);
248 //System.out.println("PPV create default page: " + page);
254 protected PageRec doCreatePage(IWorkbenchPart part) {
258 // // NOTE: If the default page should be shown, this method must return null.
262 // //System.out.println("PPV try to create page for part: " + (part != null ? part.getTitle() : null));
264 // VariablesPage page = new VariablesPage(getSite(), this);
266 // // Make sure that the adaptation is provided by this class
267 // // in order not to leave the ISessionContextProvider adaptability
268 // // up to clients to implement.
269 // page.setAdapter(this);
270 // //System.out.println("PPV created page: " + page);
271 // if (page instanceof IPageBookViewPage) {
272 // initPage((IPageBookViewPage) page);
274 // page.createControl(getPageBook());
275 // //System.out.println("PPV created page control: " + page.getControl());
276 // return new PageRec(part, page);
281 protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) {
282 //System.out.println("PPV destroy page for part: " + part.getTitle());
284 IPropertyPage page = (IPropertyPage) pageRecord.page;
286 pageRecord.dispose();
290 protected IWorkbenchPart getBootstrapPart() {
291 IWorkbenchPage page = getSite().getPage();
293 bootstrapSelection = page.getSelection();
294 return page.getActivePart();
300 protected boolean isImportant(IWorkbenchPart part) {
301 // If selection is pinned, part activations are not important.
304 // Ignore self, try all others.
309 * The <code>PropertySheet</code> implementation of this
310 * <code>IPartListener</code> method first sees if the active part is an
311 * <code>IContributedContentsView</code> adapter and if so, asks it for
312 * its contributing part.
315 public void partActivated(IWorkbenchPart part) {
316 // if (bootstrapSelection == null && bootstrapOnly)
319 // Look for a declaratively-contributed adapter - including not yet
320 // loaded adapter factories.
321 // See bug 86362 [PropertiesView] Can not access AdapterFactory, when
322 // plugin is not loaded.
323 IWorkbenchPart source = getSourcePart(part);
324 //System.out.println("PPV part activated: " + part + ",src " + source + ",view " + this + " bss: " + bootstrapSelection + " pin " + pinSelection);
325 super.partActivated(source);
327 // When the view is first opened, pass the selection to the page
328 if (bootstrapSelection != null) {
329 IPage page = getCurrentPage();
330 if (page instanceof IPropertyPage) {
331 IPropertyPage ppage = (IPropertyPage) page;
332 // FIXME: should this pass source or part ??
333 ppage.selectionChanged(part, bootstrapSelection);
335 bootstrapSelection = null;
341 protected void partHidden(IWorkbenchPart part) {
342 // Make sure that pinned view is not hidden when the editor is hidden
344 super.partHidden(part);
347 long lastSelectionChangeTime = -1000;
349 ISelectionListener immediateSelectionListener = new ISelectionListener() {
351 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
352 // Check that enough time has passed since the last selection change.
353 long time = System.currentTimeMillis();
354 long delta = time - lastSelectionChangeTime;
355 lastSelectionChangeTime = time;
357 //System.out.println("time delta: " + delta + " : " + selection);
359 if (delta > SELECTION_CHANGE_THRESHOLD) {
360 VariablesPageView.this.selectionChanged(part, selection);
365 public ISelection getLastSelection() {
366 return lastSelection;
372 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
373 * org.eclipse.jface.viewers.ISelection)
376 public void selectionChanged(IWorkbenchPart part, ISelection sel) {
377 // we ignore our own selection or null selection
378 if (part == this || sel == null) {
381 // ignore workbench selections when pinned also
385 // pass the selection change to the page
386 //System.out.println("PPV selection changed: " + sel + " " + this);
387 IPage page = getCurrentPage();
388 if (page instanceof IVariablesPage) {
389 IVariablesPage ppage = (IVariablesPage) page;
390 ppage.selectionChanged(part, sel);
392 // Make sure that the part name is not updated unnecessarily because
393 // of immediate and post selection listeners.
394 boolean sameSelection = false;
395 if (part == lastPart) {
396 if (sel != null && sel.equals(lastSelection))
397 sameSelection = true;
400 if (lastPart != null) {
401 lastPart.removePropertyListener(partPropertyListener);
405 if (lastPart != null) {
406 lastPart.addPropertyListener(partPropertyListener);
409 if (!sameSelection) {
410 final Display d = getSite().getShell().getDisplay();
411 ppage.updatePartName(sel, parameter -> {
413 d.asyncExec(() -> doSetPartName(parameter));
419 void doSetPartName(String partName) {
420 // Is the page view disposed ??
421 if (contextProvider == null)
423 if (partName == null) {
425 partName = "Selection";
427 setPartName(partName);
430 public boolean isWorkbenchSelectionPinned() {
434 public void pinWorkbenchSelection(boolean pin) {
435 if (pin == pinSelection)
439 setPartProperty(PROP_PINNED, Boolean.toString(pin));
442 setTitleImage(resourceManager.createImage(pinned));
444 setTitleImage(resourceManager.createImage(notPinned));
446 updateContentDescription(pin, lastPart);
447 // Since lastPart is another PropertyView, we do not want to listen it's changes (At least current implementation is done so)
448 if (lastPart != null) {
449 lastPart.removePropertyListener(partPropertyListener);
455 IWorkbenchPart getSourcePart(IWorkbenchPart part) {
456 IContributedContentsView view = (IContributedContentsView) part.getAdapter(IContributedContentsView.class);
458 IWorkbenchPart source = view.getContributingPart();
465 private void updateContentDescription(boolean selectionPinned, IWorkbenchPart sourcePart) {
466 if (selectionPinned) {
467 if (sourcePart == null) {
468 setContentDescription("No selection");
470 sourcePart = getSourcePart(sourcePart);
472 StringBuilder desc = new StringBuilder("Selection from ");
473 if (sourcePart instanceof IEditorPart)
474 desc.append("editor ");
475 if (sourcePart instanceof IViewPart)
476 desc.append("view ");
478 desc.append(sourcePart.getTitle());
481 setContentDescription(desc.toString());
484 setContentDescription("");
488 IPropertyListener partPropertyListener = new IPropertyListener() {
490 public void propertyChanged(Object source, int propId) {
491 if (propId == IWorkbenchPart.PROP_TITLE) {
492 updateContentDescription(pinSelection, lastPart);