]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/PerspectiveBarsActivator.java
Modified OpenWith for further customization.
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / PerspectiveBarsActivator.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.ui.workbench;
13
14 import java.util.List;
15
16 import org.eclipse.swt.widgets.Menu;
17 import org.eclipse.ui.IPerspectiveDescriptor;
18 import org.eclipse.ui.IPerspectiveListener4;
19 import org.eclipse.ui.IWindowListener;
20
21 import org.eclipse.ui.IWorkbenchPage;
22 import org.eclipse.ui.IWorkbenchPartReference;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.internal.WorkbenchWindow;
26 import org.eclipse.ui.internal.progress.ProgressRegion;
27
28 /**
29  * Handler for perspectiveBars ExtensionPoint
30  * 
31  * FIXME : statusLine widgets are not restored into original positions.
32  * 
33  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
34  *
35  */
36 @SuppressWarnings("restriction")
37 public class PerspectiveBarsActivator implements IPerspectiveListener4, IWindowListener {
38
39     private IWorkbenchWindow                    activeWindow;
40
41
42     public PerspectiveBarsActivator() {
43         PlatformUI.getWorkbench().addWindowListener(this);
44     }
45
46     public void dispose() {
47         PlatformUI.getWorkbench().removeWindowListener(this);
48     }
49
50     //------------------------------------------------------------------------
51     // IPerspectiveListener4
52     //------------------------------------------------------------------------
53
54     @Override
55     public void perspectivePreDeactivate(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
56     }
57
58     @Override
59     public void perspectiveClosed(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
60     }
61
62     @Override
63     public void perspectiveDeactivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
64     }
65
66     @Override
67     public void perspectiveOpened(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
68     }
69
70     @Override
71     public void perspectiveSavedAs(IWorkbenchPage page, IPerspectiveDescriptor oldPerspective,
72             IPerspectiveDescriptor newPerspective) {
73     }
74
75     @Override
76     public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective,
77             IWorkbenchPartReference partRef, String changeId) {
78         // See IWorkbenchPage.CHANGED_* constants for change id's.
79     }
80
81     @Override
82     public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
83         activatePerspective(perspective.getId());
84     }
85
86     @Override
87     public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, String changeId) {
88         // See IWorkbenchPage.CHANGED_* constants for change id's.
89     }
90
91     //------------------------------------------------------------------------
92     // IWindowListener
93     //------------------------------------------------------------------------
94
95     @Override
96     public void windowActivated(IWorkbenchWindow window) {
97 //        System.out.println("attaching to window: " + window);
98         attachToWindow(window);
99     }
100
101     @Override
102     public void windowClosed(IWorkbenchWindow window) {
103     }
104
105     @Override
106     public void windowDeactivated(IWorkbenchWindow window) {
107 //        System.out.println("detaching from window: " + window);
108         detachFromWindow(window);
109     }
110
111     @Override
112     public void windowOpened(IWorkbenchWindow window) {
113     }
114
115     //------------------------------------------------------------------------
116     // UTILITIES
117     //------------------------------------------------------------------------
118
119     private void attachToWindow(IWorkbenchWindow window) {
120         activeWindow = window;
121         window.addPerspectiveListener(this);
122         IPerspectiveDescriptor perspective = window.getActivePage().getPerspective();
123         if (perspective != null) {
124             activatePerspective(perspective.getId());
125         }
126     }
127
128     private void detachFromWindow(IWorkbenchWindow window) {
129         window.removePerspectiveListener(this);
130     }
131
132
133
134
135     private Menu menu;
136     
137         private void activatePerspective(String perspectiveId) {
138 //        System.out.println("activating perspective: " + perspectiveId);
139         
140         
141         List<IPerspectiveBarsExtension> exts = PerspectiveBarsManager.getInstance().getExtensions(perspectiveId);
142         Boolean menuBar = null;
143         Boolean coolBar = null;
144         Boolean statusLine = null;
145         Boolean perspectiveBar = null;
146         Boolean fastViewBar = null;
147         Boolean progressIndicator = null;
148         for (IPerspectiveBarsExtension ext : exts) {
149             if (menuBar == null)
150                 menuBar = ext.getMenuBar();
151             if (coolBar == null)
152                 coolBar = ext.getCoolBar();
153             if (statusLine == null)
154                 statusLine = ext.getStatusLine();
155             if (perspectiveBar == null)
156                 perspectiveBar = ext.getPerspectiveBar();
157             if (fastViewBar == null)
158                 fastViewBar = ext.getFastViewBar();
159             if (progressIndicator == null)
160                 progressIndicator = ext.getProgressIndicator();
161         }
162         
163         if (activeWindow instanceof WorkbenchWindow) {
164                 WorkbenchWindow window = (WorkbenchWindow)activeWindow;
165                 
166                 if (menuBar == null || menuBar == true) {
167                         Menu m = window.getShell().getMenuBar();
168                         if (m == null && menu != null) {
169                                 window.getShell().setMenuBar(menu);
170                         }
171                 } else {
172                         Menu m = window.getShell().getMenuBar();
173                         if (m != null) {
174                                 menu = m;
175                                 window.getShell().setMenuBar(null);
176                                 
177                         }
178                 }
179                 
180                 if (coolBar == null || coolBar == true) {
181                         window.setCoolBarVisible(true);
182                 } else {
183                         window.setCoolBarVisible(false);
184                 }
185                 
186                 if (statusLine == null || statusLine == true) {
187                         window.setStatusLineVisible(true);
188                         //window.showHeapStatus(true);
189                 } else {
190                         window.setStatusLineVisible(false);
191                         //window.showHeapStatus(false);
192                 }
193                 
194                 if (perspectiveBar == null || perspectiveBar == true) {
195                         window.setPerspectiveBarVisible(true);
196                 } else {
197                         window.setPerspectiveBarVisible(false);
198                 }
199
200                 // Bug 479522 - Remove unused fastView methods in WorkbenchWindow 
201                 //if (fastViewBar == null || fastViewBar == true) {
202                 //      window.setFastViewBarVisible(true);
203                 //} else {
204                 //      window.setFastViewBarVisible(false);
205                 //}
206                 
207                 if (progressIndicator == null || progressIndicator == true) {
208                         ProgressRegion r = window.getProgressRegion();
209                         if (r != null)
210                                 r.getControl().setVisible(true);
211                         
212                 } else {
213                         ProgressRegion r = window.getProgressRegion();
214                         if (r != null)
215                                 r.getControl().setVisible(false);
216                 }
217                 // Bug 486768 - Delete unused internal ITrimManager
218                 //window.getTrimManager().forceLayout();
219         }
220       
221     }
222
223 }