KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.DebugException;
17 import org.eclipse.debug.core.model.IMemoryBlock;
18 import org.eclipse.debug.core.model.IMemoryBlockExtension;
19 import org.eclipse.debug.core.model.MemoryByte;
20 import org.eclipse.debug.internal.ui.DebugUIMessages;
21 import org.eclipse.debug.internal.ui.DebugUIPlugin;
22 import org.eclipse.debug.internal.ui.views.memory.MemoryViewUtil;
23 import org.eclipse.debug.ui.IDebugUIConstants;
24 import org.eclipse.debug.ui.memory.AbstractTableRendering;
25 import org.eclipse.jface.viewers.ICellModifier;
26 import org.eclipse.swt.widgets.TableItem;
27
28 /**
29  * @since 3.1
30  */

31 public class TableRenderingCellModifier implements ICellModifier {
32
33     private boolean editActionInvoked = false;
34
35     private AbstractTableRendering fRendering;
36
37     public TableRenderingCellModifier(AbstractTableRendering rendering) {
38         fRendering = rendering;
39     }
40
41     /*
42      * (non-Javadoc)
43      *
44      * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object,
45      * java.lang.String)
46      */

47     public boolean canModify(Object JavaDoc element, String JavaDoc property) {
48         boolean canModify = true;
49         try {
50             if (!(element instanceof TableRenderingLine))
51                 return false;
52
53             if (!editActionInvoked)
54                 return false;
55
56             if (fRendering == null)
57                 return false;
58
59             if (fRendering.getMemoryBlock().supportsValueModification() == false) {
60                 return false;
61             }
62
63             TableRenderingLine line = (TableRenderingLine) element;
64             if (TableRenderingLine.P_ADDRESS.equals(property)) {
65                 return false;
66             }
67
68             // property is stored as number of addressable unit away from the
69
// line address
70
// to calculate offset to the memory line array, offset =
71
// numberofAddressableUnit * addressableSize
72
int addressableSize = getAddressableSize();
73
74             int offset = Integer.valueOf(property, 16).intValue() * addressableSize;
75             int end = offset + fRendering.getBytesPerColumn();
76
77             for (int i = offset; i < end; i++) {
78                 MemoryByte oneByte = line.getByte(i);
79
80                 if (!oneByte.isWritable()) {
81                     canModify = false;
82                 }
83             }
84             return canModify;
85         } catch (NumberFormatException JavaDoc e) {
86             canModify = false;
87             return canModify;
88         }
89     }
90
91     /**
92      * @return
93      */

94     private int getAddressableSize() {
95         int addressableSize = fRendering.getAddressableSize();
96         if (addressableSize < 1)
97             addressableSize = 1;
98         return addressableSize;
99     }
100
101     /*
102      * (non-Javadoc)
103      *
104      * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object,
105      * java.lang.String)
106      */

107     public Object JavaDoc getValue(Object JavaDoc element, String JavaDoc property) {
108         // give back the value of the column
109

110         if (!(element instanceof TableRenderingLine))
111             return null;
112
113         TableRenderingLine line = (TableRenderingLine) element;
114         try {
115             if (TableRenderingLine.P_ADDRESS.equals(property))
116                 return line.getAddress();
117
118             int offset = Integer.valueOf(property, 16).intValue() * getAddressableSize();
119             int end = offset + fRendering.getBytesPerColumn();
120
121             // Ask for label provider
122
MemoryByte[] memory = line.getBytes(offset, end);
123
124             if (line.isAvailable(offset, end)) {
125                 // ask the renderer for a string representation of the bytes
126
offset = Integer.valueOf(property, 16).intValue();
127
128                 BigInteger JavaDoc address = new BigInteger JavaDoc(((TableRenderingLine) element).getAddress(), 16);
129                 address = address.add(BigInteger.valueOf(offset));
130
131                 return fRendering.getString(fRendering.getRenderingId(), address, memory);
132             }
133             // if the range is not available, just return padded string
134
return getPaddedString(offset, end);
135         } catch (NumberFormatException JavaDoc e) {
136             return "00"; //$NON-NLS-1$
137
}
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object,
144      * java.lang.String, java.lang.Object)
145      */

146     public void modify(Object JavaDoc element, String JavaDoc property, Object JavaDoc value) {
147         TableRenderingLine line = null;
148         if (!(element instanceof TableRenderingLine)) {
149             line = (TableRenderingLine) ((TableItem) element).getData();
150         } else {
151             line = (TableRenderingLine) element;
152         }
153
154         // validate data
155
if (!(value instanceof String JavaDoc))
156             return;
157
158         try {
159             // calculate offset to update
160
IMemoryBlock memoryBlk = fRendering.getMemoryBlock();
161
162             int lineOffset = Integer.valueOf(property, 16).intValue();
163
164             // this offset is number of addressable unit from the line address
165
BigInteger JavaDoc offset = getOffset(memoryBlk, line.getAddress(), lineOffset);
166
167             byte[] bytes = null;
168
169             String JavaDoc oldValue = (String JavaDoc) getValue(line, property);
170
171             if (!oldValue.equals(value)) {
172
173                 // property is number of addressable unit from line address
174
// to calculate proper offset in the memoryViewLine's array
175
// offset = numberOfAddressableUnit * addressableSize
176
int offsetToLine = Integer.valueOf(property, 16).intValue() * getAddressableSize();
177                 int end = offsetToLine + fRendering.getBytesPerColumn();
178
179                 MemoryByte[] oldArray = line.getBytes(offsetToLine, end);
180
181                 BigInteger JavaDoc address = new BigInteger JavaDoc(line.getAddress(), 16);
182                 address = address.add(BigInteger.valueOf(offsetToLine));
183
184                 bytes = fRendering.getBytes(fRendering.getRenderingId(), address, oldArray, (String JavaDoc) value);
185
186                 if (bytes == null)
187                     return;
188
189                 if (bytes.length == 0)
190                     return;
191
192                 if (bytes.length <= oldArray.length) {
193                     boolean changed = false;
194                     // check that the bytes returned has actually changed
195
for (int i = 0; i < bytes.length; i++) {
196                         if (bytes[i] != oldArray[i].getValue()) {
197                             changed = true;
198                             break;
199                         }
200                     }
201                     if (!changed)
202                         return;
203                 }
204             } else {
205                 // return if value has not changed
206
return;
207             }
208
209             if (memoryBlk instanceof IMemoryBlockExtension)
210                 ((IMemoryBlockExtension) memoryBlk).setValue(offset, bytes);
211             else
212                 memoryBlk.setValue(offset.longValue(), bytes);
213         } catch (DebugException e) {
214             MemoryViewUtil.openError(DebugUIMessages.MemoryViewCellModifier_failure_title, DebugUIMessages.MemoryViewCellModifier_failed, e);
215         } catch (NumberFormatException JavaDoc e) {
216             MemoryViewUtil.openError(DebugUIMessages.MemoryViewCellModifier_failure_title, DebugUIMessages.MemoryViewCellModifier_failed + "\n" + DebugUIMessages.MemoryViewCellModifier_data_is_invalid, null); //$NON-NLS-1$
217
}
218
219     }
220
221     private BigInteger JavaDoc getOffset(IMemoryBlock memory, String JavaDoc lineAddress, int lineOffset) throws DebugException {
222
223         BigInteger JavaDoc lineAddr = new BigInteger JavaDoc(lineAddress, 16);
224         BigInteger JavaDoc memoryAddr;
225
226         if (memory instanceof IMemoryBlockExtension) {
227             memoryAddr = ((IMemoryBlockExtension) memory).getBigBaseAddress();
228         } else {
229             memoryAddr = BigInteger.valueOf(memory.getStartAddress());
230         }
231
232         if (memoryAddr == null)
233             memoryAddr = new BigInteger JavaDoc("0"); //$NON-NLS-1$
234

235         return lineAddr.subtract(memoryAddr).add(BigInteger.valueOf(lineOffset));
236     }
237
238     /**
239      * @param editActionInvoked
240      * The editActionInvoked to set.
241      */

242     public void setEditActionInvoked(boolean editActionInvoked) {
243         this.editActionInvoked = editActionInvoked;
244     }
245
246     /**
247      * @param start
248      * @param end
249      * @return padded string
250      */

251     public String JavaDoc getPaddedString(int start, int end) {
252         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
253         String JavaDoc paddedStr = DebugUIPlugin.getDefault().getPreferenceStore().getString(IDebugUIConstants.PREF_PADDED_STR);
254         for (int i = start; i < end; i++) {
255             buf.append(paddedStr);
256         }
257         return buf.toString();
258     }
259
260 }
261
Popular Tags