KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.debug.internal.ui.views.memory.renderings;
13
14 import java.math.BigInteger JavaDoc;
15
16 import org.eclipse.debug.core.model.MemoryByte;
17 import org.eclipse.debug.ui.memory.AbstractTableRendering;
18 import org.eclipse.debug.ui.memory.MemoryRenderingElement;
19 import org.eclipse.jface.viewers.IColorProvider;
20 import org.eclipse.jface.viewers.IFontProvider;
21 import org.eclipse.jface.viewers.ILabelProvider;
22 import org.eclipse.jface.viewers.ITableColorProvider;
23 import org.eclipse.jface.viewers.ITableFontProvider;
24 import org.eclipse.swt.graphics.Color;
25 import org.eclipse.swt.graphics.Font;
26 import org.eclipse.swt.graphics.Image;
27
28 /**
29  * This label provider is used by AbstractTableRendering if clients decide to
30  * provide customized decorations in the rendering. Otherwise the table
31  * rendering uses TableRenderingLabelProvider.
32  *
33  */

34 public class TableRenderingLabelProviderEx extends TableRenderingLabelProvider implements ITableColorProvider, ITableFontProvider {
35
36     private IFontProvider fFontProvider;
37     private ILabelProvider fLabelProvider;
38     private IColorProvider fColorProvider;
39
40     public TableRenderingLabelProviderEx(AbstractTableRendering rendering) {
41         super(rendering);
42         fLabelProvider = (ILabelProvider) rendering.getAdapter(ILabelProvider.class);
43         fColorProvider = (IColorProvider) rendering.getAdapter(IColorProvider.class);
44         fFontProvider = (IFontProvider) rendering.getAdapter(IFontProvider.class);
45     }
46
47     /*
48      * (non-Javadoc)
49      *
50      * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
51      */

52     public void dispose() {
53         if (fFontProvider != null) {
54             fFontProvider = null;
55         }
56         if (fColorProvider != null) {
57             fColorProvider = null;
58         }
59         if (fLabelProvider != null) {
60             fLabelProvider.dispose();
61             fLabelProvider = null;
62         }
63         super.dispose();
64     }
65
66     /* (non-Javadoc)
67      * @see org.eclipse.jface.viewers.ITableColorProvider#getBackground(java.lang.Object, int)
68      */

69     public Color getBackground(Object JavaDoc element, int columnIndex) {
70         if (fColorProvider != null && columnIndex > 0) {
71             MemoryRenderingElement renderingElement = getMemoryRenderingElement(element, columnIndex);
72             if (renderingElement != null) {
73                 Color color = fColorProvider.getBackground(renderingElement);
74                 if (color != null)
75                     return color;
76             }
77         }
78         return super.getBackground(element);
79     }
80
81     /* (non-Javadoc)
82      * @see org.eclipse.jface.viewers.ITableColorProvider#getForeground(java.lang.Object, int)
83      */

84     public Color getForeground(Object JavaDoc element, int columnIndex) {
85         if (fColorProvider != null && columnIndex > 0) {
86             MemoryRenderingElement renderingElement = getMemoryRenderingElement(element, columnIndex);
87             if (renderingElement != null) {
88                 Color color = fColorProvider.getForeground(renderingElement);
89                 if (color != null)
90                     return color;
91             }
92         }
93         return super.getForeground(element);
94     }
95
96     /* (non-Javadoc)
97      * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
98      */

99     public Image getColumnImage(Object JavaDoc element, int columnIndex) {
100         if (fLabelProvider != null && columnIndex > 0) {
101             MemoryRenderingElement renderingElement = getMemoryRenderingElement(element, columnIndex);
102             if (renderingElement != null) {
103                 Image image = fLabelProvider.getImage(renderingElement);
104                 if (image != null)
105                     return image;
106             }
107         }
108         return super.getColumnImage(element, columnIndex);
109     }
110
111     /* (non-Javadoc)
112      * @see org.eclipse.jface.viewers.ITableFontProvider#getFont(java.lang.Object, int)
113      */

114     public Font getFont(Object JavaDoc element, int columnIndex) {
115         if (fFontProvider != null && columnIndex > 0) {
116             MemoryRenderingElement renderingElement = getMemoryRenderingElement(element, columnIndex);
117             if (renderingElement != null) {
118                 Font font = fFontProvider.getFont(renderingElement);
119                 if (font != null)
120                     return font;
121             }
122         }
123         return null;
124     }
125
126     /**
127      * Returns a memory rendering element corresponding to the given element
128      * or <code>null</code> if none.
129      *
130      * @param element element to be rendered
131      * @param columnIndex column index at which to render
132      * @return memory rendering element or <code>null</code>
133      */

134     private MemoryRenderingElement getMemoryRenderingElement(Object JavaDoc element, int columnIndex) {
135         if (element instanceof TableRenderingLine) {
136             TableRenderingLine line = (TableRenderingLine) element;
137             BigInteger JavaDoc lineAddress = new BigInteger JavaDoc(line.getAddress(), 16);
138             int offset = (columnIndex - 1) * fRendering.getBytesPerColumn();
139             if (offset < fRendering.getBytesPerLine() && (offset + fRendering.getBytesPerColumn()) <= fRendering.getBytesPerLine()) {
140                 return getMemoryRenderingElement(line, lineAddress, offset);
141             }
142         }
143         return null;
144     }
145     
146     private MemoryRenderingElement getMemoryRenderingElement(TableRenderingLine line, BigInteger JavaDoc lineAddress, int offset) {
147         BigInteger JavaDoc cellAddress = lineAddress.add(BigInteger.valueOf(offset));
148         MemoryByte[] bytes = line.getBytes(offset, offset
149                 + fRendering.getBytesPerColumn());
150         // make a copy to ensure that the memory bytes are not overwritten
151
// by clients
152
MemoryByte[] copy = new MemoryByte[bytes.length];
153         System.arraycopy(bytes, 0, copy, 0, bytes.length);
154         MemoryRenderingElement renderingElement = new MemoryRenderingElement(
155                 fRendering, cellAddress, copy);
156         return renderingElement;
157     }
158 }
159
Popular Tags