1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 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;
15 import java.util.WeakHashMap;
16 import java.util.function.Consumer;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.resource.JFaceResources;
20 import org.eclipse.jface.resource.LocalResourceManager;
21 import org.eclipse.jface.resource.ResourceManager;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.ui.IEditorPart;
25 import org.eclipse.ui.IMemento;
26 import org.eclipse.ui.IPropertyListener;
27 import org.eclipse.ui.ISelectionListener;
28 import org.eclipse.ui.IViewPart;
29 import org.eclipse.ui.IViewSite;
30 import org.eclipse.ui.IWorkbenchPage;
31 import org.eclipse.ui.IWorkbenchPart;
32 import org.eclipse.ui.IWorkbenchPart3;
33 import org.eclipse.ui.PartInitException;
34 import org.eclipse.ui.contexts.IContextService;
35 import org.eclipse.ui.part.IContributedContentsView;
36 import org.eclipse.ui.part.IPage;
37 import org.eclipse.ui.part.IPageBookViewPage;
38 import org.eclipse.ui.part.PageBook;
39 import org.simantics.Simantics;
40 import org.simantics.db.management.ISessionContextProvider;
41 import org.simantics.selectionview.PropertyPage;
42 import org.simantics.ui.workbench.IPropertyPage;
43 import org.simantics.ui.workbench.ResourceInput;
44 import org.simantics.utils.ui.BundleUtils;
45 import org.simantics.utils.ui.SWTUtils;
48 * This is a version of the standard eclipse <code>PropertySheet</code> view a
49 * graph database access twist. It presents a property view based the active
50 * workbench part and the active part's current selection.
53 * To get a property page for your own view or editor part you can do one of the
57 * <li>Implement getAdapter for your view or editor part as follows:
60 * Object getAdapter(Class c) {
61 * if (c == IPropertyPage.class) {
62 * // Get the browse contexts to use from somewhere
63 * Set<String> browseContexts = Collections.singleton("...");
64 * return new StandardPropertyPage(getSite(), browseContexts);
66 * return super.getAdapter(c);
70 * This method also allows customization of the actual property page control
71 * that gets created. <code>PropertyPage</code> serves as a good starting point
72 * for your own version.</li>
73 * <li>Make the workbench part implement the marker interface
74 * <code>IStandardPropertyPage</code> which will make this view do the above
75 * automatically without implementing <code>getAdapter</code>.</li>
78 * @author Tuukka Lehtonen
80 * @see IStandardPropertyPage
84 public class PropertyPageView extends PageBookView implements IContributedContentsView {
86 private static final String PROPERTY_VIEW_CONTEXT = "org.simantics.modeling.ui.properties";
88 private static final String PROP_PINNED = "pinned";
90 private ISessionContextProvider contextProvider;
93 * The initial selection when the property sheet opens
95 private ISelection bootstrapSelection;
98 * A flag for indicating whether or not this view will only use the
99 * bootstrap selection and and IPropertyPage source instead of listening to
100 * the input constantly.
102 private final boolean bootstrapOnly = false;
104 private IMemento memento;
106 private boolean pinSelection = false;
108 private IWorkbenchPart lastPart;
109 private ISelection lastSelection;
110 private final Map<IWorkbenchPart, ISelection> lastSelections = new WeakHashMap<>();
112 private ResourceManager resourceManager;
114 private ImageDescriptor notPinned;
115 private ImageDescriptor pinned;
118 * The workbench post-selection listener for this view that changes the
119 * input of the pagebook page for the part which changed its selection.
121 private ISelectionListener selectionListener = this::doSelectionChanged;
124 public void createPartControl(Composite parent) {
125 super.createPartControl(parent);
127 this.resourceManager = new LocalResourceManager(JFaceResources.getResources());
128 notPinned = BundleUtils.getImageDescriptorFromPlugin("org.simantics.browsing.ui.common", "icons/table_multiple.png");
129 pinned = BundleUtils.getImageDescriptorFromPlugin("org.simantics.browsing.ui.common", "icons/table_multiple_pinned.png");
131 IContextService cs = (IContextService) getSite().getService(IContextService.class);
132 cs.activateContext(PROPERTY_VIEW_CONTEXT);
138 * @see org.eclipse.ui.part.PageBookView#getAdapter(java.lang.Class)
140 @SuppressWarnings("rawtypes")
142 public Object getAdapter(Class adapter) {
143 if (adapter == IContributedContentsView.class) {
144 // This makes it possible to duplicate a PropertyPageView with another
145 // secondary ID and make it show the same property page that was showing
146 // in the original property page view.
147 return new IContributedContentsView() {
149 public IWorkbenchPart getContributingPart() {
150 return getContributingEditor();
154 return super.getAdapter(adapter);
158 * Returns the editor which contributed the current
161 * @return the editor which contributed the current page
162 * or <code>null</code> if no editor contributed the current page
164 private IWorkbenchPart getContributingEditor() {
165 return getCurrentContributingPart();
169 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
172 public void init(IViewSite site, IMemento memento) throws PartInitException {
173 this.memento = memento;
180 * @see org.eclipse.ui.part.PageBookView#init(org.eclipse.ui.IViewSite)
183 public void init(IViewSite site) throws PartInitException {
184 String secondaryId = site.getSecondaryId();
185 if (secondaryId != null) {
186 ResourceInput input = ResourceInput.unmarshall(secondaryId);
188 //bootstrapOnly = true;
192 //System.out.println("PPV init: " + this);
195 // This prevents the Properties view from providing a selection to other
196 // workbench parts, thus making them lose their selections which is not
198 site.setSelectionProvider(null);
200 contextProvider = Simantics.getSessionContextProvider();
202 if (!bootstrapOnly) {
203 site.getPage().addPostSelectionListener(selectionListener);
208 public void saveState(IMemento memento) {
209 if (this.memento != null) {
210 memento.putMemento(this.memento);
215 * Method declared on IWorkbenchPart.
218 public void dispose() {
219 //System.out.println("PPV dispose: " + this);
220 // Dispose of this before nullifying contextProvider because this
221 // dispose may just need the context provider - at least PropertyPage
225 if (lastPart != null)
226 lastPart.removePropertyListener(partPropertyListener);
228 contextProvider = null;
230 // Remove ourselves as a workbench selection listener.
231 if (!bootstrapOnly) {
232 getSite().getPage().removePostSelectionListener(selectionListener);
235 if (resourceManager != null) {
236 resourceManager.dispose();
237 resourceManager = null;
242 protected IPage createDefaultPage(PageBook book) {
244 MessagePage page = new MessagePage();
246 page.createControl(book);
247 page.setMessage(Messages.PropertyPageView_noPropertiesAvailable);
251 PropertyPage page = new PropertyPage(getSite());
253 page.createControl(book);
254 //System.out.println("PPV create default page: " + page);
260 protected PageRec doCreatePage(IWorkbenchPart part) {
261 // NOTE: If the default page should be shown, this method must return null.
265 //System.out.println("PPV try to create page for part: " + (part != null ? part.getTitle() : null));
267 // Try to get a property page.
268 IPropertyPage page = (IPropertyPage) part.getAdapter(IPropertyPage.class);
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);
282 protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) {
283 //System.out.println("PPV destroy page for part: " + part.getTitle());
285 IPropertyPage page = (IPropertyPage) pageRecord.page;
287 pageRecord.dispose();
291 protected IWorkbenchPart getBootstrapPart() {
292 IWorkbenchPage page = getSite().getPage();
294 bootstrapSelection = page.getSelection();
295 return page.getActivePart();
300 private boolean isPropertyView(IWorkbenchPart part) {
301 boolean ignore = false;
303 if (part instanceof IWorkbenchPart3) {
304 IWorkbenchPart3 part3 = (IWorkbenchPart3) part;
305 ignore = Boolean.parseBoolean(part3.getPartProperty(PROP_PINNED));
308 // See org.simantics.modeling.ui.actions.DuplicatePinnedViewHandler
309 // ignore |= part.getSite().getId().endsWith("Pinned");
310 String thisId = getSite().getId();
311 String otherId = part.getSite().getId();
312 //System.out.println(thisId + " - " + otherId);
313 ignore |= otherId.startsWith(thisId);
315 return this == part || ignore;
319 protected boolean isImportant(IWorkbenchPart part) {
320 //System.out.println("isImportant(" + part.getSite().getId() + ")");
321 return !isWorkbenchSelectionPinned() && !isPropertyView(part);
325 * The <code>PropertySheet</code> implementation of this
326 * <code>IPartListener</code> method first sees if the active part is an
327 * <code>IContributedContentsView</code> adapter and if so, asks it for
328 * its contributing part.
331 public void partActivated(IWorkbenchPart part) {
332 // if (bootstrapSelection == null && bootstrapOnly)
335 // Look for a declaratively-contributed adapter - including not yet
336 // loaded adapter factories.
337 // See bug 86362 [PropertiesView] Can not access AdapterFactory, when
338 // plugin is not loaded.
339 IWorkbenchPart source = getSourcePart(part);
340 //System.out.println("PPV part activated: " + part + ",src " + source + ",view " + this + " bss: " + bootstrapSelection + " pin " + pinSelection);
341 super.partActivated(source);
343 // When the view is first opened, pass the selection to the page
344 if (bootstrapSelection != null) {
345 IPage page = getCurrentPage();
346 if (page instanceof IPropertyPage) {
347 IPropertyPage ppage = (IPropertyPage) page;
348 // FIXME: should this pass source or part ??
349 ppage.selectionChanged(part, bootstrapSelection);
350 updatePartName(ppage, bootstrapSelection);
352 bootstrapSelection = null;
358 public void partClosed(IWorkbenchPart part) {
359 // Make sure that pinned view is not reset even if its originating
362 super.partClosed(part);
366 protected void partHidden(IWorkbenchPart part) {
367 // Fast views are quite unusable if this code is enabled.
369 // Make sure that pinned view is not hidden when the editor is hidden
371 // super.partHidden(part);
374 public ISelection getLastSelection() {
375 return lastSelection;
378 public void selectionChanged(IWorkbenchPart part, ISelection sel) {
379 doSelectionChanged(part, sel);
385 * @return <code>true</code> if the changed selection affected the view,
386 * <code>false</code> otherwise
388 boolean doSelectionChanged(IWorkbenchPart part, ISelection sel) {
389 // we ignore our own selection or null selection
390 if (isPropertyView(part) || sel == null) {
393 // ignore workbench selections when pinned also
397 // pass the selection change to the page
398 part = getSourcePart(part);
399 IPage page = getCurrentPage();
400 //System.out.println("PPV selection changed to (" + part + ", " + sel + "): " + page);
401 if (page instanceof IPropertyPage) {
402 IPropertyPage ppage = (IPropertyPage) page;
404 // Prevent parts that do not contribute a property page from messing
405 // up the contents/title of the currently active property page.
406 PageRec pageRec = getPageRec(part);
407 if (pageRec == null || pageRec.page != page)
410 // Make sure that the part name is not updated unnecessarily because
411 // of immediate and post selection listeners.
412 ISelection lastPartSelection = lastSelections.get(part);
413 //System.out.println(" LAST PART SELECTION(" + part + "): " + lastPartSelection);
414 boolean sameSelection = lastPartSelection != null && sel.equals(lastPartSelection);
416 if (lastPart != null) {
417 lastPart.removePropertyListener(partPropertyListener);
421 lastSelections.put(part, sel);
422 if (lastPart != null) {
423 lastPart.addPropertyListener(partPropertyListener);
426 if (!sameSelection) {
427 updatePartName(ppage, sel);
428 ppage.selectionChanged(part, sel);
435 void updatePartName(IPropertyPage ppage, ISelection sel) {
436 ppage.updatePartName(sel, partNameUpdateCallback);
439 Consumer<String> partNameUpdateCallback = parameter -> {
440 // This check is not safe - there might be a burst of changes incoming
441 //if (getPartName().equals(parameter)) return;
442 //System.out.println("partNameUpdateCallback : " + parameter);
443 SWTUtils.asyncExec(getPageBook(), () -> {
444 if (!getPageBook().isDisposed()) {
445 if (getPartName().equals(parameter)) return;
446 //System.out.println("doSetParameterName : " + parameter);
447 doSetPartName(parameter);
452 void doSetPartName(String partName) {
453 // Is the page view disposed ??
454 if (contextProvider == null)
456 if (partName == null) {
458 partName = "Selection";
460 setPartName(partName);
463 public boolean isWorkbenchSelectionPinned() {
467 public void pinWorkbenchSelection(boolean pin) {
468 if (pin == pinSelection)
472 setPartProperty(PROP_PINNED, Boolean.toString(pin));
475 setTitleImage(resourceManager.createImage(pinned));
477 setTitleImage(resourceManager.createImage(notPinned));
479 updateContentDescription(pin, lastPart);
480 // Since lastPart is another PropertyView, we do not want to listen it's changes (At least current implementation is done so)
481 if (lastPart != null) {
482 lastPart.removePropertyListener(partPropertyListener);
488 IWorkbenchPart getSourcePart(IWorkbenchPart part) {
489 IContributedContentsView view = (IContributedContentsView) part.getAdapter(IContributedContentsView.class);
491 IWorkbenchPart source = view.getContributingPart();
498 private void updateContentDescription(boolean selectionPinned, IWorkbenchPart sourcePart) {
499 if (selectionPinned) {
500 if (sourcePart == null) {
501 setContentDescription("No selection");
503 sourcePart = getSourcePart(sourcePart);
505 StringBuilder desc = new StringBuilder("Selection from ");
506 if (sourcePart instanceof IEditorPart)
507 desc.append("editor ");
508 if (sourcePart instanceof IViewPart)
509 desc.append("view ");
511 desc.append(sourcePart.getTitle());
514 setContentDescription(desc.toString());
517 setContentDescription("");
521 IPropertyListener partPropertyListener = new IPropertyListener() {
523 public void propertyChanged(Object source, int propId) {
524 if (propId == IWorkbenchPart.PROP_TITLE) {
525 updateContentDescription(pinSelection, lastPart);
531 public IWorkbenchPart getContributingPart() {