KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > elements > adapters > MemorySegmentLabelAdapter


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
12 package org.eclipse.debug.internal.ui.elements.adapters;
13
14 import java.math.BigInteger JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.debug.core.model.MemoryByte;
18 import org.eclipse.debug.internal.ui.DebugPluginImages;
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
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.model.provisional.IPresentationContext;
24 import org.eclipse.debug.internal.ui.viewers.provisional.AsynchronousLabelAdapter;
25 import org.eclipse.debug.internal.ui.views.launch.DebugElementHelper;
26 import org.eclipse.debug.internal.ui.views.memory.renderings.AbstractBaseTableRendering;
27 import org.eclipse.debug.internal.ui.views.memory.renderings.MemorySegment;
28 import org.eclipse.debug.internal.ui.views.memory.renderings.TableRenderingContentDescriptor;
29 import org.eclipse.debug.ui.IDebugUIConstants;
30 import org.eclipse.debug.ui.memory.IMemoryBlockTablePresentation;
31 import org.eclipse.debug.ui.memory.MemoryRenderingElement;
32 import org.eclipse.jface.resource.ImageDescriptor;
33 import org.eclipse.jface.viewers.IColorProvider;
34 import org.eclipse.jface.viewers.IFontProvider;
35 import org.eclipse.jface.viewers.ILabelProvider;
36 import org.eclipse.swt.graphics.Color;
37 import org.eclipse.swt.graphics.Font;
38 import org.eclipse.swt.graphics.FontData;
39 import org.eclipse.swt.graphics.Image;
40 import org.eclipse.swt.graphics.RGB;
41
42 public class MemorySegmentLabelAdapter extends AsynchronousLabelAdapter {
43
44     protected String JavaDoc[] getLabels(Object JavaDoc element, IPresentationContext context)
45             throws CoreException {
46         
47         if (context instanceof MemoryViewPresentationContext)
48         {
49             MemoryViewPresentationContext tableRenderingContext = (MemoryViewPresentationContext)context;
50             if (tableRenderingContext.getRendering() != null && tableRenderingContext.getRendering() instanceof AbstractAsyncTableRendering)
51             {
52                 AbstractAsyncTableRendering tableRendering = (AbstractAsyncTableRendering)tableRenderingContext.getRendering();
53                 TableRenderingContentDescriptor descriptor = (TableRenderingContentDescriptor)tableRendering.getAdapter(TableRenderingContentDescriptor.class);
54                 if (descriptor != null)
55                 {
56                     String JavaDoc addressStr = getColumnText(element, 0, tableRendering, descriptor);
57                     int numColumns = tableRendering.getAddressableUnitPerLine() / tableRendering.getAddressableUnitPerColumn();
58                     
59                     String JavaDoc[] labels = new String JavaDoc[numColumns+2];
60                     labels[0] = addressStr;
61                     
62                     for (int i=0; i<=numColumns; i++)
63                     {
64                         labels[i+1] = getColumnText(element, i+1, tableRendering, (TableRenderingContentDescriptor)tableRendering.getAdapter(TableRenderingContentDescriptor.class));
65                     }
66                     
67                     labels[labels.length - 1 ] = ""; //$NON-NLS-1$
68
return labels;
69                 }
70             }
71         }
72         return new String JavaDoc[0];
73     }
74     
75     private String JavaDoc getColumnText(Object JavaDoc element, int columnIndex, AbstractAsyncTableRendering tableRendering, TableRenderingContentDescriptor descriptor) {
76         String JavaDoc columnLabel = null;
77
78         if (columnIndex == 0)
79         {
80             IMemoryBlockTablePresentation presentation = (IMemoryBlockTablePresentation)tableRendering.getMemoryBlock().getAdapter(IMemoryBlockTablePresentation.class);
81             if (presentation != null)
82             {
83                 String JavaDoc rowLabel = presentation.getRowLabel(tableRendering.getMemoryBlock(), ((MemorySegment)element).getAddress());
84                 if (rowLabel != null)
85                     return rowLabel;
86             }
87             
88             columnLabel = ((MemorySegment)element).getAddress().toString(16).toUpperCase();
89             
90             int addressSize = descriptor.getAddressSize();
91             int prefillLength = addressSize * 2 - columnLabel.length();
92             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
93             if (prefillLength > 0)
94             {
95                 for (int i=0; i<prefillLength; i++)
96                 {
97                     buf.append("0"); //$NON-NLS-1$
98
}
99             }
100             buf.append(columnLabel);
101             return buf.toString();
102             
103         }
104         else if (columnIndex > (tableRendering.getBytesPerLine()/tableRendering.getBytesPerColumn()))
105         {
106             columnLabel = " "; //$NON-NLS-1$
107
}
108         else
109         {
110             if (element instanceof MemorySegment)
111             {
112                 MemorySegment segment = (MemorySegment)element;
113                 if (segment.getBytes().length != tableRendering.getBytesPerLine())
114                     return ""; //$NON-NLS-1$
115
}
116             
117             ILabelProvider labelProvider = (ILabelProvider)tableRendering.getAdapter(ILabelProvider.class);
118             if (labelProvider != null && columnIndex > 0)
119             {
120                 MemoryRenderingElement renderingElement = getMemoryRenderingElement(element, columnIndex, tableRendering);
121                 if (renderingElement != null) {
122                     String JavaDoc label = labelProvider.getText(renderingElement);
123                     if (label != null)
124                         return label;
125                 }
126             }
127             
128             int start = (columnIndex-1)*tableRendering.getBytesPerColumn();
129             MemoryByte[] bytes = ((MemorySegment)element).getBytes(start, tableRendering.getBytesPerColumn());
130             BigInteger JavaDoc address = ((MemorySegment)element).getAddress();
131             address = address.add(BigInteger.valueOf(start));
132             
133             columnLabel = tableRendering.getString(tableRendering.getRenderingId(), address, bytes);
134         }
135         return columnLabel;
136     }
137
138     protected ImageDescriptor[] getImageDescriptors(Object JavaDoc element,
139             IPresentationContext context) throws CoreException {
140         if (context instanceof MemoryViewPresentationContext)
141         {
142             MemoryViewPresentationContext tableRenderingContext = (MemoryViewPresentationContext)context;
143             if (tableRenderingContext.getRendering() != null && tableRenderingContext.getRendering() instanceof AbstractAsyncTableRendering)
144             {
145                 AbstractAsyncTableRendering tableRendering = (AbstractAsyncTableRendering)tableRenderingContext.getRendering();
146                 int numColumns = tableRendering.getAddressableUnitPerLine() / tableRendering.getAddressableUnitPerColumn();
147                 
148                 ImageDescriptor[] images = new ImageDescriptor[numColumns+2];
149                 
150                 for (int i=0; i<=numColumns; i++)
151                 {
152                     images[i] = getColumnImageDescriptor(element, i, tableRendering);
153                 }
154                 
155                 images[images.length - 1 ] = null;
156                 return images;
157             }
158         }
159         return new ImageDescriptor[0];
160     }
161     
162     private ImageDescriptor getColumnImageDescriptor(Object JavaDoc element, int columnIndex, AbstractAsyncTableRendering tableRendering)
163     {
164         if (columnIndex == 0)
165             return DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_OBJECT_MEMORY);
166         
167         if (element instanceof MemorySegment)
168         {
169             MemorySegment segment = (MemorySegment)element;
170             if (segment.getBytes().length != tableRendering.getBytesPerLine())
171                 return null;
172             
173             ILabelProvider labelProvider = (ILabelProvider)tableRendering.getAdapter(ILabelProvider.class);
174             if (labelProvider != null && columnIndex > 0)
175             {
176                 MemoryRenderingElement renderingElement = getMemoryRenderingElement(element, columnIndex, tableRendering);
177                 if (renderingElement != null) {
178                     Image image = labelProvider.getImage(renderingElement);
179                     if (image != null)
180                     {
181                         return DebugElementHelper.getImageDescriptor(image);
182                     }
183                 }
184             }
185             
186             int start = (columnIndex-1)*tableRendering.getBytesPerColumn();
187
188             MemoryByte[] bytes = ((MemorySegment)element).getBytes(start, tableRendering.getBytesPerColumn());
189             boolean allKnown = true;
190             boolean unchanged = true;
191             for (int i=0; i<bytes.length; i++)
192             {
193                 if (!bytes[i].isHistoryKnown())
194                     allKnown = false;
195                 
196                 if (bytes[i].isChanged())
197                     unchanged = false;
198             }
199             
200             if (allKnown)
201             {
202                 // mark changed elements with changed icon
203
if (!unchanged)
204                     return DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_OBJECT_MEMORY_CHANGED);
205                 
206             }
207         }
208         return DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_OBJECT_MEMORY);
209     }
210
211     protected FontData[] getFontDatas(Object JavaDoc element,
212             IPresentationContext context) throws CoreException {
213         if (context instanceof MemoryViewPresentationContext)
214         {
215             MemoryViewPresentationContext tableRenderingContext = (MemoryViewPresentationContext)context;
216             if (tableRenderingContext.getRendering() != null && tableRenderingContext.getRendering() instanceof AbstractAsyncTableRendering)
217             {
218                 AbstractAsyncTableRendering tableRendering = (AbstractAsyncTableRendering)tableRenderingContext.getRendering();
219                 int numColumns = tableRendering.getAddressableUnitPerLine() / tableRendering.getAddressableUnitPerColumn();
220                 
221                 FontData[] fontData = new FontData[numColumns+2];
222
223                 for (int i=0; i<fontData.length-1; i++)
224                 {
225                     fontData[i] = getColumnFontData(element, i, tableRendering);
226                 }
227                 return fontData;
228             }
229         }
230         
231         return new FontData[0];
232     }
233     
234     private FontData getColumnFontData(Object JavaDoc element, int columnIndex, AbstractAsyncTableRendering tableRendering)
235     {
236         if (element instanceof MemorySegment)
237         {
238             MemorySegment segment = (MemorySegment)element;
239             if (segment.getBytes().length != tableRendering.getBytesPerLine())
240                 return null;
241             
242             IFontProvider fontProvider = (IFontProvider)tableRendering.getAdapter(IFontProvider.class);
243             if (fontProvider != null && columnIndex > 0)
244             {
245                 MemoryRenderingElement renderingElement = getMemoryRenderingElement(element, columnIndex, tableRendering);
246                 if (renderingElement != null) {
247                     Font font = fontProvider.getFont(renderingElement);
248                     if (font != null)
249                         return font.getFontData()[0];
250                 }
251             }
252         }
253         return null;
254     }
255
256     protected RGB[] getForegrounds(Object JavaDoc element, IPresentationContext context)
257             throws CoreException {
258         
259         if (context instanceof MemoryViewPresentationContext)
260         {
261             MemoryViewPresentationContext tableRenderingContext = (MemoryViewPresentationContext)context;
262             if (tableRenderingContext.getRendering() != null && tableRenderingContext.getRendering() instanceof AbstractAsyncTableRendering)
263             {
264                 AbstractAsyncTableRendering tableRendering = (AbstractAsyncTableRendering)tableRenderingContext.getRendering();
265                 int numColumns = tableRendering.getAddressableUnitPerLine() / tableRendering.getAddressableUnitPerColumn();
266                 
267                 RGB[] colors = new RGB[numColumns+2];
268
269                 for (int i=0; i<colors.length-1; i++)
270                 {
271                     colors[i] = getColumnForeground(element, i, tableRendering);
272                 }
273                 
274                 colors[colors.length-1] = null;
275                 
276                 return colors;
277             }
278         }
279
280         return new RGB[0];
281     }
282     
283     private RGB getColumnBackground(Object JavaDoc element, int columnIndex, AbstractAsyncTableRendering tableRendering)
284     {
285         if (columnIndex == 0)
286             return null;
287         
288         if (element instanceof MemorySegment)
289         {
290             MemorySegment segment = (MemorySegment)element;
291             if (segment.getBytes().length != tableRendering.getBytesPerLine())
292                 return null;
293             
294             IColorProvider colorProvider = (IColorProvider)tableRendering.getAdapter(IColorProvider.class);
295             if (colorProvider != null && columnIndex > 0)
296             {
297                 MemoryRenderingElement renderingElement = getMemoryRenderingElement(element, columnIndex, tableRendering);
298                 if (renderingElement != null) {
299                     Color color = colorProvider.getBackground(renderingElement);
300                     if (color != null)
301                         return color.getRGB();
302                 }
303             }
304         }
305         return null;
306     }
307     
308     private RGB getColumnForeground(Object JavaDoc element, int columnIndex, AbstractAsyncTableRendering tableRendering)
309     {
310         if (columnIndex == 0)
311             return null;
312         
313         if (element instanceof MemorySegment)
314         {
315             MemorySegment segment = (MemorySegment)element;
316             if (segment.getBytes().length != tableRendering.getBytesPerLine())
317                 return null;
318             
319             IColorProvider colorProvider = (IColorProvider)tableRendering.getAdapter(IColorProvider.class);
320             if (colorProvider != null && columnIndex > 0)
321             {
322                 MemoryRenderingElement renderingElement = getMemoryRenderingElement(element, columnIndex, tableRendering);
323                 if (renderingElement != null) {
324                     Color color = colorProvider.getForeground(renderingElement);
325                     if (color != null)
326                         return color.getRGB();
327                 }
328             }
329             
330             int start = (columnIndex-1)*tableRendering.getBytesPerColumn();
331             MemoryByte[] bytes = segment.getBytes(start, tableRendering.getBytesPerColumn());
332             boolean allKnown = true;
333             boolean unchanged = true;
334             for (int i=0; i<bytes.length; i++)
335             {
336                 if (!bytes[i].isHistoryKnown())
337                     allKnown = false;
338                 
339                 if (bytes[i].isChanged())
340                     unchanged = false;
341             }
342             
343             if (allKnown)
344             {
345                 // mark changed elements in changed color
346
if (!unchanged)
347                     return DebugUIPlugin.getPreferenceColor(IDebugUIConstants.PREF_CHANGED_DEBUG_ELEMENT_COLOR).getRGB();
348                 
349                 return DebugUIPlugin.getPreferenceColor(IDebugUIConstants.PREF_MEMORY_HISTORY_KNOWN_COLOR).getRGB();
350             }
351             
352             return DebugUIPlugin.getPreferenceColor(IDebugUIConstants.PREF_MEMORY_HISTORY_UNKNOWN_COLOR).getRGB();
353             
354         }
355         return null;
356     }
357
358     protected RGB[] getBackgrounds(Object JavaDoc element, IPresentationContext context)
359             throws CoreException {
360         
361         if (context instanceof MemoryViewPresentationContext)
362         {
363             MemoryViewPresentationContext tableRenderingContext = (MemoryViewPresentationContext)context;
364             if (tableRenderingContext.getRendering() != null && tableRenderingContext.getRendering() instanceof AbstractAsyncTableRendering)
365             {
366                 AbstractAsyncTableRendering tableRendering = (AbstractAsyncTableRendering)tableRenderingContext.getRendering();
367                 int numColumns = tableRendering.getAddressableUnitPerLine() / tableRendering.getAddressableUnitPerColumn();
368                 
369                 RGB[] colors = new RGB[numColumns+2];
370
371                 for (int i=0; i<colors.length-1; i++)
372                 {
373                     colors[i] = getColumnBackground(element, i, tableRendering);
374                 }
375                 
376                 colors[colors.length-1] = null;
377                 
378                 return colors;
379             }
380         }
381
382         return new RGB[0];
383     }
384     
385     /**
386      * Returns a memory rendering element corresponding to the given element
387      * or <code>null</code> if none.
388      *
389      * @param element element to be rendered
390      * @param columnIndex column index at which to render
391      * @return memory rendering element or <code>null</code>
392      */

393     private MemoryRenderingElement getMemoryRenderingElement(Object JavaDoc element, int columnIndex, AbstractBaseTableRendering rendering) {
394         if (element instanceof MemorySegment) {
395             MemorySegment line = (MemorySegment) element;
396             BigInteger JavaDoc address = line.getAddress();
397             int offset = (columnIndex - 1) * rendering.getBytesPerColumn();
398             if (offset < rendering.getBytesPerLine() && (offset + rendering.getBytesPerColumn()) <= rendering.getBytesPerLine()) {
399                 return getMemoryRenderingElement(line, address, offset, rendering);
400             }
401         }
402         return null;
403     }
404     
405     private MemoryRenderingElement getMemoryRenderingElement(MemorySegment line, BigInteger JavaDoc lineAddress, int offset, AbstractBaseTableRendering rendering) {
406         BigInteger JavaDoc cellAddress = lineAddress.add(BigInteger.valueOf(offset));
407         MemoryByte[] bytes = line.getBytes(offset, rendering.getBytesPerColumn());
408         // make a copy to ensure that the memory bytes are not overwritten
409
// by clients
410
MemoryByte[] copy = new MemoryByte[bytes.length];
411         System.arraycopy(bytes, 0, copy, 0, bytes.length);
412         MemoryRenderingElement renderingElement = new MemoryRenderingElement(
413                 rendering, cellAddress, copy);
414         return renderingElement;
415     }
416
417 }
418
Popular Tags