KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.ui.IMemento;
23 import org.eclipse.ui.IPersistable;
24 import org.eclipse.ui.IPropertyListener;
25 import org.eclipse.ui.IWorkbenchPart;
26 import org.eclipse.ui.IWorkbenchPart2;
27 import org.eclipse.ui.IWorkbenchPartConstants;
28 import org.eclipse.ui.internal.components.framework.Components;
29 import org.eclipse.ui.internal.components.framework.IDisposable;
30 import org.eclipse.ui.internal.part.components.interfaces.IFocusable;
31 import org.eclipse.ui.internal.part.components.services.INameable;
32 import org.eclipse.ui.internal.part.components.services.IStatusFactory;
33
34 /**
35  * @since 3.1
36  */

37 public abstract class OldPartToNewWrapper implements IFocusable, IAdaptable, IDisposable, IPersistable, IPartPropertyProvider {
38     private INameable nameable;
39     private IWorkbenchPart part;
40     private IWorkbenchPart2 wb2;
41     private IStatusFactory errorContext;
42     private Image lastImage = null;
43     private IMemento savedState = null;
44     private Composite control;
45     
46     private IPropertyListener propertyListener = new IPropertyListener() {
47         public void propertyChanged(Object JavaDoc source, int propId) {
48             OldPartToNewWrapper.this.propertyChanged(propId);
49         }
50     };
51
52     private Map JavaDoc listenerMap = new HashMap JavaDoc();
53     
54     /**
55      * IPropertyListener that changes the property source and forwards events
56      * to another listener.
57      */

58     private static final class ListenerWrapper implements IPropertyListener {
59         IWorkbenchPart part;
60         IPropertyListener listener;
61         
62         public ListenerWrapper(IWorkbenchPart part, IPropertyListener listener) {
63             this.part = part;
64             this.listener = listener;
65         }
66         
67         /* (non-Javadoc)
68          * @see org.eclipse.ui.IPropertyListener#propertyChanged(java.lang.Object, int)
69          */

70         public void propertyChanged(Object JavaDoc source, int propId) {
71             listener.propertyChanged(source, propId);
72         }
73     };
74     
75     public OldPartToNewWrapper(StandardWorkbenchServices services) {
76         this.control = services.getParentComposite();
77         this.nameable = services.getNameable();
78         this.savedState = services.getState();
79         this.errorContext = services.getStatusFactory();
80     }
81
82     protected void setPart(IWorkbenchPart newPart) {
83         part = newPart;
84         
85         if (part instanceof IWorkbenchPart2) {
86             wb2 = (IWorkbenchPart2)part;
87         }
88         
89         part.addPropertyListener(propertyListener);
90         savedState = null;
91     }
92     
93     protected IStatusFactory getErrorContext() {
94         return errorContext;
95     }
96     
97     protected IWorkbenchPart getPart() {
98         return part;
99     }
100     
101     /**
102      * @since 3.1
103      *
104      * @param propId
105      */

106     private void propertyChanged(int propId) {
107         switch(propId) {
108             case IWorkbenchPartConstants.PROP_TITLE:
109                 if (part.getTitleImage() != lastImage ) {
110                     lastImage = part.getTitleImage();
111                     nameable.setImage(ImageDescriptor.createFromImage(part.getTitleImage(), Display.getCurrent()));
112                 }
113                 nameable.setTooltip(part.getTitleToolTip());
114                 if (wb2 == null) {
115                     nameable.setContentDescription(part.getTitle());
116                 }
117                 break;
118             case IWorkbenchPartConstants.PROP_PART_NAME:
119                 if (wb2 != null) {
120                     nameable.setName(wb2.getPartName());
121                 }
122                 break;
123             case IWorkbenchPartConstants.PROP_CONTENT_DESCRIPTION:
124                 if (wb2 != null) {
125                     nameable.setContentDescription(wb2.getContentDescription());
126                 }
127                 break;
128         }
129     }
130     
131     public void dispose() {
132         if (part != null) {
133             part.dispose();
134             for (Iterator JavaDoc iter = listenerMap.values().iterator(); iter.hasNext();) {
135                 IPropertyListener listener = (IPropertyListener) iter.next();
136                 
137                 part.removePropertyListener(listener);
138             }
139             listenerMap.clear();
140         }
141     }
142
143     /* (non-Javadoc)
144      * @see org.eclipse.ui.internal.part.components.interfaces.IFocusable#setFocus()
145      */

146     public boolean setFocus() {
147         if (part != null) {
148             part.setFocus();
149             return true;
150         }
151         
152         return false;
153     }
154
155     /* (non-Javadoc)
156      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
157      */

158     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
159         if (adapter.isInstance(this)) {
160             return this;
161         }
162         
163         return Components.getAdapter(part, adapter);
164     }
165
166     /* (non-Javadoc)
167      * @see org.eclipse.ui.internal.part.components.interfaces.IPersistable#saveState(org.eclipse.ui.IMemento)
168      */

169     public void saveState(IMemento memento) {
170         if (part != null) {
171             if (part instanceof IPersistable) {
172                 IPersistable persistable = (IPersistable) part;
173                 persistable.saveState(memento);
174             }
175         } else if (savedState != null) {
176             memento.putMemento(savedState);
177         }
178     }
179
180     /* (non-Javadoc)
181      * @see org.eclipse.ui.internal.part.IPartPropertyProvider#addPropertyListener(org.eclipse.ui.IWorkbenchPart, org.eclipse.ui.IPropertyListener)
182      */

183     public void addPropertyListener(IWorkbenchPart part, IPropertyListener l) {
184         if (part != null) {
185             IPropertyListener wrapper = new ListenerWrapper(part, l);
186             part.addPropertyListener(wrapper);
187             
188             listenerMap.put(l, wrapper);
189         }
190     }
191
192     /* (non-Javadoc)
193      * @see org.eclipse.ui.internal.part.IPartPropertyProvider#removePropertyListener(org.eclipse.ui.IWorkbenchPart, org.eclipse.ui.IPropertyListener)
194      */

195     public void removePropertyListener(IWorkbenchPart part, IPropertyListener l) {
196         if (part != null) {
197             IPropertyListener wrapper = (IPropertyListener)listenerMap.get(l);
198             
199             if (wrapper != null) {
200                 part.removePropertyListener(wrapper);
201                 listenerMap.remove(l);
202             }
203         }
204     }
205
206     /* (non-Javadoc)
207      * @see org.eclipse.ui.internal.part.IPartPropertyProvider#getTitleToolTip()
208      */

209     public String JavaDoc getTitleToolTip() {
210         return part.getTitleToolTip();
211     }
212
213     /* (non-Javadoc)
214      * @see org.eclipse.ui.internal.part.IPartPropertyProvider#getTitleImage()
215      */

216     public Image getTitleImage() {
217         return part.getTitleImage();
218     }
219
220     /* (non-Javadoc)
221      * @see org.eclipse.ui.internal.part.IPartPropertyProvider#getTitle()
222      */

223     public String JavaDoc getTitle() {
224         return part.getTitle();
225     }
226 }
227
Popular Tags