KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > presentations > PresentablePart


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.presentations;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.ListenerList;
17 import org.eclipse.jface.util.IPropertyChangeListener;
18 import org.eclipse.jface.util.PropertyChangeEvent;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.graphics.Rectangle;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.ui.IPropertyListener;
25 import org.eclipse.ui.ISharedImages;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.internal.PartPane;
28 import org.eclipse.ui.internal.WorkbenchPartReference;
29 import org.eclipse.ui.internal.dnd.SwtUtil;
30 import org.eclipse.ui.presentations.IPartMenu;
31 import org.eclipse.ui.presentations.IPresentablePart;
32
33 /**
34  * This is a lightweight adapter that allows PartPanes to be used by a StackPresentation. All methods
35  * either redirect directly to PartPane or do trivial type conversions. All listeners registered by
36  * the presentation are kept here rather than registering them directly on the PartPane. This allows
37  * us to remove all listeners registered by a presentation that has been disposed, offering some
38  * protection against memory leaks.
39  */

40 public class PresentablePart implements IPresentablePart {
41
42     private PartPane part;
43
44     /**
45      * Local listener list -- we use this rather than registering listeners directly on the part
46      * in order to protect against memory leaks in badly behaved presentations.
47      */

48     private List JavaDoc listeners = new ArrayList JavaDoc();
49
50     // Lazily initialized. Use getPropertyListenerProxy() to access.
51
private IPropertyListener lazyPropertyListenerProxy;
52     
53     private ListenerList partPropertyChangeListeners = new ListenerList();
54     
55     private IPropertyChangeListener lazyPartPropertyChangeListener;
56
57     // Lazily initialized. Use getMenu() to access
58
private IPartMenu viewMenu;
59     
60     // True iff the "set" methods on this object are talking to the real part (disabled
61
// if the part is currently being managed by another presentation stack)
62
private boolean enableInputs = true;
63     
64     // True iff the "get" methods are returning up-to-date info from the real part (disabled
65
// for efficency if the presentation is invisible)
66
private boolean enableOutputs = true;
67     private Rectangle savedBounds = new Rectangle(0,0,0,0);
68     private boolean isVisible = false;
69     
70     // Saved state (only used when the part is inactive)
71
private String JavaDoc name = ""; //$NON-NLS-1$
72
private String JavaDoc titleStatus = ""; //$NON-NLS-1$
73
private boolean isDirty = false;
74     private boolean isBusy = false;
75     private boolean hasViewMenu = false;
76    
77     /**
78      * Constructor
79      *
80      * @param part
81      */

82     public PresentablePart(PartPane part, Composite parent) {
83         this.part = part;
84         getPane().addPropertyListener(getPropertyListenerProxy());
85         getPane().addPartPropertyListener(getPartPropertyListenerProxy());
86     }
87
88     public PartPane getPane() {
89         return part;
90     }
91     
92     private IPropertyListener getPropertyListenerProxy() {
93         if (lazyPropertyListenerProxy == null) {
94             lazyPropertyListenerProxy = new IPropertyListener() {
95                 public void propertyChanged(Object JavaDoc source, int propId) {
96                     firePropertyChange(propId);
97                 }
98             };
99         }
100
101         return lazyPropertyListenerProxy;
102     }
103     
104     private IPropertyChangeListener getPartPropertyListenerProxy() {
105         if (lazyPartPropertyChangeListener == null) {
106             lazyPartPropertyChangeListener = new IPropertyChangeListener() {
107                 public void propertyChange(PropertyChangeEvent event) {
108                     PropertyChangeEvent e = new PropertyChangeEvent(this,
109                             event.getProperty(), event.getOldValue(), event.getNewValue());
110                     firePartPropertyChange(e);
111                 }
112             };
113         }
114         return lazyPartPropertyChangeListener;
115     }
116
117     /**
118      * Detach this PresentablePart from the real part. No further methods should
119      * be invoked on this object.
120      */

121     public void dispose() {
122         // Ensure that the property listener is detached (necessary to prevent leaks)
123
getPane().removePropertyListener(getPropertyListenerProxy());
124         getPane().removePartPropertyListener(getPartPropertyListenerProxy());
125
126         // Null out the various fields to ease garbage collection (optional)
127
part = null;
128         listeners.clear();
129         listeners = null;
130         partPropertyChangeListeners.clear();
131         partPropertyChangeListeners = null;
132     }
133
134     public void firePropertyChange(int propertyId) {
135         for (int i = 0; i < listeners.size(); i++) {
136             ((IPropertyListener) listeners.get(i)).propertyChanged(this, propertyId);
137         }
138     }
139
140     public void addPropertyListener(final IPropertyListener listener) {
141         listeners.add(listener);
142     }
143
144     public void removePropertyListener(final IPropertyListener listener) {
145         listeners.remove(listener);
146     }
147
148     protected void firePartPropertyChange(PropertyChangeEvent event) {
149         Object JavaDoc[] l = partPropertyChangeListeners.getListeners();
150         for (int i = 0; i < l.length; i++) {
151             ((IPropertyChangeListener) l[i]).propertyChange(event);
152         }
153     }
154     
155     public void addPartPropertyListener(IPropertyChangeListener listener) {
156         partPropertyChangeListeners.add(listener);
157     }
158     
159     public void removePartPropertyListener(IPropertyChangeListener listener) {
160         partPropertyChangeListeners.remove(listener);
161     }
162     
163     /* (non-Javadoc)
164      * @see org.eclipse.ui.presentations.IPresentablePart#setBounds(org.eclipse.swt.graphics.Rectangle)
165      */

166     public void setBounds(Rectangle bounds) {
167         savedBounds = bounds;
168         if (enableInputs && !SwtUtil.isDisposed(part.getControl())) {
169             part.setBounds(bounds);
170         }
171     }
172
173     /* (non-Javadoc)
174      * @see org.eclipse.ui.presentations.IPresentablePart#setVisible(boolean)
175      */

176     public void setVisible(boolean isVisible) {
177         this.isVisible = isVisible;
178         if (enableInputs) {
179             part.setVisible(isVisible);
180         }
181     }
182
183     /* (non-Javadoc)
184      * @see org.eclipse.ui.presentations.IPresentablePart#setFocus()
185      */

186     public void setFocus() {
187         if (!SwtUtil.isDisposed(part.getControl())) {
188             if (part.getPage().getActivePart() == part.getPartReference().getPart(false)) {
189                 part.setFocus();
190             } else {
191                 part.requestActivation();
192             }
193         }
194     }
195
196     
197     private WorkbenchPartReference getPartReference() {
198         return (WorkbenchPartReference) part.getPartReference();
199     }
200
201     
202     /* (non-Javadoc)
203      * @see org.eclipse.ui.presentations.IPresentablePart#getName()
204      */

205     public String JavaDoc getName() {
206         if (enableOutputs) {
207             return getPartReference().getPartName();
208         }
209         return name;
210     }
211
212     /* (non-Javadoc)
213      * @see org.eclipse.ui.presentations.IPresentablePart#getTitle()
214      */

215     public String JavaDoc getTitle() {
216         return getPartReference().getTitle();
217     }
218
219     /* (non-Javadoc)
220      * @see org.eclipse.ui.presentations.IPresentablePart#getTitleStatus()
221      */

222     public String JavaDoc getTitleStatus() {
223         if (enableOutputs) {
224             return getPartReference().getContentDescription();
225         }
226
227         return titleStatus;
228     }
229
230     /*
231      * (non-Javadoc)
232      *
233      * @see org.eclipse.ui.presentations.IPresentablePart#getTitleImage()
234      */

235     public Image getTitleImage() {
236 //
237
// return PlatformUI.getWorkbench().getSharedImages().getImage(
238
// ISharedImages.IMG_DEF_VIEW);
239
//
240
if (enableOutputs) {
241             return getPartReference().getTitleImage();
242         }
243
244         return PlatformUI.getWorkbench().getSharedImages().getImage(
245                 ISharedImages.IMG_DEF_VIEW);
246     }
247
248     /*
249      * (non-Javadoc)
250      *
251      * @see org.eclipse.ui.presentations.IPresentablePart#getTitleToolTip()
252      */

253     public String JavaDoc getTitleToolTip() {
254         return getPartReference().getTitleToolTip();
255     }
256
257     /* (non-Javadoc)
258      * @see org.eclipse.ui.presentations.IPresentablePart#isDirty()
259      */

260     public boolean isDirty() {
261         if (enableOutputs) {
262             return getPartReference().isDirty();
263         }
264         return isDirty;
265     }
266
267     /*
268      * (non-Javadoc)
269      *
270      * @see org.eclipse.ui.presentations.IPresentablePart#isBusy()
271      */

272     public boolean isBusy() {
273         if (enableOutputs) {
274             return part.isBusy();
275         }
276         return isBusy;
277     }
278
279     /*
280      * (non-Javadoc)
281      *
282      * @see org.eclipse.ui.presentations.IPresentablePart#getToolBar()
283      */

284     public Control getToolBar() {
285         if (enableOutputs) {
286             return getPane().getToolBar();
287         }
288         return null;
289     }
290
291     /*
292      * (non-Javadoc)
293      *
294      * @see org.eclipse.ui.presentations.IPresentablePart#getMenu()
295      */

296     public IPartMenu getMenu() {
297         boolean hasMenu;
298         
299         if (enableOutputs) {
300             hasMenu = part.hasViewMenu();
301         } else {
302             hasMenu = this.hasViewMenu;
303         }
304         
305         if (!hasMenu) {
306             return null;
307         }
308
309         if (viewMenu == null) {
310             viewMenu = new IPartMenu() {
311                 public void showMenu(Point location) {
312                     part.showViewMenu(location);
313                 }
314             };
315         }
316
317         return viewMenu;
318     }
319
320     /* (non-Javadoc)
321      * @see org.eclipse.ui.presentations.IPresentablePart#isCloseable()
322      */

323     public boolean isCloseable() {
324         return part.isCloseable();
325     }
326     
327     /* (non-Javadoc)
328      * @see org.eclipse.ui.presentations.IPresentablePart#getControl()
329      */

330     public Control getControl() {
331         return part.getControl();
332     }
333
334     public void enableOutputs(boolean isActive) {
335         if (isActive == this.enableOutputs) {
336             return;
337         }
338         
339         this.enableOutputs = isActive;
340         
341         if (isActive) {
342             if (isBusy != getPane().isBusy()) {
343                 firePropertyChange(PROP_BUSY);
344             }
345             if (isDirty != isDirty()) {
346                 firePropertyChange(PROP_DIRTY);
347             }
348             if (!name.equals(getName())) {
349                 firePropertyChange(PROP_PART_NAME);
350             }
351             if (!titleStatus.equals(getTitleStatus())) {
352                 firePropertyChange(PROP_CONTENT_DESCRIPTION);
353             }
354             if (hasViewMenu != getPane().hasViewMenu()) {
355                 firePropertyChange(PROP_PANE_MENU);
356             }
357             // Always assume that the toolbar and title has changed (keeping track of this for real
358
// would be too expensive)
359
firePropertyChange(PROP_TOOLBAR);
360             firePropertyChange(PROP_TITLE);
361             
362             getPane().addPropertyListener(getPropertyListenerProxy());
363         } else {
364             getPane().removePropertyListener(getPropertyListenerProxy());
365             
366             WorkbenchPartReference ref = getPartReference();
367             isBusy = getPane().isBusy();
368             isDirty = ref.isDirty();
369             name = ref.getPartName();
370             titleStatus = ref.getContentDescription();
371             hasViewMenu = getPane().hasViewMenu();
372             firePropertyChange(PROP_TITLE);
373             firePropertyChange(PROP_TOOLBAR);
374         }
375     }
376     
377     public void enableInputs(boolean isActive) {
378         if (isActive == this.enableInputs) {
379             return;
380         }
381         
382         this.enableInputs = isActive;
383         
384         if (isActive) {
385             if (isActive && !SwtUtil.isDisposed(part.getControl())) {
386                 part.setBounds(savedBounds);
387             }
388             
389             part.setVisible(isVisible);
390         }
391     }
392
393     /* (non-Javadoc)
394      * @see org.eclipse.ui.presentations.IPresentablePart#getPartProperty(java.lang.String)
395      */

396     public String JavaDoc getPartProperty(String JavaDoc key) {
397         return getPartReference().getPartProperty(key);
398     }
399
400 }
401
Popular Tags