KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.renderings;
12
13 import java.math.BigInteger JavaDoc;
14
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.debug.core.DebugException;
19 import org.eclipse.debug.core.model.IMemoryBlock;
20 import org.eclipse.debug.core.model.IMemoryBlockExtension;
21 import org.eclipse.debug.internal.ui.memory.provisional.AbstractAsyncTableRendering;
22 import org.eclipse.debug.internal.ui.memory.provisional.MemoryViewPresentationContext;
23 import org.eclipse.debug.internal.ui.viewers.TableUpdatePolicy;
24 import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelChangedListener;
25 import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelDelta;
26 import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelProxy;
27 import org.eclipse.debug.ui.memory.IMemoryRendering;
28 import org.eclipse.ui.progress.UIJob;
29
30 /**
31  * This update policy updates immediately after a model changed event. The update policy will
32  * only update if the rendering is visible. Cache from the content manager is cleared
33  * when the memory block has changed when the rendering is not visible
34  *
35  */

36 public class AsyncTableRenderingUpdatePolicy extends TableUpdatePolicy
37 {
38     public void modelChanged(IModelDelta node, IModelProxy proxy) {
39         
40         // clear current cache as it becomes invalid when the memory block is changed
41
AbstractVirtualContentTableModel model = getTableViewer().getVirtualContentModel();
42         
43         if (model != null)
44         {
45             IContentChangeComputer computer = null;
46             if (model instanceof IContentChangeComputer)
47                 computer = (IContentChangeComputer)model;
48             
49             clearCache(computer);
50             
51             if (!containsEvent(node))
52             {
53                 return;
54             }
55             
56             if (node.getElement() instanceof IMemoryBlock && (node.getFlags() & IModelDelta.CONTENT) != 0)
57             {
58                 if (computer != null && getTableViewer() != null)
59                 {
60                     // only cache if the rendering is not currently displaying error
61
if (!getTableViewer().getRendering().isDisplayingError())
62                     {
63                         // cache visible elelements
64
computer.cache(model.getElements());
65                     }
66                 }
67
68                 // update policy figured out what's changed in the memory block
69
// and will tell rendering to update accordinly.
70
// Updating the rendering indirectly update the table viewer
71
notifyRendering(node, proxy);
72                 handleMemoryBlockChanged((IMemoryBlock)node.getElement(), node);
73                 return;
74                 
75             }
76             else if (node.getElement() instanceof IMemoryBlock && (node.getFlags() & IModelDelta.STATE) != 0)
77             {
78                 // override handling of state change event
79
// let the super class deals with the rest of the changes
80
handleMemoryBlockChanged((IMemoryBlock)node.getElement(), node);
81                 return;
82             }
83         }
84         
85         super.modelChanged(node, proxy);
86     }
87
88     /**
89      * @param computer
90      */

91     protected void clearCache(IContentChangeComputer computer) {
92         if (computer != null)
93             computer.clearCache();
94     }
95
96     private void notifyRendering(IModelDelta node, IModelProxy proxy) {
97         if (getTableViewer() != null)
98         {
99             IModelChangedListener listener = (IModelChangedListener)getTableViewer().getRendering().getAdapter(IModelChangedListener.class);
100             if (listener != null)
101                 listener.modelChanged(node, proxy);
102         }
103     }
104     
105     protected void handleMemoryBlockChanged(IMemoryBlock mb, IModelDelta delta)
106     {
107         try {
108             if (getViewer().getPresentationContext() instanceof MemoryViewPresentationContext)
109             {
110                 MemoryViewPresentationContext context = (MemoryViewPresentationContext)getViewer().getPresentationContext();
111                 final AbstractAsyncTableRendering rendering = getTableRendering(context);
112                 if (rendering != null)
113                 {
114                     if ((delta.getFlags() & IModelDelta.CONTENT) != 0)
115                     {
116                         TableRenderingContentDescriptor descriptor = (TableRenderingContentDescriptor)rendering.getAdapter(TableRenderingContentDescriptor.class);
117                         
118                         if (descriptor != null)
119                         {
120                             final BigInteger JavaDoc address = getMemoryBlockBaseAddress(mb);
121                             if (!descriptor.isMemoryBlockBaseAddressInitialized() || !address.equals(descriptor.getContentBaseAddress()))
122                             {
123                                 descriptor.updateContentBaseAddress();
124                                 UIJob job = new UIJob("go to address"){ //$NON-NLS-1$
125

126                                     public IStatus runInUIThread(IProgressMonitor monitor) {
127                                         try {
128                                             rendering.goToAddress(address);
129                                         } catch (DebugException e) {
130                                             if (getTableViewer() != null)
131                                                 getTableViewer().handlePresentationFailure(null, e.getStatus());
132                                         }
133                                         return Status.OK_STATUS;
134                                     }};
135                                 job.setSystem(true);
136                                 job.schedule();
137                             }
138                             else
139                             {
140                                 rendering.refresh();
141                             }
142                         }
143                     }
144                     else
145                     {
146                         rendering.updateLabels();
147                     }
148                 }
149             }
150         } catch (DebugException e) {
151             if (getTableViewer() != null)
152                 getTableViewer().handlePresentationFailure(null, e.getStatus());
153         }
154     }
155     
156     private BigInteger JavaDoc getMemoryBlockBaseAddress(IMemoryBlock mb) throws DebugException
157     {
158         if (mb instanceof IMemoryBlockExtension)
159             return ((IMemoryBlockExtension)mb).getBigBaseAddress();
160         else
161             return BigInteger.valueOf(mb.getStartAddress());
162     }
163     
164     private AsyncTableRenderingViewer getTableViewer()
165     {
166         if (getViewer() instanceof AsyncTableRenderingViewer)
167             return (AsyncTableRenderingViewer)getViewer();
168         return null;
169     }
170     
171     private boolean containsEvent(IModelDelta delta)
172     {
173         if (getViewer().getPresentationContext() instanceof MemoryViewPresentationContext)
174         {
175             MemoryViewPresentationContext context = (MemoryViewPresentationContext) getViewer().getPresentationContext();
176             if (context.getRendering() instanceof AbstractAsyncTableRendering)
177             {
178                 AbstractAsyncTableRendering rendering = (AbstractAsyncTableRendering)context.getRendering();
179                 if (!rendering.isVisible())
180                     return false;
181             }
182         }
183         return true;
184     }
185     
186     protected AbstractAsyncTableRendering getTableRendering(MemoryViewPresentationContext context)
187     {
188         IMemoryRendering memRendering = context.getRendering();
189         if (memRendering != null && memRendering instanceof AbstractAsyncTableRendering)
190         {
191             return (AbstractAsyncTableRendering)memRendering;
192         }
193         return null;
194     }
195 }
Popular Tags