Skip to main content
summaryrefslogtreecommitdiffstats
blob: e9a5326589013765d9a6df92faa84468918a08a9 (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
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.debug.internal.ui.views.memory;

import java.math.BigInteger;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.core.model.IMemoryBlockExtension;
import org.eclipse.debug.internal.ui.DebugUIMessages;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.DelegatingModelPresentation;
import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
import org.eclipse.debug.internal.ui.LazyModelPresentation;
import org.eclipse.debug.internal.ui.preferences.IDebugPreferenceConstants;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

/**
 * @since 3.0
 */
public class MemoryViewTab extends AbstractMemoryViewTab implements SelectionListener, ControlListener, KeyListener, ITableMemoryViewTab, ISynchronizedMemoryBlockView{	
	
	private static final String PREFIX = "MemoryViewTab."; //$NON-NLS-1$
	private static final String ADDRESS = PREFIX + "Address"; //$NON-NLS-1$
	private static final String ERROR = PREFIX + "Error"; //$NON-NLS-1$
	private static final String FORMAT_IS_INVALID = PREFIX + "Format_is_invalid"; //$NON-NLS-1$
	private static final String ADDRESS_IS_OUT_OF_RANGE = PREFIX + "Address_is_out_of_range"; //$NON-NLS-1$
	private static final String COLUMN_SIZE = PREFIX + "Column_size"; //$NON-NLS-1$
	//private static final String ADD_RENDERING = PREFIX + "Add_rendering"; //$NON-NLS-1$
	private static final String UNABLE_TO_GET_BASE_ADDRESS = PREFIX +"Unable_to_retrieve_base_address"; //$NON-NLS-1$
	private static final String UNKNOWN = PREFIX + "Unknown"; //$NON-NLS-1$
	
	// menu group names
	private static final String MEMORY_ACTIONS_GROUP = IDebugUIConstants.PLUGIN_ID + ".MemoryViewActionsGroup"; //$NON-NLS-1$
	private static final String MEMORY_ACTIONS_FORMAT_GROUP = IDebugUIConstants.PLUGIN_ID + ".MemoryViewActionsGroup.format"; //$NON-NLS-1$
	//private static final String MEMORY_ACTIONS_RENDERING_GROUP = IDebugUIConstants.PLUGIN_ID + ".MemoryViewActionsGroup.rendering"; //$NON-NLS-1$
	
	private MemoryViewContentProvider contentProvider;
	private TableViewer fTableViewer = null;
	private boolean fEnabled;
	private ViewTabCursorManager fCursorManager;

	private IMemoryBlockModelPresentation fMemoryBlockPresentation;
	private boolean fNoPresentation = false;
	private boolean fShowAddressColumn = true;
	
	public int TABLE_PREBUFFER = 20;
	public int TABLE_POSTBUFFER = 20;
	public int TABLE_DEFAULTBUFFER = 20;
	
	private TextViewer fTextViewer = null;
	private boolean errorOccurred = false;
	
	protected  BigInteger fSelectedAddress = null;
	
	private boolean fTabCreated = false;
	
	private CellEditor fEditors[];
	private ICellModifier fCellModifier;
	
	private CopyViewTabToClipboardAction fCopyToClipboardAction;
	private GoToAddressAction fGoToAddressAction;
	private ResetMemoryBlockAction fResetMemoryBlockAction;
	private PrintViewTabAction fPrintViewTabAction;
	private Action[] fFormatColumnActions;
	private ReformatAction fReformatAction;
	private ShowAddressColumnAction fShowAddColumnAction;
	
	private boolean fIsDisposed = false;
	
	private int fBytePerLine;								// number of bytes per line: 16
	private int fColumnSize;								// number of bytes per column:  1,2,4,8
	
	// font change listener
	private FontChangeListener fFontChangeListener;
	private TabFolderDisposeListener fTabFolderDisposeListener;
	
	private boolean fUpdateTabLabel = true;

	private static final int[] ignoreEvents =
	{
		SWT.ARROW_UP,
		SWT.ARROW_DOWN,
		SWT.ARROW_LEFT,
		SWT.ARROW_RIGHT,
		SWT.PAGE_UP,
		SWT.PAGE_DOWN,
		SWT.HOME,
		SWT.END,
		SWT.INSERT,
		SWT.F1,
		SWT.F2,
		SWT.F3,
		SWT.F4,
		SWT.F5,
		SWT.F6,
		SWT.F7,
		SWT.F8,
		SWT.F9,
		SWT.F10,
		SWT.F11,
		SWT.F12,
		SWT.F13,
		SWT.F14,
		SWT.F15,
		SWT.HELP,
		SWT.CAPS_LOCK,
		SWT.NUM_LOCK,
		SWT.SCROLL_LOCK,
		SWT.PAUSE,
		SWT.BREAK,
		SWT.PRINT_SCREEN,
		SWT.ESC,
		SWT.CTRL,
		SWT.ALT
	};
	
	private final class TabFolderDisposeListener implements DisposeListener
	{
		MemoryViewTab fViewTab;
		TabFolderDisposeListener(MemoryViewTab viewTab)
		{
			fViewTab = viewTab;
		}
		
		public void widgetDisposed(DisposeEvent e)
		{
			if (!fViewTab.fIsDisposed)
			{
				// remove listeners
				if (contentProvider != null)
					contentProvider.dispose();
				
				JFaceResources.getFontRegistry().removeListener(fFontChangeListener);
				getMemoryBlockViewSynchronizer().removeView(fViewTab);				
			}
		}
	}

	class FontChangeListener implements IPropertyChangeListener
	{
		/* (non-Javadoc)
		 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
		 */
		public void propertyChange(PropertyChangeEvent event)
		{
			// if memory view table font has changed
			if (event.getProperty().equals(IInternalDebugUIConstants.FONT_NAME))
			{
				if (!fIsDisposed)
				{			
					Font memoryViewFont = JFaceResources.getFont(IInternalDebugUIConstants.FONT_NAME);
					setFont(memoryViewFont);					
				}
			}
		}	
	}
	
	// **  Referring to internal class:  DelegatingModelPresentation and LazyModelPresentation
	// **  This should be ok when Memory View is contributed to Eclipse platform?
	class MemoryViewDelegatingModelPresentation extends DelegatingModelPresentation
	{
		
		MemoryViewDelegatingModelPresentation()
		{
			IExtensionPoint point= Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.ID_DEBUG_MODEL_PRESENTATION);
			if (point != null) {
				IExtension[] extensions= point.getExtensions();
				for (int i= 0; i < extensions.length; i++) {
					IExtension extension= extensions[i];
					IConfigurationElement[] configElements= extension.getConfigurationElements();
					for (int j= 0; j < configElements.length; j++) {
						IConfigurationElement elt= configElements[j];
						String id= elt.getAttribute("id"); //$NON-NLS-1$
						if (id != null) {
							IDebugModelPresentation lp= new MemoryViewLazyModelPresentation(elt);
							getLabelProviders().put(id, lp);
						}
					}
				}
			}			
		}

	}
	
	class MemoryViewLazyModelPresentation extends LazyModelPresentation implements IMemoryBlockModelPresentation
	{

		MemoryViewLazyModelPresentation(IConfigurationElement element)
		{
			super(element);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.debug.ui.IMemoryBlockModelPresentation#getTabLabel(org.eclipse.debug.core.model.IMemoryBlock)
		 */
		public String getTabLabel(IMemoryBlock blk, String renderingId)
		{
			IDebugModelPresentation presentation = getPresentation();
			
			if (presentation instanceof IMemoryBlockModelPresentation)
			{
				return ((IMemoryBlockModelPresentation)presentation).getTabLabel(blk, getRenderingId()); 
			}
			return null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.debug.ui.IMemoryBlockModelPresentation#getColumnLabels(org.eclipse.debug.core.model.IMemoryBlock, int, int)
		 */
		public String[] getColumnLabels(IMemoryBlock blk, int bytesPerLine, int columnSize)
		{
			IDebugModelPresentation presentation = getPresentation();
			
			if (presentation instanceof IMemoryBlockModelPresentation)
			{
				return ((IMemoryBlockModelPresentation)presentation).getColumnLabels(blk, bytesPerLine, columnSize); 
			}
			return new String[0];
		}

		/* (non-Javadoc)
		 * @see org.eclipse.debug.ui.IMemoryBlockModelPresentation#getAddressPresentation(org.eclipse.debug.core.model.IMemoryBlock, java.math.BigInteger)
		 */
		public String getAddressPresentation(IMemoryBlock blk, BigInteger address)
		{
			IDebugModelPresentation presentation = getPresentation();
			
			if (presentation instanceof IMemoryBlockModelPresentation)
			{
				return ((IMemoryBlockModelPresentation)presentation).getAddressPresentation(blk, address); 
			}
			return null;
		}
	}

	public MemoryViewTab(IMemoryBlock newMemory, TabItem newTab, MenuManager menuMgr, IMemoryRendering rendering, AbstractMemoryRenderer renderer) {
		super(newMemory, newTab, menuMgr, rendering);
			
		setTabName(newMemory, true);
		
		fTabItem.setControl(createFolderPage(renderer));
		
		if (!(newMemory instanceof IMemoryBlockExtension))
		{		
			// If not extended memory block, do not create any buffer
			// no scrolling
			TABLE_PREBUFFER=0;
			TABLE_POSTBUFFER=0;
			TABLE_DEFAULTBUFFER=0;
		}

		if (fTableViewer != null)
		{	
			fTableViewer.getTable().setTopIndex(TABLE_PREBUFFER);
		}
		
		addViewTabToSynchronizer();
		
		// otherwise, this is a totally new synchronize info

		fEnabled = true;
		fTabCreated = true;
		
		//synchronize
		synchronize();		
		
		createActions();
		
		// Need to resize column after content is filled in
		// Pack function does not work unless content is not filled in
		// since the table is not able to compute the preferred size.
		packColumns();
		
		// add listeners in the end to make sure that the resize event
		// does not affect synchronization
		if (fTableViewer != null){
			fTableViewer.getTable().addSelectionListener(this);
			fTabFolderDisposeListener = new TabFolderDisposeListener(this);
			fTabItem.addDisposeListener(fTabFolderDisposeListener);
		}
		
		if (fMemoryBlock instanceof IMemoryBlockExtension)
		{
			if(((IMemoryBlockExtension)fMemoryBlock).getBigBaseAddress() == null)
			{
				DebugException e = new DebugException(DebugUIPlugin.newErrorStatus(DebugUIMessages.getString(UNABLE_TO_GET_BASE_ADDRESS), null));
				displayError(e);				
			}
		}
	}
	
	private void addViewTabToSynchronizer()
	{
		getMemoryBlockViewSynchronizer().addView(this, null);
	
		// check if there is already synchronization info available
		Object selectedAddress =getSynchronizedProperty( IMemoryViewConstants.PROPERTY_SELECTED_ADDRESS);
		Object size =getSynchronizedProperty( IMemoryViewConstants.PROPERTY_COL_SIZE);
		Object topAddress =getSynchronizedProperty( IMemoryViewConstants.PROPERTY_TOP_ADDRESS);
		
		// if info is available, some other view tab has already been created
		// do not overwirte info int he synchronizer if that's the case
		if (selectedAddress == null)
		{
			updateSyncSelectedAddress(true);
		}	
		
		if (size == null)
		{
			updateSyncColSize();	
		}
		if (topAddress == null)
		{
			updateSyncTopAddress(true);
		}
		
	}
	
	/**
	 * update selected address in synchronizer if update is true.
	 */
	private void updateSyncSelectedAddress(boolean update) {
		
		if (update)
			getMemoryBlockViewSynchronizer().setSynchronizedProperty(getMemoryBlock(), IMemoryViewConstants.PROPERTY_SELECTED_ADDRESS, fSelectedAddress);
	}

	/**
	 * update column size in synchronizer
	 */
	private void updateSyncColSize() {
		getMemoryBlockViewSynchronizer().setSynchronizedProperty(getMemoryBlock(), IMemoryViewConstants.PROPERTY_COL_SIZE, new Integer(fColumnSize));
	}
	
	/**
	 * update top visible address in synchronizer
	 */
	protected void updateSyncTopAddress(boolean updateToSynchronizer) {
		
		if (updateToSynchronizer)
		{
			getMemoryBlockViewSynchronizer().setSynchronizedProperty(getMemoryBlock(), IMemoryViewConstants.PROPERTY_TOP_ADDRESS, getTopVisibleAddress());
		}
	}

	protected void setTabName(IMemoryBlock newMemory, boolean showAddress)
	{
		if (!fUpdateTabLabel)
			return;
		
		String tabName = null;
		
		if (getMemoryBlockPresentation() != null)
			tabName = getMemoryBlockPresentation().getTabLabel(newMemory, getRenderingId());
		
		if (tabName == null)
		{
		
			tabName = ""; //$NON-NLS-1$
			try {			
				if (newMemory instanceof IMemoryBlockExtension)
				{
					tabName = ((IMemoryBlockExtension)newMemory).getExpression();
					
					if (tabName.startsWith("&")) //$NON-NLS-1$
						tabName = "&" + tabName; //$NON-NLS-1$
					
					if (tabName == null)
					{
						tabName = DebugUIMessages.getString(UNKNOWN);
					}
					
					if (showAddress && ((IMemoryBlockExtension)newMemory).getBigBaseAddress() != null)
					{	
						tabName += " : 0x"; //$NON-NLS-1$
						tabName += ((IMemoryBlockExtension)newMemory).getBigBaseAddress().toString(16);
					}
				}
				else
				{
					long address = newMemory.getStartAddress();
					tabName = Long.toHexString(address);
				}
			} catch (DebugException e) {
				tabName = DebugUIMessages.getString(UNKNOWN);					
				DebugUIPlugin.log(e.getStatus());
			}
			
			String preName = MemoryRenderingManager.getMemoryRenderingManager().getRenderingTypeById(getRenderingId()).getName();
			
			if (preName != null)
				tabName += " <" + preName + ">"; //$NON-NLS-1$ //$NON-NLS-2$

		}
		fTabItem.setText(tabName);	
	}


	/**
	 * Create actions for the view tab
	 */
	protected void createActions() {
		fCopyToClipboardAction = new CopyViewTabToClipboardContextAction(this);
		fGoToAddressAction = new GoToAddressAction(this);
		fResetMemoryBlockAction = new ResetMemoryBlockContextAction(this);
		fPrintViewTabAction = new PrintViewTabContextAction(this);
		
		fFormatColumnActions = new Action[6];
		fFormatColumnActions[0] =  new FormatColumnAction(1, this);
		fFormatColumnActions[1] =  new FormatColumnAction(2, this);
		fFormatColumnActions[2] =  new FormatColumnAction(4, this);
		fFormatColumnActions[3] =  new FormatColumnAction(8, this);
		fFormatColumnActions[4] =  new FormatColumnAction(16, this);
		fFormatColumnActions[5] =  new SetColumnSizeDefaultAction(this);
		
		fReformatAction = new ReformatAction(this);
		fShowAddColumnAction = new ShowAddressColumnAction(this);
	}


	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.IMemoryViewTab#fillContextMenu(org.eclipse.jface.action.IMenuManager)
	 */
	public void fillContextMenu(IMenuManager menu) {
	
		menu.add(new Separator(MEMORY_ACTIONS_GROUP));
		menu.add(fResetMemoryBlockAction);
		menu.add(fGoToAddressAction);
	
		menu.add(new Separator());
		
		if (fFormatColumnActions.length > 0)
		{
			// Format view tab actions
			IMenuManager formatMenu = new MenuManager(DebugUIMessages.getString(COLUMN_SIZE), 
				MEMORY_ACTIONS_FORMAT_GROUP);
				
			menu.appendToGroup(MEMORY_ACTIONS_GROUP, formatMenu);
		
			for (int i=0; i<fFormatColumnActions.length; i++)
			{
				formatMenu.add(fFormatColumnActions[i]);	
		
				// add check mark to the action to reflect current format of the view tab
				if (fFormatColumnActions[i] instanceof FormatColumnAction)
				{
					if (((FormatColumnAction)fFormatColumnActions[i]).getColumnSize() == getColumnSize())
					{
						fFormatColumnActions[i].setChecked(true);
					}
					else
					{
						fFormatColumnActions[i].setChecked(false);
					}
				}
			}
		}
		
		menu.add(new Separator());
		menu.add(fReformatAction);
		menu.add(fShowAddColumnAction);
		menu.add(new Separator());
		menu.add(fCopyToClipboardAction);
		menu.add(fPrintViewTabAction);
	}

	private Control createFolderPage(AbstractMemoryRenderer renderer) {
		
		contentProvider = new MemoryViewContentProvider(fMemoryBlock, fTabItem);
		fTableViewer= new TableViewer(fTabItem.getParent(),  SWT.FULL_SELECTION | SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.HIDE_SELECTION | SWT.BORDER);
		fTableViewer.setContentProvider(contentProvider);		
		
		if (renderer != null)
		{	
			renderer.setRenderingId(getRenderingId());
			MemoryViewTabLabelProvider labelProvider = new MemoryViewTabLabelProvider(this, renderer);
			fTableViewer.setLabelProvider(labelProvider);
			((AbstractTableViewTabLabelProvider)labelProvider).setViewTab(this);
		}
		else
		{	
			renderer = new EmptyRenderer();
			renderer.setRenderingId(getRenderingId());
			renderer.setViewTab(this);

			MemoryViewTabLabelProvider labelProvider = new MemoryViewTabLabelProvider(this, renderer);
			fTableViewer.setLabelProvider(labelProvider);

			DebugUIPlugin.log(DebugUIPlugin.newErrorStatus("Renderer property is not defined for: " + getRenderingId(), null)); //$NON-NLS-1$
		}
		
		contentProvider.setViewer(fTableViewer);
		
		ScrollBar scroll = ((Table)fTableViewer.getControl()).getVerticalBar();
		scroll.addSelectionListener(this);
		scroll.setMinimum(-100);
		scroll.setMaximum(200);
		
		fTableViewer.getControl().addControlListener(this);
		fTableViewer.getControl().addKeyListener(this);	

		fTableViewer.getTable().setHeaderVisible(true);
		fTableViewer.getTable().setLinesVisible(true);
		
		int bytePerLine = IInternalDebugUIConstants.BYTES_PER_LINE;
		
		// get default column size from preference store
		IPreferenceStore prefStore = DebugUIPlugin.getDefault().getPreferenceStore();
		int columnSize = prefStore.getInt(IDebugPreferenceConstants.PREF_COLUMN_SIZE);
		
		// check synchronized col size
		Integer colSize = (Integer)getSynchronizedProperty(IMemoryViewConstants.PROPERTY_COL_SIZE);
		
		if (colSize != null)
		{
			int syncColSize = colSize.intValue(); 
			if (syncColSize > 0)
			{
				columnSize = syncColSize;
			}	
		}
		
		// format memory block with specified "bytesPerLine" and "columnSize"	
		boolean ok = format(bytePerLine, columnSize);

		if (!ok)
		{
			DebugException e = new DebugException(DebugUIPlugin.newErrorStatus(DebugUIMessages.getString(FORMAT_IS_INVALID), null));
			displayError(e);
			return fTextViewer.getControl();
		}
		
		fTableViewer.setInput(fMemoryBlock);
		
		fCellModifier = new MemoryViewCellModifier(this);
		fTableViewer.setCellModifier(fCellModifier);

		// set to a non-proportional font
		fTableViewer.getTable().setFont(JFaceResources.getFont(IInternalDebugUIConstants.FONT_NAME));
		
		int row = 0;
		int col = 1;
		
		// set up cursor manager
		// manager sets up initial position of the cursor
		fCursorManager = new ViewTabCursorManager(this, row, col, fMenuMgr);
		
		if (fMemoryBlock instanceof IMemoryBlockExtension)
		{
			BigInteger address = ((IMemoryBlockExtension)fMemoryBlock).getBigBaseAddress();
			
			if (address == null)
			{
				address = new BigInteger("0"); //$NON-NLS-1$
			}
			
			BigInteger syncAddress = (BigInteger)getSynchronizedProperty(IMemoryViewConstants.PROPERTY_SELECTED_ADDRESS);
			
//			set initial selected address
			if (syncAddress != null)
			{
				setSelectedAddress(syncAddress, false);
			}
			else
			{
				setSelectedAddress(address, true);
			}
			updateCursorPosition();
		}
		else
		{
			long address = fMemoryBlock.getStartAddress();	
			BigInteger syncAddress = (BigInteger)getSynchronizedProperty(IMemoryViewConstants.PROPERTY_SELECTED_ADDRESS);
			
			if (syncAddress != null)
			{
//				set initial selected address
				setSelectedAddress(syncAddress, false);
			}
			else
			{
				setSelectedAddress(BigInteger.valueOf(address), true);
			}
			updateCursorPosition();
		}
		
		// add font change listener and update font when the font has been changed
		fFontChangeListener = new FontChangeListener();
		JFaceResources.getFontRegistry().addListener(fFontChangeListener);

		// finish initialization and return text viewer as the control
		if (errorOccurred)
		{
			return fTextViewer.getControl();
		}
		
		return fTableViewer.getControl();
	}
	

	/**
	 * Format view tab based on parameters.
	 * @param bytesPerLine - number of bytes per line, possible values: 16
	 * @param columnSize - number of bytes per column, possible values: 1, 2, 4, 8
	 * @return true if format is successful, false, otherwise
	 */
	public boolean format(int bytesPerLine, int columnSize)
	{			
		// check parameter, bytesPerLine be 16
		if (bytesPerLine != IInternalDebugUIConstants.BYTES_PER_LINE)
		{
			return false;
		}
		// bytes per cell must be divisible to bytesPerLine
		if (bytesPerLine % columnSize != 0)
		{
			return false;
		}
		
		// do not format if the view tab is already in that format
		if(fBytePerLine == bytesPerLine && fColumnSize == columnSize){
			return false;
		}
		
		fBytePerLine = bytesPerLine;
		fColumnSize = columnSize;
		
		// if the tab is already created and is being reformated
		if (fTabCreated)
		{
			getTopVisibleAddress();
			
			if (fTableViewer == null)
				return false;
			
			if (fTableViewer.getTable() == null)
				return false;
			
			// clean up old columns
			TableColumn[] oldColumns = fTableViewer.getTable().getColumns();
			
			for (int i=0; i<oldColumns.length; i++)
			{
				oldColumns[i].dispose();
			}
			
			// clean up old cell editors
			CellEditor[] oldCellEditors = fTableViewer.getCellEditors();
			
			for (int i=0; i<oldCellEditors.length; i++)
			{
				oldCellEditors[i].dispose();
			}
		}
		
		TableColumn column0 = new TableColumn(fTableViewer.getTable(),SWT.LEFT,0);
		column0.setText(DebugUIMessages.getString(ADDRESS));
		
		// create new byte columns
		TableColumn [] byteColumns = new TableColumn[bytesPerLine/columnSize];		
		
		String[] columnLabels = new String[0];
		if (getMemoryBlockPresentation() != null)
			columnLabels = getMemoryBlockPresentation().getColumnLabels(getMemoryBlock(), bytesPerLine, columnSize);
		
		for (int i=0;i<byteColumns.length; i++)
		{
			TableColumn column = new TableColumn(fTableViewer.getTable(), SWT.LEFT, i+1);
			
			// if the number of column labels returned is correct
			// use supplied column labels
			if (columnLabels.length == byteColumns.length)
			{
				column.setText(columnLabels[i]);
			}
			else
			{
				// otherwise, use default
				if (getColumnSize() >= 4)
				{
					column.setText(Integer.toHexString(i*columnSize).toUpperCase() + 
						" - " + Integer.toHexString(i*columnSize+columnSize-1).toUpperCase()); //$NON-NLS-1$
				}
				else
				{
					column.setText(Integer.toHexString(i*columnSize).toUpperCase());
				}
			}
		}
		
		//Empty column for cursor navigation
		TableColumn emptyCol = new TableColumn(fTableViewer.getTable(),SWT.LEFT,byteColumns.length+1);
		emptyCol.setText(" "); //$NON-NLS-1$
		emptyCol.setWidth(1);
		emptyCol.setResizable(false);

		// +2 to include properties for address and navigation column
		String[] columnProperties = new String[byteColumns.length+2];
		columnProperties[0] = MemoryViewLine.P_ADDRESS;

		// use column beginning offset to the row address as properties
		for (int i=1; i<columnProperties.length-1; i++)
		{
			columnProperties[i] = Integer.toHexString((i-1)*columnSize);
		}
		
		// Empty column for cursor navigation
		columnProperties[columnProperties.length-1] = " "; //$NON-NLS-1$
		
		fTableViewer.setColumnProperties(columnProperties);		
		
		// create and set cell editors
		fTableViewer.setCellEditors(getCellEditors());	
		
		if (fTabCreated)
		{
			refreshTableViewer();	

			// after refresh, make sure cursor position is up-to-date
			if (isAddressVisible(fSelectedAddress))
				updateCursorPosition();
		}
		
		packColumns();
		
		updateSyncColSize();
		
		return true;
	}
	
	/**
	 * 
	 */
	private void refreshTableViewer() {
		
		int i = fTableViewer.getTable().getTopIndex();
		
		// refresh if the view is already created
		fTableViewer.refresh();
		
		// if top index has changed, restore it
		if (i != fTableViewer.getTable().getTopIndex())
			fTableViewer.getTable().setTopIndex(i);
	}

	private void setColumnHeadings()
	{
		String[] columnLabels = new String[0];

		if (getMemoryBlockPresentation() != null)
			columnLabels = getMemoryBlockPresentation().getColumnLabels(getMemoryBlock(), fBytePerLine, fColumnSize);		
		
		int numByteColumns = fBytePerLine/fColumnSize;
		
		TableColumn[] columns = fTableViewer.getTable().getColumns();
		
		int j=0;
		for (int i=1; i<columns.length-1; i++)
		{	
			// if the number of column labels returned is correct
			// use supplied column labels
			if (columnLabels.length == numByteColumns)
			{
				columns[i].setText(columnLabels[j]);
				j++;
			}
			else
			{
				// otherwise, use default
				if (fColumnSize >= 4)
				{
					columns[i].setText(Integer.toHexString(i*fColumnSize).toUpperCase() + 
							" - " + Integer.toHexString(i*fColumnSize+fColumnSize-1).toUpperCase()); //$NON-NLS-1$
				}
				else
				{
					columns[i].setText(Integer.toHexString(i*fColumnSize).toUpperCase());
				}
			}
		}
	}
	
	/**
	 * Resize column to the preferred size
	 */
	public void packColumns() {
		// pack columns
		Table table = fTableViewer.getTable();
		TableColumn[] columns = table.getColumns();
		
		for (int i=0 ;i<columns.length-1; i++)
		{	
			columns[i].pack();
		}
		
		if (fCursorManager != null)
		{
			if (isAddressVisible(fSelectedAddress))
				fCursorManager.redrawCursors();
		}
		
		if (!fShowAddressColumn)
		{
			columns[0].setWidth(0);
		}
	}

	/**
	 * @return tab item for the view tab
	 */
	protected TabItem getTab()
	{
		return fTabItem;
	}
	
	/**
	 * Force focus on th ecursor if the selected address is not out of range
	 * Cursor cannot be shown if it's out of range.  Otherwise, it messes up
	 * the top index of the table and affects scrolling.
	 */
	protected void setCursorFocus()
	{
		if (!isAddressOutOfRange(fSelectedAddress) && fCursorManager != null)
			fCursorManager.setCursorFocus();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.IMemoryViewTab#getMemoryBlock()
	 */
	public IMemoryBlock getMemoryBlock()
	{
		IMemoryBlock mem = fMemoryBlock;
		return mem;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPart#dispose()
	 */
	public void dispose() {
		try {
			fIsDisposed = true;
			
			// clean up listeners
			if (fTableViewer != null)
			{
				fTableViewer.getControl().removeControlListener(this);
				fTableViewer.getControl().removeKeyListener(this);
				fTableViewer.getTable().removeSelectionListener(this);
			}
			
			if (contentProvider != null)
				contentProvider.dispose();
			
			ScrollBar scroll = ((Table)fTableViewer.getControl()).getVerticalBar();
			
			if (scroll != null)
				scroll.removeSelectionListener(this);
			
			// dispose cursor
			if (fCursorManager != null)
				fCursorManager.dispose();

			// remove selection listener for tab folder
			fTabItem.removeDisposeListener(fTabFolderDisposeListener);
			fTabItem.dispose();
			
			fTextViewer = null;
			fTableViewer = null;
			
			// clean up cell editors
			for (int i=0; i<fEditors.length; i++)
			{
				fEditors[i].dispose();
			}
			
			// remove font change listener when the view tab is disposed
			JFaceResources.getFontRegistry().removeListener(fFontChangeListener);
			
			// remove the view tab from the synchronizer
			getMemoryBlockViewSynchronizer().removeView(this);
			
			super.dispose();

		} catch (Exception e) {}
	}


	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	public void widgetSelected(SelectionEvent event) {

		if (event.getSource() instanceof ScrollBar)
		{
			handleScrollBarSelection(event);
		}
	}
	
	/**
	 * Based on cursor position, update table selection.
	 * If a lead cursor is not available, the cursor is not visible.
	 * Update will not be performed if the cursor is not visible.
	 */
	protected void updateTableSelection()
	{
		// do not update selection if address is out of range
		// otherwise, screws up top index
		if (isAddressOutOfRange(fSelectedAddress))
			return;
		
		int index = findAddressIndex(getTopVisibleAddress());

		// update table selection
		fTableViewer.getTable().setSelection(fCursorManager.fRow);
		
		// if top index has changed, restore
		if (fTableViewer.getTable().getTopIndex() != index)
			fTableViewer.getTable().setTopIndex(index);
	}
	
	/**
	 * Calculate and set selected address based on provided row and column
	 */
	protected void updateSelectedAddress(TableItem row, int col)
	{
	
		// get row address
		String temp = ((MemoryViewLine)row.getData()).getAddress();
		BigInteger rowAddress = new BigInteger(temp, 16);
		
		int offset;
		if (col > 0)
		{	
			// 	get address offset
			offset = (col-1) * getColumnSize();
		}
		else
		{
			offset = 0;
		}
		
		// update selected address
		setSelectedAddress(rowAddress.add(BigInteger.valueOf(offset)), true);		
	}


	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	public void widgetDefaultSelected(SelectionEvent e) {
		 
		
	}


	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.ControlListener#controlMoved(org.eclipse.swt.events.ControlEvent)
	 */
	public void controlMoved(ControlEvent e) {
		 
		
	}


	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.ControlListener#controlResized(org.eclipse.swt.events.ControlEvent)
	 */
	public void controlResized(ControlEvent e) {
		//this method gets called many times as the user drags the window to a new size
		//TODO: only refresh the data at the end of the resize, if possible
		
		// do not handle resize if the tab is not yet created completely
		if (fTabCreated)		
			resizeTable();
		
	}
	
	/**
	 * Handles key events in viewer.
	 */
	protected void handleKeyPressed(KeyEvent evt) {

		final KeyEvent event = evt;
		
		// Must run on UI Thread asynchronously
		// Otherwise, another event could have been recevied before the reload is completed
		Display.getDefault().syncExec(new Runnable()
		{			
			public void run()
			{
				if (event.stateMask != 0)
				{
					return;
				}
				
				if (event.getSource() instanceof Text)
						return;
				
				// allow edit if user hits return
				if (event.character == '\r' && event.getSource() instanceof TableCursor)
				{
					fCursorManager.activateCellEditor(null);
					return;
				}
				
				try
				{	
					switch (event.keyCode)
					{	
						case SWT.HOME :
						case SWT.PAGE_UP :
						case SWT.ARROW_UP :
						case SWT.ARROW_LEFT:
						case SWT.END :
						case SWT.PAGE_DOWN :
						case SWT.ARROW_DOWN :
						case SWT.ARROW_RIGHT:
							// If blocking an extended memory block,
							// check to see if additional memory needs to be obtained.
							if (fMemoryBlock instanceof IMemoryBlockExtension)
							{
								// User could have used scroll bar to scroll away
								// from the highlighted address.
								// When user hits arrow keys or page up/down keys
								// we should go back to the selected address and moves the cursor
								// based on the key pressed.
								if (isAddressOutOfRange(fSelectedAddress))
								{
									reloadTable(fSelectedAddress, false);
									
									updateSyncTopAddress(true);
									updateSyncSelectedAddress(true);
									
									fCursorManager.setCursorFocus();
									break;
								}
								//if we are approaching the limits of the currently loaded memory, reload the table
								if (needMoreLines())
								{
									BigInteger topAddress = getTopVisibleAddress();
									//if we're near 0, just go there immediately (hard stop at 0, don't try to scroll/wrap)
									if (topAddress.compareTo(BigInteger.valueOf(96)) <= 0)
									{
										if (topAddress.equals(BigInteger.valueOf(0)))
										{
											// do not reload if we are already at zero
											break;
										}
										reloadTable(BigInteger.valueOf(0), false);
										fCursorManager.setCursorFocus();
									}
									else
									{
										//otherwise, just load the next portion of the memory
										reloadTable(topAddress, false);
										fCursorManager.setCursorFocus();
									}
								}
								else if (!isAddressVisible(fSelectedAddress))
								{
									// address is in range, but not visible
									// just go to the address and make sure
									// that the cursor is in focus
									
									goToAddress(fSelectedAddress);
									fCursorManager.setCursorFocus();
									updateSyncTopAddress(true);
									
								}
								else
								{
									// in place of the commented lines
									updateCursorPosition();
									fCursorManager.setCursorFocus();
									// since cursor is going to be visible
									// synchronization event will be fired by the cursor
									// when it is selected
								}
						}

							break;
						default :
							
							// if it's a valid key for edit
							if (isValidEditEvent(event.keyCode))
							{	
								// activate edit as soon as user types something at the cursor
								if (event.getSource() instanceof TableCursor)
								{
									String initialValue = String.valueOf(event.character);
									fCursorManager.activateCellEditor(initialValue);
								}
							}
							break;									
					}
				}
				catch (DebugException e)
				{
					displayError(e);
					DebugUIPlugin.log(e.getStatus());
				}
			}
		});
	}
		
	/**
	 * @return top visible address of this view tab
	 */
	public BigInteger getTopVisibleAddress() {
		
		if (fTableViewer == null)
			return BigInteger.valueOf(0);

		Table table = fTableViewer.getTable();
		int topIndex = table.getTopIndex();

		if (topIndex < 1) { topIndex = 0; }

		if (table.getItemCount() > topIndex) 
		{
			MemoryViewLine topItem = (MemoryViewLine)table.getItem(topIndex).getData();
			
			String calculatedAddress = null;
			if (topItem == null)
			{
				calculatedAddress = table.getItem(topIndex).getText();
			}
			else
			{
				calculatedAddress = topItem.getAddress();				
			}
			
			BigInteger bigInt = new BigInteger(calculatedAddress, 16);
			
			return bigInt;
		}
		return BigInteger.valueOf(0);
	}

	/**
	 * Reload table at the topAddress.
	 * Delta will be re-computed if updateDelta is true.
	 * @param topAddress
	 * @param updateDelta
	 * @throws DebugException
	 */
	synchronized protected void reloadTable(BigInteger topAddress, boolean updateDelta) throws DebugException{
		
		if (fTableViewer == null)
			return;
			
		Table table = (Table)fTableViewer.getControl();	

		// Calculate top buffer address
		// This is where we will start asking for memory from debug adapter.
		BigInteger topBufferAddress = topAddress;
		if (topBufferAddress.compareTo(BigInteger.valueOf(32)) <= 0) {
			TABLE_PREBUFFER = 0;
		} else {
			TABLE_PREBUFFER = topBufferAddress.divide(BigInteger.valueOf(32)).min(BigInteger.valueOf(TABLE_DEFAULTBUFFER)).intValue();
		}

		topBufferAddress = topAddress.subtract(BigInteger.valueOf(getBytesPerLine()*TABLE_PREBUFFER));

		// calculate number of lines needed
		long numLines = 0;
		if (fMemoryBlock instanceof IMemoryBlockExtension)
		{
			// number of lines is number of visible lines + buffered lines
			numLines = getNumberOfVisibleLines()+TABLE_PREBUFFER+TABLE_POSTBUFFER;
		}


		// tell content provider to get memory and refresh
		contentProvider.getMemoryToFitTable(topBufferAddress, numLines, updateDelta);
		contentProvider.forceRefresh();
		

		if (fMemoryBlock instanceof IMemoryBlockExtension)
		{
			int topIdx = findAddressIndex(topAddress);
			
			if (topIdx != -1)
			{
				table.setTopIndex(topIdx);
			}
			
			// TODO:  Revisit this part again
			// if allow cursor update when the cursor is
			// not visible, causes flashing on the screen
			// if not updated... then cursor may not
			// show properly (table selection not hidden)
			// if selected address is not out of range
			// restore cursor
			if (isAddressVisible(fSelectedAddress) && findAddressIndex(fSelectedAddress) != -1)
			{
				getTopVisibleAddress();
				getTopVisibleAddress().add(BigInteger.valueOf(getBytesPerLine()*getNumberOfVisibleLines()));

				// if the cursor is not visible but in buffered range
				// updating and showing the cursor will move the top index of the table
				updateCursorPosition();
				
				int newIdx = findAddressIndex(getTopVisibleAddress());
				
				if (newIdx != topIdx  && topIdx != -1)
				{	
					table.setTopIndex(topIdx);
				}
							
				if (isAddressVisible(fSelectedAddress))
				{
					fCursorManager.showCursor();	
				}
				else
				{
					fCursorManager.hideCursor();
				}
			}
			else
			{
				fCursorManager.hideCursor();
			}
		}		
		
		// try to display the table every time it's reloaded
		displayTable();
	}
	
	private int findAddressIndex(BigInteger address)
	{
		TableItem items[] = fTableViewer.getTable().getItems();
	
		for (int i=0; i<items.length; i++){
			
			// Again, when the table resizes, the table may have a null item
			// at then end.  This is to handle that.
			if (items[i] != null)
			{	
				MemoryViewLine line = (MemoryViewLine)items[i].getData();
				BigInteger lineAddress = new BigInteger(line.getAddress(), 16);
				BigInteger endLineAddress = lineAddress.add(BigInteger.valueOf(getBytesPerLine()));
				
				if (lineAddress.compareTo(address) <= 0 && endLineAddress.compareTo(address) > 0)
				{	
					return i;
				}
			}
		}
		
		return -1;
	}
	
	/**
	 * Update cursor position based on selected address.
	 * @return true if cursor is visible, false otherwise
	 */
	private boolean updateCursorPosition()
	{			
		// selected address is out of range, simply return false
		if (fSelectedAddress.compareTo(contentProvider.getBufferTopAddress()) < 0)
			return false;
		
		// calculate selected row address
		int numOfRows = fSelectedAddress.subtract(contentProvider.getBufferTopAddress()).intValue()/getBytesPerLine();
		BigInteger rowAddress = contentProvider.getBufferTopAddress().add(BigInteger.valueOf(numOfRows * getBytesPerLine()));

		// try to find the row of the selected address
		int row = findAddressIndex(fSelectedAddress);
			
		if (row == -1)
		{
			return false;
		}
		
		// calculate offset to the row address
		BigInteger offset = fSelectedAddress.subtract(rowAddress);
		
		// locate column
		int col = ((offset.intValue()/getColumnSize())+1);
		
		// setting cursor selection or table selection changes
		// the top index of the table... and may mess up top index in the talbe
		// save up old top index
		int oldTop = fTableViewer.getTable().getTopIndex();
		
		// update cursor position and table selection
		fCursorManager.updateCursorPosition(row, col, isAddressVisible(fSelectedAddress));
		updateTableSelection();

		// reset top index to make sure the table is not moved
		fTableViewer.getTable().setTopIndex(oldTop);
		
		if (isAddressVisible(fSelectedAddress))
		{	
			fCursorManager.showCursor();
			fTableViewer.getTable().deselectAll();
		}
		else
			fCursorManager.hideCursor();
		
		return true;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ITableMemoryViewTab#getNumberOfVisibleLines()
	 */
	public int getNumberOfVisibleLines()
	{
		if(fTableViewer == null)
			return -1;
		
		Table table = fTableViewer.getTable();
		int height = fTableViewer.getTable().getSize().y;
		
		// when table is not yet created, height is zero
		if (height == 0)
		{
			// make use of the table viewer to estimate table size
			height = fTableViewer.getTable().getParent().getSize().y;
		}
		
		// height of border
		int border = fTableViewer.getTable().getHeaderHeight();
		
		// height of scroll bar
		int scroll = fTableViewer.getTable().getHorizontalBar().getSize().y;

		// height of table is table's area minus border and scroll bar height		
		height = height-border-scroll;

		// calculate number of visible lines
		int lineHeight = table.getItemHeight();
		
		int numberOfLines = height/lineHeight;
	
		return numberOfLines;		
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.IMemoryViewTab#refresh()
	 */
	public void refresh()
	{
		try {
			
			// refresh at start address of this memory block
			// address may change if expression is evaluated to a different value
			IMemoryBlock mem = fMemoryBlock;
			BigInteger address;
			
			if (mem instanceof IMemoryBlockExtension)
			{
				address = ((IMemoryBlockExtension)mem).getBigBaseAddress();
				
				if (address == null)
				{	
					DebugException e = new DebugException(DebugUIPlugin.newErrorStatus(DebugUIMessages.getString(UNABLE_TO_GET_BASE_ADDRESS), null));
					displayError(e);
					return;
				}
				
				setTabName(mem, true);
				
				// base address has changed
				if (address.compareTo(contentProvider.getContentBaseAddress()) != 0)
				{
					// get to new address
					reloadTable(address, true);
				}
				else
				{
					// reload at top of table
//					address = contentProvider.getBufferTopAddress().add(BigInteger.valueOf(getBytesPerLine()*TABLE_PREBUFFER));
					address = getTopVisibleAddress();
					reloadTable(address, true);
				}				
			}
			else
			{
				address = BigInteger.valueOf(mem.getStartAddress());
				reloadTable(address, true);
			}
			
			if (isAddressVisible(fSelectedAddress))
			{	
				// redraw cursors if cursor is visible					
				getCursorManager().redrawCursors();
			}
					
		} catch (DebugException e) {
			displayError(e);
			DebugUIPlugin.log(e.getStatus());
		}
	}
	
	/**
	 * Handle resize of the table.
	 */
	private void resizeTable() {
		
		if (!(fMemoryBlock instanceof IMemoryBlockExtension))
			return;
		
		if (!isEnabled())
			return;
			
		Display.getDefault().syncExec(new Runnable()
		{
			public void run()
			{
				boolean reloaded = false;
				
				// this code is is running on the UI thread with a delay
				// The view tab may have been disposed when this actually gets executed.
				if (fTableViewer == null)
					return;
				
				Table table = fTableViewer.getTable();
				
				// make sure table is still valid
				if (table.isDisposed())
					return;
				
				int topIndex = table.getTopIndex();
				if (topIndex < 0)
				{
					return;
				}
				BigInteger oldTopAddress = getTopVisibleAddress();
				if (oldTopAddress.compareTo(BigInteger.valueOf(32)) <= 0)
				{
					TABLE_PREBUFFER = 0;
				}
				else
				{
					TABLE_PREBUFFER =
						oldTopAddress.divide(BigInteger.valueOf(32)).min(BigInteger.valueOf(TABLE_DEFAULTBUFFER)).intValue();
				}
				
				// check pre-condition before we can check on number of lines left in the table
				if (table.getItemCount() > topIndex)
				{
					try
					{
						//if new window size exceeds the number of lines available in the table, reload the table
						if (needMoreLines())
						{
							reloadTable(oldTopAddress, false);
							reloaded = true;
						}
						if (oldTopAddress.compareTo(BigInteger.valueOf(96)) <= 0)
						{
							reloadTable(BigInteger.valueOf(0), false);
							reloaded = true;
						}
					}
					catch (DebugException e)
					{
						displayError(e);
						DebugUIPlugin.log(e.getStatus());
					}
				}
				
				if (!reloaded){
					// if not reload, still need to update the cursor position
					// since the position may change
					
					updateCursorPosition();
					fTableViewer.getTable().deselectAll();
					
					if (!getTopVisibleAddress().equals(oldTopAddress))
					{	
						int i = findAddressIndex(oldTopAddress);
						
						if (i != -1)
							fTableViewer.getTable().setTopIndex(i);
					}
				}
				
				updateSyncTopAddress(true);
			}
		});
	}
	
	/**
	 * Handle scrollling and reload table if necessary
	 * @param event
	 */
	private void handleScrollBarSelection(SelectionEvent event)
	{	
		if (!(fMemoryBlock instanceof IMemoryBlockExtension))
		{
			// if not instance of extended memory block
			// just get current top visible address and fire event
			
			updateSyncTopAddress(true);
	
		}			
		
		final SelectionEvent evt = event;
		
		// Must run on UI Thread asynchronously
		// Otherwise, another event could have been recevied before the reload is completed
		Display.getDefault().asyncExec(new Runnable()
		{
			public void run()
			{
				try
				{	
					switch (evt.detail)
					{
						case 0 : //the end of a drag
						case SWT.END :
						case SWT.PAGE_DOWN :
						case SWT.ARROW_DOWN :
						case SWT.HOME :
						case SWT.PAGE_UP :
						case SWT.ARROW_UP :
							if (fMemoryBlock instanceof IMemoryBlockExtension)
							{
								updateSyncTopAddress(true);
								//if we are approaching the limits of the currently loaded memory, reload the table
								if (needMoreLines())
								{
									BigInteger topAddress = getTopVisibleAddress();
									//if we're near 0, just go there immediately (hard stop at 0, don't try to scroll/wrap)
									if (topAddress.compareTo(BigInteger.valueOf(96)) <= 0)
									{
										if (topAddress.equals(BigInteger.valueOf(0)))
										{
											// do not reload if we are already at zero
											break;
										}
										reloadTable(BigInteger.valueOf(0), false);
									}
									else
									{

										//otherwise, just load the next portion of the memory
										reloadTable(topAddress, false);
									}
								}
							}
							if (isAddressVisible(fSelectedAddress))
							{
								updateCursorPosition();
								fCursorManager.setCursorFocus();
							}
							break;
						default:
							break;
					}
				}
				catch (DebugException e)
				{
					displayError(e);
					DebugUIPlugin.log(e.getStatus());
				}
			}
		});
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.IMemoryViewTab#setEnabled(boolean)
	 */
	public void setEnabled(boolean enable)
	{	
		super.setEnabled(enable);
		
		fEnabled = enable;
		IMemoryBlock mem = fMemoryBlock;
		
		if (fEnabled)
		{
			BigInteger oldBase = contentProvider.getContentBaseAddress();

			// debug adapter may ignore the enable request
			// some adapter does not block memory and may not do anything
			// with the enable/disable request
			// As a result, we need to force a refresh
			// and to make sure content is updated
			refresh();
			
			if (mem instanceof IMemoryBlockExtension)
			{
				BigInteger baseAddress = ((IMemoryBlockExtension)mem).getBigBaseAddress();
				
				if (baseAddress == null)
				{
					if (fSelectedAddress != null)
						baseAddress = fSelectedAddress;
					else
						baseAddress = new BigInteger("0"); //$NON-NLS-1$
				}
				
				Object[] connected = ((IMemoryBlockExtension)mem).getConnected();				
				
				// if the base address has changed, update cursor
				// and this is the first time this memory block is enabled
				if (!baseAddress.equals(oldBase) && connected.length == 1)
				{
					setSelectedAddress(baseAddress, true);
					updateCursorPosition();
					
					updateSyncTopAddress(true);
					updateSyncSelectedAddress(true);
				}
				else
				{
					// otherwise, take synchronized settings
					synchronize();
				}
			}
			else
			{
				synchronize();
			}
		}
		else
		{
			if (mem instanceof IMemoryBlockExtension)
			{	
				setTabName(mem, false);
			}
				
			// once the view tab is disabled, all deltas information becomes invalid.
			// reset changed information and recompute if data has really changed when
			// user revisits the same tab.	
			contentProvider.resetDeltas();
		}
	}
	public boolean isEnabled()
	{
		return fEnabled;
	}
	
	/**
	 * Display an error in the view tab.
	 * Make use of the text viewer instead of the table viewer.
	 * @param e
	 */
	protected void displayError(DebugException e)
	{
		StyledText styleText = null;
		errorOccurred = true;

		if (fTextViewer == null)
		{
			// create text viewer
			fTextViewer = new TextViewer(fTabItem.getParent(), SWT.NONE);	
			fTabItem.setControl(fTextViewer.getControl());
			fTextViewer.setDocument(new Document());
			styleText = fTextViewer.getTextWidget();
			styleText.setEditable(false);
			styleText.setEnabled(false);
		}
		else if (fTextViewer.getControl() != fTabItem.getControl())
		{	
			// switch to text viewer
			fTabItem.setControl(fTextViewer.getControl());
		}
		
		styleText = fTextViewer.getTextWidget();
		
		if (styleText != null)
			styleText.setText(DebugUIMessages.getString(ERROR) + e);	
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.IMemoryViewTab#isDisplayingError()
	 */
	public boolean isDisplayingError()
	{	
		if(fTextViewer == null)
			return false;
		
		if (fTabItem.getControl() == fTextViewer.getControl()) {
			return true;
		}
		return false;
	}
	
	public void displayTable()
	{
		
		if (fTableViewer!= null && fTabItem.getControl() != fTableViewer.getControl())
		{	
			errorOccurred = false;
			fTabItem.setControl(fTableViewer.getControl());
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.KeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)
	 */
	public void keyPressed(KeyEvent e)
	{
		handleKeyPressed(e);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
	 */
	public void keyReleased(KeyEvent e)
	{	
	}

	/**
	 * @return cell editors for the table
	 */
	private CellEditor[] getCellEditors() {
		Table table = fTableViewer.getTable();
		fEditors = new CellEditor[table.getColumnCount()];
		
		for (int i=0; i<fEditors.length; i++)
		{
			fEditors[i] = new TextCellEditor(table);
		}
		
		return fEditors;
	}	

	public TableViewer getTableViewer()
	{
		return fTableViewer;
	}
	
	protected ViewTabCursorManager getCursorManager()
	{
		return fCursorManager; 
	}
	
	/**
	 * This function must be made synchronized.
	 * Otherwise, another thread could modify the selected address while it is being updated.
	 * It is the case when user scrolls up/down the table using arrow key.
	 * When the table reaches its limit, it is being reloaded.  However, cursor receives a selection
	 * event and update the selected address at the same time.  It messes up the selected address
	 * and causes cursor selection to behave unexpectedly.
	 * @param address
	 */
	synchronized protected void setSelectedAddress(BigInteger address, boolean updateSynchronizer)
	{
		fSelectedAddress = address;
		
		updateSyncSelectedAddress(updateSynchronizer);
	}

	/**
	 * Return the offset from the base address of the memory block.
	 * @param memory
	 * @param lineAddress
	 * @param lineOffset
	 * @return
	 * TODO: this method is never called
	 */
	protected long getOffset(IMemoryBlock memory, String lineAddress, int lineOffset) {
		
		BigInteger lineAddr = new BigInteger(lineAddress, 16);
		BigInteger memoryAddr;
		
		if (memory instanceof IMemoryBlockExtension)
		{
			memoryAddr = ((IMemoryBlockExtension)memory).getBigBaseAddress();
		}
		else
		{
			memoryAddr = BigInteger.valueOf(memory.getStartAddress());
		}
		
		if (memoryAddr == null)
			memoryAddr = new BigInteger("0"); //$NON-NLS-1$
		
		long offset = lineAddr.subtract(memoryAddr).longValue();
		
		return offset + lineOffset;
	}
	
	/**
	 * Reset this view tab to the base address of the memory block
	 */
	public void resetAtBaseAddress() throws DebugException
	{
		try
		{	
			IMemoryBlock mem = getMemoryBlock();
			if (mem instanceof IMemoryBlockExtension)
			{
				// if text editor is activated, removes its focus and commit
				// any changes made
				setCursorFocus();
					
				// reload table at base address	
				BigInteger address = ((IMemoryBlockExtension)mem).getBigBaseAddress();
				
				if (address == null)
				{
					// unable to get the base address
					// pop up error message an do nothing
					Shell shell = DebugUIPlugin.getShell();
					MessageDialog.openError(shell, DebugUIMessages.getString("DebugUITools.Error_1"), DebugUIMessages.getString(UNABLE_TO_GET_BASE_ADDRESS)); //$NON-NLS-1$
					return;
				}
				
				setSelectedAddress(address, true);
				reloadTable(address, false);		
				
				// make sure cursor has focus when the user chooses to reset
				setCursorFocus();
			}
			else
			{
				// go to top of the table 
				BigInteger address = BigInteger.valueOf(mem.getStartAddress());
				setSelectedAddress(address, true);
				getTableViewer().getTable().setTopIndex(0);
				updateCursorPosition();
				updateTableSelection();
				setCursorFocus();
			}
			
			updateSyncTopAddress(true);
		}
		catch (DebugException e)
		{
			throw e;
		}		
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.IMemoryViewTab#goToAddress(java.math.BigInteger)
	 */
	public void goToAddress(BigInteger address) throws DebugException
	{
		goToAddress(address, true);
		fCursorManager.setCursorFocus();
	}

	/**
	 * @param address
	 * @throws DebugException
	 */
	private void goToAddress(BigInteger address, boolean updateSynchronizer) throws DebugException {
		try
		{	
			// if address is within the range, highlight			
			if (!isAddressOutOfRange(address))
			{
				// Defer update so that top visible address is updated before
				// the selected address
				// This is to ensure that the other view tabs get the top
				// visible address change events first in case the selected
				// address is not already visible.
				// If this is not done, the other view tab may not show selected address.
				setSelectedAddress(address, false);
				updateCursorPosition();				
				updateTableSelection();
				
				// force the cursor to be shown
				if (!isAddressVisible(fSelectedAddress))
				{	
					int i = findAddressIndex(fSelectedAddress);
					
					fTableViewer.getTable().showItem(fTableViewer.getTable().getItem(i));
					getCursorManager().showCursor();
					
					updateSyncTopAddress(updateSynchronizer);
				}
				
				// update selected address in synchronizer
				updateSyncSelectedAddress(updateSynchronizer);
			}
			else
			{
				// if not extended memory block
				// do not allow user to go to an address that's out of range
				if (!(fMemoryBlock instanceof IMemoryBlockExtension))
				{
					Status stat = new Status(
					 IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(),
					 DebugException.NOT_SUPPORTED, DebugUIMessages.getString(ADDRESS_IS_OUT_OF_RANGE), null 
					);
					DebugException e = new DebugException(stat);
					throw e;
				}
				
				setSelectedAddress(address, updateSynchronizer);
				
				//otherwise, reload at the address
				reloadTable(address, false);
				updateSyncTopAddress(updateSynchronizer);
			}
		}
		catch (DebugException e)
		{
			throw e;
		}
	}

	/**
	 * @return current column size
	 */
	public int getColumnSize()
	{
		return fColumnSize;
	}

	/**
	 * @return number of bytes per line
	 */
	public int getBytesPerLine()
	{
		return fBytePerLine;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.IMemoryViewTab#setFont(org.eclipse.swt.graphics.Font)
	 */
	public void setFont(Font font)
	{	
		int oldIdx = fTableViewer.getTable().getTopIndex();
		
		// BUG in table, if font is changed when table is not starting
		// from the top, causes table gridline to be misaligned.
		fTableViewer.getTable().setTopIndex(0);
		
		// set font
		fTableViewer.getTable().setFont(font);
		fCursorManager.setFont(font);
		
		fTableViewer.getTable().setTopIndex(oldIdx);
		
		packColumns();
		
		// update table cursor and force redraw
		updateCursorPosition();
	}
	
	/**
	 * @return memory block presentation to allow for customization
	 */
	protected IMemoryBlockModelPresentation getMemoryBlockPresentation()
	{
		// only try to create a model presentation once
		if (fMemoryBlockPresentation == null && !fNoPresentation)
		{
			//	create model presentation for memory block
			 DelegatingModelPresentation presentation = new MemoryViewDelegatingModelPresentation();
			 String id = fMemoryBlock.getModelIdentifier();
			 fMemoryBlockPresentation = (MemoryViewLazyModelPresentation)presentation.getPresentation(id);
			 
			 // if a memory block presentation cannot be retrieved
			 if (fMemoryBlockPresentation == null)
			 	fNoPresentation = true;
		}
		return fMemoryBlockPresentation; 
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.IMemoryViewTab#setTabLabel(java.lang.String)
	 */
	public void setTabLabel(String label)
	{
		if (label != null)
		{
			fUpdateTabLabel = false;
			fTabItem.setText(label);
		}
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.IMemoryViewTab#getTabLabel()
	 */
	public String getTabLabel()
	{
		if (fTabItem != null) {
			return fTabItem.getText();
		}
		return null;	
	}

	/**
	 * Handle column size changed event from synchronizer
	 * @param newColumnSize
	 */
	private void columnSizeChanged(final int newColumnSize)
	{	
//		ignore event if view tab is disabled	
		if (!isEnabled())
			return;
		
		Display.getDefault().asyncExec(new Runnable()
		{
			public void run()
			{
				format(16, newColumnSize);				
			}
		});
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ISynchronizedMemoryBlockView#scrollBarSelectionChanged(int)
	 */
	public void scrollBarSelectionChanged(int newSelection)
	{
		 
		
	}

	/**
	 * Handle selected address change event from synchronizer
	 * @param address
	 */
	private void selectedAddressChanged(final BigInteger address)
	{
		// ignore event if view tab is disabled
		if (!isEnabled())
		{
			return;
		}
		
		try
		{
			if (!fSelectedAddress.equals(address))
			{	
				if (getMemoryBlock() instanceof IMemoryBlockExtension)
				{
					goToAddress(address, false);
				}
				else
				{
					if (!isAddressOutOfRange(address))
					{
						goToAddress(address, false);
					}
				}
			} 	
	
		}
		catch (DebugException e)
		{
			displayError(e);
		}
	}
	
	/**
	 * Handle top visible address change event from synchronizer
	 * @param address
	 */
	private void topVisibleAddressChanged(final BigInteger address)
	{
		try
		{
			// do not handle event if view tab is disabled
			if (!isEnabled())
				return;
			
			if (!address.equals(getTopVisibleAddress()))
			{
				if (getMemoryBlock() instanceof IMemoryBlockExtension)
				{
				
					if (!isAddressOutOfRange(address))
					{
						int index = -1;
						// within buffer range, just set top index
						Table table = getTableViewer().getTable();
						for (int i = 0; i < table.getItemCount(); i++)
						{
							MemoryViewLine line = (MemoryViewLine) table.getItem(i).getData();
							if (line != null)
							{
								BigInteger lineAddress = new BigInteger(line.getAddress(), 16);
								if (lineAddress.equals(address))
								{
									index = i;
									break;
								}
							}
						}
						if (index >= 3 && table.getItemCount() - (index+getNumberOfVisibleLines()) >= 3)
						{
							// update cursor position
							table.setTopIndex(index);
							
							if (!isAddressVisible(fSelectedAddress))
							{
								fCursorManager.hideCursor();
							}
							else
							{
								updateCursorPosition();
								updateTableSelection();
								table.setTopIndex(index);
								
								// BUG 64831:  to get around SWT problem with
								// the table cursor not painted properly after
								// table.setTopIndex is called
								fCursorManager.getLeadCursor().setVisible(false);
								fCursorManager.getLeadCursor().setVisible(true);
							}
						}
						else
						{	
							// approaching limit, reload table
							reloadTable(address, false);	
						}
					}
					else
					{	
						// approaching limit, reload table
						reloadTable(address, false);
					}
				}
				else
				{
					// IMemoryBlock support
					int index = -1;
					// within buffer range, just set top index
					Table table = getTableViewer().getTable();
					for (int i = 0; i < table.getItemCount(); i++)
					{
						MemoryViewLine line = (MemoryViewLine) table.getItem(i).getData();
						if (line != null)
						{
							BigInteger lineAddress = new BigInteger(line.getAddress(), 16);
							if (lineAddress.equals(address))
							{
								index = i;
								break;
							}
						}
					}
					
					if (index >= 0)
					{
						table.setTopIndex(index);
								
						if (!isAddressVisible(fSelectedAddress))
						{
							fCursorManager.hideCursor();
						}
						else
						{
							updateCursorPosition();
							updateTableSelection();
							table.setTopIndex(index);
							
							// BUG 64831:  to get around SWT problem with
							// the table cursor not painted properly after
							// table.setTopIndex is called
							fCursorManager.getLeadCursor().setVisible(false);
							fCursorManager.getLeadCursor().setVisible(true);
						}
					}
				}
			}
		}
		catch (DebugException e)
		{
			displayError(e);
		}
	}
	
	/**
	 * Check if address provided is out of buffered range
	 * @param address
	 * @return if address is out of bufferred range
	 */
	private boolean isAddressOutOfRange(BigInteger address)
	{
		return contentProvider.isAddressOutOfRange(address);
	}
	
	/**
	 * Check if address is visible
	 * @param address
	 * @return if the given address is visible
	 */
	protected boolean isAddressVisible(BigInteger address)
	{
		// if view tab is not yet created 
		// cursor should always be visible
		if (!fTabCreated)
			return true;
		
		BigInteger topVisible = getTopVisibleAddress();
		BigInteger lastVisible = getTopVisibleAddress().add(BigInteger.valueOf((getNumberOfVisibleLines()) * getBytesPerLine() + getBytesPerLine()));
		
		if (topVisible.compareTo(address) <= 0 && lastVisible.compareTo(address) > 0)
		{
			return true;
		}
		return false;
	}

	/**
	 * Get properties from synchronizer and synchronize settings
	 */
	private void synchronize()
	{
		Integer columnSize = (Integer) getSynchronizedProperty(IMemoryViewConstants.PROPERTY_COL_SIZE);
		BigInteger selectedAddress = (BigInteger)getSynchronizedProperty(IMemoryViewConstants.PROPERTY_SELECTED_ADDRESS);
		BigInteger topAddress = (BigInteger)getSynchronizedProperty(IMemoryViewConstants.PROPERTY_TOP_ADDRESS);
		
		if (columnSize != null)
		{
			int colSize = columnSize.intValue();	
			
			if (colSize > 0 && colSize != fColumnSize)
			{
				columnSizeChanged(colSize);
			}
		}
		
		if (topAddress != null)
		{
			if (!topAddress.equals(getTopVisibleAddress()))
			{
				if (!fSelectedAddress.equals(selectedAddress))
				{
					setSelectedAddress(selectedAddress, true);
				}
				
				topVisibleAddressChanged(topAddress);
			}
		}		
		
		if (selectedAddress != null)
		{
			if (selectedAddress.compareTo(fSelectedAddress) != 0)
			{
				selectedAddressChanged(selectedAddress);
			}
		}

	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ISynchronizedMemoryBlockView#propertyChanged(java.lang.String, java.lang.Object)
	 */
	public void propertyChanged(String propertyName, Object value)
	{	
		if (isDisplayingError())
			return;
		
		if (propertyName.equals(IMemoryViewConstants.PROPERTY_SELECTED_ADDRESS) && value instanceof BigInteger)
		{
			try {
				if (needMoreLines())
				{
					reloadTable(getTopVisibleAddress(), false);
				}
			} catch (DebugException e) {
				displayError(e);
			}
			
			selectedAddressChanged((BigInteger)value);
		}
		else if (propertyName.equals(IMemoryViewConstants.PROPERTY_COL_SIZE) && value instanceof Integer)
		{
			columnSizeChanged(((Integer)value).intValue());
		}
		else if (propertyName.equals(IMemoryViewConstants.PROPERTY_TOP_ADDRESS) && value instanceof BigInteger)
		{
			try {
				if (needMoreLines())
				{
					reloadTable(getTopVisibleAddress(), false);
				}
			} catch (DebugException e) {
				displayError(e);
			}
			topVisibleAddressChanged((BigInteger)value);
			return;
		}
	}
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ISynchronizedMemoryBlockView#getProperty(java.lang.String)
	 */
	public Object getProperty(String propertyId)
	{
		if (propertyId.equals(IMemoryViewConstants.PROPERTY_SELECTED_ADDRESS))
		{
			return fSelectedAddress;
		}
		else if (propertyId.equals(IMemoryViewConstants.PROPERTY_COL_SIZE))
		{
			return new Integer(fColumnSize);
		}
		else if (propertyId.equals(IMemoryViewConstants.PROPERTY_TOP_ADDRESS))
		{
			return getTopVisibleAddress();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.IMemoryViewTab#getSelectedAddress()
	 */
	public BigInteger getSelectedAddress() {
		return fSelectedAddress;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.IMemoryViewTab#getSelectedContent()
	 */
	public String getSelectedContent() {

		// check precondition
		if (fCursorManager.fCol == 0 || fCursorManager.fCol > getBytesPerLine()/getColumnSize())
		{
			return ""; //$NON-NLS-1$
		}
				
		TableItem tableItem = getTableViewer().getTable().getItem(fCursorManager.fRow);
		
		return tableItem.getText(fCursorManager.fCol);	
	}
	
	/**
	 * Update labels in the view tab
	 */
	protected void updateLabels()
	{
		// update tab labels
		setTabName(getMemoryBlock(), true);
		
		if (fTableViewer != null)
		{
			// update column labels
			setColumnHeadings();
			
			refreshTableViewer();
		}
	}

	protected boolean needMoreLines()
	{
		if (getMemoryBlock() instanceof IMemoryBlockExtension)
		{		
			Table table = fTableViewer.getTable();
			TableItem firstItem = table.getItem(0);
			TableItem lastItem = table.getItem(table.getItemCount()-1);
			
			if (firstItem == null || lastItem == null)
				return true;
			
			MemoryViewLine first = (MemoryViewLine)firstItem.getData();
			MemoryViewLine last = (MemoryViewLine) lastItem.getData();
			
			if (first == null ||last == null)
			{
				// For some reason, the table does not return the correct number
				// of table items in table.getItemCount(), causing last to be null.
				// This check is to ensure that we don't get a null pointer exception.
				return true;
			}
			
			BigInteger startAddress = new BigInteger(first.getAddress(), 16);
			BigInteger lastAddress = new BigInteger(last.getAddress(), 16);
			lastAddress = lastAddress.add(BigInteger.valueOf(getBytesPerLine()));
			
			BigInteger topVisibleAddress = getTopVisibleAddress();
			long numVisibleLines = getNumberOfVisibleLines();
			long numOfBytes = numVisibleLines * getBytesPerLine();
			
			BigInteger lastVisibleAddrss = topVisibleAddress.add(BigInteger.valueOf(numOfBytes));
			
			// if there are only 3 lines left at the top, refresh
			BigInteger numTopLine = topVisibleAddress.subtract(startAddress).divide(BigInteger.valueOf(getBytesPerLine()));
			if (numTopLine.compareTo(BigInteger.valueOf(3)) <= 0)
				return true;
			
			// if there are only 3 lines left at the bottom, refresh
			BigInteger numBottomLine = lastAddress.subtract(lastVisibleAddrss).divide(BigInteger.valueOf(getBytesPerLine()));
			if (numBottomLine.compareTo(BigInteger.valueOf(3)) <= 0)
			{
				return true;
			}
			
			return false;
		}
		
		return false;
	}
	
	private Object getSynchronizedProperty(String propertyId)
	{
		return getMemoryBlockViewSynchronizer().getSynchronizedProperty(getMemoryBlock(), propertyId);	
	}
	
	/**
	 * Checks to see if the event is valid for activating
	 * cell editing in a view tab
	 * @param event
	 * @return true if the edit event is valid  for activating the cell editor
	 */
	public boolean isValidEditEvent(int event) {
		for (int i = 0; i < MemoryViewTab.ignoreEvents.length; i++) {
			if (event == MemoryViewTab.ignoreEvents[i])
				return false;
		}
		return true;
	}
	
	private IMemoryBlockViewSynchronizer getMemoryBlockViewSynchronizer() {
		return DebugUIPlugin.getDefault().getMemoryBlockViewSynchronizer();
	}
	
	public void showAddressColumn(boolean show)
	{
		fShowAddressColumn = show;
		packColumns();
	}
	
	public boolean isShowAddressColumn()
	{
		return fShowAddressColumn;
	}
}	

Back to the top