]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/layer/LayersView.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / layer / LayersView.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.layer;
13
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;
30
31 public class LayersView extends PageBookView implements ISelectionProvider, ISelectionChangedListener {
32
33         /**
34          * The plugin prefix.
35          */
36         public static final String PREFIX = PlatformUI.PLUGIN_ID + "."; //$NON-NLS-1$
37
38         /**
39          * Help context id used for the content outline view
40          * (value <code>"org.eclipse.ui.content_outline_context"</code>).
41          */
42         public static final String LAYERS_VIEW_HELP_CONTEXT_ID = PREFIX + "layers_context";//$NON-NLS-1$
43
44         /**
45          * Message to show on the default page.
46          */
47         private String defaultText = Messages.LayersView_noLayers; 
48
49         /**
50          * Creates a content outline view with no content outline pages.
51          */
52         public LayersView() {
53                 super();
54         }
55
56         /* (non-Javadoc)
57          * Method declared on ISelectionProvider.
58          */
59         public void addSelectionChangedListener(ISelectionChangedListener listener) {
60                 getSelectionProvider().addSelectionChangedListener(listener);
61         }
62
63         /* (non-Javadoc)
64          * Method declared on PageBookView.
65          */
66         protected IPage createDefaultPage(PageBook book) {
67                 MessagePage page = new MessagePage();
68                 initPage(page);
69                 page.createControl(book);
70                 page.setMessage(defaultText);
71                 return page;
72         }
73
74         /**
75          * The <code>PageBookView</code> implementation of this <code>IWorkbenchPart</code>
76          * method creates a <code>PageBook</code> control with its default page showing.
77          */
78         public void createPartControl(Composite parent) {
79                 super.createPartControl(parent);
80                 PlatformUI.getWorkbench().getHelpSystem().setHelp(getPageBook(),
81                                 LAYERS_VIEW_HELP_CONTEXT_ID);
82         }
83
84         /* (non-Javadoc)
85          * Method declared on PageBookView.
86          */
87         protected PageRec doCreatePage(IWorkbenchPart part) {
88                 // Try to get an outline page.
89                 ILayersViewPage page = (ILayersViewPage)part.getAdapter(ILayersViewPage.class);
90                 if (page != null) {
91                         if (page instanceof IPageBookViewPage) {
92                                 initPage((IPageBookViewPage) page);
93                         }
94                         page.createControl(getPageBook());
95                         return new PageRec(part, page);
96                 }
97                 // There is no content outline
98                 return null;
99         }
100
101         /* (non-Javadoc)
102          * Method declared on PageBookView.
103          */
104         protected void doDestroyPage(IWorkbenchPart part, PageRec rec) {
105                 ILayersViewPage page = (ILayersViewPage) rec.page;
106                 page.dispose();
107                 rec.dispose();
108         }
109
110         /* (non-Javadoc)
111          * Method declared on IAdaptable.
112          */
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();
119                                 }
120                         };
121                 }
122                 return super.getAdapter(key);
123         }
124
125         /* (non-Javadoc)
126          * Method declared on PageBookView.
127          */
128         protected IWorkbenchPart getBootstrapPart() {
129                 IWorkbenchPage page = getSite().getPage();
130                 if (page != null) {
131                         return page.getActiveEditor();
132                 }
133
134                 return null;
135         }
136
137         /**
138          * Returns the editor which contributed the current 
139          * page to this view.
140          *
141          * @return the editor which contributed the current page
142          * or <code>null</code> if no editor contributed the current page
143          */
144         private IWorkbenchPart getContributingEditor() {
145                 return getCurrentContributingPart();
146         }
147
148         /* (non-Javadoc)
149          * Method declared on ISelectionProvider.
150          */
151         public ISelection getSelection() {
152                 // get the selection from the selection provider
153                 return getSelectionProvider().getSelection();
154         }
155
156         /* (non-Javadoc)
157          * Method declared on PageBookView.
158          * We only want to track editors.
159          */
160         protected boolean isImportant(IWorkbenchPart part) {
161                 //We only care about editors
162                 return (part instanceof IEditorPart);
163         }
164
165         /* (non-Javadoc)
166          * Method declared on IViewPart.
167          * Treat this the same as part activation.
168          */
169         public void partBroughtToTop(IWorkbenchPart part) {
170                 partActivated(part);
171         }
172
173         /* (non-Javadoc)
174          * Method declared on ISelectionProvider.
175          */
176         public void removeSelectionChangedListener(
177                         ISelectionChangedListener listener) {
178                 getSelectionProvider().removeSelectionChangedListener(listener);
179         }
180
181         /* (non-Javadoc)
182          * Method declared on ISelectionChangedListener.
183          */
184         public void selectionChanged(SelectionChangedEvent event) {
185                 getSelectionProvider().selectionChanged(event);
186         }
187
188         /* (non-Javadoc)
189          * Method declared on ISelectionProvider.
190          */
191         public void setSelection(ISelection selection) {
192                 getSelectionProvider().setSelection(selection);
193         }
194
195         /**
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.
198          * 
199          * @param pageRec the page record containing the page to show
200          */
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);
208                 }
209                 super.showPageRec(pageRec);
210         }
211 }