Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 625c6562c4b5df8ffa5b30e791ab0e0253f90ba7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
/*******************************************************************************
 * Copyright (c) 2006, 2014 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Ted R Williams (Wind River Systems, Inc.) - initial implementation
 *     Randy Rohrbach (Wind River Systems, Inc.) - Copied and modified to create the floating point plugin
 *******************************************************************************/

package org.eclipse.cdt.debug.ui.memory.floatingpoint;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import org.eclipse.cdt.debug.ui.memory.floatingpoint.FPutilities.FPDataType;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IDebugEventSetListener;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.core.model.IMemoryBlockExtension;
import org.eclipse.debug.core.model.MemoryByte;
import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
import org.eclipse.debug.internal.ui.views.memory.MemoryViewUtil;
import org.eclipse.debug.internal.ui.views.memory.renderings.GoToAddressComposite;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.UIJob;


@SuppressWarnings("restriction")
public class Rendering extends Composite implements IDebugEventSetListener
{
    // The IMemoryRendering parent

    private FPRendering fParent;

    // Controls

    protected FPAddressPane fAddressPane;
    protected FPDataPane fDataPane;
    private GoToAddressComposite fAddressBar;
    protected Control fAddressBarControl;
    private Selection fSelection = new Selection();

    // Internal management

    BigInteger fViewportAddress = null;             // Default visibility for performance
    BigInteger fMemoryBlockStartAddress = null;     // Starting address
    BigInteger fMemoryBlockEndAddress = null;       // Ending address
    protected BigInteger fBaseAddress = null;       // Base address
    protected int fColumnCount = 0;                 // Auto calculate can be disabled by user, making this user settable
    protected int fBytesPerRow = 0;                 // Number of bytes per row are displayed
    int fScrollSelection = 0;               // Scroll selection
    private BigInteger fCaretAddress = null;        // Caret/cursor position
    private boolean fCellEditState = false;         // Cell editing mode:  'true' = currently editing a cell; 'false' = not editing a cell
    private BigInteger cellEditAddress = null;      // The address of the cell currently being edited
    private BigInteger memoryAddress = null;        // The memory address associated with the cell that currently being edited
    private StringBuffer fEditBuffer = null;        // Character buffer used during editing
    static boolean initialDisplayModeSet = false;   // Initial display mode been set to the same endianness as the target

    // Constants used to identify the panes

    public final static int PANE_ADDRESS = 1;
    public final static int PANE_DATA = 2;

    // Decimal precision used when converting between scroll units and number of memory
    // rows. Calculations do not need to be exact; two decimal places is good enough.

    static private final MathContext SCROLL_CONVERSION_PRECISION = new MathContext(2);

    // Constants used to identify text, maybe java should be queried for all available sets

    public final static int TEXT_ISO_8859_1 = 1;
    public final static int TEXT_USASCII = 2;
    public final static int TEXT_UTF8 = 3;
    protected final static int TEXT_UTF16 = 4;

    // Internal constants

    public final static int COLUMNS_AUTO_SIZE_TO_FIT = 0;

    // View internal settings

    private int fCellPadding =  2;
    private int fPaneSpacing = 16;
    private String fPaddingString = " "; //$NON-NLS-1$

    // Flag whether the memory cache is dirty

    private boolean fCacheDirty = false;

    // Update modes

    public final static int UPDATE_ALWAYS = 1;
    public final static int UPDATE_ON_BREAKPOINT = 2;
    public final static int UPDATE_MANUAL = 3;
    public int fUpdateMode = UPDATE_ALWAYS;

    // Constants for cell-width calculations

    private static final int DECIMAL_POINT_SIZE = 1;
    private static final int SIGN_SIZE = 1;
    private static final int EXPONENT_CHARACTER_SIZE = 1;
    private static final int EXPONENT_VALUE_SIZE = 3;

    // User settings

    private FPDataType fFPDataType = FPDataType.FLOAT;          // Default to float data type
    private int fDisplayedPrecision = 8;                        // The default number of digits of displayed precision
    private int fCharsPerColumn = charsPerColumn();             // Figure out the initial cell-width size
    private int fColumnsSetting = COLUMNS_AUTO_SIZE_TO_FIT;     // Default column setting
    private boolean fIsTargetLittleEndian  = true;              // Default target endian setting
    private boolean fIsDisplayLittleEndian = true;              // Default display endian setting
    private boolean fEditInserMode = false;                     // Insert mode:  true = replace existing number, false = overstrike

    // Constructors

    public Rendering(Composite parent, FPRendering renderingParent)
    {
        super(parent, SWT.DOUBLE_BUFFERED | SWT.NO_BACKGROUND | SWT.H_SCROLL | SWT.V_SCROLL);
        this.setFont(JFaceResources.getFont(IInternalDebugUIConstants.FONT_NAME)); // TODO: internal?
        this.fParent = renderingParent;

        // Initialize the viewport start

        if (fParent.getMemoryBlock() != null)
        {
        	// This is base address from user.
        	// Honor it if the block has no limits or the base is within the block limits.
        	// Fix Bug 414519.  
        	BigInteger base = fParent.getBigBaseAddress();

            fViewportAddress = fParent.getMemoryBlockStartAddress();

            // The viewport address will be null if memory may be retrieved at any
            // address less than this memory block's base.  If so use the base address.
            if (fViewportAddress == null)
                fViewportAddress = base;
            else {
            	BigInteger blockEndAddr = fParent.getMemoryBlockEndAddress();
            	if (base.compareTo(fViewportAddress) > 0) {
            		if (blockEndAddr == null || base.compareTo(blockEndAddr) < 0)
            			fViewportAddress = base;
            	}
            }

            fBaseAddress = fViewportAddress;
        }

        // Instantiate the panes, TODO default visibility from state or plugin.xml?

        this.fAddressPane = createAddressPane();
        this.fDataPane = createDataPane();

        fAddressBar = new GoToAddressComposite();
        fAddressBarControl = fAddressBar.createControl(parent);
        Button button = fAddressBar.getButton(IDialogConstants.OK_ID);

        if (button != null)
        {
            button.addSelectionListener(new SelectionAdapter()
            {

                @Override
                public void widgetSelected(SelectionEvent e)
                {
                    doGoToAddress();
                }
            });

            button = fAddressBar.getButton(IDialogConstants.CANCEL_ID);
            if (button != null)
            {
                button.addSelectionListener(new SelectionAdapter()
                {
                    @Override
                    public void widgetSelected(SelectionEvent e)
                    {
                        setVisibleAddressBar(false);
                    }
                });
            }
        }

        fAddressBar.getExpressionWidget().addSelectionListener(new SelectionAdapter()
        {
            @Override
            public void widgetDefaultSelected(SelectionEvent e)
            {
                doGoToAddress();
            }
        });

        fAddressBar.getExpressionWidget().addKeyListener(new KeyAdapter()
        {
            @Override
            public void keyPressed(KeyEvent e)
            {
                if (e.keyCode == SWT.ESC)
                    setVisibleAddressBar(false);
                super.keyPressed(e);
            }
        });

        this.fAddressBarControl.setVisible(false);

        getHorizontalBar().addSelectionListener(createHorizontalBarSelectionListener());
        getVerticalBar().addSelectionListener(createVerticalBarSelectinListener());

        this.addPaintListener(new PaintListener()
        {
            @Override
            public void paintControl(PaintEvent pe)
            {
                pe.gc.setBackground(Rendering.this.getFPRendering().getColorBackground());
                pe.gc.fillRectangle(0, 0, Rendering.this.getBounds().width, Rendering.this.getBounds().height);
            }
        });

        setLayout();

        this.addControlListener(new ControlListener()
        {
            @Override
            public void controlMoved(ControlEvent ce)
            {
            }

            @Override
            public void controlResized(ControlEvent ce)
            {
                packColumns();
            }
        });

        DebugPlugin.getDefault().addDebugEventListener(this);
    }

    // Determine how many characters are allowed in the column

    private int charsPerColumn()
    {
        return fDisplayedPrecision + DECIMAL_POINT_SIZE + SIGN_SIZE + EXPONENT_CHARACTER_SIZE + EXPONENT_VALUE_SIZE + fCellPadding;
    }

    // Establish the visible layout of the view

    protected void setLayout()
    {
        this.setLayout(new Layout()
        {
            @Override
            public void layout(Composite composite, boolean changed)
            {
                int xOffset = 0;

                if (Rendering.this.getHorizontalBar().isVisible())
                    xOffset = Rendering.this.getHorizontalBar().getSelection();

                int x = xOffset * -1;
                int y = 0;

                if (fAddressBarControl.isVisible())
                {
                    fAddressBarControl.setBounds(0, 0, Rendering.this.getBounds().width, fAddressBarControl.computeSize(100, 30).y); // FIXME
                    // y = fAddressBarControl.getBounds().height;
                }

                if (fAddressPane.isPaneVisible())
                {
                    fAddressPane.setBounds(x, y, fAddressPane.computeSize(0, 0).x, Rendering.this.getBounds().height - y);
                    x = fAddressPane.getBounds().x + fAddressPane.getBounds().width;
                }

                if (fDataPane.isPaneVisible())
                {
                    fDataPane.setBounds(x, y, fDataPane.computeSize(0, 0).x, Rendering.this.getBounds().height - y);
                    x = fDataPane.getBounds().x + fDataPane.getBounds().width;
                }

                ScrollBar horizontal = Rendering.this.getHorizontalBar();

                horizontal.setVisible(true);
                horizontal.setMinimum(0);
                horizontal.setMaximum(fDataPane.getBounds().x + fDataPane.getBounds().width + xOffset);
                @SuppressWarnings("unused")
                int temp = horizontal.getMaximum();
                horizontal.setThumb(getClientArea().width);
                horizontal.setPageIncrement(40);    // TODO ?
                horizontal.setIncrement(20);        // TODO ?
            }

            @Override
            protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache)
            {
                return new Point(100, 100); // dummy data
            }
        });
    }

    // Handles for the caret/cursor movement keys

    protected void handleDownArrow()
    {
        fViewportAddress = fViewportAddress.add(BigInteger.valueOf(getAddressableCellsPerRow()));
        ensureViewportAddressDisplayable();
        redrawPanes();
    }

    protected void handleUpArrow()
    {
        fViewportAddress = fViewportAddress.subtract(BigInteger.valueOf(getAddressableCellsPerRow()));
        ensureViewportAddressDisplayable();
        redrawPanes();
    }

    protected void handlePageDown()
    {
        fViewportAddress = fViewportAddress.add(BigInteger.valueOf(getAddressableCellsPerRow() * (Rendering.this.getRowCount() - 1)));
        ensureViewportAddressDisplayable();
        redrawPanes();
    }

    protected void handlePageUp()
    {
        fViewportAddress = fViewportAddress.subtract(BigInteger.valueOf(getAddressableCellsPerRow() * (Rendering.this.getRowCount() - 1)));
        ensureViewportAddressDisplayable();
        redrawPanes();
    }

    protected SelectionListener createHorizontalBarSelectionListener()
    {
        return new SelectionListener()
        {
            @Override
            public void widgetSelected(SelectionEvent se)
            {
                Rendering.this.layout();
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent se)
            {
                // Do nothing
            }
        };
    }

    protected SelectionListener createVerticalBarSelectinListener()
    {
        return new SelectionListener()
        {
            @Override
            public void widgetSelected(SelectionEvent se)
            {
                switch (se.detail)
                {
                    case SWT.ARROW_DOWN:
                        handleDownArrow();
                        break;

                    case SWT.PAGE_DOWN:
                        handlePageDown();
                        break;

                    case SWT.ARROW_UP:
                        handleUpArrow();
                        break;

                    case SWT.PAGE_UP:
                        handlePageUp();
                        break;

                    case SWT.SCROLL_LINE:
                        // See: BUG 203068 selection event details broken on GTK < 2.6

                    default:
                    {
                        if (getVerticalBar().getSelection() == getVerticalBar().getMinimum())
                        {
                            // Set view port start address to the start address of the Memory Block
                            fViewportAddress = Rendering.this.getMemoryBlockStartAddress();
                        }
                        else if (getVerticalBar().getSelection() == getVerticalBar().getMaximum())
                        {
                            // The view port end address should be less or equal to the the end address of the Memory Block
                            // Set view port address to be bigger than the end address of the Memory Block for now
                            // and let ensureViewportAddressDisplayable() to figure out the correct view port start address
                            fViewportAddress = Rendering.this.getMemoryBlockEndAddress();
                        }
                        else
                        {
                            // Figure out the delta, ignore events with no delta
                            int deltaScroll = getVerticalBar().getSelection() - fScrollSelection;
                            if (deltaScroll == 0) break;
                            BigInteger deltaRows = scrollbar2rows(deltaScroll);
                            BigInteger newAddress = fViewportAddress.add(BigInteger.valueOf(getAddressableCellsPerRow()).multiply(deltaRows));
                            fViewportAddress = newAddress;
                        }

                        ensureViewportAddressDisplayable();

                        // Update tooltip; FIXME conversion from slider to scrollbar
                        // getVerticalBar().setToolTipText(Rendering.this.getAddressString(fViewportAddress));

                        // Update the addresses in the Address pane.

                        if (fAddressPane.isPaneVisible())
                            fAddressPane.redraw();

                        redrawPanes();

                        break;
                    }
                }
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent se)
            {
                // do nothing
            }
        };
    }

    protected FPAddressPane createAddressPane()
    {
        return new FPAddressPane(this);
    }

    protected FPDataPane createDataPane()
    {
        return new FPDataPane(this);
    }

    public FPRendering getFPRendering() // TODO rename
    {
        return fParent;
    }

    protected void setCaretAddress(BigInteger address)
    {
        fCaretAddress = address;
    }

    protected BigInteger getCaretAddress()
    {
        // Return the caret address if it has been set.  Otherwise return the viewport address.
        // When the rendering is first created, the caret is unset until the user clicks somewhere
        // in the rendering. It also reset (unset) when the user gives us a new viewport address

        return (fCaretAddress != null) ? fCaretAddress : fViewportAddress;
    }

    void doGoToAddress()
    {
        try
        {
            BigInteger address = fAddressBar.getGoToAddress(this.getMemoryBlockStartAddress(), this.getCaretAddress());
            getFPRendering().gotoAddress(address);
            setVisibleAddressBar(false);
        }
        catch (NumberFormatException e1)
        {
            // FIXME log?
        }
    }

    // Ensure that all addresses displayed are within the addressable range
    protected void ensureViewportAddressDisplayable()
    {
        if (fViewportAddress.compareTo(Rendering.this.getMemoryBlockStartAddress()) < 0)
        {
            fViewportAddress = Rendering.this.getMemoryBlockStartAddress();
        }
        else if (getViewportEndAddress().compareTo(getMemoryBlockEndAddress().add(BigInteger.ONE)) > 0)
        {
            fViewportAddress = getMemoryBlockEndAddress().subtract(BigInteger.valueOf(getAddressableCellsPerRow() * getRowCount() - 1));
        }

        setScrollSelection();
    }

    public FPIMemorySelection getSelection()
    {
        return fSelection;
    }

    protected int getHistoryDepth()
    {
        return fViewportCache.getHistoryDepth();
    }

    protected void setHistoryDepth(int depth)
    {
        fViewportCache.setHistoryDepth(depth);
    }

    public void logError(String message, Exception e)
    {
        Status status = new Status(IStatus.ERROR, fParent.getRenderingId(), DebugException.INTERNAL_ERROR, message, e);
        FPRenderingPlugin.getDefault().getLog().log(status);
    }

    public void handleFontPreferenceChange(Font font)
    {
        setFont(font);

        Control controls[] = this.getRenderingPanes();
        for (int index = 0; index < controls.length; index++)
            controls[index].setFont(font);

        packColumns();
        layout(true);
    }

    public void setPaddingString(String padding)
    {
        fPaddingString = padding;
        refresh();
    }

    public char getPaddingCharacter()
    {
        return fPaddingString.charAt(0); // use only the first character
    }

    static int suspendCount = 0;

    @Override
    public void handleDebugEvents(DebugEvent[] events)
    {
        if (this.isDisposed()) return;

        boolean isChangeOnly = false;
        boolean isSuspend = false;
        boolean isBreakpointHit = false;

        for (int index = 0; index < events.length; index++)
        {
            if (events[0].getSource() instanceof IDebugElement)
            {
                final int kind = events[index].getKind();
                final int detail = events[index].getDetail();
                final IDebugElement source = (IDebugElement) events[index].getSource();

                /*
                 * We have to make sure we are comparing memory blocks here. It pretty much is now the
                 * case that the IDebugTarget is always null.  Almost no one in the Embedded Space  is
                 * using anything but CDT/DSF or CDT/TCF at this point. The older CDI stuff will still
                 * be using the old Debug Model API. But this will generate the same memory block  and
                 * a legitimate IDebugTarget which will match properly.
                 */
                if( source.equals( getMemoryBlock() ) && source.getDebugTarget() == getMemoryBlock().getDebugTarget() )
                {
                    if ((detail & DebugEvent.BREAKPOINT) != 0) isBreakpointHit = true;

                    if (kind == DebugEvent.SUSPEND)
                    {
                        handleSuspendEvent(detail);
                        isSuspend = true;
                    }
                    else if (kind == DebugEvent.CHANGE)
                    {
                        handleChangeEvent();
                        isChangeOnly = true;
                    }
                }
            }
        }

        if (isSuspend)
            handleSuspend(isBreakpointHit);
        else if (isChangeOnly)
            handleChange();
    }

    protected void handleSuspend(boolean isBreakpointHit)
    {
        if (getUpdateMode() == UPDATE_ALWAYS || (getUpdateMode() == UPDATE_ON_BREAKPOINT && isBreakpointHit))
        {
            Display.getDefault().asyncExec(new Runnable()
            {
                @Override
                public void run()
                {
                    archiveDeltas();
                    refresh();
                }
            });
        }
    }

    protected void handleChange()
    {
        if (getUpdateMode() == UPDATE_ALWAYS)
        {
            Display.getDefault().asyncExec(new Runnable()
            {
                @Override
                public void run()
                {
                    refresh();
                }
            });
        }
    }

    protected void handleSuspendEvent(int detail)
    {
    }

    protected void handleChangeEvent()
    {
    }

    // Return true to enable development debug print statements

    public boolean isDebug()
    {
        return false;
    }

    protected IMemoryBlockExtension getMemoryBlock()
    {
        IMemoryBlock block = fParent.getMemoryBlock();
        if (block != null)
            return (IMemoryBlockExtension) block.getAdapter(IMemoryBlockExtension.class);

        return null;
    }

    public BigInteger getBigBaseAddress()
    {
        return fParent.getBigBaseAddress();
    }

    public int getAddressableSize()
    {
        return fParent.getAddressableSize();
    }

    protected FPIViewportCache getViewportCache()
    {
        return fViewportCache;
    }

    public FPMemoryByte[] getBytes(BigInteger address, int bytes) throws DebugException
    {
        return getViewportCache().getBytes(address, bytes);
    }

    // Default visibility for performance

    ViewportCache fViewportCache = new ViewportCache();

    private interface Request
    {
    }

    class ViewportCache extends Thread implements FPIViewportCache
    {
        class ArchiveDeltas implements Request
        {
        }

        class AddressPair implements Request
        {
            BigInteger startAddress;
            BigInteger endAddress;

            public AddressPair(BigInteger start, BigInteger end)
            {
                startAddress = start;
                endAddress = end;
            }

            @Override
            public boolean equals(Object obj)
            {
                if (obj == null)
                    return false;
                if (obj instanceof AddressPair)
                {
                    return ((AddressPair) obj).startAddress.equals(startAddress) && ((AddressPair) obj).endAddress.equals(endAddress);
                }

                return false;
            }

			@Override
			public int hashCode() {
				return super.hashCode() + startAddress.hashCode() + endAddress.hashCode();
			}

        }

        class MemoryUnit implements Cloneable
        {
            BigInteger start;

            BigInteger end;

            FPMemoryByte[] bytes;

            @Override
            public MemoryUnit clone()
            {
                MemoryUnit b = new MemoryUnit();

                b.start = this.start;
                b.end = this.end;
                b.bytes = new FPMemoryByte[this.bytes.length];
                for (int index = 0; index < this.bytes.length; index++)
                    b.bytes[index] = new FPMemoryByte(this.bytes[index].getValue());

                return b;
            }

            public boolean isValid()
            {
                return this.start != null && this.end != null && this.bytes != null;
            }
        }

        @SuppressWarnings("hiding")
        private HashMap<BigInteger, FPMemoryByte[]> fEditBuffer = new HashMap<>();
        private boolean fDisposed = false;
        private Object fLastQueued = null;
        private Vector<Object> fQueue = new Vector<>();
        protected MemoryUnit fCache = null;
        protected MemoryUnit fHistoryCache[] = new MemoryUnit[0];
        protected int fHistoryDepth = 0;

        public ViewportCache()
        {
            start();
        }

        @Override
        public void dispose()
        {
            fDisposed = true;
            synchronized (fQueue)
            {
                fQueue.notify();
            }
        }

        public int getHistoryDepth()
        {
            return fHistoryDepth;
        }

        public void setHistoryDepth(int depth)
        {
            fHistoryDepth = depth;
            fHistoryCache = new MemoryUnit[fHistoryDepth];
        }

        @Override
        public void refresh()
        {
            assert Thread.currentThread().equals(Display.getDefault().getThread()) : FPRenderingMessages.getString("CALLED_ON_NON_DISPATCH_THREAD"); //$NON-NLS-1$

            if (fCache != null)
            {
                queueRequest(fViewportAddress, getViewportEndAddress());
            }
        }

        @Override
        public void archiveDeltas()
        {
            assert Thread.currentThread().equals(Display.getDefault().getThread()) : FPRenderingMessages.getString("CALLED_ON_NON_DISPATCH_THREAD"); //$NON-NLS-1$

            if (fCache != null)
            {
                queueRequestArchiveDeltas();
            }
        }

        private void queueRequest(BigInteger startAddress, BigInteger endAddress)
        {
            AddressPair pair = new AddressPair(startAddress, endAddress);
            queue(pair);
        }

        private void queueRequestArchiveDeltas()
        {
            ArchiveDeltas archive = new ArchiveDeltas();
            queue(archive);
        }

        private void queue(Object element)
        {
            synchronized (fQueue)
            {
                if (!(fQueue.size() > 0 && element.equals(fLastQueued)))
                {
                    fQueue.addElement(element);
                    fLastQueued = element;
                }
                fQueue.notify();
            }
        }

        @Override
        public void run()
        {
            while (!fDisposed)
            {
                AddressPair pair = null;
                boolean archiveDeltas = false;
                synchronized (fQueue)
                {
                    if (fQueue.size() > 0)
                    {
                        Request request = (Request) fQueue.elementAt(0);
                        Class<?> type = request.getClass();

                        while (fQueue.size() > 0 && type.isInstance(fQueue.elementAt(0)))
                        {
                            request = (Request) fQueue.elementAt(0);
                            fQueue.removeElementAt(0);
                        }

                        if (request instanceof ArchiveDeltas)
                            archiveDeltas = true;
                        else if (request instanceof AddressPair)
                            pair = (AddressPair) request;
                    }
                }

                if (archiveDeltas)
                {
                    for (int i = fViewportCache.getHistoryDepth() - 1; i > 0; i--)
                        fHistoryCache[i] = fHistoryCache[i - 1];

                    fHistoryCache[0] = fCache.clone();
                }
                else if (pair != null)
                {
                    populateCache(pair.startAddress, pair.endAddress);
                }
                else
                {
                    synchronized (fQueue)
                    {
                        try
                        {
                            if (fQueue.isEmpty())
                            {
                                fQueue.wait();
                            }
                        } catch (Exception e)
                        {
                            // do nothing
                        }
                    }
                }
            }
        }

        // Cache memory necessary to paint viewport
        // TODO: user setting to buffer +/- x lines
        // TODO: reuse existing cache? probably only a minor performance gain

        private void populateCache(final BigInteger startAddress, final BigInteger endAddress)
        {
            try
            {
                IMemoryBlockExtension memoryBlock = getMemoryBlock();

                BigInteger lengthInBytes = endAddress.subtract(startAddress);
                BigInteger addressableSize = BigInteger.valueOf(getAddressableSize());

                long units = lengthInBytes.divide(addressableSize)
                        .add(lengthInBytes.mod(addressableSize).compareTo(BigInteger.ZERO) > 0 ? BigInteger.ONE : BigInteger.ZERO).longValue();

                // CDT (and maybe other backends) will call setValue() on these MemoryBlock objects.  We
                // don't want this to happen, because it interferes with this rendering's own change history.
                // Ideally, we should strictly use the back end change notification and history, but it is
                // only guaranteed to work for bytes within the address range of the MemoryBlock.

                MemoryByte readBytes[] = memoryBlock.getBytesFromAddress(startAddress, units);
                FPMemoryByte cachedBytes[] = new FPMemoryByte[readBytes.length];

                for (int index = 0; index < readBytes.length; index++)
                    cachedBytes[index] = new FPMemoryByte(readBytes[index].getValue(), readBytes[index].getFlags());

                // Derive the target endian from the read MemoryBytes.

                if (cachedBytes.length > 0)
                    if (cachedBytes[0].isEndianessKnown())
                        setTargetLittleEndian(!cachedBytes[0].isBigEndian());

                // The first time we execute this method, set the display endianness to the target endianness.

                if (!initialDisplayModeSet)
                {
                    setDisplayLittleEndian(isTargetLittleEndian());
                    initialDisplayModeSet = true;
                }

                // Re-order bytes within unit to be a sequential byte stream if the endian is already little

                if (isTargetLittleEndian())
                {
                    // There isn't an order when the unit size is one, so skip for performance

                    if (addressableSize.compareTo(BigInteger.ONE) != 0)
                    {
                        int unitSize = addressableSize.intValue();
                        FPMemoryByte cachedBytesAsByteSequence[] = new FPMemoryByte[cachedBytes.length];
                        for (int unit = 0; unit < units; unit++)
                        {
                            for (int unitbyte = 0; unitbyte < unitSize; unitbyte++)
                            {
                                cachedBytesAsByteSequence[unit * unitSize + unitbyte] = cachedBytes[unit * unitSize + unitSize - unitbyte];
                            }
                        }
                        cachedBytes = cachedBytesAsByteSequence;
                    }
                }

                final FPMemoryByte[] cachedBytesFinal = cachedBytes;

                fCache = new MemoryUnit();
                fCache.start = startAddress;
                fCache.end = endAddress;
                fCache.bytes = cachedBytesFinal;

                Display.getDefault().asyncExec(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        // Generate deltas

                        for (int historyIndex = 0; historyIndex < getHistoryDepth(); historyIndex++)
                        {
                            if (fHistoryCache[historyIndex] != null && fHistoryCache[historyIndex].isValid())
                            {
                                BigInteger maxStart = startAddress.max(fHistoryCache[historyIndex].start);
                                BigInteger minEnd = endAddress.min(fHistoryCache[historyIndex].end).subtract(BigInteger.ONE);

                                BigInteger overlapLength = minEnd.subtract(maxStart);
                                if (overlapLength.compareTo(BigInteger.valueOf(0)) > 0)
                                {
                                    // there is overlap

                                    int offsetIntoOld = maxStart.subtract(fHistoryCache[historyIndex].start).intValue();
                                    int offsetIntoNew = maxStart.subtract(startAddress).intValue();

                                    for (int i = overlapLength.intValue(); i >= 0; i--)
                                    {
                                        cachedBytesFinal[offsetIntoNew + i].setChanged(historyIndex,
                                                cachedBytesFinal[offsetIntoNew + i].getValue() != fHistoryCache[historyIndex].bytes[offsetIntoOld + i]
                                                        .getValue());
                                    }

                                    // There are several scenarios where the history cache must be updated from the data cache, so that when a
                                    // cell is edited the font color changes appropriately. The following code deals with the different cases.

                                    if (historyIndex != 0) continue;

                                    int dataStart     = fCache.start.intValue();
                                    int dataEnd       = fCache.end.intValue();
                                    int dataLength    = fCache.bytes.length;

                                    int historyStart  = fHistoryCache[0].start.intValue();
                                    int historyEnd    = fHistoryCache[0].end.intValue();
                                    int historyLength = fHistoryCache[0].bytes.length;

                                    // Case 1: The data cache is smaller than the history cache; the data cache's
                                    //         address range is fully covered by the history cache.  Do nothing.

                                    if ((dataStart >= historyStart) && (dataEnd <= historyEnd))
                                        continue;

                                    // Case 2: The data and history cache's do not overlap at all

                                    if (((dataStart < historyStart) && (dataEnd < historyStart)) || (dataStart > historyEnd))
                                    {
                                        // Create a new history cache: Copy the data cache bytes to the history cache

                                        MemoryUnit newHistoryCache = new MemoryUnit();

                                        newHistoryCache.start   = fCache.start;
                                        newHistoryCache.end     = fCache.end;
                                        int newHistoryCacheSize = fCache.bytes.length;
                                        newHistoryCache.bytes   = new FPMemoryByte[newHistoryCacheSize];

                                        for (int index = 0; index < newHistoryCacheSize; index++)
                                            newHistoryCache.bytes[index] = new FPMemoryByte(fCache.bytes[index].getValue());

                                        fHistoryCache[0] = newHistoryCache;

                                        continue;
                                    }

                                    // Case 3: The data cache starts at a lower address than the history cache, but overlaps the history cache

                                    if ((dataStart < historyStart) && ((dataEnd >= historyStart) && (dataEnd <= historyEnd)))
                                    {
                                        // Create a new history cache with the missing data from the main cache and append the old history to it.

                                        int missingDataByteCount = historyStart - dataStart;
                                        int historyCacheSize     = historyLength;
                                        int newHistoryCacheSize  = missingDataByteCount + historyLength;

                                        if (missingDataByteCount <= 0 && historyCacheSize <= 0) break;

                                        MemoryUnit newHistoryCache = new MemoryUnit();

                                        newHistoryCache.start = fCache.start;
                                        newHistoryCache.end   = fHistoryCache[0].end;
                                        newHistoryCache.bytes = new FPMemoryByte[newHistoryCacheSize];

                                        // Copy the missing bytes from the beginning of the main cache to the history cache.

                                        for (int index = 0; index < missingDataByteCount; index++)
                                            newHistoryCache.bytes[index] = new FPMemoryByte(fCache.bytes[index].getValue());

                                        // Copy the remaining bytes from the old history cache to the new history cache

                                        for (int index = 0; index < historyCacheSize; index++)
                                            newHistoryCache.bytes[index + missingDataByteCount] =
                                                new FPMemoryByte(fHistoryCache[0].bytes[index].getValue());

                                        fHistoryCache[0] = newHistoryCache;

                                        continue;
                                    }

                                    // Case 4: The data cache starts at a higher address than the history cache

                                    if (((dataStart >= historyStart) && (dataStart <= historyEnd)) && (dataEnd > historyEnd))
                                    {
                                        // Append the missing main cache bytes to the history cache.

                                        int missingDataByteCount = dataEnd - historyEnd;
                                        int historyCacheSize     = historyEnd - historyStart;
                                        int newHistoryCacheSize  = missingDataByteCount + historyLength;

                                        if (missingDataByteCount > 0 && historyCacheSize > 0)
                                        {
                                            MemoryUnit newHistoryCache = new MemoryUnit();

                                            newHistoryCache.start = fHistoryCache[0].start;
                                            newHistoryCache.end   = fCache.end;
                                            newHistoryCache.bytes = new FPMemoryByte[newHistoryCacheSize];

                                            // Copy the old history bytes to the new history cache

                                            System.arraycopy(fHistoryCache[0].bytes, 0, newHistoryCache.bytes, 0, historyLength);

                                            // Copy the bytes from the main cache that are not in the history cache to the end of the new history cache.

                                            for (int index = 0; index < missingDataByteCount; index++)
                                            {
                                                int srcIndex = dataLength - missingDataByteCount + index;
                                                int dstIndex = historyLength + index;
                                                newHistoryCache.bytes[dstIndex] = new FPMemoryByte(fCache.bytes[srcIndex].getValue());
                                            }

                                            fHistoryCache[0] = newHistoryCache;

                                            continue;
                                        }
                                    }

                                    // Case 5 - The data cache is greater than the history cache and fully covers it

                                    if (dataStart < historyStart && dataEnd > historyEnd)
                                    {
                                        int start = 0;
                                        int end   = 0;

                                        // Create a new history cache to reflect the entire data cache

                                        MemoryUnit newHistoryCache = new MemoryUnit();

                                        newHistoryCache.start   = fCache.start;
                                        newHistoryCache.end     = fCache.end;
                                        int newHistoryCacheSize = fCache.bytes.length;
                                        newHistoryCache.bytes   = new FPMemoryByte[newHistoryCacheSize];

                                        int topByteCount    = historyStart - dataStart;
                                        int bottomByteCount = dataEnd - historyEnd;

                                        // Copy the bytes from the beginning of the data cache to the new history cache

                                        for (int index = 0; index < topByteCount; index++)
                                            newHistoryCache.bytes[index] = new FPMemoryByte(fCache.bytes[index].getValue());

                                        // Copy the old history cache bytes to the new history cache

                                        start = topByteCount;
                                        end   = topByteCount + historyLength;

                                        for (int index = start; index < end; index++)
                                            newHistoryCache.bytes[index] = new FPMemoryByte(fCache.bytes[index].getValue());

                                        // Copy the bytes from the end of the data cache to the new history cache

                                        start = topByteCount + historyLength;
                                        end   = topByteCount + historyLength + bottomByteCount;

                                        for (int index = start; index < end; index++)
                                            newHistoryCache.bytes[index] = new FPMemoryByte(fCache.bytes[index].getValue());

                                        fHistoryCache[0] = newHistoryCache;

                                        continue;
                                    }
                                }
                            }
                        }

                        // If the history does not exist, populate the history with the just populated
                        // cache.  This solves the use case of (1) connect to target; (2) edit memory
                        // before the first suspend debug event; (3) paint differences in changed color.

                        if (fHistoryCache[0] == null)
                            fHistoryCache[0] = fCache.clone();

                        Rendering.this.redrawPanes();
                    }
                });

            }
            catch (Exception e)
            {
                // User can scroll to any memory, whether it's valid on the target or not.  It doesn't make
                // much sense to fill up the Eclipse error log with such "failures."  So, comment out for now.
                // logError(FPRenderingMessages.getString("FAILURE_READ_MEMORY"), e); //$NON-NLS-1$
            }
        }

        // Bytes will be fetched from cache

        @Override
        public FPMemoryByte[] getBytes(BigInteger address, int bytesRequested) throws DebugException
        {
            assert Thread.currentThread().equals(Display.getDefault().getThread()) : FPRenderingMessages.getString("CALLED_ON_NON_DISPATCH_THREAD"); //$NON-NLS-1$

            if (containsEditedCell(address))        // Cell size cannot be switched during an edit
                return getEditedMemory(address);

            boolean contains = false;
            if (fCache != null && fCache.start != null)
            {
                // See if all of the data requested is in the cache

                BigInteger dataEnd = address.add(BigInteger.valueOf(bytesRequested));

                if (fCache.start.compareTo(address) <= 0 && fCache.end.compareTo(dataEnd) >= 0 && fCache.bytes.length > 0)
                    contains = true;
            }

            if (contains)
            {
                int offset = address.subtract(fCache.start).intValue();
                FPMemoryByte bytes[] = new FPMemoryByte[bytesRequested];

                for (int index = 0; index < bytes.length; index++)
                    bytes[index] = fCache.bytes[offset + index];

                return bytes;
            }

            FPMemoryByte bytes[] = new FPMemoryByte[bytesRequested];

            for (int index = 0; index < bytes.length; index++)
            {
                bytes[index] = new FPMemoryByte();
                bytes[index].setReadable(false);
            }

            fViewportCache.queueRequest(fViewportAddress, getViewportEndAddress());

            return bytes;
        }

        @Override
        public boolean containsEditedCell(BigInteger address)
        {
            assert Thread.currentThread().equals(Display.getDefault().getThread()) : FPRenderingMessages.getString("CALLED_ON_NON_DISPATCH_THREAD"); //$NON-NLS-1$
            return fEditBuffer.containsKey(address);
        }

        public FPMemoryByte[] getEditedMemory(BigInteger address)
        {
            assert Thread.currentThread().equals(Display.getDefault().getThread()) : FPRenderingMessages.getString("CALLED_ON_NON_DISPATCH_THREAD"); //$NON-NLS-1$
            return fEditBuffer.get(address);
        }

        @Override
        public void clearEditBuffer()
        {
            assert Thread.currentThread().equals(Display.getDefault().getThread()) : FPRenderingMessages.getString("CALLED_ON_NON_DISPATCH_THREAD"); //$NON-NLS-1$
            fEditBuffer.clear();
            Rendering.this.redrawPanes();
        }

        @Override
        public void writeEditBuffer()
        {
            assert Thread.currentThread().equals(Display.getDefault().getThread()) : FPRenderingMessages.getString("CALLED_ON_NON_DISPATCH_THREAD"); //$NON-NLS-1$

            Set<BigInteger> keySet = fEditBuffer.keySet();
            Iterator<BigInteger> iterator = keySet.iterator();

            while (iterator.hasNext())
            {
                BigInteger address = iterator.next();
                FPMemoryByte[] bytes = fEditBuffer.get(address);

                byte byteValue[] = new byte[bytes.length];

                for (int index = 0; index < bytes.length; index++)
                    byteValue[index] = bytes[index].getValue();

                try
                {
                    IMemoryBlockExtension block = getMemoryBlock();
                    BigInteger offset = address.subtract(block.getBigBaseAddress());
                    block.setValue(offset, byteValue);
                }
                catch (Exception e)
                {
                    MemoryViewUtil.openError(FPRenderingMessages.getString("FAILURE_WRITE_MEMORY"), "", e); //$NON-NLS-1$ //$NON-NLS-2$
                    logError(FPRenderingMessages.getString("FAILURE_WRITE_MEMORY"), e); //$NON-NLS-1$
                }
            }

            clearEditBuffer();
        }

        @Override
        public void setEditedValue(BigInteger address, FPMemoryByte[] bytes)
        {
            assert Thread.currentThread().equals(Display.getDefault().getThread()) : FPRenderingMessages.getString("CALLED_ON_NON_DISPATCH_THREAD"); //$NON-NLS-1$
            fEditBuffer.put(address, bytes);
            Rendering.this.redrawPanes();
        }
    }

    public void setVisibleAddressBar(boolean visible)
    {
        fAddressBarControl.setVisible(visible);
        if (visible)
        {
            String selectedStr = "0x" + getCaretAddress().toString(16); //$NON-NLS-1$
            Text text = fAddressBar.getExpressionWidget();
            text.setText(selectedStr);
            text.setSelection(0, text.getCharCount());
            fAddressBar.getExpressionWidget().setFocus();
        }

        layout(true);
        layoutPanes();
    }

    public void setDirty(boolean needRefresh)
    {
        fCacheDirty = needRefresh;
    }

    public boolean isDirty()
    {
        return fCacheDirty;
    }

    @Override
    public void dispose()
    {
        DebugPlugin.getDefault().removeDebugEventListener(this);
        if (fViewportCache != null)
        {
            fViewportCache.dispose();
            fViewportCache = null;
        }
        super.dispose();
    }

    class Selection implements FPIMemorySelection
    {
        private BigInteger fStartHigh = null;
        private BigInteger fStartLow = null;

        private BigInteger fEndHigh = null;
        private BigInteger fEndLow = null;

        @Override
        public void clear()
        {
            fEndHigh = fEndLow = fStartHigh = fStartLow = null;
            redrawPanes();
        }

        @Override
        public boolean hasSelection()
        {
            return fStartHigh != null && fStartLow != null && fEndHigh != null && fEndLow != null;
        }

        @Override
        public boolean isSelected(BigInteger address)
        {
            // Do we have valid start and end addresses?

            if (getEnd() == null || getStart() == null) return false;

            // If end is greater than start

            if (getEnd().compareTo(getStart()) >= 0)
            {
                // If address is greater-than-or-equal-to start and less then end, return true
                if (address.compareTo(getStart()) >= 0 && address.compareTo(getEnd()) < 0) return true;
            }

            // If start is greater than end

            else if (getStart().compareTo(getEnd()) >= 0)
            {
                // If address is greater-than-or-equal-to zero and less than start, return true
                if (address.compareTo(getEnd()) >= 0 && address.compareTo(getStart()) < 0) return true;
            }

            return false;
        }

        // Set selection start

        @Override
        public void setStart(BigInteger high, BigInteger low)
        {
            if (high == null && low == null)
            {
                if (fStartHigh != null && fStartLow != null)
                {
                    fStartHigh = null;
                    fStartLow = null;
                    redrawPanes();
                }

                return;
            }

            boolean changed = false;

            if (fStartHigh == null || !high.equals(fStartHigh))
            {
                fStartHigh = high;
                changed = true;
            }

            if (fStartLow == null || !low.equals(fStartLow))
            {
                fStartLow = low;
                changed = true;
            }

            if (changed) redrawPanes();
        }

        // Set selection end

        @Override
        public void setEnd(BigInteger high, BigInteger low)
        {
            if (high == null && low == null)
            {
                if (fEndHigh != null && fEndLow != null)
                {
                    fEndHigh = null;
                    fEndLow = null;
                    redrawPanes();
                }

                return;
            }

            boolean changed = false;

            if (fEndHigh == null || !high.equals(fEndHigh))
            {
                fEndHigh = high;
                changed = true;
            }

            if (fEndLow == null || !low.equals(fEndLow))
            {
                fEndLow = low;
                changed = true;
            }

            if (changed) redrawPanes();
        }

        @Override
        public BigInteger getHigh()
        {
            if (!hasSelection()) return null;
            return getStart().max(getEnd());
        }

        @Override
        public BigInteger getLow()
        {
            if (!hasSelection()) return null;
            return getStart().min(getEnd());
        }

        @Override
        public BigInteger getStart()
        {
            // If there is no start, return null
            if (fStartHigh == null) return null;

            // If there is no end, return the high address of the start
            if (fEndHigh == null) return fStartHigh;

            // If Start High/Low equal End High/Low, return a low start and high end
            if (fStartHigh.equals(fEndHigh) && fStartLow.equals(fEndLow)) return fStartLow;

            BigInteger differenceEndToStartHigh = fEndHigh.subtract(fStartHigh).abs();
            BigInteger differenceEndToStartLow  = fEndHigh.subtract(fStartLow).abs();

            // Return the start high or start low based on which creates a larger selection
            if (differenceEndToStartHigh.compareTo(differenceEndToStartLow) > 0)
                return fStartHigh;

            return fStartLow;
        }

        @Override
        public BigInteger getStartLow()
        {
            return fStartLow;
        }

        @Override
        public BigInteger getEnd()
        {
            // If there is no end, return null
            if (fEndHigh == null) return null;

            // *** Temporary for debugging ***

            if (fStartHigh == null || fStartLow == null)
            {
                return null;
            }

            // If Start High/Low equal End High/Low, return a low start and high end
            if (fStartHigh.equals(fEndHigh) && fStartLow.equals(fEndLow)) return fStartHigh;

            BigInteger differenceStartToEndHigh = fStartHigh.subtract(fEndHigh).abs();
            BigInteger differenceStartToEndLow = fStartHigh.subtract(fEndLow).abs();

            // Return the start high or start low based on which creates a larger selection
            if (differenceStartToEndHigh.compareTo(differenceStartToEndLow) >= 0)
                return fEndHigh;

            return fEndLow;
        }
    }

    public void setPaneVisible(int pane, boolean visible)
    {
        switch (pane)
        {
            case PANE_ADDRESS:
                fAddressPane.setPaneVisible(visible);
                break;
            case PANE_DATA:
                fDataPane.setPaneVisible(visible);
                break;
        }

        fireSettingsChanged();
        layoutPanes();
    }

    public boolean getPaneVisible(int pane)
    {
        switch (pane)
        {
            case PANE_ADDRESS:
                return fAddressPane.isPaneVisible();
            case PANE_DATA:
                return fDataPane.isPaneVisible();
            default:
                return false;
        }
    }

    protected void packColumns()
    {
        int availableWidth = Rendering.this.getSize().x;

        if (fAddressPane.isPaneVisible())
        {
            availableWidth -= fAddressPane.computeSize(0, 0).x;
            availableWidth -= Rendering.this.getRenderSpacing() * 2;
        }

        int combinedWidth = 0;

        if (fDataPane.isPaneVisible()) combinedWidth += fDataPane.getCellWidth();

        if (getColumnsSetting() == Rendering.COLUMNS_AUTO_SIZE_TO_FIT)
        {
            if (combinedWidth == 0)
                fColumnCount = 0;
            else
            {
                fColumnCount = availableWidth / combinedWidth;
                if (fColumnCount == 0) fColumnCount = 1;    // Paint one column even if only part can show in view
            }
        }
        else
            fColumnCount = getColumnsSetting();

        try
        {
            // Update the number of bytes per row; the max/min scroll range and the current thumbnail position.

            fBytesPerRow = getCharsPerColumn() * getColumnCount();
            getVerticalBar().setMinimum(1);

            // scrollbar maximum range is Integer.MAX_VALUE.

            getVerticalBar().setMaximum(getMaxScrollRange().min(BigInteger.valueOf(Integer.MAX_VALUE)).intValue());
            getVerticalBar().setIncrement(1);
            getVerticalBar().setPageIncrement(this.getRowCount() - 1);

            // FIXME: conversion of slider to scrollbar
            // fScrollBar.setToolTipText(Rendering.this.getAddressString(fViewportAddress));

            setScrollSelection();
        }
        catch (Exception e)
        {
            // FIXME precautionary
        }

        Rendering.this.redraw();
        Rendering.this.redrawPanes();
    }

    public FPAbstractPane[] getRenderingPanes()
    {
        return new FPAbstractPane[] { fAddressPane, fDataPane };
    }

    public int getCellPadding()
    {
        return fCellPadding;
    }

    protected int getRenderSpacing()
    {
        return fPaneSpacing;
    }

    public void refresh()
    {
        if (!this.isDisposed())
        {
            if (this.isVisible() && getViewportCache() != null)
            {
                getViewportCache().refresh();
            }
            else
            {
                setDirty(true);
                fParent.updateRenderingLabels();
            }
        }
    }

    protected void archiveDeltas()
    {
        this.getViewportCache().archiveDeltas();
    }

    public void gotoAddress(BigInteger address)
    {
        // Ensure that the GoTo address is within the addressable range

        if ((address.compareTo(this.getMemoryBlockStartAddress()) < 0) || (address.compareTo(this.getMemoryBlockEndAddress()) > 0))
            return;

        fViewportAddress = address;

        // Reset the caret and selection state (no caret and no selection)

        fCaretAddress = null;
        fSelection = new Selection();

        redrawPanes();
    }

    public void setViewportStartAddress(BigInteger newAddress)
    {
        fViewportAddress = newAddress;
    }

    public BigInteger getViewportStartAddress()
    {
        return fViewportAddress;
    }

    public BigInteger getViewportEndAddress()
    {
        return fViewportAddress.add(BigInteger.valueOf(this.getBytesPerRow() * getRowCount() / getAddressableSize()));
    }

    public String getAddressString(BigInteger address)
    {
        StringBuffer addressString = new StringBuffer(address.toString(16).toUpperCase());

        for (int chars = getAddressBytes() * 2 - addressString.length(); chars > 0; chars--)
            addressString.insert(0, '0');

        addressString.insert(0, "0x"); //$NON-NLS-1$

        return addressString.toString();
    }

    protected int getAddressBytes()
    {
        return fParent.getAddressSize();
    }

    public Control getAddressBarControl()
    {
        return fAddressBarControl;
    }

    public int getColumnCount()
    {
        return fColumnCount;
    }

    public int getColumnsSetting()
    {
        return fColumnsSetting;
    }

    protected void setBytesPerRow(int count)
    {
        fBytesPerRow = count;
    }

    protected void setColumnCount(int count)
    {
        fColumnCount = count;
    }

    public void setColumnsSetting(int columns)
    {
        if (fColumnsSetting != columns)
        {
            fColumnsSetting = columns;
            fireSettingsChanged();
            layoutPanes();
        }
    }

    protected void ensureVisible(BigInteger address)
    {
        BigInteger viewportStart = this.getViewportStartAddress();
        BigInteger viewportEnd = this.getViewportEndAddress();

        boolean isAddressBeforeViewportStart = address.compareTo(viewportStart) < 0;
        boolean isAddressAfterViewportEnd = address.compareTo(viewportEnd) > 0;

        if (isAddressBeforeViewportStart || isAddressAfterViewportEnd) gotoAddress(address);
    }

    protected int getRowCount()
    {
        int rowCount = 0;
        Control panes[] = getRenderingPanes();
        for (int i = 0; i < panes.length; i++)
            if (panes[i] instanceof FPAbstractPane)
                rowCount = Math.max(rowCount, ((FPAbstractPane) panes[i]).getRowCount());

        return rowCount;
    }

    protected int getBytesPerRow()
    {
        return fBytesPerRow;
    }

    protected int getAddressableCellsPerRow()
    {
        return getBytesPerRow() / getAddressableSize();
    }

    public int getAddressesPerColumn()
    {
        return this.getCharsPerColumn() / getAddressableSize();
    }

    /**
     * @return Set current scroll selection
     */
    protected void setScrollSelection()
    {
        BigInteger selection = getViewportStartAddress().divide(BigInteger.valueOf(getAddressableCellsPerRow()));

        fScrollSelection = rows2scrollbar(selection);
        getVerticalBar().setSelection(fScrollSelection);
    }

    /**
     * compute the maximum scrolling range.
     *
     * @return number of lines that rendering can display
     */
    private BigInteger getMaxScrollRange()
    {
        BigInteger difference = getMemoryBlockEndAddress().subtract(getMemoryBlockStartAddress()).add(BigInteger.ONE);
        BigInteger maxScrollRange = difference.divide(BigInteger.valueOf(getAddressableCellsPerRow()));
        if (maxScrollRange.multiply(BigInteger.valueOf(getAddressableCellsPerRow())).compareTo(difference) != 0)
            maxScrollRange = maxScrollRange.add(BigInteger.ONE);

        // Support targets with an addressable size greater than 1

        maxScrollRange = maxScrollRange.divide(BigInteger.valueOf(getAddressableSize()));
        return maxScrollRange;
    }

    /**
     * The scroll range is limited by SWT. Because it can be less than the number
     * of rows (of memory) that we need to display, we need an arithmetic mapping.
     *
     * @return ratio this function returns how many rows a scroll bar unit
     *         represents. The number will be some fractional value, up to but
     *         not exceeding the value 1. I.e., when the scroll range exceeds
     *         the row range, we use a 1:1 mapping.
     */
    private final BigDecimal getScrollRatio()
    {
        BigInteger maxRange = getMaxScrollRange();
        if (maxRange.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0)
        {
            return new BigDecimal(maxRange).divide(BigDecimal.valueOf(Integer.MAX_VALUE), SCROLL_CONVERSION_PRECISION);
        }

        return BigDecimal.ONE;
    }

    /**
     * Convert memory row units to scroll bar units. The scroll range is limited
     * by SWT. Because it can be less than the number of rows (of memory) that
     * we need to display, we need an arithmetic mapping.
     *
     * @param rows
     *            units of memory
     * @return scrollbar units
     */
    private int rows2scrollbar(BigInteger rows)
    {
        return new BigDecimal(rows).divide(getScrollRatio(), SCROLL_CONVERSION_PRECISION).intValue();
    }

    /**
     * Convert scroll bar units to memory row units. The scroll range is limited
     * by SWT. Because it can be less than the number of rows (of memory) that
     * we need to display, we need an arithmetic mapping.
     *
     * @param scrollbarUnits
     *            scrollbar units
     * @return number of rows of memory
     */
    BigInteger scrollbar2rows(int scrollbarUnits)
    {
        return getScrollRatio().multiply(BigDecimal.valueOf(scrollbarUnits), SCROLL_CONVERSION_PRECISION).toBigInteger();
    }

    /**
     * @return start address of the memory block
     */
    protected BigInteger getMemoryBlockStartAddress()
    {
        if (fMemoryBlockStartAddress == null)
            fMemoryBlockStartAddress = fParent.getMemoryBlockStartAddress();
        if (fMemoryBlockStartAddress == null)
            fMemoryBlockStartAddress = BigInteger.ZERO;

        return fMemoryBlockStartAddress;
    }

    /**
     * @return end address of the memory block
     */
    protected BigInteger getMemoryBlockEndAddress()
    {
        if (fMemoryBlockEndAddress == null)
            fMemoryBlockEndAddress = fParent.getMemoryBlockEndAddress();

        return fMemoryBlockEndAddress;
    }

    public FPDataType getFPDataType()
    {
        return fFPDataType;
    }

    public void setFPDataType(FPDataType numberType)
    {
        if (fFPDataType == numberType) return;

        fFPDataType = numberType;
        fireSettingsChanged();
        layoutPanes();
    }

    public int getUpdateMode()
    {
        return fUpdateMode;
    }

    public void setUpdateMode(int fUpdateMode)
    {
        this.fUpdateMode = fUpdateMode;
    }

    protected String getCharacterSet(int mode)
    {
        switch (mode)
        {
            case Rendering.TEXT_UTF8:
                return "UTF8"; //$NON-NLS-1$

            case Rendering.TEXT_UTF16:
                return "UTF16"; //$NON-NLS-1$

            case Rendering.TEXT_USASCII:
                return "US-ASCII"; //$NON-NLS-1$

            case Rendering.TEXT_ISO_8859_1:
            default:
                return "ISO-8859-1"; //$NON-NLS-1$
        }
    }

    public int getBytesPerCharacter()
    {
        return 1;
    }

    public boolean isTargetLittleEndian()
    {
        return fIsTargetLittleEndian;
    }

    public void setTargetLittleEndian(boolean littleEndian)
    {
        if (fIsTargetLittleEndian == littleEndian) return;

        fParent.setTargetMemoryLittleEndian(littleEndian);
        fIsTargetLittleEndian = littleEndian;

        Display.getDefault().asyncExec(new Runnable()
        {
            @Override
            public void run()
            {
                fireSettingsChanged();
                layoutPanes();
            }
        });
    }

    public boolean isDisplayLittleEndian()
    {
        return fIsDisplayLittleEndian;
    }

    public void setDisplayLittleEndian(boolean isLittleEndian)
    {
        if (fIsDisplayLittleEndian == isLittleEndian) return;
        fIsDisplayLittleEndian = isLittleEndian;
        fireSettingsChanged();

        Display.getDefault().asyncExec(new Runnable()
        {
            @Override
            public void run()
            {
                layoutPanes();
            }
        });
    }

    public int getCharsPerColumn()
    {
        return fCharsPerColumn;
    }

    public int getDisplayedPrecision()
    {
        return fDisplayedPrecision;
    }

    // Set the number of precision digits that are displayed in the view

    public void setDisplayedPrecision(int displayedPrecision)
    {
        if (fDisplayedPrecision != displayedPrecision)
        {
            fDisplayedPrecision = displayedPrecision;
            fCharsPerColumn = charsPerColumn();
            fireSettingsChanged();
            layoutPanes();
        }
    }

    protected void redrawPane(int paneId)
    {
        if (!isDisposed() && this.isVisible())
        {
            FPAbstractPane pane = null;

            if (paneId == Rendering.PANE_ADDRESS)
            {
                pane = fAddressPane;
            }
            else if (paneId == Rendering.PANE_DATA)
            {
                pane = fDataPane;
            }

            if (pane != null && pane.isPaneVisible())
            {
                pane.redraw();
                pane.setRowCount();
                if (pane.isFocusControl()) pane.updateTheCaret();
            }
        }

        fParent.updateRenderingLabels();
    }

    protected void redrawPanes()
    {
        if (!isDisposed() && this.isVisible())
        {
            if (fAddressPane.isPaneVisible())
            {
                fAddressPane.redraw();
                fAddressPane.setRowCount();
                if (fAddressPane.isFocusControl()) fAddressPane.updateTheCaret();
            }

            if (fDataPane.isPaneVisible())
            {
                fDataPane.redraw();
                fDataPane.setRowCount();
                if (fDataPane.isFocusControl()) fDataPane.updateTheCaret();
            }
        }

        fParent.updateRenderingLabels();
    }

    void layoutPanes()
    {
        packColumns();
        layout(true);

        redraw();
        redrawPanes();
    }

    void fireSettingsChanged()
    {
        fAddressPane.settingsChanged();
        fDataPane.settingsChanged();
    }

    protected void copyAddressToClipboard()
    {
        Clipboard clip = null;

        try
        {
            clip = new Clipboard(getDisplay());

            String addressString = "0x" + getCaretAddress().toString(16); //$NON-NLS-1$

            TextTransfer plainTextTransfer = TextTransfer.getInstance();
            clip.setContents(new Object[] { addressString }, new Transfer[] { plainTextTransfer });
        }
        finally
        {
            if (clip != null)
            {
                clip.dispose();
            }
        }
    }

    // Given an array of bytes, the data type and endianness, return a scientific notation string representation

    public String sciNotationString(FPMemoryByte byteArray[], FPDataType fpDataType, boolean isLittleEndian)
    {
        StringBuffer textString = null;

        // Check the byte array for readability

        for (int index = 0; index < byteArray.length; index++)
            if (!byteArray[index].isReadable())
                return FPutilities.fillString(fCharsPerColumn, '?');

        // Convert the byte array to a floating point value and check to see if it's within the range
        // of the data type.  If the valid range is exceeded, set string to "-Infinity" or "Infinity".

        ByteOrder byteOrder = isLittleEndian ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN;

        if (fpDataType == FPDataType.FLOAT)
        {
            float floatValue = ByteBuffer.wrap(FPutilities.memoryBytesToByteArray(byteArray)).order(byteOrder).getFloat();
            floatValue = FPutilities.floatLimitCheck(floatValue);

            if (floatValue == Float.NEGATIVE_INFINITY)
                textString = new StringBuffer("-Infinity"); //$NON-NLS-1$

            if (floatValue == Float.POSITIVE_INFINITY)
                textString = new StringBuffer(" Infinity"); //$NON-NLS-1$
        }

        if (fpDataType == FPDataType.DOUBLE)
        {
            double doubleValue = ByteBuffer.wrap(FPutilities.memoryBytesToByteArray(byteArray)).order(byteOrder).getDouble();
            doubleValue = FPutilities.doubleLimitCheck(doubleValue);

            if (doubleValue == Double.NEGATIVE_INFINITY)
                textString = new StringBuffer("-Infinity"); //$NON-NLS-1$

            if (doubleValue == Double.POSITIVE_INFINITY)
                textString = new StringBuffer(" Infinity"); //$NON-NLS-1$
        }

        // If we do not already have a StringBuffer value, Convert the value to
        // a string.  In any case, pad the string with spaces to the cell width.

        if (textString == null)
            textString = new StringBuffer(FPutilities.byteArrayToSciNotation(fpDataType, isLittleEndian, byteArray, fDisplayedPrecision));

        return (textString.append(FPutilities.fillString(fCharsPerColumn - textString.length(), getPaddingCharacter()))).toString();
    }

    // Convert the floating point edit buffer string to a byte array and update memory

    public void convertAndUpdateCell(BigInteger memoryAddress, String editBuffer)
    {
        if (editBuffer != null && editBuffer.length() > 0)
        {
            // Convert the edit buffer string to byte arrays

            byte[] targetByteArray = new byte[getFPDataType().getByteLength()];

            targetByteArray = FPutilities.floatingStringToByteArray(getFPDataType(), editBuffer, getFPDataType().getBitsize());

            // If we're in little endian mode, reverse the bytes, which is required
            // due to lower-level internal conversion when written to memory.

            if (fIsDisplayLittleEndian)
                targetByteArray = FPutilities.reverseByteOrder(targetByteArray);

            FPMemoryByte[] newMemoryBytes = new FPMemoryByte[targetByteArray.length];

            for (int index = 0; index < targetByteArray.length; index++)
            {
                newMemoryBytes[index] = new FPMemoryByte(targetByteArray[index]);
                newMemoryBytes[index].setBigEndian(!isTargetLittleEndian());
                newMemoryBytes[index].setEdited(true);
                newMemoryBytes[index].setChanged(true);
            }

            //  Apply the change and make it visible in the view

            getViewportCache().setEditedValue(memoryAddress, newMemoryBytes);
            getViewportCache().writeEditBuffer();
            redraw();
        }
        else
        {
            // The edit buffer string is null or has a zero length.

            final String errorText = NLS.bind(FPRenderingMessages.getString("FPRendering.ERROR_FPENTRY_POPUP_TEXT"), "<blanks>"); //$NON-NLS-1$ //$NON-NLS-2$

            try
            {
                // Restore the previous value
                setEditBuffer(new StringBuffer(fDataPane.bytesToSciNotation(getBytes(fCaretAddress, getFPDataType().getByteLength()))));
            }
            catch (DebugException e)
            {
                e.printStackTrace();
            }

            // Put together the pop-up window components and show the user the error

            String statusString = FPRenderingMessages.getString("FPRendering.ERROR_FPENTRY_STATUS"); //$NON-NLS-1$
            Status status = new Status(IStatus.ERROR, FPRenderingPlugin.getUniqueIdentifier(), statusString);
            FPutilities.popupMessage(FPRenderingMessages.getString("FPRendering.ERROR_FPENTRY_POPUP_TITLE"), errorText, status); //$NON-NLS-1$
        }
    }

    // Getter/setter for Insert Mode state

    public void setInsertMode(boolean insertMode)
    {
        this.fEditInserMode = insertMode;
    }

    public boolean insertMode()
    {
        return fEditInserMode;
    }

    // Getter/setter for cell edit state:  true = we're in cell edit mode; false = we're not in cell edit mode

    public boolean isEditingCell()
    {
        return fCellEditState;
    }

    public void setEditingCell(boolean state)
    {
        this.fCellEditState = state;
    }

    // Getter/setter for the address of the cell currently being edited

    public BigInteger getCellEditAddress()
    {
        return cellEditAddress;
    }

    public void setCellEditAddress(BigInteger cellEditAddress)
    {
        this.cellEditAddress = cellEditAddress;
    }

    // Getter/Setter for the memory address of the cell currently being edited

    public BigInteger getMemoryAddress()
    {
        return memoryAddress;
    }

    public void setMemoryAddress(BigInteger memoryAddress)
    {
        this.memoryAddress = memoryAddress;
    }

    // Getter/setter for storing the raw/uninterpreted/not-converted-to-scientific-notation text entry string

    public StringBuffer getEditBuffer()
    {
        return fEditBuffer;
    }

    public void setEditBuffer(StringBuffer cellChars)
    {
        this.fEditBuffer = cellChars;
    }

    // Enter cell-edit mode

    public void startCellEditing(BigInteger cellAddress, BigInteger memoryAddress, String editString)
    {
        setEditBuffer(new StringBuffer(editString));
        setCellEditAddress(cellAddress);
        setMemoryAddress(memoryAddress);
        setEditingCell(true);
    }

    // Exit cell-edit mode

    public void endCellEditing()
    {
        setEditingCell(false);
        setCellEditAddress(null);
        setMemoryAddress(null);
        setEditBuffer(null);

    }

    // Floating point number edit mode status-line display: 'true' = display edit mode, 'false' clear edit mode

    public void displayEditModeIndicator(final boolean indicatorON)
    {
        UIJob job = new UIJob("FP Renderer Edit Indicator") //$NON-NLS-1$
        {
            @Override
            public IStatus runInUIThread(IProgressMonitor monitor)
            {
                String statusLineMessage;
                IViewPart viewInstance = null;

                if (indicatorON)
                {
                    // Construct the edit mode message
                    statusLineMessage = NLS.bind(FPRenderingMessages.getString("FPRendering.EDIT_MODE"), //$NON-NLS-1$
                                 (insertMode() ? FPRenderingMessages.getString("FPRendering.EDIT_MODE_INSERT") : //$NON-NLS-1$
                                                 FPRenderingMessages.getString("FPRendering.EDIT_MODE_OVERWRITE"))); //$NON-NLS-1$
                }
                else
                {
                    // 'null' = clear the message
                    statusLineMessage = null;
                }

                // Get the window and page references

                IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                if (window == null) return Status.OK_STATUS;

                IWorkbenchPage page = window.getActivePage();
                if (page == null) return Status.OK_STATUS;

                // Update (or clear) the Workbench status line when the Memory and Memory Browser views are in focus

                viewInstance = page.findView("org.eclipse.debug.ui.MemoryView");    // TODO:  programmatically retrieve ID //$NON-NLS-1$

                if (viewInstance != null)
                    viewInstance.getViewSite().getActionBars().getStatusLineManager().setMessage(statusLineMessage);

                viewInstance = page.findView("org.eclipse.cdt.debug.ui.memory.memorybrowser.MemoryBrowser");    // TODO:  programmatically retrieve ID //$NON-NLS-1$

                if (viewInstance != null)
                    viewInstance.getViewSite().getActionBars().getStatusLineManager().setMessage(statusLineMessage);

                return Status.OK_STATUS;
            }
        };

        job.setSystem(true);
        job.schedule();
    }

    // Calculate memory address from the cell address

    public BigInteger cellToMemoryAddress(BigInteger cellAddress)
    {
        BigInteger vpStart  = getViewportStartAddress();
        BigInteger colChars = BigInteger.valueOf(getCharsPerColumn());
        BigInteger dtBytes  = BigInteger.valueOf(getFPDataType().getByteLength());

        return vpStart.add(((cellAddress.subtract(vpStart)).divide(colChars)).multiply(dtBytes));
    }
}

Back to the top