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) {
174 * Method declared on ISelectionProvider.
176 public void removeSelectionChangedListener(
177 ISelectionChangedListener listener) {
178 getSelectionProvider().removeSelectionChangedListener(listener);
182 * Method declared on ISelectionChangedListener.
184 public void selectionChanged(SelectionChangedEvent event) {
185 getSelectionProvider().selectionChanged(event);
189 * Method declared on ISelectionProvider.
191 public void setSelection(ISelection selection) {
192 getSelectionProvider().setSelection(selection);
196 * The <code>ContentOutline</code> implementation of this <code>PageBookView</code> method
197 * extends the behavior of its parent to use the current page as a selection provider.
199 * @param pageRec the page record containing the page to show
201 protected void showPageRec(PageRec pageRec) {
202 IPageSite pageSite = getPageSite(pageRec.page);
203 ISelectionProvider provider = pageSite.getSelectionProvider();
204 if (provider == null && (pageRec.page instanceof ILayersViewPage)) {
205 // This means that the page did not set a provider during its initialization
206 // so for backward compatibility we will set the page itself as the provider.
207 pageSite.setSelectionProvider((ILayersViewPage) pageRec.page);
209 super.showPageRec(pageRec);