Commit 41c4a154 authored by Stephan Eberle's avatar Stephan Eberle
Browse files

RESOLVED - bug 526269: Properties page cannot be opened from Example

parent 63646c8e
Loading
Loading
Loading
Loading
+45 −23
Original line number Diff line number Diff line
/**
 * <copyright>
 *
 * Copyright (c) 2008-2012 itemis, See4sys and others.
 * Copyright (c) 2008-2017 itemis, See4sys and others.
 * 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
@@ -11,6 +11,7 @@
 *     See4sys - Initial API and implementation
 *     itemis - [393477] Provider hook for unwrapping elements before letting BasicTabbedPropertySheetTitleProvider retrieve text or image for them
 *     itemis - [393479] Enable BasicTabbedPropertySheetTitleProvider to retrieve same AdapterFactory as underlying IWorkbenchPart is using
 *     itemis - [526269] Properties page cannot be opened from Example Form Editor
 *
 * </copyright>
 */
@@ -23,6 +24,7 @@ import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.ui.provider.TransactionalAdapterFactoryLabelProvider;
import org.eclipse.jface.viewers.ILabelProvider;
@@ -30,9 +32,9 @@ import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.osgi.util.NLS;
import org.eclipse.sphinx.emf.ui.internal.messages.Messages;
import org.eclipse.sphinx.platform.ui.util.ExtendedPlatformUI;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.navigator.IDescriptionProvider;
import org.eclipse.ui.navigator.INavigatorContentService;

@@ -113,17 +115,18 @@ public class BasicTabbedPropertySheetTitleProvider extends LabelProvider {
	}

	protected void initProviders() {
		IWorkbenchPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
		INavigatorContentService contentService = (INavigatorContentService) part.getAdapter(INavigatorContentService.class);
		IWorkbenchPart part = ExtendedPlatformUI.getActivePart();
		if (part != null) {
			INavigatorContentService contentService = part.getAdapter(INavigatorContentService.class);
			if (contentService != null) {
				labelProvider = contentService.createCommonLabelProvider();
				descriptionProvider = contentService.createCommonDescriptionProvider();
			} else {
			IEditingDomainProvider editingDomainProvider = (IEditingDomainProvider) part.getAdapter(IEditingDomainProvider.class);
				IEditingDomainProvider editingDomainProvider = part.getAdapter(IEditingDomainProvider.class);
				if (editingDomainProvider != null) {
					EditingDomain editingDomain = editingDomainProvider.getEditingDomain();
					if (editingDomain instanceof TransactionalEditingDomain) {
					AdapterFactory adapterFactory = (AdapterFactory) part.getAdapter(AdapterFactory.class);
						AdapterFactory adapterFactory = part.getAdapter(AdapterFactory.class);
						if (adapterFactory == null && editingDomain instanceof AdapterFactoryEditingDomain) {
							adapterFactory = ((AdapterFactoryEditingDomain) editingDomain).getAdapterFactory();
						}
@@ -133,18 +136,31 @@ public class BasicTabbedPropertySheetTitleProvider extends LabelProvider {
				}
			}
		}
	}

	protected void disposeProviders() {
		// Dispose label provider (description provider just wraps and delegates to label provider and therefore does not need
		// to be disposed on its own)
		/*
		 * !! Important Note !! Only dispose label provider if it has been created by ourselves, but not if it is managed by
		 * another view (e.g., Common Navigator).
		 */
		if (labelProvider instanceof AdapterFactoryLabelProvider) {
			labelProvider.dispose();
		}
	}

	/**
	 * Extracts the actual element to rendered from given {@link Object element}.
	 * <p>
	 * This implementation calls {@link AdapterFactoryEditingDomain#unwrap()} for that purpose. Subclasses may override
	 * and extend as appropriate.
	 * This implementation calls {@link AdapterFactoryEditingDomain#unwrap()} for that purpose. Subclasses may override and
	 * extend as appropriate.
	 * </p>
	 *
	 * @param element
	 *            The element to be unwrapped.
	 * @return The extracted {@link Object element} if the original element could be successfully unwrapped or the
	 *         original element otherwise.
	 * @return The extracted {@link Object element} if the original element could be successfully unwrapped or the original
	 *         element otherwise.
	 */
	protected Object unwrap(Object element) {
		return AdapterFactoryEditingDomain.unwrap(element);
@@ -201,4 +217,10 @@ public class BasicTabbedPropertySheetTitleProvider extends LabelProvider {
		}
		return null;
	}

	@Override
	public void dispose() {
		disposeProviders();
		super.dispose();
	}
}
+75 −9
Original line number Diff line number Diff line
/**
 * <copyright>
 *
 * Copyright (c) 2008-2010 See4sys and others.
 * Copyright (c) 2008-2017 See4sys, itemis and others.
 * 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
@@ -9,6 +9,7 @@
 *
 * Contributors:
 *     See4sys - Initial API and implementation
 *     itemis - [526269] Properties page cannot be opened from Example Form Editor
 *
 * </copyright>
 */
@@ -20,13 +21,17 @@ import java.util.Collection;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.sphinx.platform.messages.PlatformMessages;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
@@ -48,6 +53,8 @@ public final class ExtendedPlatformUI {

	public static final String SYSTEM_CONSOLE_NAME = "System Console"; //$NON-NLS-1$

	private static final String ECLIPSE_PROPERTIES_VIEW_ID = "org.eclipse.ui.views.PropertySheet"; //$NON-NLS-1$

	// Prevent from instantiation
	private ExtendedPlatformUI() {
	}
@@ -87,6 +94,63 @@ public final class ExtendedPlatformUI {
		return page[0];
	}

	/**
	 * Returns the active {@link IWorkbenchPart part} if any.
	 * <p>
	 * Essentially does the same thing as {@link IPartService#getActivePart()}. However, instead of giving up when the
	 * latter returns <code>null</code>, this implementation moves on and attempts to find the active editor or view based
	 * on the current selection in the active {@link IWorkbenchWindow window}.
	 * </p>
	 *
	 * @return The active {@link IWorkbenchPart part}, or <code>null</code> if no part is currently activ
	 */
	public static IWorkbenchPart getActivePart() {
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		if (page == null) {
			return null;
		}

		// Try direct retrieval of active part
		IWorkbenchPart activePart = page.getActivePart();
		if (activePart != null) {
			return activePart;
		}

		// Try to retrieve current selection
		ISelection currentSelection = page.getSelection();
		if (currentSelection != null && !currentSelection.isEmpty()) {
			// See if active editor holds current selection and return it as active part if so
			IEditorPart activeEditor = page.getActiveEditor();
			if (activeEditor != null) {
				ISelectionProvider selectionProvider = activeEditor.getSite().getSelectionProvider();
				if (selectionProvider != null) {
					ISelection selection = selectionProvider.getSelection();
					if (currentSelection.equals(selection)) {
						return activeEditor;
					}
				}
			}

			// Try to find the view that holds current selection and return it as active part if any
			IViewReference[] viewRefs = page.getViewReferences();
			for (IViewReference viewRef : viewRefs) {
				IWorkbenchPart viewPart = viewRef.getPart(false);
				if (viewPart != null && page.isPartVisible(viewPart) && !ECLIPSE_PROPERTIES_VIEW_ID.equals(viewPart.getSite().getId())) {
					ISelectionProvider selectionProvider = viewPart.getSite().getSelectionProvider();
					if (selectionProvider != null) {
						ISelection selection = selectionProvider.getSelection();
						if (currentSelection.equals(selection)) {
							return viewPart;
						}
					}
				}
			}
		}

		// Currently no idea of anything else what we could try to do
		return null;
	}

	/**
	 * A convenience method to write on a Sphinx console.
	 *
@@ -131,7 +195,8 @@ public final class ExtendedPlatformUI {
	/**
	 * @param objectName
	 *            The name of the object for which deletion must be confirmed.
	 * @return <ul>
	 * @return
	 *         <ul>
	 *         <li><tt><b>true</b>&nbsp;&nbsp;</tt> if the concerned object can be deleted;</li>
	 *         <li><tt><b>false</b>&nbsp;</tt> otherwise.</li>
	 *         </ul>
@@ -156,7 +221,8 @@ public final class ExtendedPlatformUI {
	 *            The title of the 'confirm delete' dialog.
	 * @param message
	 *            The message to display in the 'confirm delete' dialog.
	 * @return <ul>
	 * @return
	 *         <ul>
	 *         <li><tt><b>true</b>&nbsp;&nbsp;</tt> if the concerned object can be deleted;</li>
	 *         <li><tt><b>false</b>&nbsp;</tt> otherwise.</li>
	 *         </ul>