KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > memory > MemoryViewTab


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.debug.internal.ui.views.memory;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.ISafeRunnable;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.SafeRunner;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.debug.internal.ui.views.memory.renderings.ErrorRendering;
19 import org.eclipse.debug.ui.memory.IMemoryRendering;
20 import org.eclipse.jface.util.IPropertyChangeListener;
21 import org.eclipse.jface.util.PropertyChangeEvent;
22 import org.eclipse.jface.viewers.IBasicPropertyConstants;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.DisposeEvent;
25 import org.eclipse.swt.events.DisposeListener;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Event;
29 import org.eclipse.swt.widgets.Listener;
30 import org.eclipse.swt.widgets.TabItem;
31 import org.eclipse.ui.progress.WorkbenchJob;
32
33 /**
34  * Represents a tab in the Memory View. This is where memory renderings
35  * are hosted in the Memory View.
36  * @since 3.1
37  */

38 public class MemoryViewTab implements IMemoryViewTab, IPropertyChangeListener, Listener {
39
40     private IMemoryRendering fRendering;
41     private TabItem fTabItem;
42     private DisposeListener fDisposeListener;
43     private boolean fEnabled;
44     private boolean fIsDisposed = false;
45     private Control fControl;
46     private RenderingViewPane fContainer;
47     
48     
49     public MemoryViewTab(TabItem tabItem, IMemoryRendering rendering, RenderingViewPane container)
50     {
51         fTabItem = tabItem;
52         fRendering = rendering;
53         fContainer = container;
54         
55         // set the rendering as the synchronization provider
56
// as the new rendering should be in focus and have control
57
// after it's created
58

59         if (container.getMemoryRenderingSite().getSynchronizationService() != null)
60             container.getMemoryRenderingSite().getSynchronizationService().setSynchronizationProvider(rendering);
61         Control control = createViewTab();
62         
63         control.addListener(SWT.Activate, this);
64         control.addListener(SWT.Deactivate, this);
65         
66         fTabItem.setControl(control);
67         fTabItem.setData(this);
68         fTabItem.setText(getLabel());
69         fTabItem.setImage(getImage());
70         
71         fTabItem.addDisposeListener(fDisposeListener = new DisposeListener() {
72
73             public void widgetDisposed(DisposeEvent e) {
74                 fTabItem.removeDisposeListener(fDisposeListener);
75                 dispose();
76             }});
77     }
78     
79     private Control createViewTab()
80     {
81         ISafeRunnable safeRunnable = new ISafeRunnable() {
82
83             public void handleException(Throwable JavaDoc exception) {
84                 // create an error rendering to fill the view tab
85
ErrorRendering rendering = new ErrorRendering(fRendering.getRenderingId(), exception);
86                 rendering.init(fContainer, fRendering.getMemoryBlock());
87
88                 // dispose the rendering
89
fRendering.dispose();
90                 
91                 fRendering = rendering;
92                 fControl = rendering.createControl(fTabItem.getParent());
93             }
94
95             public void run() throws Exception JavaDoc {
96                 fControl = fRendering.createControl(fTabItem.getParent());
97                 fRendering.addPropertyChangeListener(getInstance());
98             }};
99             
100         SafeRunner.run(safeRunnable);
101         return fControl;
102     }
103     
104     private String JavaDoc getLabel()
105     {
106         return fRendering.getLabel();
107     }
108     
109     private Image getImage()
110     {
111         return fRendering.getImage();
112     }
113     
114     /* (non-Javadoc)
115      * @see org.eclipse.debug.internal.ui.views.memory.IMemoryViewTab#dispose()
116      */

117     public void dispose() {
118         
119         if (fIsDisposed)
120             return;
121         
122         fIsDisposed = true;
123         
124         fRendering.removePropertyChangeListener(this);
125         
126         if (!fControl.isDisposed())
127         {
128             fControl.removeListener(SWT.Activate, this);
129             fControl.removeListener(SWT.Deactivate, this);
130         }
131         
132         // always deactivate rendering before disposing it.
133
fRendering.deactivated();
134         
135         fRendering.dispose();
136     }
137     
138     public boolean isDisposed()
139     {
140         return fIsDisposed;
141     }
142
143     /* (non-Javadoc)
144      * @see org.eclipse.debug.internal.ui.views.memory.IMemoryViewTab#isEnabled()
145      */

146     public boolean isEnabled() {
147         return fEnabled;
148     }
149
150     /* (non-Javadoc)
151      * @see org.eclipse.debug.internal.ui.views.memory.IMemoryViewTab#setEnabled(boolean)
152      */

153     public void setEnabled(boolean enabled) {
154         fEnabled = enabled;
155         
156         if (fEnabled)
157             fRendering.becomesVisible();
158         else
159             fRendering.becomesHidden();
160
161     }
162
163     /* (non-Javadoc)
164      * @see org.eclipse.debug.internal.ui.views.memory.IMemoryViewTab#setTabLabel(java.lang.String)
165      */

166     public void setTabLabel(String JavaDoc label) {
167         fTabItem.setText(label);
168     }
169
170     /* (non-Javadoc)
171      * @see org.eclipse.debug.internal.ui.views.memory.IMemoryViewTab#getTabLabel()
172      */

173     public String JavaDoc getTabLabel() {
174         return fTabItem.getText();
175     }
176
177     /* (non-Javadoc)
178      * @see org.eclipse.debug.internal.ui.views.memory.IMemoryViewTab#getRendering()
179      */

180     public IMemoryRendering getRendering() {
181         return fRendering;
182     }
183
184     /* (non-Javadoc)
185      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
186      */

187     public void propertyChange(final PropertyChangeEvent event) {
188         
189         // make sure this runs on the UI thread, otherwise, it
190
// will get to a swt exception
191

192         WorkbenchJob job = new WorkbenchJob("MemoryViewTab PropertyChanged") { //$NON-NLS-1$
193

194             public IStatus runInUIThread(IProgressMonitor monitor) {
195                 if (isDisposed())
196                     return Status.OK_STATUS;
197                     
198                 if (event.getSource() == fRendering)
199                 {
200                     if (event.getProperty().equals(IBasicPropertyConstants.P_TEXT))
201                     {
202                         Object JavaDoc value = event.getNewValue();
203                         if (value != null && value instanceof String JavaDoc)
204                         {
205                             String JavaDoc label = (String JavaDoc)value;
206                             setTabLabel(label);
207                         }
208                         else
209                         {
210                             setTabLabel(fRendering.getLabel());
211                         }
212                     }
213                     
214                     if (event.getProperty().equals(IBasicPropertyConstants.P_IMAGE))
215                     {
216                         Object JavaDoc value = event.getNewValue();
217                         if (value != null && value instanceof Image)
218                         {
219                             Image image = (Image)value;
220                             fTabItem.setImage(image);
221                         }
222                         else
223                         {
224                             fTabItem.setImage(fRendering.getImage());
225                         }
226                     }
227                 }
228                 return Status.OK_STATUS;
229             }
230         };
231         job.setSystem(true);
232         job.schedule();
233     }
234     
235     private MemoryViewTab getInstance()
236     {
237         return this;
238     }
239
240     /* (non-Javadoc)
241      * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
242      */

243     public void handleEvent(Event event) {
244         if (event.type == SWT.Activate)
245         {
246             fRendering.activated();
247             fContainer.setRenderingSelection(fRendering);
248         }
249         if (event.type == SWT.Deactivate)
250         {
251             fRendering.deactivated();
252         }
253     }
254 }
255
Popular Tags