KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > part > PartPropertyProvider


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.part;
12
13 import java.text.MessageFormat JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.jface.resource.JFaceResources;
21 import org.eclipse.jface.resource.ResourceManager;
22 import org.eclipse.jface.util.SafeRunnable;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.IPropertyListener;
26 import org.eclipse.ui.ISharedImages;
27 import org.eclipse.ui.IWorkbenchPart;
28 import org.eclipse.ui.IWorkbenchPartConstants;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.internal.WorkbenchMessages;
31 import org.eclipse.ui.internal.components.framework.IDisposable;
32 import org.eclipse.ui.internal.part.components.services.IDirtyHandler;
33 import org.eclipse.ui.internal.part.components.services.IInputHandler;
34 import org.eclipse.ui.internal.part.components.services.INameable;
35 import org.eclipse.ui.internal.part.components.services.IPartDescriptor;
36 import org.eclipse.ui.internal.util.Util;
37
38 /**
39  * Note: this class is used in an addInstance call -- if this class implements an
40  * interface, that interface will override anything that the object had in its local
41  * context.
42  *
43  * @since 3.1
44  */

45 public class PartPropertyProvider implements IPartPropertyProvider, INameable, IDirtyHandler, IDisposable, IInputHandler {
46
47     // Dependencies
48
private INameable nameable;
49     private IDirtyHandler dirty;
50     private List JavaDoc listeners = new ArrayList JavaDoc();
51     
52     private String JavaDoc tooltip = ""; //$NON-NLS-1$
53
private String JavaDoc partName;
54     private String JavaDoc contentDescription = ""; //$NON-NLS-1$
55
private String JavaDoc title = ""; //$NON-NLS-1$
56
private boolean isDirty = false;
57     private ImageDescriptor titleImage;
58     private Image image = null;
59     private IEditorInput input;
60     private ResourceManager mgr;
61     
62     private final static class ListenerRec {
63         public ListenerRec(IPropertyListener l, IWorkbenchPart p) {
64             this.l = l;
65             this.part = p;
66         }
67         
68         public boolean equals(Object JavaDoc other) {
69             if (!(other instanceof ListenerRec)) return false;
70             
71             ListenerRec lr = (ListenerRec)other;
72             return (lr.l == l) && (lr.part == part);
73         }
74         
75         public IPropertyListener l;
76         public IWorkbenchPart part;
77     };
78     
79     public PartPropertyProvider(ResourceManager manager, INameable parentNameable,
80             IDirtyHandler dirtyListener, IPartDescriptor descriptor, IEditorInput initialInput) {
81         this.nameable = parentNameable;
82         this.dirty = dirtyListener;
83         this.partName = descriptor.getLabel();
84         this.titleImage = descriptor.getImage();
85         this.input = initialInput;
86         this.mgr = manager;
87     }
88     
89     private ResourceManager getManager() {
90         if (mgr == null) {
91             mgr = JFaceResources.getResources();
92         }
93         return mgr;
94     }
95     
96     /* (non-Javadoc)
97      * @see org.eclipse.ui.internal.part.IPartPropertyProvider#addPropertyListener(org.eclipse.ui.IWorkbenchPart, org.eclipse.ui.IPropertyListener)
98      */

99     public void addPropertyListener(IWorkbenchPart part, IPropertyListener l) {
100         listeners.add(new ListenerRec(l, part));
101     }
102
103     public void removePropertyListener(IWorkbenchPart part, IPropertyListener l) {
104         listeners.remove(new ListenerRec(l, part));
105     }
106         
107     /**
108      * @return Returns the title.
109      */

110     public String JavaDoc getTitle() {
111         return title;
112     }
113     
114     /* (non-Javadoc)
115      * @see org.eclipse.ui.internal.part.IPartPropertyProvider#getTitleToolTip()
116      */

117     public String JavaDoc getTitleToolTip() {
118         return tooltip;
119     }
120
121     /* (non-Javadoc)
122      * @see org.eclipse.ui.internal.part.IPartPropertyProvider#getTitleImage()
123      */

124     public Image getTitleImage() {
125         if (image == null) {
126             if (titleImage == null) {
127                 return PlatformUI.getWorkbench().getSharedImages().getImage(
128                         ISharedImages.IMG_DEF_VIEW);
129             }
130             
131             image = getManager().createImageWithDefault(titleImage);
132         }
133         
134         return image;
135     }
136
137     /* (non-Javadoc)
138      * @see org.eclipse.ui.internal.part.IPartPropertyProvider#getPartName()
139      */

140     public String JavaDoc getPartName() {
141         return partName;
142     }
143
144     /* (non-Javadoc)
145      * @see org.eclipse.ui.internal.part.IPartPropertyProvider#getContentDescription()
146      */

147     public String JavaDoc getContentDescription() {
148         return contentDescription;
149     }
150
151     /* (non-Javadoc)
152      * @see org.eclipse.ui.internal.part.IPartPropertyProvider#getEditorInput()
153      */

154     public IEditorInput getEditorInput() {
155         return input;
156     }
157
158     /* (non-Javadoc)
159      * @see org.eclipse.ui.internal.part.IPartPropertyProvider#isDirty()
160      */

161     public boolean isDirty() {
162         return isDirty;
163     }
164
165     /* (non-Javadoc)
166      * @see org.eclipse.core.component.IDisposable#dispose()
167      */

168     public void dispose() {
169         listeners.clear();
170         
171         if (image != null) {
172             getManager().destroy(titleImage);
173         }
174     }
175     
176     /* (non-Javadoc)
177      * @see org.eclipse.ui.internal.part.components.services.INameable#setContentDescription(java.lang.String)
178      */

179     public void setContentDescription(String JavaDoc contentDescription) {
180         if (this.contentDescription.equals(contentDescription)) {
181             return;
182         }
183         
184         this.contentDescription = contentDescription;
185         
186         if (nameable != null) {
187             nameable.setContentDescription(contentDescription);
188         }
189         
190         firePropertyChange(IWorkbenchPartConstants.PROP_CONTENT_DESCRIPTION);
191     }
192     
193     public void setDirty(boolean isDirty) {
194         if (isDirty == this.isDirty) {
195             return;
196         }
197         
198         this.isDirty = isDirty;
199         
200         if (dirty != null) {
201             dirty.setDirty(isDirty);
202         }
203         
204         firePropertyChange(IWorkbenchPartConstants.PROP_DIRTY);
205     }
206     
207     /* (non-Javadoc)
208      * @see org.eclipse.ui.internal.part.components.services.INameable#setImage(org.eclipse.jface.resource.ImageDescriptor)
209      */

210     public void setImage(ImageDescriptor theImage) {
211         ImageDescriptor oldImageDescriptor = this.titleImage;
212         Image oldImage = image;
213         
214         if (oldImageDescriptor == theImage) {
215             return;
216         }
217         
218         this.titleImage = theImage;
219         this.image = null;
220         
221         if (nameable != null) {
222             nameable.setImage(theImage);
223         }
224         
225         firePropertyChange(IWorkbenchPartConstants.PROP_TITLE);
226         
227         if (oldImage != null) {
228             getManager().destroy(oldImageDescriptor);
229         }
230     }
231     
232     /* (non-Javadoc)
233      * @see org.eclipse.ui.internal.part.components.services.INameable#setName(java.lang.String)
234      */

235     public void setName(String JavaDoc newName) {
236         if (this.partName.equals(newName)) {
237             return;
238         }
239         
240         this.partName = newName;
241         
242         if (nameable != null) {
243             nameable.setName(newName);
244         }
245         
246         firePropertyChange(IWorkbenchPartConstants.PROP_PART_NAME);
247         
248         setDefaultTitle();
249     }
250     
251     /* (non-Javadoc)
252      * @see org.eclipse.ui.internal.part.components.services.INameable#setTooltip(java.lang.String)
253      */

254     public void setTooltip(String JavaDoc toolTip) {
255         if (this.tooltip.equals(toolTip)) {
256             return;
257         }
258         
259         this.tooltip = toolTip;
260         
261         if (nameable != null) {
262             nameable.setTooltip(toolTip);
263         }
264         
265         firePropertyChange(IWorkbenchPartConstants.PROP_TITLE);
266     }
267
268     /**
269      * @param input The input to set.
270      */

271     public void setEditorInput(IEditorInput input) {
272         if (this.input.equals(input)) {
273             return;
274         }
275         
276         this.input = input;
277         
278         firePropertyChange(IWorkbenchPartConstants.PROP_INPUT);
279     }
280
281     private void setTitle(String JavaDoc newTitle) {
282         if (newTitle.equals(this.title)) {
283             return;
284         }
285         
286         this.title = newTitle;
287         firePropertyChange(IWorkbenchPartConstants.PROP_TITLE);
288     }
289     
290     void setDefaultTitle() {
291         String JavaDoc description = getContentDescription();
292         String JavaDoc name = getPartName();
293         String JavaDoc newTitle = name;
294
295         if (!Util.equals(description, "")) { //$NON-NLS-1$
296
newTitle = MessageFormat
297                     .format(
298                             WorkbenchMessages.WorkbenchPart_AutoTitleFormat, new String JavaDoc[] { name, description });
299         }
300
301         setTitle(newTitle);
302     }
303     
304     /**
305      * Fires a property changed event.
306      *
307      * @param propertyId the id of the property that changed
308      */

309     protected void firePropertyChange(final int propertyId) {
310         for (Iterator JavaDoc iter = listeners.iterator(); iter.hasNext();) {
311             final ListenerRec rec = (ListenerRec) iter.next();
312
313             Platform.run(new SafeRunnable() {
314                 public void run() {
315                     rec.l.propertyChanged(rec.part, propertyId);
316                 }
317             });
318         }
319     }
320 }
321
Popular Tags