KickJava   Java API By Example, From Geeks To Geeks.

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


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

11
12 package org.eclipse.debug.internal.ui.views.memory;
13
14 import java.math.BigInteger JavaDoc;
15
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.debug.internal.core.memory.IExtendedMemoryBlock;
18 import org.eclipse.debug.internal.ui.DebugUIMessages;
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
21 import org.eclipse.jface.action.MenuManager;
22 import org.eclipse.jface.resource.JFaceResources;
23 import org.eclipse.jface.viewers.IBaseLabelProvider;
24 import org.eclipse.jface.viewers.ICellModifier;
25 import org.eclipse.jface.viewers.TableViewer;
26 import org.eclipse.jface.viewers.TextCellEditor;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.custom.TableCursor;
29 import org.eclipse.swt.custom.TableEditor;
30 import org.eclipse.swt.events.DisposeEvent;
31 import org.eclipse.swt.events.DisposeListener;
32 import org.eclipse.swt.events.FocusEvent;
33 import org.eclipse.swt.events.FocusListener;
34 import org.eclipse.swt.events.KeyEvent;
35 import org.eclipse.swt.events.KeyListener;
36 import org.eclipse.swt.events.MouseEvent;
37 import org.eclipse.swt.events.MouseListener;
38 import org.eclipse.swt.events.SelectionEvent;
39 import org.eclipse.swt.events.SelectionListener;
40 import org.eclipse.swt.events.TraverseEvent;
41 import org.eclipse.swt.events.TraverseListener;
42 import org.eclipse.swt.graphics.Font;
43 import org.eclipse.swt.graphics.Rectangle;
44 import org.eclipse.swt.widgets.Control;
45 import org.eclipse.swt.widgets.Display;
46 import org.eclipse.swt.widgets.Menu;
47 import org.eclipse.swt.widgets.Table;
48 import org.eclipse.swt.widgets.TableItem;
49 import org.eclipse.swt.widgets.Text;
50
51
52 /**
53  * Manages the cursor in the table.
54  * Since table content is dynamic, default TableCursor does not handle dynamic
55  * content so well. This manager is created to make sure selection is maintainted properly.
56  * In the future, it should be moidified such that it allows user to do multiple
57  * cell selection. (use fTrailCursors)
58  *
59  * @since 3.0
60  */

61 public class ViewTabCursorManager
62 {
63     private TableCursor fTableCursor;
64     private MemoryViewTab fViewTab;
65     private MenuManager fMenuManager;
66     protected int fRow;
67     protected int fCol;
68     private TableEditor editor;
69     private boolean fMenuDisposed = false;
70     
71     private Table fTable;
72     private TableViewer fTableViewer;
73     private String JavaDoc fRenderingId;
74     private IFixedLengthOutputRenderer fRenderer;
75     
76     private TraverseEventListener fTraverseEvtListener;
77     private TextFocusListener fTextFocusListener;
78     private TextKeyListener fTextKeyListener;
79     private MouseEventListener fMouseEventListener;
80     
81     class MouseEventListener implements MouseListener
82     {
83
84         /* (non-Javadoc)
85          * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
86          */

87         public void mouseDoubleClick(MouseEvent e)
88         {
89         }
90
91         /* (non-Javadoc)
92          * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
93          */

94         public void mouseDown(MouseEvent e)
95         {
96             if (e.getSource() instanceof Table)
97                 handleTableMouseEvent(e);
98             else if (e.getSource() instanceof TableCursor)
99                 handleCursorMouseEvent(e);
100         }
101         
102         private void handleCursorMouseEvent(MouseEvent e){
103             if (e.button == 1)
104             {
105                 if (fCol > 0 && fCol <= (getNumCol()))
106                     activateCellEditor(null);
107             }
108         }
109
110         /**
111          * @param e
112          */

113         private void handleTableMouseEvent(MouseEvent e) {
114             // figure out new cursor position based on here the mouse is pointing
115
TableItem[] selections = fTableViewer.getTable().getSelection();
116             TableItem selectedRow = null;
117             int colNum = -1;
118             
119             if (selections.length > 0)
120             {
121                 selectedRow = selections[0];
122                 
123                 int numCol = fTableViewer.getColumnProperties().length;
124                 
125                 for (int i=0; i<numCol; i++)
126                 {
127                     Rectangle bound = selectedRow.getBounds(i);
128                     if (bound.contains(e.x, e.y))
129                     {
130                         colNum = i;
131                         break;
132                     }
133                 }
134             }
135             
136             // if column position cannot be determined, return
137
if (colNum < 1)
138                 return;
139             
140             
141             // handle user mouse click onto table
142
// move cursor to new position
143
if (selectedRow != null)
144             {
145                 int row = fTableViewer.getTable().indexOf(selectedRow);
146                 
147                 updateCursorPosition(row, colNum, true);
148             }
149             
150             fViewTab.updateSyncTopAddress(true);
151             // selected address changes because user has selected a new position
152
fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn());
153
154             // keep table selection up to date
155
fViewTab.updateTableSelection();
156             
157             setCursorFocus();
158         }
159
160         /* (non-Javadoc)
161          * @see org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
162          */

163         public void mouseUp(MouseEvent e)
164         {
165
166         }
167     }
168     
169     /**
170      * Text Traverse Listener for the edit text box in Memory View Tab
171      * Moves cursor around as user tabs in the view tab.
172      */

173     class TraverseEventListener implements TraverseListener
174     {
175         /* (non-Javadoc)
176          * @see org.eclipse.swt.events.TraverseListener#keyTraversed(org.eclipse.swt.events.TraverseEvent)
177          */

178         public void keyTraversed(TraverseEvent event)
179         {
180             if (event.getSource() instanceof Text)
181                 handleTextTraverseEvt(event);
182             else if (event.getSource() instanceof TableCursor)
183                 handleCursorTraverseEvt(event);
184         }
185
186         /**
187          * @param event
188          */

189         private void handleTextTraverseEvt(TraverseEvent event) {
190             final TraverseEvent e = event;
191             Display.getDefault().asyncExec(new Runnable JavaDoc()
192             {
193                 public void run()
194                 {
195                     try
196                     {
197                         // if cursor is at boundry, reload
198
if (fViewTab.getMemoryBlock() instanceof IExtendedMemoryBlock)
199                         {
200                             if (fRow+1 >= fTable.getItemCount() || fRow-1 <= 0)
201                             {
202                                 BigInteger JavaDoc topAddress = fViewTab.getTopVisibleAddress();
203                                 fViewTab.reloadTable(topAddress, false);
204                             }
205                         }
206                     }
207                     catch (DebugException e1)
208                     {
209                         DebugUIPlugin.log(e1);
210                         return;
211                     }
212                     
213                     if (e.detail == SWT.TRAVERSE_TAB_NEXT)
214                     {
215                         if (!(fViewTab.getMemoryBlock() instanceof IExtendedMemoryBlock))
216                         {
217                             if (fRow+1 >= fTable.getItemCount() &&
218                                 fCol == getNumCol())
219                             {
220                                 return;
221                             }
222                         }
223                         
224                         // if the cursor is at the end of the line, move to next line
225
if (fCol == getNumCol())
226                         {
227                             fCol = 1;
228                             fRow++;
229                         }
230                         else
231                         {
232                             fCol++;
233                         }
234                     }
235                     else if (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)
236                     {
237                         if (!(fViewTab.getMemoryBlock() instanceof IExtendedMemoryBlock))
238                         {
239                             if (fRow-1 < 0 && fCol == 1)
240                             {
241                                 return;
242                             }
243                         }
244                                                 
245                         // if the cursor is at the beginning of the line, move to previous line
246
if (fCol == 1)
247                         {
248                             fCol = getNumCol();
249                             fRow--;
250                         }
251                         else
252                         {
253                             fCol--;
254                         }
255                     }
256                     else
257                         return;
258                         
259                     // update cursor location and selection in table
260
updateCursorPosition(fRow, fCol, true);
261                     
262                     fViewTab.updateSyncTopAddress(true);
263                     fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn());
264                     fViewTab.updateTableSelection();
265                     
266                     Text text = (Text)e.getSource();
267                     removeListeners(text);
268
269                     activateCellEditor(null);
270                 }
271
272
273             });
274         }
275         
276         private void handleCursorTraverseEvt(TraverseEvent e){
277             if (fCol == getNumCol() && e.keyCode == SWT.ARROW_RIGHT)
278             {
279                 if (fRow + 1>= fTable.getItemCount())
280                 {
281                     return;
282                 }
283                 
284                 fRow = fRow +1;
285                 fCol = 0;
286                 
287                 updateCursorPosition(fRow, fCol, true);
288             }
289             if (fCol == 1 && e.keyCode == SWT.ARROW_LEFT)
290             {
291                 if (fRow-1 < 0)
292                 {
293                     return;
294                 }
295                 
296                 fRow = fRow - 1;
297                 fCol = getNumCol()+1;
298                 
299                 updateCursorPosition(fRow, fCol, true);
300             }
301         }
302     }
303         
304     /**
305      * Focus listener for the text editor box in Memory View Tab
306      * Commit changes to IMemoryBlock when the text box has lost focus.
307      */

308     class TextFocusListener implements FocusListener
309     {
310
311         /* (non-Javadoc)
312          * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
313          */

314         public void focusGained(FocusEvent e)
315         {
316         }
317
318         /* (non-Javadoc)
319          * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
320          */

321         public void focusLost(FocusEvent event)
322         {
323             
324             final FocusEvent e = event;
325
326             Display.getDefault().syncExec(new Runnable JavaDoc() {
327
328                 public void run()
329                 {
330                     try
331                     {
332                         Text text = (Text)e.getSource();
333                         removeListeners(text);
334     
335                         // get new value
336
String JavaDoc newValue = text.getText();
337                         
338                         // modify memory at fRow and fCol
339
modifyValue(fRow, fCol, newValue);
340                                 
341                         // show cursor after modification is completed
342
showCursor();
343                     }
344                     catch (NumberFormatException JavaDoc e1)
345                     {
346                         MemoryViewUtil.openError(DebugUIMessages.getString(MemoryViewCellModifier.TITLE),
347                             DebugUIMessages.getString(MemoryViewCellModifier.DATA_IS_INVALID), null);
348                     }
349                 }
350             });
351         }
352     }
353     
354     /**
355      * Key Listener to the edit text box in Memory View Tab
356      * Moves the text box arround as user hits the arrow keys.
357      * Change is not committed as user hits the Escape key.
358      * Move edit box to the next cell as user has typed in the max
359      * number of characters for a cell.
360      */

361     class TextKeyListener implements KeyListener
362     {
363
364         /* (non-Javadoc)
365          * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
366          */

367         public void keyTyped(KeyEvent e)
368         {
369         }
370
371         /* (non-Javadoc)
372          * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
373          */

374         public void keyPressed(KeyEvent event)
375         {
376             if (event.getSource() instanceof Text)
377                 handleTextKeyEvt(event);
378         }
379         
380         /**
381          * @param event
382          */

383         private void handleTextKeyEvt(KeyEvent event) {
384             final KeyEvent e = event;
385             Display.getDefault().asyncExec(new Runnable JavaDoc()
386             {
387                 public void run()
388                 {
389                     Text text = (Text)e.getSource();
390                     
391                     try
392                     {
393                         switch (e.keyCode)
394                         {
395                             case SWT.ARROW_UP :
396                                 
397                                 // move text editor box up one row
398

399                                 if (fRow-1 < 0)
400                                     return;
401                             
402                                 // modify value for current cell
403
modifyValue(fRow, fCol, text.getText());
404                                                         
405                                 fRow--;
406
407                                 // update cursor location and selection in table
408
updateCursorPosition(fRow, fCol, true);
409                                 
410                                 fViewTab.updateSyncTopAddress(true);
411                                 fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn());
412                                 fViewTab.updateTableSelection();
413                         
414                                 // remove listeners when focus is lost
415
removeListeners(text);
416                                 activateCellEditor(null);
417                                 break;
418                             case SWT.ARROW_DOWN :
419                                 
420                                 // move text editor box down one row
421

422                                 if (fRow+1 >= fTable.getItemCount())
423                                     return;
424                                 
425                                 // modify value for current cell
426
modifyValue(fRow, fCol, text.getText());
427                             
428                                 fRow++;
429                                 
430                                 // update cursor location and selection in table
431
updateCursorPosition(fRow, fCol, true);
432                                 
433                                 fViewTab.updateSyncTopAddress(true);
434                                 fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn());
435                                 fViewTab.updateTableSelection();
436                         
437                                 // remove traverse listener when focus is lost
438
removeListeners(text);
439                                 activateCellEditor(null);
440                                 break;
441                             case 0:
442                                 
443                                 // if user has entered the max number of characters allowed in a cell, move to next cell
444
// Extra changes will be used as initial value for the next cell
445

446                                 if (fRenderer != null)
447                                 {
448                                     if (text.getText().length() > fViewTab.getColumnSize()*fRenderer.getNumCharPerByte())
449                                     {
450                                         String JavaDoc newValue = text.getText();
451                                         text.setText(newValue.substring(0, fViewTab.getColumnSize()*fRenderer.getNumCharPerByte()));
452                                         
453                                         modifyValue(fRow, fCol, text.getText());
454                                         
455                                         // if cursor is at the end of a line, move to next line
456
if (fCol >= getNumCol())
457                                         {
458                                             fCol = 1;
459                                             fRow++;
460                                         }
461                                         else
462                                         {
463                                             // move to next column
464
fCol++;
465                                         }
466                                         
467                                         // update cursor position and selected address
468
updateCursorPosition(fRow, fCol, true);
469                                         
470                                         fViewTab.updateSyncTopAddress(true);
471                                         fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn());
472                                         fViewTab.updateTableSelection();
473                                         
474                                         removeListeners(text);
475                             
476                                         // activate text editor at next cell
477
activateCellEditor(newValue.substring(fViewTab.getColumnSize()*fRenderer.getNumCharPerByte()));
478                                     }
479                                 }
480                                 break;
481                             case SWT.ESC:
482
483                                 // if user has pressed escape, do not commit the changes
484
// that's why "modifyValue" is not called
485
updateCursorPosition(fRow, fCol, true);
486                                 fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn());
487                                 fViewTab.updateTableSelection();
488                         
489                                 removeListeners(text);
490                                 
491                                 // cursor needs to have focus to remove focus from cell editor
492
setCursorFocus();
493                                 break;
494                             default :
495                             if (fRenderer != null)
496                             {
497                                 if (text.getText().length()> fViewTab.getColumnSize()* fRenderer.getNumCharPerByte())
498                                 {
499                                     String JavaDoc newValue = text.getText();
500                                     text.setText(newValue.substring(0,fViewTab.getColumnSize()* fRenderer.getNumCharPerByte()));
501                                     modifyValue(fRow, fCol, text.getText());
502                                     // if cursor is at the end of a line, move to next line
503
if (fCol >= getNumCol())
504                                     {
505                                         fCol = 1;
506                                         fRow++;
507                                     }
508                                     else
509                                     {
510                                         fCol++;
511                                     }
512                                     
513                                     updateCursorPosition(fRow, fCol, true);
514                                     
515                                     fViewTab.updateSyncTopAddress(true);
516                                     fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn());
517                                     fViewTab.updateTableSelection();
518                                     removeListeners(text);
519                                     activateCellEditor(
520                                         newValue.substring(
521                                             fViewTab.getColumnSize()
522                                                 * fRenderer.getNumCharPerByte()));
523                                 }
524                             }
525                             break;
526                         }
527                     }
528                     catch (NumberFormatException JavaDoc e1)
529                     {
530                         MemoryViewUtil.openError(DebugUIMessages.getString(MemoryViewCellModifier.TITLE),
531                             DebugUIMessages.getString(MemoryViewCellModifier.DATA_IS_INVALID), null);
532                         
533                         updateCursorPosition(fRow, fCol, true);
534                         fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn());
535                         fViewTab.updateTableSelection();
536                 
537                         removeListeners(text);
538                         showCursor();
539                     }
540                 }
541             });
542         }
543
544         /* (non-Javadoc)
545          * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
546          */

547         public void keyReleased(KeyEvent e)
548         {
549             
550         }
551     }
552     
553     /**
554      * Create cursor manager for view tab
555      * @param viewTab
556      * @param initialRow
557      * @param initialCol
558      * @param menuManager
559      */

560     public ViewTabCursorManager(MemoryViewTab viewTab, int initialRow, int initialCol, MenuManager menuManager)
561     {
562         fViewTab = viewTab;
563         fMenuManager = menuManager;
564         fRow = initialRow;
565         fCol = initialCol;
566         fTableViewer = viewTab.getTableViewer();
567         fTable = fTableViewer.getTable();
568         editor = new TableEditor (fTable);
569         fRenderingId = fViewTab.getRenderingId();
570         
571         IBaseLabelProvider labelProvider = fTableViewer.getLabelProvider();
572         if (labelProvider instanceof AbstractTableViewTabLabelProvider)
573         {
574             AbstractMemoryRenderer renderer = ((AbstractTableViewTabLabelProvider)labelProvider).getRenderer();
575             if (renderer instanceof IFixedLengthOutputRenderer)
576                 fRenderer = (IFixedLengthOutputRenderer)renderer;
577         }
578         
579         fTraverseEvtListener = new TraverseEventListener();
580         fTextFocusListener = new TextFocusListener();
581         fTextKeyListener = new TextKeyListener();
582
583         fMouseEventListener = new MouseEventListener();
584         fTable.addMouseListener(fMouseEventListener);
585         
586         createCursor();
587     }
588     
589     private void createCursor()
590     {
591         fTableCursor = new TableCursor(fTable, SWT.NONE);
592         
593         Display display = fTableCursor.getDisplay();
594         
595         // set up cursor color
596
fTableCursor.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
597         fTableCursor.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
598
599         updateCursorPosition(fRow, fCol, true);
600         
601         fTableCursor.setFont(JFaceResources.getFont(IInternalDebugUIConstants.FONT_NAME));
602         fTableCursor.setVisible(true);
603         fTableCursor.setFocus();
604     
605         fTableCursor.addSelectionListener(new SelectionListener() {
606
607             // for keeping track of cursor positions
608
public void widgetSelected(SelectionEvent e)
609             {
610                 if (fTableCursor != null)
611                 {
612                     fViewTab.updateSyncTopAddress(true);
613                     
614                     fRow = fTable.indexOf(fTableCursor.getRow());
615                     fCol = fTableCursor.getColumn();
616                     
617                     // cursor position has changed, update selected address
618
fViewTab.updateSelectedAddress(fTableCursor.getRow(), fTableCursor.getColumn());
619                     fViewTab.updateTableSelection();
620                 }
621             }
622
623             public void widgetDefaultSelected(SelectionEvent e)
624             {
625             
626             }});
627             
628         fTableCursor.addTraverseListener(fTraverseEvtListener);
629             
630         // set up context menu for the cursor
631
// otherwise, cell will not show context menu when it is highlighted
632
if (fMenuManager != null)
633         {
634             createContextMenu();
635         }
636         
637         // TODO: Moved this to viewTabCursorManager?
638
// need to revisit this
639
fTableCursor.addKeyListener(fViewTab);
640         fTableCursor.addMouseListener(fMouseEventListener);
641     }
642     
643
644     /**
645      * Hook up context menu with the cursor.
646      * This is needed when user RMC at cursor. Without this the context
647      * menu activation will not be detected by the cursor
648      * @return
649      */

650     private Menu createContextMenu()
651     {
652         fMenuDisposed = false;
653         
654         Menu menu = fMenuManager.createContextMenu(fTable);
655         
656         fTable.setMenu(menu);
657         
658         if (fTableCursor != null)
659         {
660             Control menuControl = fTableCursor;
661             menuControl.setMenu(menu);
662             
663             menu.addDisposeListener(new DisposeListener(){
664
665                 public void widgetDisposed(DisposeEvent e)
666                 {
667                     fMenuDisposed = true;
668                     
669                     Menu disposedMenu = (Menu)e.getSource();
670                     disposedMenu.removeDisposeListener(this);
671                 }});
672         }
673         
674         return menu;
675     }
676     
677     /**
678      * Redraw cursors
679      */

680     public void redrawCursors()
681     {
682         if (fTableCursor != null && fTableCursor.isVisible())
683         {
684             fTableCursor.setSelection(fRow, fCol);
685             fTableCursor.redraw();
686         }
687     }
688     
689     /**
690      * Clean up cursor manager
691      */

692     public void dispose()
693     {
694         if (fTableCursor != null)
695         {
696             // TableCursor changes the way how it is disposed.
697
// SWT disposes the cursor when the view tab is dispose
698
// disposing the cursor now causes SWT error
699
// fTableCursor.dispose();
700
fTableCursor = null;
701         }
702             
703         if (editor != null)
704         {
705             editor.dispose();
706             editor = null;
707         }
708         
709         if (fTable != null)
710             fTable.removeMouseListener(fMouseEventListener);
711     }
712     
713     /**
714      * @return lead cursor
715      */

716     public TableCursor getLeadCursor()
717     {
718         return fTableCursor;
719     }
720     
721     /**
722      * Hide cursor
723      */

724     public void hideCursor()
725     {
726         fTableCursor.setVisible(false);
727     }
728     
729     /**
730      * When cursor is to be shown again, if the cursor is previously dispose,
731      * create a new cursor and position at last remembered position
732      */

733     public void showCursor()
734     {
735         if (fTableCursor == null)
736         {
737             createCursor();
738         }
739         if (fMenuDisposed)
740         {
741             createContextMenu();
742         }
743         
744         if (!fTableCursor.isVisible())
745         {
746             fTableCursor.setVisible(true);
747         }
748     }
749     
750     /**
751      * Causes the cursor to gain focus
752      */

753     public void setCursorFocus()
754     {
755         if (fTableCursor != null)
756             fTableCursor.setFocus();
757     }
758     
759     
760     /**
761      * Set new row/col position in cursor. If cursor is not visible, (cursor is null),
762      * call showCursor to create new cursor.
763      * @param row
764      * @param col
765      */

766     public void updateCursorPosition(int row, int col, boolean showCursor)
767     {
768         if (row < 0
769             || row >= fTable.getItemCount()
770             || col < 0
771             || col >= fTable.getColumnCount()) {
772             return;
773         }
774
775         this.fRow = row;
776         this.fCol = col;
777
778         fTableCursor.setSelection(row, col);
779
780         if (showCursor)
781             showCursor();
782     }
783     
784     /**
785      * Activate celll editor and prefill it with initial value.
786      * If initialValue is null, use cell content as initial value
787      * @param initialValue
788      */

789     public void activateCellEditor(String JavaDoc initialValue) {
790         
791         // do not allow user to edit address column
792
if (fCol == 0 || fCol > getNumCol())
793         {
794             return;
795         }
796         
797         ICellModifier cellModifier = null;
798         
799         if (fTableViewer != null)
800         {
801             cellModifier = fTableViewer.getCellModifier();
802         }
803         
804         TableItem tableItem = fTable.getItem(fRow);
805         
806         Object JavaDoc element = tableItem.getData();
807         Object JavaDoc property = fTableViewer.getColumnProperties()[fCol];
808         Object JavaDoc value = cellModifier.getValue(element, (String JavaDoc)property);
809         
810         // The cell modifier canModify function always returns false if the edit action
811
// is not invoked from here. This is to prevent data to be modified when
812
// the table cursor loses focus from a cell. By default, data will
813
// be changed in a table when the cell loses focus. This is to workaround
814
// this default behaviour and only change data when the cell editor
815
// is activated.
816
((MemoryViewCellModifier)cellModifier).setEditActionInvoked(true);
817         boolean canEdit = cellModifier.canModify(element, (String JavaDoc)property);
818         ((MemoryViewCellModifier)cellModifier).setEditActionInvoked(false);
819         
820         if (!canEdit)
821             return;
822         
823         // activate based on current cursor position
824
TextCellEditor selectedEditor = (TextCellEditor)fTableViewer.getCellEditors()[fCol];
825
826         
827         if (fTableViewer != null && cellModifier != null && selectedEditor != null && tableItem != null)
828         {
829             // The control that will be the editor must be a child of the Table
830
Text text = (Text)selectedEditor.getControl();
831             
832             String JavaDoc cellValue = null;
833             
834             if (initialValue != null)
835             {
836                 cellValue = initialValue;
837             }
838             else
839             {
840                 cellValue = ((String JavaDoc)value);
841             }
842             
843             if (fRenderingId.equals(IMemoryViewConstants.RENDERING_RAW_MEMORY))
844                 cellValue = cellValue.toUpperCase();
845             
846             text.setText(cellValue);
847     
848             editor.horizontalAlignment = SWT.LEFT;
849             editor.grabHorizontal = true;
850     
851             // Open the text editor in selected column of the selected row.
852
editor.setEditor (text, tableItem, fCol);
853     
854             // Assign focus to the text control
855
selectedEditor.setFocus();
856             
857             if (initialValue != null)
858             {
859                 text.clearSelection();
860             }
861             
862             text.setFont(JFaceResources.getFont(IInternalDebugUIConstants.FONT_NAME));
863
864             // add listeners for the text control
865
addListeners(text);
866             
867             // move cursor below text control
868
fTableCursor.moveBelow(text);
869         }
870     }
871     
872     /**
873      * @return
874      */

875     private int getNumCol() {
876         
877         int bytesPerLine = fViewTab.getBytesPerLine();
878         int columnSize = fViewTab.getColumnSize();
879         
880         return bytesPerLine/columnSize;
881     }
882
883     /**
884      * Modify value and send new value to debug adapter
885      * @param row
886      * @param col
887      * @param newValue
888      * @throws NumberFormatException
889      */

890     private void modifyValue(int row, int col, String JavaDoc newValue) throws NumberFormatException JavaDoc
891     {
892         if (newValue.length() == 0)
893         {
894             // do not do anything if user has not entered anything
895
return;
896         }
897         
898         TableItem tableItem = fTable.getItem(row);
899
900         Object JavaDoc property = fTableViewer.getColumnProperties()[col];
901
902         // get old value
903
Object JavaDoc element = tableItem.getData();
904         
905         ICellModifier cellModifier = fTableViewer.getCellModifier();
906         String JavaDoc oldValue = (String JavaDoc)cellModifier.getValue(element, (String JavaDoc)property);
907
908         if (fRenderingId.equals(IMemoryViewConstants.RENDERING_RAW_MEMORY))
909         {
910             oldValue = oldValue.toUpperCase();
911             newValue = newValue.toUpperCase();
912
913             // only compare up to the length of new value, leave the rest of the memory unchanged
914
if (newValue.length() <= oldValue.length())
915             {
916                 oldValue = oldValue.substring(0, newValue.length());
917             }
918     
919             if (!oldValue.equals(newValue))
920             {
921                 fTableViewer.getCellModifier().modify(tableItem, (String JavaDoc)property, newValue);
922             }
923         }
924         else
925         {
926             // just modify
927
fTableViewer.getCellModifier().modify(tableItem, (String JavaDoc)property, newValue);
928         }
929     }
930     
931     /**
932      * @param font
933      */

934     protected void setFont(Font font)
935     {
936         if (fTableCursor != null)
937         {
938             fTableCursor.setFont(font);
939         }
940     }
941     
942     /**
943      * @param text
944      */

945     private void addListeners(Text text) {
946         // add listeners to text
947
text.addFocusListener(fTextFocusListener);
948         text.addTraverseListener(fTraverseEvtListener);
949         text.addKeyListener(fTextKeyListener);
950         text.addKeyListener(fViewTab);
951     }
952     
953     /**
954      * @param text
955      */

956     private void removeListeners(Text text) {
957         // remove traverse listener when focus is lost
958
text.removeTraverseListener(fTraverseEvtListener);
959         text.removeFocusListener(fTextFocusListener);
960         text.removeKeyListener(fTextKeyListener);
961         text.removeKeyListener(fViewTab);
962     }
963 }
964
Popular Tags