KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ViewIntroAdapterPart


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.swt.custom.BusyIndicator;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.ui.IMemento;
21 import org.eclipse.ui.IPropertyListener;
22 import org.eclipse.ui.IViewSite;
23 import org.eclipse.ui.IWorkbenchPartSite;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.internal.intro.IntroMessages;
26 import org.eclipse.ui.intro.IIntroPart;
27 import org.eclipse.ui.intro.IIntroSite;
28 import org.eclipse.ui.part.ViewPart;
29
30 /**
31  * Simple view that will wrap an <code>IIntroPart</code>.
32  *
33  * @since 3.0
34  */

35 public final class ViewIntroAdapterPart extends ViewPart {
36
37     private IIntroPart introPart;
38
39     private IIntroSite introSite;
40
41     private boolean handleZoomEvents = true;
42
43     /**
44      * Adds a listener that toggles standby state if the view pane is zoomed.
45      */

46     private void addPaneListener() {
47         IWorkbenchPartSite site = getSite();
48         if (site instanceof PartSite) {
49             final WorkbenchPartReference ref = ((WorkbenchPartReference)((PartSite) site).getPartReference());
50             ref.addInternalPropertyListener(
51                     new IPropertyListener() {
52                         public void propertyChanged(Object JavaDoc source, int propId) {
53                             if (handleZoomEvents) {
54                                 if (propId == WorkbenchPartReference.INTERNAL_PROPERTY_ZOOMED) {
55                                     setStandby(!ref.getPane().isZoomed());
56                                 }
57                             }
58                         }
59                     });
60         }
61     }
62
63     /**
64      * Forces the standby state of the intro part.
65      *
66      * @param standby update the standby state
67      */

68     public void setStandby(final boolean standby) {
69         final Control control = ((PartSite) getSite()).getPane().getControl();
70         BusyIndicator.showWhile(control.getDisplay(), new Runnable JavaDoc() {
71             public void run() {
72                 try {
73                     control.setRedraw(false);
74                     introPart.standbyStateChanged(standby);
75                 } finally {
76                     control.setRedraw(true);
77                 }
78
79                 setBarVisibility(standby);
80             }
81         });
82     }
83
84     /**
85      * Toggles handling of zoom events.
86      *
87      * @param handle whether to handle zoom events
88      */

89     public void setHandleZoomEvents(boolean handle) {
90         handleZoomEvents = handle;
91     }
92
93     /* (non-Javadoc)
94      * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
95      */

96     public void createPartControl(Composite parent) {
97         addPaneListener();
98         introPart.createPartControl(parent);
99     }
100
101     /* (non-Javadoc)
102      * @see org.eclipse.ui.IWorkbenchPart#dispose()
103      */

104     public void dispose() {
105         setBarVisibility(true);
106         super.dispose();
107         getSite().getWorkbenchWindow().getWorkbench().getIntroManager()
108                 .closeIntro(introPart);
109         introPart.dispose();
110     }
111
112     /* (non-Javadoc)
113      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
114      */

115     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
116         return introPart.getAdapter(adapter);
117     }
118
119     /* (non-Javadoc)
120      * @see org.eclipse.ui.IWorkbenchPart#getTitleImage()
121      */

122     public Image getTitleImage() {
123         return introPart.getTitleImage();
124     }
125     
126     /* (non-Javadoc)
127      * @see org.eclipse.ui.part.WorkbenchPart#getTitle()
128      */

129     public String JavaDoc getTitle() {
130         // this method is called eagerly before our init method is called (and
131
// therefore before our intropart is created). By default return
132
// the view title from the view declaration. We will fire a property
133
// change to set the title to the proper value in the init method.
134
return introPart == null ? super.getTitle() : introPart.getTitle();
135     }
136
137     /* (non-Javadoc)
138      * @see org.eclipse.ui.IViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
139      */

140     public void init(IViewSite site, IMemento memento) throws PartInitException {
141         super.init(site);
142         Workbench workbench = (Workbench) site.getWorkbenchWindow()
143                 .getWorkbench();
144         try {
145             introPart = workbench.getWorkbenchIntroManager()
146                     .createNewIntroPart();
147             // reset the part name of this view to be that of the intro title
148
setPartName(introPart.getTitle());
149             introPart.addPropertyListener(new IPropertyListener() {
150                 public void propertyChanged(Object JavaDoc source, int propId) {
151                     firePropertyChange(propId);
152                 }
153             });
154             introSite = new ViewIntroAdapterSite(site, workbench
155                     .getIntroDescriptor());
156             introPart.init(introSite, memento);
157             
158         } catch (CoreException e) {
159             WorkbenchPlugin
160                     .log(
161                             IntroMessages.Intro_could_not_create_proxy, new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, IStatus.ERROR, IntroMessages.Intro_could_not_create_proxy, e));
162         }
163     }
164
165     /*
166      * (non-Javadoc)
167      *
168      * @see org.eclipse.ui.IWorkbenchPart#setFocus()
169      */

170     public void setFocus() {
171         introPart.setFocus();
172     }
173
174     /* (non-Javadoc)
175      * @see org.eclipse.ui.IViewPart#saveState(org.eclipse.ui.IMemento)
176      */

177     public void saveState(IMemento memento) {
178         introPart.saveState(memento);
179     }
180
181     /**
182      * Sets whether the CoolBar/PerspectiveBar should be visible.
183      *
184      * @param visible whether the CoolBar/PerspectiveBar should be visible
185      * @since 3.1
186      */

187     private void setBarVisibility(final boolean visible) {
188         WorkbenchWindow window = (WorkbenchWindow) getSite()
189                 .getWorkbenchWindow();
190         
191         final boolean layout = (visible != window.getCoolBarVisible())
192                 || (visible != window.getPerspectiveBarVisible()); // don't layout unless things have actually changed
193
if (visible) {
194             window.setCoolBarVisible(true);
195             window.setPerspectiveBarVisible(true);
196         } else {
197             window.setCoolBarVisible(false);
198             window.setPerspectiveBarVisible(false);
199         }
200
201         if (layout) {
202             window.getShell().layout();
203         }
204     }
205 }
206
Popular Tags