KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.PlatformObject;
16 import org.eclipse.debug.core.model.MemoryByte;
17
18 /**
19  * @since 3.0
20  */

21
22 public class TableRenderingLine extends PlatformObject {
23     private String JavaDoc fAddress;
24     private String JavaDoc fStrRep;
25     private MemoryByte[] fBytes;
26     private byte[] fByteArray;
27     private int fTableIndex = -1;
28     private String JavaDoc fPaddedString;
29     public boolean isMonitored;
30
31     public static final String JavaDoc P_ADDRESS = "address"; //$NON-NLS-1$
32

33     // for raw hex data, it's 2 characters per byte
34
private static final int numCharPerByteForHex = 2;
35
36     public TableRenderingLine(String JavaDoc address, MemoryByte[] bytes, int tableIndex, String JavaDoc paddedString) {
37         fAddress = address;
38         fBytes = bytes;
39         fTableIndex = tableIndex;
40         fPaddedString = paddedString;
41     }
42
43     public String JavaDoc getAddress() {
44         return fAddress;
45     }
46
47     public void setAddress(String JavaDoc address) {
48         fAddress = address;
49     }
50     
51     public MemoryByte[] getBytes()
52     {
53         return fBytes;
54     }
55     
56     public MemoryByte getByte(int offset)
57     {
58         if (fBytes == null)
59             return null;
60         
61         if (offset < fBytes.length) {
62             return fBytes[offset];
63         }
64         
65         return null;
66     }
67     
68     public MemoryByte[] getBytes(int start, int end)
69     {
70         ArrayList JavaDoc ret = new ArrayList JavaDoc();
71         
72         for (int i=start; i<end; i++)
73         {
74             ret.add(fBytes[i]);
75         }
76         return (MemoryByte[]) ret.toArray(new MemoryByte[ret.size()]);
77     }
78     
79     public String JavaDoc getRawMemoryString()
80     {
81         if (fStrRep == null)
82         {
83             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
84             fStrRep = RenderingsUtil.convertByteArrayToHexString(getByteArray());
85             fStrRep = fStrRep.toUpperCase();
86             
87             buffer = buffer.append(fStrRep);
88             
89             // pad unavailable bytes with padded string from memory block
90
String JavaDoc paddedString = null;
91             int bufferCounter = 0;
92             for (int i=0; i<fBytes.length; i++)
93             {
94                 // if byte is invalid
95
if (!fBytes[i].isReadable())
96                 {
97                     if (paddedString == null)
98                     {
99                         paddedString = fPaddedString;
100                         
101                         if (paddedString.length() > TableRenderingLine.numCharPerByteForHex)
102                             paddedString = paddedString.substring(0, TableRenderingLine.numCharPerByteForHex);
103                     }
104                     buffer.replace(bufferCounter, bufferCounter+TableRenderingLine.numCharPerByteForHex, paddedString);
105                 }
106                 bufferCounter += TableRenderingLine.numCharPerByteForHex;
107             }
108             
109             fStrRep = buffer.toString();
110         }
111         
112         return fStrRep;
113     }
114
115     /**
116      * @param start
117      * @param end
118      * @return is the bytes between start and end are all valid
119      */

120     public boolean isAvailable(int start, int end) {
121         boolean available = true;
122         for (int i=start; i<end; i++)
123         {
124             if (!fBytes[i].isReadable())
125             {
126                 available = false;
127                 break;
128             }
129         }
130         return available;
131     }
132
133
134     public byte[] getByteArray()
135     {
136         if (fByteArray == null)
137         {
138             fByteArray = new byte[fBytes.length];
139             for (int i=0; i<fBytes.length; i++)
140             {
141                 fByteArray[i] = fBytes[i].getValue();
142             }
143         }
144         
145         return fByteArray;
146     }
147     
148     public byte[] getByteArray(int start, int end)
149     {
150         byte[] ret = new byte[end-start];
151         int j=0;
152         
153         for (int i=start; i<end; i++)
154         {
155             ret[j] = fBytes[i].getValue();
156             j++;
157         }
158         return ret;
159     }
160     
161     public void markDeltas(TableRenderingLine oldData)
162     {
163         if (oldData == null)
164             return;
165         
166         // if address is not the same, no need to compare
167
if (!oldData.getAddress().equals(this.getAddress()))
168             return;
169         
170         // if the string representation is the same, no need to compare
171
if (oldData.getRawMemoryString().equals(getRawMemoryString()))
172         {
173             for (int i=0; i<fBytes.length; i++)
174             {
175                 // set history as known if we have old data for this line
176
fBytes[i].setHistoryKnown(true);
177             }
178             return;
179         }
180         
181         MemoryByte[] oldMemory = oldData.getBytes();
182         
183         if (oldMemory.length != fBytes.length)
184             return;
185             
186         for (int i=0; i<fBytes.length; i++)
187         {
188             // turn on known bit
189
fBytes[i].setHistoryKnown(true);
190             
191             if ((fBytes[i].getFlags() & MemoryByte.READABLE) != (oldMemory[i].getFlags() & MemoryByte.READABLE))
192             {
193                 fBytes[i].setChanged(true);
194                 continue;
195             }
196                 
197             if (fBytes[i].isReadable() && oldMemory[i].isReadable())
198             {
199                 if (fBytes[i].getValue() != oldMemory[i].getValue())
200                 {
201                     fBytes[i].setChanged(true);
202                 }
203             }
204         }
205     }
206     
207     public void copyDeltas(TableRenderingLine oldData)
208     {
209         if (oldData == null)
210             return;
211         
212         // if address is not the same, do not copy
213
if (!oldData.getAddress().equals(this.getAddress()))
214             return;
215         
216         // reuse delta information from old data
217
MemoryByte[] oldMemory = oldData.getBytes();
218         
219         if (oldMemory.length != fBytes.length)
220             return;
221             
222         for (int i=0; i<fBytes.length; i++)
223         {
224             fBytes[i].setFlags(oldMemory[i].getFlags());
225         }
226     }
227     
228     public boolean isLineChanged(TableRenderingLine oldData)
229     {
230         if (oldData == null)
231             return false;
232         
233         // if address is not the same, no need to compare
234
if (!oldData.getAddress().equals(this.getAddress()))
235             return false;
236         
237         // if the string representation is not the same, this line has changed
238
if (oldData.getRawMemoryString().equals(getRawMemoryString())) {
239             return false;
240         }
241         return true;
242     }
243     
244     /**
245      * @param offset
246      * @param endOffset
247      * @return true if the specified range of memory has changed, false otherwise
248      * */

249     
250     public boolean isRangeChange(int offset, int endOffset)
251     {
252         boolean allBytesKnown = true;
253         boolean allBytesUnchanged = true;
254         
255         for (int i=offset; i<=endOffset; i++)
256         {
257             if (!fBytes[i].isHistoryKnown())
258                 allBytesKnown = false;
259             if (fBytes[i].isChanged())
260                 allBytesUnchanged = false;
261         }
262         
263         if (allBytesKnown && !allBytesUnchanged) {
264             return true;
265         }
266         return false;
267     }
268     
269     public void unmarkDeltas()
270     {
271         for (int i=0; i<fBytes.length; i++)
272         {
273             // unset the change bit
274
if (fBytes[i].isChanged())
275                 fBytes[i].setChanged(false);
276         }
277     }
278     
279     /* (non-Javadoc)
280      * @see java.lang.Object#toString()
281      */

282     public String JavaDoc toString()
283     {
284         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
285         buf.append(getAddress());
286         
287         buf.append(": "); //$NON-NLS-1$
288

289         buf.append(getRawMemoryString());
290         
291         return buf.toString();
292     }
293     
294     public int getTableIndex()
295     {
296         return fTableIndex;
297     }
298     
299     public int getLength()
300     {
301         return fBytes.length;
302     }
303
304 }
305
306
Popular Tags