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.diagram.layer;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.ISelectionChangedListener;
16 import org.eclipse.jface.viewers.ISelectionProvider;
17 import org.eclipse.jface.viewers.SelectionChangedEvent;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.IWorkbenchPage;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.part.IContributedContentsView;
24 import org.eclipse.ui.part.IPage;
25 import org.eclipse.ui.part.IPageBookViewPage;
26 import org.eclipse.ui.part.IPageSite;
27 import org.eclipse.ui.part.MessagePage;
28 import org.eclipse.ui.part.PageBook;
29 import org.eclipse.ui.part.PageBookView;
31 public class LayersView extends PageBookView implements ISelectionProvider, ISelectionChangedListener {
36 public static final String PREFIX = PlatformUI.PLUGIN_ID + "."; //$NON-NLS-1$
39 * Help context id used for the content outline view
40 * (value <code>"org.eclipse.ui.content_outline_context"</code>).
42 public static final String LAYERS_VIEW_HELP_CONTEXT_ID = PREFIX + "layers_context";//$NON-NLS-1$
45 * Message to show on the default page.
47 private String defaultText = Messages.LayersView_noLayers;
50 * Creates a content outline view with no content outline pages.
57 * Method declared on ISelectionProvider.
59 public void addSelectionChangedListener(ISelectionChangedListener listener) {
60 getSelectionProvider().addSelectionChangedListener(listener);
64 * Method declared on PageBookView.
66 protected IPage createDefaultPage(PageBook book) {
67 MessagePage page = new MessagePage();
69 page.createControl(book);
70 page.setMessage(defaultText);
75 * The <code>PageBookView</code> implementation of this <code>IWorkbenchPart</code>
76 * method creates a <code>PageBook</code> control with its default page showing.
78 public void createPartControl(Composite parent) {
79 super.createPartControl(parent);
80 PlatformUI.getWorkbench().getHelpSystem().setHelp(getPageBook(),
81 LAYERS_VIEW_HELP_CONTEXT_ID);
85 * Method declared on PageBookView.
87 protected PageRec doCreatePage(IWorkbenchPart part) {
88 // Try to get an outline page.
89 ILayersViewPage page = (ILayersViewPage)part.getAdapter(ILayersViewPage.class);
91 if (page instanceof IPageBookViewPage) {
92 initPage((IPageBookViewPage) page);
94 page.createControl(getPageBook());
95 return new PageRec(part, page);
97 // There is no content outline
102 * Method declared on PageBookView.
104 protected void doDestroyPage(IWorkbenchPart part, PageRec rec) {
105 ILayersViewPage page = (ILayersViewPage) rec.page;
111 * Method declared on IAdaptable.
113 @SuppressWarnings("rawtypes")
114 public Object getAdapter(Class key) {
115 if (key == IContributedContentsView.class) {
116 return new IContributedContentsView() {
117 public IWorkbenchPart getContributingPart() {
118 return getContributingEditor();
122 return super.getAdapter(key);
126 * Method declared on PageBookView.
128 protected IWorkbenchPart getBootstrapPart() {
129 IWorkbenchPage page = getSite().getPage();
131 return page.getActiveEditor();
138 * Returns the editor which contributed the current
141 * @return the editor which contributed the current page
142 * or <code>null</code> if no editor contributed the current page
144 private IWorkbenchPart getContributingEditor() {
145 return getCurrentContributingPart();
149 * Method declared on ISelectionProvider.
151 public ISelection getSelection() {
152 // get the selection from the selection provider
153 return getSelectionProvider().getSelection();
157 * Method declared on PageBookView.
158 * We only want to track editors.
160 protected boolean isImportant(IWorkbenchPart part) {
161 //We only care about editors
162 return (part instanceof IEditorPart);
166 * Method declared on IViewPart.
167 * Treat this the same as part activation.
169 public void partBroughtToTop(IWorkbenchPart part) {
173 // Dirty hack to ensure that a new Page will be created if the previous one has been disposed due to disposal of AWTChassis
174 protected PageRec getPageRec(IWorkbenchPart part) {
175 PageRec rec = super.getPageRec(part);
176 if (rec == null || rec.page == null || rec.page.getControl() == null || rec.page.getControl().isDisposed())
183 * Method declared on ISelectionProvider.
185 public void removeSelectionChangedListener(
186 ISelectionChangedListener listener) {
187 getSelectionProvider().removeSelectionChangedListener(listener);
191 * Method declared on ISelectionChangedListener.
193 public void selectionChanged(SelectionChangedEvent event) {
194 getSelectionProvider().selectionChanged(event);
198 * Method declared on ISelectionProvider.
200 public void setSelection(ISelection selection) {
201 getSelectionProvider().setSelection(selection);
205 * The <code>ContentOutline</code> implementation of this <code>PageBookView</code> method
206 * extends the behavior of its parent to use the current page as a selection provider.
208 * @param pageRec the page record containing the page to show
210 protected void showPageRec(PageRec pageRec) {
211 IPageSite pageSite = getPageSite(pageRec.page);
212 ISelectionProvider provider = pageSite.getSelectionProvider();
213 if (provider == null && (pageRec.page instanceof ILayersViewPage)) {
214 // This means that the page did not set a provider during its initialization
215 // so for backward compatibility we will set the page itself as the provider.
216 pageSite.setSelectionProvider((ILayersViewPage) pageRec.page);
218 super.showPageRec(pageRec);