KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.debug.internal.ui.views.memory;
13
14 import java.math.BigInteger JavaDoc;
15 import org.eclipse.debug.core.DebugException;
16 import org.eclipse.debug.core.model.IMemoryBlock;
17 import org.eclipse.debug.internal.core.memory.IExtendedMemoryBlock;
18 import org.eclipse.debug.internal.core.memory.MemoryByte;
19 import org.eclipse.debug.internal.ui.DebugUIMessages;
20 import org.eclipse.jface.viewers.IBaseLabelProvider;
21 import org.eclipse.jface.viewers.ICellModifier;
22 import org.eclipse.swt.widgets.TableItem;
23
24 /**
25  * @since 3.0
26  */

27 public class MemoryViewCellModifier implements ICellModifier
28 {
29     private static final String JavaDoc PREFIX = "MemoryViewCellModifier."; //$NON-NLS-1$
30
public static final String JavaDoc TITLE = PREFIX + "failure_title"; //$NON-NLS-1$
31
public static final String JavaDoc FAILED = PREFIX + "failed"; //$NON-NLS-1$
32
public static final String JavaDoc DATA_IS_INVALID = PREFIX + "data_is_invalid"; //$NON-NLS-1$
33
public static final String JavaDoc DATA_IS_TOO_LONG = PREFIX + "data_is_too_long"; //$NON-NLS-1$
34

35     private boolean editActionInvoked = false;
36     private ITableMemoryViewTab fViewTab;
37     
38     public MemoryViewCellModifier(ITableMemoryViewTab viewTab)
39     {
40         fViewTab = viewTab;
41     }
42     
43     /* (non-Javadoc)
44      * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
45      */

46     public boolean canModify(Object JavaDoc element, String JavaDoc property)
47     {
48         boolean canModify = true;
49         try
50         {
51             if (!(element instanceof MemoryViewLine))
52                 return false;
53                 
54             if (!editActionInvoked)
55                 return false;
56                 
57             if (fViewTab == null)
58                 return false;
59                 
60             if (fViewTab.getMemoryBlock().supportsValueModification() == false)
61             {
62                 return false;
63             }
64             
65             MemoryViewLine line = (MemoryViewLine)element;
66             if (MemoryViewLine.P_ADDRESS.equals(property))
67                return false;
68             else
69             {
70                 int offset = Integer.valueOf(property, 16).intValue();
71                 int end = offset + fViewTab.getColumnSize();
72                 
73             
74                 for (int i=offset; i<end; i++)
75                 {
76                     MemoryByte oneByte = line.getByte(i);
77 // if (((oneByte.flags & MemoryByte.VALID) != MemoryByte.VALID) ||
78
// ((oneByte.flags & MemoryByte.READONLY) == MemoryByte.READONLY))
79
if ((oneByte.flags & MemoryByte.READONLY) == MemoryByte.READONLY)
80                     {
81                         canModify = false;
82                     }
83                 }
84                 return canModify;
85             }
86         }
87         catch (NumberFormatException JavaDoc e)
88         {
89             canModify = false;
90             return canModify;
91         }
92     }
93
94     /* (non-Javadoc)
95      * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
96      */

97     public Object JavaDoc getValue(Object JavaDoc element, String JavaDoc property)
98     {
99         // give back the value of the column
100

101         if (!(element instanceof MemoryViewLine))
102             return null;
103             
104         MemoryViewLine line = (MemoryViewLine)element;
105         try
106         {
107             if (MemoryViewLine.P_ADDRESS.equals(property))
108                return line.getAddress();
109             else
110             {
111                 int offset = Integer.valueOf(property, 16).intValue();
112                 int end = offset + fViewTab.getColumnSize();
113                 
114
115                 //Ask for label provider
116
MemoryByte[] memory = line.getBytes(offset, end);
117
118                 IBaseLabelProvider labelProvider = ((MemoryViewTab)fViewTab).getTableViewer().getLabelProvider();
119                 if(labelProvider instanceof AbstractTableViewTabLabelProvider)
120                 {
121                     
122                     if (line.isAvailable(offset, end))
123                     {
124                         // ask the renderer for a string representation of the bytes
125
offset = Integer.valueOf(property, 16).intValue();
126                         AbstractMemoryRenderer renderer = ((AbstractTableViewTabLabelProvider)labelProvider).getRenderer();
127                         
128                         BigInteger JavaDoc address = new BigInteger JavaDoc(((MemoryViewLine)element).getAddress(), 16);
129                         address = address.add(BigInteger.valueOf(offset));
130                         
131                         return renderer.getString(fViewTab.getRenderingId(), address, memory, line.getPaddedString());
132                     }
133                     else
134                     {
135                         // if the range is not available, just return padded string
136
return line.getPaddedString(offset, end);
137                     }
138                 }
139                 
140                 return ""; //$NON-NLS-1$
141
}
142         }
143         catch (NumberFormatException JavaDoc e)
144         {
145             return "00"; //$NON-NLS-1$
146
}
147     }
148
149     /* (non-Javadoc)
150      * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
151      */

152     public void modify(Object JavaDoc element, String JavaDoc property, Object JavaDoc value)
153     {
154         MemoryViewLine line = null;
155         if (!(element instanceof MemoryViewLine))
156         {
157             line = (MemoryViewLine)((TableItem)element).getData();
158         }
159         else
160         {
161             line = (MemoryViewLine)element;
162         }
163             
164         // calculate offset to update
165
IMemoryBlock memory = fViewTab.getMemoryBlock();
166
167         int lineOffset = Integer.valueOf(property, 16).intValue();
168         
169         long offset = getOffset(memory, line.getAddress(), lineOffset);
170         
171         // validate data
172
if (!(value instanceof String JavaDoc))
173             return;
174                     
175         if (!(fViewTab instanceof MemoryViewTab))
176             return;
177         
178         try
179         {
180             byte[] bytes = null;
181             
182             String JavaDoc oldValue = (String JavaDoc)getValue(line, property);
183             
184             if (!oldValue.equals(value))
185             {
186                 // try label provider
187
IBaseLabelProvider labelProvider = ((MemoryViewTab)fViewTab).getTableViewer().getLabelProvider();
188                 if(labelProvider instanceof AbstractTableViewTabLabelProvider)
189                 {
190                     int offsetToLine = Integer.valueOf(property, 16).intValue();
191                     int end = offsetToLine + fViewTab.getColumnSize();
192                     
193                     MemoryByte[] oldArray= line.getBytes(offsetToLine, end);
194                     
195                     BigInteger JavaDoc address = new BigInteger JavaDoc(line.getAddress(), 16);
196                     address = address.add(BigInteger.valueOf(offsetToLine));
197                     
198                     bytes = ((AbstractTableViewTabLabelProvider)labelProvider).getRenderer().getBytes(fViewTab.getRenderingId(), address, oldArray, (String JavaDoc)value);
199                     
200                     if (bytes == null)
201                         return;
202                     
203                     if (bytes.length == 0)
204                         return;
205                 }
206                 else
207                     return;
208             }
209             else
210             {
211                 // return if value has not changed
212
return;
213             }
214         
215         
216             memory.setValue(offset, bytes);
217         }
218         catch (DebugException e)
219         {
220             MemoryViewUtil.openError(DebugUIMessages.getString(TITLE), DebugUIMessages.getString(FAILED), e);
221         }
222         catch(NumberFormatException JavaDoc e)
223         {
224             MemoryViewUtil.openError(DebugUIMessages.getString(TITLE),
225                     DebugUIMessages.getString(FAILED) + "\n" + DebugUIMessages.getString(DATA_IS_INVALID), null); //$NON-NLS-1$
226
}
227         
228     }
229
230     private long getOffset(IMemoryBlock memory, String JavaDoc lineAddress, int lineOffset) {
231         
232         BigInteger JavaDoc lineAddr = new BigInteger JavaDoc(lineAddress, 16);
233         BigInteger JavaDoc memoryAddr;
234         
235         if (memory instanceof IExtendedMemoryBlock)
236         {
237             memoryAddr = ((IExtendedMemoryBlock)memory).getBigBaseAddress();
238         }
239         else
240         {
241             memoryAddr = BigInteger.valueOf(memory.getStartAddress());
242         }
243         
244         if (memoryAddr == null)
245             memoryAddr = new BigInteger JavaDoc("0"); //$NON-NLS-1$
246

247         long offset = lineAddr.subtract(memoryAddr).longValue();
248         
249         return offset + lineOffset;
250     }
251     
252
253
254     /**
255      * @param editActionInvoked The editActionInvoked to set.
256      */

257     public void setEditActionInvoked(boolean editActionInvoked) {
258         this.editActionInvoked = editActionInvoked;
259     }
260
261 }
262
Popular Tags