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 java.util.HashSet;
17 import java.util.WeakHashMap;
18 import java.util.function.Consumer;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IExtension;
22 import org.eclipse.core.runtime.IExtensionPoint;
23 import org.eclipse.core.runtime.IExtensionRegistry;
24 import org.eclipse.core.runtime.RegistryFactory;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.resource.JFaceResources;
27 import org.eclipse.jface.resource.LocalResourceManager;
28 import org.eclipse.jface.resource.ResourceManager;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.ui.IEditorPart;
32 import org.eclipse.ui.IMemento;
33 import org.eclipse.ui.IPropertyListener;
34 import org.eclipse.ui.ISelectionListener;
35 import org.eclipse.ui.IViewPart;
36 import org.eclipse.ui.IViewSite;
37 import org.eclipse.ui.IWorkbenchPage;
38 import org.eclipse.ui.IWorkbenchPart;
39 import org.eclipse.ui.IWorkbenchPart3;
40 import org.eclipse.ui.PartInitException;
41 import org.eclipse.ui.PlatformUI;
42 import org.eclipse.ui.contexts.IContextService;
43 import org.eclipse.ui.part.IContributedContentsView;
44 import org.eclipse.ui.part.IPage;
45 import org.eclipse.ui.part.IPageBookViewPage;
46 import org.eclipse.ui.part.PageBook;
47 import org.simantics.db.management.ISessionContextProvider;
48 import org.simantics.selectionview.PropertyPage;
49 import org.simantics.ui.SimanticsUI;
50 import org.simantics.ui.workbench.IPropertyPage;
51 import org.simantics.ui.workbench.ResourceInput;
52 import org.simantics.utils.threads.SWTThread;
53 import org.simantics.utils.threads.Throttler;
54 import org.simantics.utils.ui.BundleUtils;
55 import org.simantics.utils.ui.SWTUtils;
58 * This is a version of the standard eclipse <code>PropertySheet</code> view a
59 * graph database access twist. It presents a property view based the active
60 * workbench part and the active part's current selection.
63 * To get a property page for your own view or editor part you can do one of the
67 * <li>Implement getAdapter for your view or editor part as follows:
70 * Object getAdapter(Class c) {
71 * if (c == IPropertyPage.class) {
72 * // Get the browse contexts to use from somewhere
73 * Set<String> browseContexts = Collections.singleton("...");
74 * return new StandardPropertyPage(getSite(), browseContexts);
76 * return super.getAdapter(c);
80 * This method also allows customization of the actual property page control
81 * that gets created. <code>PropertyPage</code> serves as a good starting point
82 * for your own version.</li>
83 * <li>Make the workbench part implement the marker interface
84 * <code>IStandardPropertyPage</code> which will make this view do the above
85 * automatically without implementing <code>getAdapter</code>.</li>
88 * @author Tuukka Lehtonen
90 * @see IStandardPropertyPage
94 public class PropertyPageView extends PageBookView implements ISelectionListener, IContributedContentsView {
97 * Extension point used to modify behavior of the view
99 private static final String EXT_POINT = "org.eclipse.ui.propertiesView"; //$NON-NLS-1$
101 private static final String PROPERTY_VIEW_CONTEXT = "org.simantics.modeling.ui.properties";
103 private static final String PROP_PINNED = "pinned";
105 protected static final long SELECTION_CHANGE_THRESHOLD = 500;
107 private ISessionContextProvider contextProvider;
110 * The initial selection when the property sheet opens
112 private ISelection bootstrapSelection;
115 * A flag for indicating whether or not this view will only use the
116 * bootstrap selection and and IPropertyPage source instead of listening to
117 * the input constantly.
119 private final boolean bootstrapOnly = false;
121 private IMemento memento;
123 private boolean pinSelection = false;
125 private IWorkbenchPart lastPart;
126 private ISelection lastSelection;
127 private final Map<IWorkbenchPart, ISelection> lastSelections = new WeakHashMap<IWorkbenchPart, ISelection>();
129 private ResourceManager resourceManager;
131 private ImageDescriptor notPinned;
132 private ImageDescriptor pinned;
135 * Set of workbench parts, which should not be used as a source for PropertySheet
137 private Set<String> ignoredViews;
140 public void createPartControl(Composite parent) {
141 super.createPartControl(parent);
143 this.resourceManager = new LocalResourceManager(JFaceResources.getResources());
144 notPinned = BundleUtils.getImageDescriptorFromPlugin("org.simantics.browsing.ui.common", "icons/table_multiple.png");
145 pinned = BundleUtils.getImageDescriptorFromPlugin("org.simantics.browsing.ui.common", "icons/table_multiple_pinned.png");
147 IContextService cs = (IContextService) getSite().getService(IContextService.class);
148 cs.activateContext(PROPERTY_VIEW_CONTEXT);
154 * @see org.eclipse.ui.part.PageBookView#getAdapter(java.lang.Class)
156 @SuppressWarnings("rawtypes")
158 public Object getAdapter(Class adapter) {
159 if (adapter == IContributedContentsView.class) {
160 // This makes it possible to duplicate a PropertyPageView with another
161 // secondary ID and make it show the same property page that was showing
162 // in the original property page view.
163 return new IContributedContentsView() {
165 public IWorkbenchPart getContributingPart() {
166 return getContributingEditor();
170 return super.getAdapter(adapter);
174 * Returns the editor which contributed the current
177 * @return the editor which contributed the current page
178 * or <code>null</code> if no editor contributed the current page
180 private IWorkbenchPart getContributingEditor() {
181 return getCurrentContributingPart();
185 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
188 public void init(IViewSite site, IMemento memento) throws PartInitException {
189 this.memento = memento;
196 * @see org.eclipse.ui.part.PageBookView#init(org.eclipse.ui.IViewSite)
199 public void init(IViewSite site) throws PartInitException {
200 String secondaryId = site.getSecondaryId();
201 if (secondaryId != null) {
202 ResourceInput input = ResourceInput.unmarshall(secondaryId);
204 //bootstrapOnly = true;
208 //System.out.println("PPV init: " + this);
211 // This prevents the Properties view from providing a selection to other
212 // workbench parts, thus making them lose their selections which is not
214 // site.setSelectionProvider(null);
216 contextProvider = SimanticsUI.getSessionContextProvider();
218 if (!bootstrapOnly) {
219 site.getPage().addSelectionListener(immediateSelectionListener);
220 site.getPage().addPostSelectionListener(this);
225 public void saveState(IMemento memento) {
226 if (this.memento != null) {
227 memento.putMemento(this.memento);
232 * Method declared on IWorkbenchPart.
235 public void dispose() {
236 //System.out.println("PPV dispose: " + this);
237 // Dispose of this before nullifying contextProvider because this
238 // dispose may just need the context provider - at least PropertyPage
242 if (lastPart != null)
243 lastPart.removePropertyListener(partPropertyListener);
245 contextProvider = null;
247 // Remove ourselves as a workbench selection listener.
248 if (!bootstrapOnly) {
249 getSite().getPage().removePostSelectionListener(this);
250 getSite().getPage().removeSelectionListener(immediateSelectionListener);
253 if (resourceManager != null) {
254 resourceManager.dispose();
255 resourceManager = null;
260 protected IPage createDefaultPage(PageBook book) {
262 MessagePage page = new MessagePage();
264 page.createControl(book);
265 page.setMessage(Messages.PropertyPageView_noPropertiesAvailable);
269 PropertyPage page = new PropertyPage(getSite());
271 page.createControl(book);
272 //System.out.println("PPV create default page: " + page);
278 protected PageRec doCreatePage(IWorkbenchPart part) {
280 // NOTE: If the default page should be shown, this method must return null.
284 //System.out.println("PPV try to create page for part: " + (part != null ? part.getTitle() : null));
286 // Try to get a property page.
287 IPropertyPage page = (IPropertyPage) part.getAdapter(IPropertyPage.class);
289 //System.out.println("PPV created page: " + page);
290 if (page instanceof IPageBookViewPage) {
291 initPage((IPageBookViewPage) page);
293 page.createControl(getPageBook());
294 //System.out.println("PPV created page control: " + page.getControl());
295 return new PageRec(part, page);
301 protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) {
302 //System.out.println("PPV destroy page for part: " + part.getTitle());
304 IPropertyPage page = (IPropertyPage) pageRecord.page;
306 pageRecord.dispose();
310 protected IWorkbenchPart getBootstrapPart() {
311 IWorkbenchPage page = getSite().getPage();
313 bootstrapSelection = page.getSelection();
314 return page.getActivePart();
319 private boolean isPropertyView(IWorkbenchPart part) {
320 boolean ignore = false;
322 if (part instanceof IWorkbenchPart3) {
323 IWorkbenchPart3 part3 = (IWorkbenchPart3) part;
324 ignore = Boolean.parseBoolean(part3.getPartProperty(PROP_PINNED));
327 // See org.simantics.modeling.ui.actions.DuplicatePinnedViewHandler
328 // ignore |= part.getSite().getId().endsWith("Pinned");
329 String thisId = getSite().getId();
330 String otherId = part.getSite().getId();
331 //System.out.println(thisId + " - " + otherId);
332 ignore |= otherId.startsWith(thisId);
334 return this == part || ignore;
337 private Set<String> getIgnoredViews() {
338 if (ignoredViews == null) {
339 ignoredViews = new HashSet<String>();
340 IExtensionRegistry registry = RegistryFactory.getRegistry();
341 IExtensionPoint ep = registry.getExtensionPoint(EXT_POINT);
343 IExtension[] extensions = ep.getExtensions();
344 for (int i = 0; i < extensions.length; i++) {
345 IConfigurationElement[] elements = extensions[i].getConfigurationElements();
346 for (int j = 0; j < elements.length; j++) {
347 if ("excludeSources".equalsIgnoreCase(elements[j].getName())) { //$NON-NLS-1$
348 String id = elements[j].getAttribute("id"); //$NON-NLS-1$
350 ignoredViews.add(id);
359 private boolean isViewIgnored(String partID) {
360 return getIgnoredViews().contains(partID);
364 protected boolean isImportant(IWorkbenchPart part) {
365 String partID = part.getSite().getId();
366 //System.out.println("isImportant(" + partID + ")");
367 return !isWorkbenchSelectionPinned() && !isPropertyView(part) && !isViewIgnored(partID);
371 * The <code>PropertySheet</code> implementation of this
372 * <code>IPartListener</code> method first sees if the active part is an
373 * <code>IContributedContentsView</code> adapter and if so, asks it for
374 * its contributing part.
377 public void partActivated(IWorkbenchPart part) {
378 // if (bootstrapSelection == null && bootstrapOnly)
381 // Look for a declaratively-contributed adapter - including not yet
382 // loaded adapter factories.
383 // See bug 86362 [PropertiesView] Can not access AdapterFactory, when
384 // plugin is not loaded.
385 IWorkbenchPart source = getSourcePart(part);
386 //System.out.println("PPV part activated: " + part + ",src " + source + ",view " + this + " bss: " + bootstrapSelection + " pin " + pinSelection);
387 super.partActivated(source);
389 // When the view is first opened, pass the selection to the page
390 if (bootstrapSelection != null) {
391 IPage page = getCurrentPage();
392 if (page instanceof IPropertyPage) {
393 IPropertyPage ppage = (IPropertyPage) page;
394 // FIXME: should this pass source or part ??
395 ppage.selectionChanged(part, bootstrapSelection);
396 updatePartName(ppage, bootstrapSelection);
398 bootstrapSelection = null;
404 public void partClosed(IWorkbenchPart part) {
405 // Make sure that pinned view is not reset even if its originating
408 super.partClosed(part);
412 protected void partHidden(IWorkbenchPart part) {
413 // Fast views are quite unusable if this code is enabled.
415 // Make sure that pinned view is not hidden when the editor is hidden
417 // super.partHidden(part);
420 ISelectionListener immediateSelectionListener = new ISelectionListener() {
422 private Throttler throttler = new Throttler(SWTThread.getThreadAccess(PlatformUI.getWorkbench().getDisplay()), 500, 3);
425 public void selectionChanged(final IWorkbenchPart part, final ISelection selection) {
427 // Do not process selections from self
428 if(PropertyPageView.this == part) return;
430 throttler.schedule(new Runnable() {
434 PropertyPageView.this.doSelectionChanged(part, selection);
442 public ISelection getLastSelection() {
443 return lastSelection;
449 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
450 * org.eclipse.jface.viewers.ISelection)
453 public void selectionChanged(IWorkbenchPart part, ISelection sel) {
454 doSelectionChanged(part, sel);
460 * @return <code>true</code> if the changed selection affected the view,
461 * <code>false</code> otherwise
463 boolean doSelectionChanged(IWorkbenchPart part, ISelection sel) {
465 // we ignore our own selection or null selection
466 if (isPropertyView(part) || sel == null) {
469 // ignore workbench selections when pinned also
473 // pass the selection change to the page
474 part = getSourcePart(part);
475 IPage page = getCurrentPage();
476 //System.out.println("PPV selection changed to (" + part + ", " + sel + "): " + page);
477 if (page instanceof IPropertyPage) {
478 IPropertyPage ppage = (IPropertyPage) page;
480 // Prevent parts that do not contribute a property page from messing
481 // up the contents/title of the currently active property page.
482 PageRec pageRec = getPageRec(part);
483 if (pageRec == null || pageRec.page != page)
486 // Make sure that the part name is not updated unnecessarily because
487 // of immediate and post selection listeners.
488 ISelection lastPartSelection = lastSelections.get(part);
489 //System.out.println(" LAST PART SELECTION(" + part + "): " + lastPartSelection);
490 boolean sameSelection = lastPartSelection != null && sel.equals(lastPartSelection);
492 if (lastPart != null) {
493 lastPart.removePropertyListener(partPropertyListener);
497 lastSelections.put(part, sel);
498 if (lastPart != null) {
499 lastPart.addPropertyListener(partPropertyListener);
502 updatePartName(ppage, sel);
503 if (!sameSelection) {
504 ppage.selectionChanged(part, sel);
511 void updatePartName(IPropertyPage ppage, ISelection sel) {
512 ppage.updatePartName(sel, partNameUpdateCallback);
515 Consumer<String> partNameUpdateCallback = parameter -> {
516 // This check is not safe - there might be a burst of changes incoming
517 //if (getPartName().equals(parameter)) return;
518 //System.out.println("partNameUpdateCallback : " + parameter);
519 SWTUtils.asyncExec(getPageBook(), new Runnable() {
522 if (!getPageBook().isDisposed()) {
523 if (getPartName().equals(parameter)) return;
524 //System.out.println("doSetParameterName : " + parameter);
525 doSetPartName(parameter);
531 void doSetPartName(String partName) {
532 // Is the page view disposed ??
533 if (contextProvider == null)
535 if (partName == null) {
537 partName = "Selection";
539 setPartName(partName);
542 public boolean isWorkbenchSelectionPinned() {
546 public void pinWorkbenchSelection(boolean pin) {
547 if (pin == pinSelection)
551 setPartProperty(PROP_PINNED, Boolean.toString(pin));
554 setTitleImage(resourceManager.createImage(pinned));
556 setTitleImage(resourceManager.createImage(notPinned));
558 updateContentDescription(pin, lastPart);
559 // Since lastPart is another PropertyView, we do not want to listen it's changes (At least current implementation is done so)
560 if (lastPart != null) {
561 lastPart.removePropertyListener(partPropertyListener);
567 IWorkbenchPart getSourcePart(IWorkbenchPart part) {
568 IContributedContentsView view = (IContributedContentsView) part.getAdapter(IContributedContentsView.class);
570 IWorkbenchPart source = view.getContributingPart();
577 private void updateContentDescription(boolean selectionPinned, IWorkbenchPart sourcePart) {
578 if (selectionPinned) {
579 if (sourcePart == null) {
580 setContentDescription("No selection");
582 sourcePart = getSourcePart(sourcePart);
584 StringBuilder desc = new StringBuilder("Selection from ");
585 if (sourcePart instanceof IEditorPart)
586 desc.append("editor ");
587 if (sourcePart instanceof IViewPart)
588 desc.append("view ");
590 desc.append(sourcePart.getTitle());
593 setContentDescription(desc.toString());
596 setContentDescription("");
600 IPropertyListener partPropertyListener = new IPropertyListener() {
602 public void propertyChanged(Object source, int propId) {
603 if (propId == IWorkbenchPart.PROP_TITLE) {
604 updateContentDescription(pinSelection, lastPart);
610 public IWorkbenchPart getContributingPart() {