1 11 12 package org.eclipse.debug.internal.ui.views.memory.renderings; 13 14 import java.math.BigInteger ; 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 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 47 public boolean canModify(Object element, String 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 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 e) { 86 canModify = false; 87 return canModify; 88 } 89 } 90 91 94 private int getAddressableSize() { 95 int addressableSize = fRendering.getAddressableSize(); 96 if (addressableSize < 1) 97 addressableSize = 1; 98 return addressableSize; 99 } 100 101 107 public Object getValue(Object element, String property) { 108 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 MemoryByte[] memory = line.getBytes(offset, end); 123 124 if (line.isAvailable(offset, end)) { 125 offset = Integer.valueOf(property, 16).intValue(); 127 128 BigInteger address = new BigInteger (((TableRenderingLine) element).getAddress(), 16); 129 address = address.add(BigInteger.valueOf(offset)); 130 131 return fRendering.getString(fRendering.getRenderingId(), address, memory); 132 } 133 return getPaddedString(offset, end); 135 } catch (NumberFormatException e) { 136 return "00"; } 138 } 139 140 146 public void modify(Object element, String property, Object 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 if (!(value instanceof String )) 156 return; 157 158 try { 159 IMemoryBlock memoryBlk = fRendering.getMemoryBlock(); 161 162 int lineOffset = Integer.valueOf(property, 16).intValue(); 163 164 BigInteger offset = getOffset(memoryBlk, line.getAddress(), lineOffset); 166 167 byte[] bytes = null; 168 169 String oldValue = (String ) getValue(line, property); 170 171 if (!oldValue.equals(value)) { 172 173 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 address = new BigInteger (line.getAddress(), 16); 182 address = address.add(BigInteger.valueOf(offsetToLine)); 183 184 bytes = fRendering.getBytes(fRendering.getRenderingId(), address, oldArray, (String ) 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 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; 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 e) { 216 MemoryViewUtil.openError(DebugUIMessages.MemoryViewCellModifier_failure_title, DebugUIMessages.MemoryViewCellModifier_failed + "\n" + DebugUIMessages.MemoryViewCellModifier_data_is_invalid, null); } 218 219 } 220 221 private BigInteger getOffset(IMemoryBlock memory, String lineAddress, int lineOffset) throws DebugException { 222 223 BigInteger lineAddr = new BigInteger (lineAddress, 16); 224 BigInteger 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 ("0"); 235 return lineAddr.subtract(memoryAddr).add(BigInteger.valueOf(lineOffset)); 236 } 237 238 242 public void setEditActionInvoked(boolean editActionInvoked) { 243 this.editActionInvoked = editActionInvoked; 244 } 245 246 251 public String getPaddedString(int start, int end) { 252 StringBuffer buf = new StringBuffer (); 253 String 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 |