KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > update > MemoryRetrievalProxy


1 /*******************************************************************************
2  * Copyright (c) 2005, 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  
12 package org.eclipse.debug.internal.ui.viewers.update;
13
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.debug.core.DebugPlugin;
20 import org.eclipse.debug.core.IMemoryBlockListener;
21 import org.eclipse.debug.core.model.IMemoryBlock;
22 import org.eclipse.debug.core.model.IMemoryBlockRetrieval;
23 import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelDelta;
24 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
25 import org.eclipse.debug.internal.ui.viewers.model.provisional.ModelDelta;
26 import org.eclipse.debug.internal.ui.viewers.provisional.AbstractModelProxy;
27 import org.eclipse.debug.ui.memory.IMemoryRendering;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.StructuredSelection;
31 import org.eclipse.jface.viewers.StructuredViewer;
32 import org.eclipse.jface.viewers.Viewer;
33 import org.eclipse.ui.progress.UIJob;
34
35 public class MemoryRetrievalProxy extends AbstractModelProxy implements IMemoryBlockListener {
36     private IMemoryBlockRetrieval fRetrieval;
37
38     public MemoryRetrievalProxy(IMemoryBlockRetrieval retrieval)
39     {
40         fRetrieval = retrieval;
41     
42     }
43     
44     public void memoryBlocksAdded(IMemoryBlock[] memory) {
45         IMemoryBlock[] allMB = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(fRetrieval);
46         int lastIndex = allMB.length - memory.length;
47         ModelDelta delta = new ModelDelta(fRetrieval, 0, IModelDelta.NO_CHANGE, allMB.length);
48         
49         for (int i=0; i<memory.length; i++){
50             IMemoryBlockRetrieval retrieval = (IMemoryBlockRetrieval)memory[i].getAdapter(IMemoryBlockRetrieval.class);
51             if (retrieval == null)
52                 retrieval = memory[i].getDebugTarget();
53             
54             if (retrieval != null)
55             {
56                 if (retrieval == fRetrieval)
57                 {
58                     // select the newly added memory block
59
delta.addNode(memory[i], lastIndex+i, IModelDelta.ADDED | IModelDelta.SELECT, 0);
60                 }
61             }
62         }
63         
64         fireModelChanged(delta);
65     }
66
67     public void memoryBlocksRemoved(final IMemoryBlock[] memory) {
68         
69         UIJob job = new UIJob("memory blocks removed"){ //$NON-NLS-1$
70

71             public IStatus runInUIThread(IProgressMonitor monitor) {
72                 ModelDelta delta = new ModelDelta(fRetrieval, IModelDelta.NO_CHANGE);
73                 
74                 // find a memory block to select
75

76                 for (int i=0; i<memory.length; i++){
77                     IMemoryBlockRetrieval retrieval = (IMemoryBlockRetrieval)memory[i].getAdapter(IMemoryBlockRetrieval.class);
78                     if (retrieval == null)
79                         retrieval = memory[i].getDebugTarget();
80                     
81                     if (retrieval != null)
82                     {
83                         if (retrieval == fRetrieval)
84                         {
85                             // do not change selection if the memory block removed is not
86
// currently selected
87
// #getCurrentSelection must be run on the UI thread
88
if (isMemoryBlockSelected(getCurrentSelection(), memory[i]))
89                                 addSelectDeltaNode(delta);
90                             delta.addNode(memory[i], IModelDelta.REMOVED);
91                         }
92                     }
93                 }
94                 
95                 fireModelChanged(delta);
96                 return Status.OK_STATUS;
97             }};
98         job.setSystem(true);
99         job.schedule();
100     }
101
102     public void init(IPresentationContext context) {
103         super.init(context);
104         DebugPlugin.getDefault().getMemoryBlockManager().addListener(this);
105     }
106
107     public synchronized void dispose() {
108         super.dispose();
109         DebugPlugin.getDefault().getMemoryBlockManager().removeListener(this);
110     }
111
112     private void addSelectDeltaNode(ModelDelta delta)
113     {
114         IMemoryBlock[] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(fRetrieval);
115         if (memoryBlocks != null && memoryBlocks.length > 0)
116         {
117             delta.addNode(memoryBlocks[0], IModelDelta.SELECT);
118         }
119     }
120     
121     private IStructuredSelection getCurrentSelection() {
122         Viewer viewer = getViewer();
123         if (viewer instanceof StructuredViewer) {
124             StructuredViewer sv = (StructuredViewer) viewer;
125             ISelection selection = sv.getSelection();
126             if (selection instanceof IStructuredSelection)
127                 return (IStructuredSelection)selection;
128         }
129         return StructuredSelection.EMPTY;
130     }
131     
132     private boolean isMemoryBlockSelected(IStructuredSelection selection, IMemoryBlock memoryBlock)
133     {
134         if (!selection.isEmpty())
135         {
136             Iterator JavaDoc iter = selection.iterator();
137             while (iter.hasNext())
138             {
139                 Object JavaDoc sel = iter.next();
140                 if (sel == memoryBlock)
141                     return true;
142                 
143                 if (sel instanceof IMemoryRendering)
144                 {
145                     if (((IMemoryRendering)sel).getMemoryBlock() == memoryBlock)
146                         return true;
147                 }
148             }
149         }
150         return false;
151     }
152 }
153
Popular Tags