KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.debug.internal.ui.views.memory;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Hashtable JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.debug.core.DebugException;
24 import org.eclipse.debug.core.DebugPlugin;
25 import org.eclipse.debug.core.model.IDebugElement;
26 import org.eclipse.debug.core.model.IDebugTarget;
27 import org.eclipse.debug.core.model.IMemoryBlock;
28 import org.eclipse.debug.core.model.IMemoryBlockRetrieval;
29 import org.eclipse.debug.internal.ui.DebugUIPlugin;
30 import org.eclipse.debug.internal.ui.views.memory.renderings.CreateRendering;
31 import org.eclipse.debug.ui.DebugUITools;
32 import org.eclipse.debug.ui.IDebugUIConstants;
33 import org.eclipse.debug.ui.contexts.DebugContextEvent;
34 import org.eclipse.debug.ui.memory.IMemoryRendering;
35 import org.eclipse.debug.ui.memory.IMemoryRenderingContainer;
36 import org.eclipse.debug.ui.memory.IMemoryRenderingSite;
37 import org.eclipse.debug.ui.memory.IMemoryRenderingSynchronizationService;
38 import org.eclipse.debug.ui.memory.IResettableMemoryRendering;
39 import org.eclipse.jface.action.IAction;
40 import org.eclipse.jface.viewers.ISelection;
41 import org.eclipse.jface.viewers.IStructuredSelection;
42 import org.eclipse.jface.viewers.StructuredSelection;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.SWTException;
45 import org.eclipse.swt.events.SelectionEvent;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Control;
48 import org.eclipse.swt.widgets.Display;
49 import org.eclipse.swt.widgets.TabFolder;
50 import org.eclipse.swt.widgets.TabItem;
51 import org.eclipse.ui.IViewPart;
52 import org.eclipse.ui.IWorkbenchPart;
53 import org.eclipse.ui.PlatformUI;
54 import org.eclipse.ui.progress.UIJob;
55
56
57
58 /**
59  * Represents a rendering view pane in the Memory View.
60  * This hosts the memory view tabs in the view.
61  * @since 3.1
62  *
63  */

64 public class RenderingViewPane extends AbstractMemoryViewPane implements IMemoryRenderingContainer{
65
66     public static final String JavaDoc RENDERING_VIEW_PANE_ID = DebugUIPlugin.getUniqueIdentifier() + ".MemoryView.RenderingViewPane"; //$NON-NLS-1$
67

68     private Hashtable JavaDoc fTabFolderForMemoryBlock = new Hashtable JavaDoc();
69     private Hashtable JavaDoc fMemoryBlockFromTabFolder = new Hashtable JavaDoc();
70
71     private AddMemoryRenderingAction fAddMemoryRenderingAction;
72
73     private IAction fRemoveMemoryRenderingAction;
74     private ViewPaneRenderingMgr fRenderingMgr;
75     
76     private IMemoryRenderingSite fRenderingSite;
77     private Set JavaDoc fAddedRenderings = new HashSet JavaDoc();
78     private Set JavaDoc fAddedMemoryBlocks = new HashSet JavaDoc();
79
80     private boolean fIsDisposed = false;
81     
82     /**
83      * @param parent is the view hosting this view pane
84      * @param paneId is the identifier assigned by the Memory View
85      *
86      * Pane id is assigned with the following format.
87      * Rendering view pane created has its id assigned to
88      * org.eclipse.debug.ui.MemoryView.RenderingViewPane.#.
89      * # is a number indicating the order of which the rendering view
90      * pane is created. First rendering view pane created will have its
91      * id assigned to org.eclipse.debug.ui.MemoryView.RenderingViewPane.1.
92      * Second rendering view pane created will have its id assigned to
93      * org.eclipse.debug.ui.MemoryView.RenderingViewPane.2. and so on.
94      * View pane are created from left to right by the Memory View.
95      *
96      */

97     public RenderingViewPane(IViewPart parent) {
98         super(parent);
99         
100         if (parent instanceof IMemoryRenderingSite)
101             fRenderingSite = (IMemoryRenderingSite)parent;
102         else
103         {
104             DebugUIPlugin.logErrorMessage("Parent for the rendering view pane is invalid."); //$NON-NLS-1$
105
}
106     }
107     
108     /* (non-Javadoc)
109      * @see org.eclipse.debug.internal.core.memory.IMemoryBlockListener#MemoryBlockAdded(org.eclipse.debug.core.model.IMemoryBlock)
110      */

111     public void memoryBlocksAdded(final IMemoryBlock[] memoryBlocks) {
112         Display.getDefault().asyncExec(new Runnable JavaDoc() {
113             public void run() {
114                 
115                 if (isDisposed())
116                     return;
117                         
118                 // check condition before doing anything
119
if (memoryBlocks == null || memoryBlocks.length <= 0)
120                     return;
121                 
122                 for (int i=0; i<memoryBlocks.length; i++)
123                 {
124                     IMemoryBlock memory = memoryBlocks[i];
125                 
126                     if (!fTabFolderForMemoryBlock.containsKey(memory))
127                     {
128                         createFolderForMemoryBlock(memory);
129                     }
130                     fAddedMemoryBlocks.add(memory);
131                     updateToolBarActionsEnablement();
132                 }
133             }});
134     }
135     
136     private IMemoryBlockRetrieval getMemoryBlockRetrieval(IMemoryBlock memoryBlock)
137     {
138         IMemoryBlockRetrieval retrieval = (IMemoryBlockRetrieval)memoryBlock.getAdapter(IMemoryBlockRetrieval.class);
139         
140         if (retrieval == null)
141         {
142             retrieval = memoryBlock.getDebugTarget();
143         }
144         
145         return retrieval;
146     }
147
148     /* (non-Javadoc)
149      * @see org.eclipse.debug.internal.core.memory.IMemoryBlockListener#MemoryBlockRemoved(org.eclipse.debug.core.model.IMemoryBlock)
150      */

151     public void memoryBlocksRemoved(final IMemoryBlock[] memoryBlocks) {
152         Display.getDefault().asyncExec(new Runnable JavaDoc() {
153             public void run() {
154                 for (int j=0; j<memoryBlocks.length; j++)
155                 {
156                     IMemoryBlock memory = memoryBlocks[j];
157                     if (fTabFolderForMemoryBlock == null)
158                     {
159                         return;
160                     }
161                     
162                     // get all renderings from this memroy block and remove them from the view
163
IMemoryRendering[] renderings = fRenderingMgr.getRenderingsFromMemoryBlock(memory);
164                     
165                     for (int k=0; k<renderings.length; k++)
166                     {
167                         removeMemoryRendering(renderings[k]);
168                     }
169                     
170                     // remove a the tab folder if the memory block is removed
171
TabFolder tabFolder =
172                         (TabFolder) fTabFolderForMemoryBlock.get(memory);
173                     
174                     if (tabFolder == null)
175                         continue;
176                     
177                     fTabFolderForMemoryBlock.remove(memory);
178                     fMemoryBlockFromTabFolder.remove(tabFolder);
179                     fTabFolderForDebugView.remove(getMemoryBlockRetrieval(memory));
180                     
181                     if (!tabFolder.isDisposed()) {
182                         
183                         IMemoryBlockRetrieval retrieve = (IMemoryBlockRetrieval)memory.getAdapter(IMemoryBlockRetrieval.class);
184                         if (retrieve == null)
185                         {
186                             retrieve = memory.getDebugTarget();
187                         }
188                         
189                         if (fTabFolderForDebugView.contains(tabFolder))
190                         {
191                             fTabFolderForDebugView.remove(retrieve);
192                         }
193                         
194                         // dispose all view tabs belonging to the tab folder
195
TabItem[] items = tabFolder.getItems();
196                         
197                         for (int i=0; i<items.length; i++)
198                         {
199                             disposeTab(items[i]);
200                         }
201                         
202                         // dispose the tab folder
203
tabFolder.dispose();
204                         
205                         // if this is the top control
206
if (tabFolder == fStackLayout.topControl)
207                         {
208                             
209                             // if memory view is visible and have a selection
210
// follow memory view's selection
211

212                             ISelection selection = DebugUIPlugin.getActiveWorkbenchWindow().getSelectionService().getSelection(IDebugUIConstants.ID_MEMORY_VIEW);
213                             IMemoryBlock blk = getMemoryBlock(selection);
214                             
215                             if (blk != null)
216                             {
217                                         
218                                 // memory view may not have got the event and is still displaying
219
// the deleted memory block
220
if (blk != memory)
221                                     handleMemoryBlockSelection(null, blk);
222                                 else if ((MemoryViewUtil.getMemoryBlockManager().getMemoryBlocks(memory.getDebugTarget()).length > 0))
223                                 {
224                                     blk = MemoryViewUtil.getMemoryBlockManager().getMemoryBlocks(memory.getDebugTarget())[0];
225                                     handleMemoryBlockSelection(null, blk);
226                                 }
227                                 else
228                                 {
229                                     emptyFolder();
230                                 }
231                             }
232                             else if (MemoryViewUtil.getMemoryBlockManager().getMemoryBlocks(memory.getDebugTarget()).length > 0)
233                             { // get to the next folder
234
blk = MemoryViewUtil.getMemoryBlockManager().getMemoryBlocks(memory.getDebugTarget())[0];
235                                 handleMemoryBlockSelection(null, blk);
236                             }
237                             else
238                             {
239                                 emptyFolder();
240             
241                             }
242                         }
243                         
244                         // if not the top control
245
// no need to do anything
246
}
247                     
248                     fAddedMemoryBlocks.remove(memory);
249                     updateToolBarActionsEnablement();
250                 }
251             }
252         });
253
254     }
255
256     /* (non-Javadoc)
257      * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
258      */

259     public void selectionChanged(final IWorkbenchPart part, final ISelection selection) {
260         if (fIsDisposed)
261             return;
262         
263         // do not schedule job if any of these conditions are true
264
if(part == RenderingViewPane.this)
265             return;
266         
267         if (!(selection instanceof IStructuredSelection))
268             return;
269         
270         if (selection == AbstractMemoryViewPane.EMPTY)
271             return;
272         
273         UIJob job = new UIJob("RenderingViewPane selectionChanged"){ //$NON-NLS-1$
274

275             public IStatus runInUIThread(IProgressMonitor monitor) {
276                 try {
277
278                     if (isDisposed())
279                         return Status.OK_STATUS;
280                     
281                     if (selection.isEmpty())
282                     {
283                         // if the event comes from Memory View
284
// pick empty tab folder as the memory view is no longer displaying anything
285
if (part.getSite().getId().equals(IDebugUIConstants.ID_MEMORY_VIEW))
286                         {
287                             if (part == getMemoryRenderingSite().getSite().getPart())
288                             {
289                                 IMemoryViewTab lastViewTab = getTopMemoryTab();
290                                 
291                                 if (lastViewTab != null)
292                                     lastViewTab.setEnabled(false);
293                                 
294                                 emptyFolder();
295                             }
296                         }
297                         
298                         // do not do anything if there is no selection
299
// In the case when a debug adpater fires a debug event incorrectly, Launch View sets
300
// selection to nothing. If the view tab is disabled, it erases all the "delta" information
301
// in the content. This may not be desirable as it will cause memory to show up as
302
// unchanged when it's actually changed. Do not disable the view tab until there is a
303
// valid selection.
304

305                         return Status.OK_STATUS;
306                     }
307                     
308                     // back up current view tab
309
IMemoryViewTab lastViewTab = getTopMemoryTab();
310                     
311                     if (!(selection instanceof IStructuredSelection))
312                         return Status.OK_STATUS;
313
314                     Object JavaDoc elem = ((IStructuredSelection)selection).getFirstElement();
315                     
316                     if (elem instanceof IMemoryBlock)
317                     {
318                         // if the selection event comes from this view
319
if (part == getMemoryRenderingSite())
320                         {
321                             // find the folder associated with the given IMemoryBlockRetrieval
322
IMemoryBlock memBlock = (IMemoryBlock)elem;
323                             
324                             // should never get here... added code for safety
325
if (fTabFolderForMemoryBlock == null)
326                             {
327                                 if (lastViewTab != null)
328                                     lastViewTab.setEnabled(false);
329                                 
330                                 emptyFolder();
331                                 return Status.OK_STATUS;
332                             }
333             
334                             handleMemoryBlockSelection(lastViewTab, memBlock);
335                         }
336                     }
337                 }
338                 catch(SWTException se)
339                 {
340                     DebugUIPlugin.log(se);
341                 }
342                 return Status.OK_STATUS;
343             }};
344         job.setSystem(true);
345         job.schedule();
346     }
347     
348     public void handleMemoryBlockSelection(final IMemoryViewTab lastViewTab, final IMemoryBlock memBlock) {
349         // don't do anything if the debug target is already terminated
350
if (memBlock.getDebugTarget().isDisconnected()
351                 || memBlock.getDebugTarget().isTerminated()) {
352             emptyFolder();
353             return;
354         }
355
356         // check current memory block
357
TabFolder currentFolder = (TabFolder) fStackLayout.topControl;
358         if (currentFolder != null && !currentFolder.isDisposed()) {
359             IMemoryBlock currentBlk = (IMemoryBlock) fMemoryBlockFromTabFolder.get(currentFolder);
360             if (currentBlk != null) {
361                 if (currentBlk == memBlock)
362                     return;
363             }
364         }
365
366         if (getTopMemoryTab() != null) {
367             if (getTopMemoryTab().getRendering().getMemoryBlock() == memBlock) {
368                 return;
369             }
370         }
371
372         // if we've got a tabfolder to go with the IMemoryBlock, display
373
// it
374
if (fTabFolderForMemoryBlock.containsKey(memBlock)) {
375             if (fStackLayout.topControl != (TabFolder) fTabFolderForMemoryBlock.get(memBlock)) {
376                 setTabFolder((TabFolder) fTabFolderForMemoryBlock.get(memBlock));
377                 fViewPaneCanvas.layout();
378             }
379         } else { // otherwise, add a new one
380
TabFolder folder = new TabFolder(fViewPaneCanvas, SWT.NULL);
381
382             fTabFolderForMemoryBlock.put(memBlock, folder);
383             fMemoryBlockFromTabFolder.put(folder, memBlock);
384             setTabFolder((TabFolder) fTabFolderForMemoryBlock.get(memBlock));
385             fViewPaneCanvas.layout();
386             fAddedMemoryBlocks.add(memBlock);
387         }
388
389         // restore view tabs
390
IMemoryRendering[] renderings = fRenderingMgr.getRenderingsFromMemoryBlock(memBlock);
391         TabFolder toDisplay = (TabFolder) fStackLayout.topControl;
392
393         if (toDisplay.getItemCount() == 0) {
394             restoreViewTabs(renderings);
395         }
396
397         // disable last view tab as it becomes hidden
398
IMemoryViewTab newViewTab = getTopMemoryTab();
399
400         if (lastViewTab != null && lastViewTab != newViewTab) {
401             lastViewTab.setEnabled(false);
402         }
403
404         if (newViewTab != null) {
405             // if new view tab is not already enabled, enable it
406
if (!newViewTab.isEnabled()) {
407                 // if the view tab is visible, enable it
408
if (fVisible) {
409                     newViewTab.setEnabled(fVisible);
410                 }
411             }
412         }
413
414         IMemoryViewTab viewTab = getTopMemoryTab();
415         if (viewTab != null)
416             setRenderingSelection(viewTab.getRendering());
417
418         if (viewTab == null && fRenderingMgr.getRenderings().length == 0) {
419             // do not ever want to put it on the empty folder
420
if (toDisplay != fEmptyTabFolder) {
421                 TabItem newItem = new TabItem(toDisplay, SWT.NULL);
422                 CreateRendering rendering = new CreateRendering(getInstance());
423                 rendering.init(getInstance(), memBlock);
424                 MemoryViewTab createTab = new MemoryViewTab(newItem,rendering, getInstance());
425                 setRenderingSelection(createTab.getRendering());
426             }
427         }
428
429         //set toolbar actions enabled/disabled
430
updateToolBarActionsEnablement();
431     }
432
433     public void memoryBlockRenderingAdded(final IMemoryRendering rendering) {
434
435         Display.getDefault().asyncExec(new Runnable JavaDoc() {
436             public void run() {
437                 
438                 if (isDisposed())
439                     return;
440                 
441                 if (fAddedRenderings.contains(rendering))
442                     return;
443
444                 IMemoryBlock memoryblk = rendering.getMemoryBlock();
445
446                 TabFolder tabFolder = (TabFolder) fTabFolderForMemoryBlock.get(memoryblk);
447                 
448                 if (tabFolder == null)
449                 {
450                     tabFolder = createFolderForMemoryBlock(memoryblk);
451                 }
452
453                 if (tabFolder.getItemCount() >= 1) {
454                     TabItem[] items = tabFolder.getItems();
455                     for (int i=0; i<items.length; i++)
456                     {
457                         // remove "Create rendering tab"
458
TabItem item = items[i];
459                         if (item.getData() instanceof MemoryViewTab) {
460                             MemoryViewTab viewTab = (MemoryViewTab) item.getData();
461                             if (viewTab.getRendering() instanceof CreateRendering) {
462                                 disposeTab(item);
463                             }
464                         }
465                     }
466                 }
467                 
468                 if (tabFolder == fStackLayout.topControl)
469                 {
470                     // disable current view tab
471
if (getTopMemoryTab() != null) {
472                         deactivateRendering(getTopMemoryTab());
473                         getTopMemoryTab().setEnabled(false);
474                     }
475                 }
476                 fAddedRenderings.add(rendering);
477                 TabItem tab = new TabItem(tabFolder, SWT.NULL);
478
479                 MemoryViewTab viewTab = new MemoryViewTab(tab, rendering,getInstance());
480                 tabFolder.setSelection(tabFolder.indexOf(tab));
481                 
482                 if (tabFolder == fStackLayout.topControl)
483                 {
484                     setRenderingSelection(viewTab.getRendering());
485
486                     // disable top view tab if the view pane is not visible
487
IMemoryViewTab top = getTopMemoryTab();
488                     if (top != null)
489                         top.setEnabled(fVisible);
490                 }
491                 else
492                 {
493                     deactivateRendering(viewTab);
494                     viewTab.setEnabled(false);
495                 }
496
497                 updateToolBarActionsEnablement();
498             }
499         });
500     }
501
502     /* (non-Javadoc)
503      * @see org.eclipse.debug.internal.core.memory.IMemoryRenderingListener#MemoryBlockRenderingRemoved(org.eclipse.debug.internal.core.memory.IMemoryRendering)
504      */

505     public void memoryBlockRenderingRemoved(final IMemoryRendering rendering) {
506         final IMemoryBlock memory = rendering.getMemoryBlock();
507         
508         // need to run the following code on the UI Thread to avoid invalid thread access exception
509
Display.getDefault().asyncExec(new Runnable JavaDoc()
510         {
511             public void run()
512             {
513                 if (!fAddedRenderings.contains(rendering))
514                     return;
515                 
516                 fAddedRenderings.remove(rendering);
517                 
518                 TabFolder tabFolder = (TabFolder) fStackLayout.topControl;
519                 
520                 if (tabFolder.isDisposed())
521                     return;
522                 
523                 TabItem[] tabs = tabFolder.getItems();
524                 boolean foundTab = false;
525                 for (int i = 0; i < tabs.length; i++)
526                 {
527                     IMemoryViewTab viewTab = (IMemoryViewTab) tabs[i].getData();
528                     
529                     if (tabs[i].isDisposed())
530                         continue;
531                     
532
533                     if (viewTab.getRendering().getMemoryBlock() == memory)
534                     {
535                         if (viewTab.getRendering() == rendering)
536                         {
537                             foundTab = true;
538                             disposeTab(tabs[i]);
539                             break;
540                         }
541                         
542                     }
543                 }
544
545                 // if a tab is not found in the current top control
546
// this deletion is a result of a debug target termination
547
// find memory from other folder and dispose the view tab
548
if (!foundTab)
549                 {
550                     Enumeration JavaDoc enumeration = fTabFolderForMemoryBlock.elements();
551                     while (enumeration.hasMoreElements())
552                     {
553                         TabFolder otherTabFolder = (TabFolder) enumeration.nextElement();
554                         tabs = otherTabFolder.getItems();
555                         IMemoryViewTab viewTab = null;
556                         for (int i = 0; i < tabs.length; i++)
557                         {
558                             viewTab = (IMemoryViewTab) tabs[i].getData();
559                             if (viewTab.getRendering().getMemoryBlock() == memory)
560                             {
561                                 if (viewTab.getRendering() == rendering)
562                                 {
563                                     foundTab = true;
564                                     disposeTab(tabs[i]);
565                                     break;
566                                 }
567                             }
568                         }
569                     }
570                 }
571                 IMemoryViewTab top = getTopMemoryTab();
572                 
573                 // update selection
574
if (top != null)
575                     setRenderingSelection(top.getRendering());
576                 else
577                 {
578                     if (tabFolder != fEmptyTabFolder)
579                     {
580                         IDebugTarget target = memory.getDebugTarget();
581                         
582                         // do not create if the target is already terminated or if the memory block is removed
583
if (!target.isDisconnected() && !target.isTerminated() && !isMeomryBlockRemoved(memory))
584                         {
585                             TabItem newItem = new TabItem(tabFolder, SWT.NULL);
586                             CreateRendering createRendering = new CreateRendering(getInstance());
587                             createRendering.init(getInstance(), memory);
588                             
589                             MemoryViewTab viewTab = new MemoryViewTab(newItem, createRendering, getInstance());
590                             tabFolder.setSelection(0);
591                             setRenderingSelection(viewTab.getRendering());
592                         }
593                     }
594                 }
595                     
596                 updateToolBarActionsEnablement();
597             }
598         });
599         
600     }
601     
602     /**
603      * @param memoryBlock
604      * @return if this memory block is removed
605      */

606     private boolean isMeomryBlockRemoved(IMemoryBlock memoryBlock)
607     {
608         IMemoryBlockRetrieval retrieval = getMemoryBlockRetrieval(memoryBlock);
609         IMemoryBlock[] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval);
610         boolean removed = true;
611         
612         for (int i=0; i<memoryBlocks.length; i++)
613         {
614             if (memoryBlocks[i] == memoryBlock)
615                 removed = false;
616         }
617         
618         return removed;
619     }
620     
621     
622     /**
623      * @param viewTab
624      */

625     protected void setRenderingSelection(IMemoryRendering rendering) {
626
627         if (rendering != null)
628         {
629             fSelectionProvider.setSelection(new StructuredSelection(rendering));
630         }
631     }
632     
633     private void restoreViewTabs(IMemoryRendering[] renderings)
634     {
635         for (int i=0; i<renderings.length; i++)
636         {
637             memoryBlockRenderingAdded(renderings[i]);
638         }
639     }
640     
641     private void handleDebugElementSelection(final IMemoryViewTab lastViewTab, final IDebugElement element)
642     {
643         // get current memory block retrieval and debug target
644
IMemoryBlockRetrieval currentRetrieve = null;
645         
646         // get tab folder
647
TabFolder tabFolder = (TabFolder) fStackLayout.topControl;
648         
649         // get memory block
650
IMemoryBlock currentBlock = (IMemoryBlock)fMemoryBlockFromTabFolder.get(tabFolder);
651         
652         if (currentBlock != null)
653         {
654             currentRetrieve = (IMemoryBlockRetrieval)currentBlock.getAdapter(IMemoryBlockRetrieval.class);
655             
656             if (currentRetrieve == null)
657             {
658                 currentRetrieve = currentBlock.getDebugTarget();
659             }
660             
661             // backup current retrieve and tab folder
662
if (currentRetrieve != null && tabFolder != null)
663             {
664                 fTabFolderForDebugView.put(currentRetrieve, tabFolder);
665             }
666         }
667         
668         // find the folder associated with the given IMemoryBlockRetrieval
669
IMemoryBlockRetrieval retrieve = (IMemoryBlockRetrieval)element.getAdapter(IMemoryBlockRetrieval.class);
670         IDebugTarget debugTarget = element.getDebugTarget();
671         
672         // if IMemoryBlockRetrieval is null, use debugtarget
673
if (retrieve == null)
674             retrieve = debugTarget;
675
676         // if debug target has changed
677
// switch to that tab folder
678
if (retrieve != currentRetrieve)
679         {
680             TabFolder folder = (TabFolder)fTabFolderForDebugView.get(retrieve);
681             
682             if (folder != null)
683             {
684                 setTabFolder(folder);
685                 fTabFolderForDebugView.put(retrieve, folder);
686                 fViewPaneCanvas.layout();
687             }
688             else
689             {
690                 // find out if there is any memory block for this debug target
691
// and set up tab folder for the memory blocks
692
IMemoryBlock blocks[] = MemoryViewUtil.getMemoryBlockManager().getMemoryBlocks(retrieve);
693                 
694                 if (blocks.length > 0)
695                 {
696                     handleMemoryBlockSelection(null, blocks[0]);
697                 }
698                 else
699                 {
700                     emptyFolder();
701                     fTabFolderForDebugView.put(retrieve, fEmptyTabFolder);
702                     fViewPaneCanvas.layout();
703                 }
704             }
705         }
706         
707         // disable last view tab as it becomes hidden
708
IMemoryViewTab newViewTab = getTopMemoryTab();
709
710         if (lastViewTab != null && lastViewTab != newViewTab)
711         {
712             lastViewTab.setEnabled(false);
713         }
714
715         if (newViewTab != null)
716         {
717             // if new view tab is not already enabled, enable it
718
if (!newViewTab.isEnabled())
719             {
720                 // if the view tab is visible, enable it
721
if (fVisible)
722                 {
723                     newViewTab.setEnabled(fVisible);
724                 }
725             }
726             
727             // should only change selection if the new view tab is different
728
if (lastViewTab != newViewTab)
729                 setRenderingSelection(newViewTab.getRendering());
730         }
731         //set toolbar actions enabled/disabled
732
updateToolBarActionsEnablement();
733     }
734     
735     protected void addListeners() {
736         super.addListeners();
737         
738         // must directly listen for selection events from parent's selection provider
739
// to ensure that we get the selection event from the tree viewer pane even
740
// if the view does not have focuse
741
fParent.getSite().getSelectionProvider().addSelectionChangedListener(this);
742     }
743     protected void removeListeners() {
744         super.removeListeners();
745         fParent.getSite().getSelectionProvider().removeSelectionChangedListener(this);
746     }
747
748     /* (non-Javadoc)
749      * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
750      */

751     public void widgetSelected(SelectionEvent e) {
752         
753         if (getTopMemoryTab() == null)
754             return;
755         
756         IMemoryRendering rendering = getTopMemoryTab().getRendering();
757         
758         if (rendering != null)
759         {
760             fSelectionProvider.setSelection(new StructuredSelection(rendering));
761         }
762         
763     }
764
765     /* (non-Javadoc)
766      * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
767      */

768     public void widgetDefaultSelected(SelectionEvent e) {
769     }
770     
771     public Object JavaDoc getCurrentSelection() {
772         if (getTopMemoryTab() != null)
773             if (getTopMemoryTab().getRendering() != null)
774                 return getTopMemoryTab().getRendering();
775         return null;
776     }
777
778     /* (non-Javadoc)
779      * @see com.ibm.debug.extended.ui.IMemoryView#getAllViewTabs()
780      */

781     public IMemoryViewTab[] getAllViewTabs() {
782         
783         // otherwise, find the view tab to display
784
TabFolder folder = (TabFolder) fStackLayout.topControl;
785         TabItem[] items = folder.getItems();
786         
787         IMemoryViewTab[] viewTabs = new IMemoryViewTab[folder.getItemCount()];
788         
789         for(int i=0; i<items.length; i++){
790             viewTabs[i] = (IMemoryViewTab)items[i].getData();
791         }
792         
793         return viewTabs;
794     }
795
796     /* (non-Javadoc)
797      * @see com.ibm.debug.extended.ui.IMemoryView#moveToTop(com.ibm.debug.extended.ui.IMemoryViewTab)
798      */

799     public void moveToTop(IMemoryViewTab viewTab) {
800         
801         IMemoryViewTab lastViewTab = getTopMemoryTab();
802         
803         if (viewTab == lastViewTab)
804             return;
805         
806         // otherwise, find the view tab to display
807
TabFolder folder = (TabFolder) fStackLayout.topControl;
808         TabItem[] items = folder.getItems();
809
810         for (int i = 0; i < items.length; i++) {
811             IMemoryViewTab tab =
812                 (IMemoryViewTab) items[i].getData();
813             if (viewTab == tab) {
814
815                 boolean isEnabled = lastViewTab.isEnabled();
816
817                 // switch to that viewTab
818
lastViewTab.setEnabled(false);
819                 folder.setSelection(i);
820                 
821                 setRenderingSelection(tab.getRendering());
822                 
823                 getTopMemoryTab().setEnabled(isEnabled && fVisible);
824                 break;
825             }
826         }
827     }
828
829     public void restoreViewPane() {
830         
831         // get current selection from memory view
832

833         ISelection selection = null;
834         if (fParent.getSite().getSelectionProvider() != null)
835             selection = fParent.getSite().getSelectionProvider().getSelection();
836         
837         IMemoryBlock memoryBlock = null;
838         if (selection != null)
839         {
840             memoryBlock = getMemoryBlock(selection);
841         }
842         
843         if (memoryBlock == null)
844         {
845             // get selection from this view
846
selection = fSelectionProvider.getSelection();
847             
848             if (MemoryViewUtil.isValidSelection(selection))
849             {
850                 Object JavaDoc elem = ((IStructuredSelection)selection).getFirstElement();
851
852                 if (!(elem instanceof IMemoryBlock))
853                     return;
854                 
855                 memoryBlock = (IMemoryBlock)elem;
856             }
857         }
858         
859         if (memoryBlock == null)
860         {
861             // get a memory block from current debug context
862
IAdaptable context = DebugUITools.getDebugContext();
863             if (context != null)
864             {
865                 IMemoryBlockRetrieval retrieval = (IMemoryBlockRetrieval)context.getAdapter(IMemoryBlockRetrieval.class);
866                 if (retrieval == null && context instanceof IDebugElement)
867                 {
868                     retrieval = ((IDebugElement)context).getDebugTarget();
869                 }
870                 if (retrieval != null)
871                 {
872                     IMemoryBlock[] blocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval);
873                     if (blocks.length > 0)
874                         memoryBlock = blocks[0];
875                 }
876             }
877         }
878         
879         if (memoryBlock != null)
880         {
881             if (!fTabFolderForMemoryBlock.containsKey(memoryBlock))
882             {
883                 // create tab folder if a tab folder does not already exist
884
// for the memory block
885
TabFolder folder = new TabFolder(fViewPaneCanvas, SWT.NULL);
886                 
887                 fTabFolderForMemoryBlock.put(memoryBlock, folder);
888                 fMemoryBlockFromTabFolder.put(folder, memoryBlock);
889                 setTabFolder((TabFolder)fTabFolderForMemoryBlock.get(memoryBlock));
890                 fTabFolderForDebugView.put(getMemoryBlockRetrieval(memoryBlock), fTabFolderForMemoryBlock.get(memoryBlock));
891                 fViewPaneCanvas.layout();
892                 fAddedMemoryBlocks.add(memoryBlock);
893             }
894             
895             if (fTabFolderForMemoryBlock.containsKey(memoryBlock))
896             {
897                 TabFolder toDisplay = (TabFolder)fTabFolderForMemoryBlock.get(memoryBlock);
898                 
899                 if (toDisplay != null)
900                 {
901                     setTabFolder(toDisplay);
902                     fTabFolderForDebugView.put(getMemoryBlockRetrieval(memoryBlock), toDisplay);
903                     fViewPaneCanvas.layout();
904                     
905                     // restore view tabs
906
IMemoryRendering[] renderings = fRenderingMgr.getRenderingsFromMemoryBlock(memoryBlock);
907                     
908                     if (toDisplay.getItemCount() == 0 || (getTopMemoryTab().getRendering() instanceof CreateRendering))
909                     {
910                         restoreViewTabs(renderings);
911                     }
912                 }
913             }
914             
915             // disable current storag block
916

917             IMemoryViewTab top = getTopMemoryTab();
918         
919             if (top != null)
920             {
921                 top.setEnabled(fVisible);
922             }
923             else
924             {
925                 TabFolder folder = (TabFolder)fStackLayout.topControl;
926                 if (folder != fEmptyTabFolder)
927                 {
928                     TabItem newItem = new TabItem(folder, SWT.NULL);
929                     CreateRendering rendering = new CreateRendering(this);
930                     rendering.init(getInstance(), memoryBlock);
931                     new MemoryViewTab(newItem, rendering, this);
932                     folder.setSelection(0);
933                     setRenderingSelection(rendering);
934                 }
935             }
936         }
937     }
938     
939     public void dispose() {
940         fIsDisposed = true;
941         super.dispose();
942         fAddMemoryRenderingAction.dispose();
943         
944         fTabFolderForMemoryBlock.clear();
945         fTabFolderForMemoryBlock = null;
946         
947         fMemoryBlockFromTabFolder.clear();
948         fMemoryBlockFromTabFolder = null;
949         
950         fRenderingMgr.dispose();
951         fRenderingMgr = null;
952         
953         fAddedMemoryBlocks.clear();
954         fAddedRenderings.clear();
955     }
956     
957     public Control createViewPane(Composite parent, String JavaDoc paneId, String JavaDoc label) {
958         Control control = super.createViewPane(parent, paneId, label);
959         fRenderingMgr = new ViewPaneRenderingMgr(this);
960         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IDebugUIConstants.PLUGIN_ID + ".MemoryRenderingView_context"); //$NON-NLS-1$
961
return control;
962     }
963     
964     public IAction[] getActions() {
965         ArrayList JavaDoc actions = new ArrayList JavaDoc();
966         
967         if (fAddMemoryRenderingAction == null)
968             fAddMemoryRenderingAction = new AddMemoryRenderingAction(this);
969         actions.add(fAddMemoryRenderingAction);
970         
971         if (fRemoveMemoryRenderingAction == null)
972             fRemoveMemoryRenderingAction = new RemoveMemoryRenderingAction(this);
973         
974         fRemoveMemoryRenderingAction.setEnabled(false);
975         actions.add(fRemoveMemoryRenderingAction);
976         
977         return (IAction[])actions.toArray(new IAction[actions.size()]);
978     }
979     
980     // enable/disable toolbar action
981
protected void updateToolBarActionsEnablement()
982     {
983         IDebugTarget target = getSelectedDebugTarget();
984         if (target != null)
985         {
986             ISelection selection = fSelectionProvider.getSelection();
987             if (selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection)
988             {
989                 Object JavaDoc sel = ((IStructuredSelection)selection).getFirstElement();
990                 if (sel instanceof CreateRendering)
991                     fRemoveMemoryRenderingAction.setEnabled(false);
992                 else
993                     fRemoveMemoryRenderingAction.setEnabled(true);
994             }
995             else
996             {
997                 fRemoveMemoryRenderingAction.setEnabled(false);
998             }
999         }
1000        else
1001        {
1002            fRemoveMemoryRenderingAction.setEnabled(false);
1003        }
1004    }
1005    
1006    private IDebugTarget getSelectedDebugTarget()
1007    {
1008        Object JavaDoc context = DebugUITools.getDebugContext();
1009        if (context != null && context instanceof IDebugElement)
1010        {
1011            return ((IDebugElement)context).getDebugTarget();
1012        }
1013        return null;
1014    }
1015    /* (non-Javadoc)
1016     * @see org.eclipse.debug.internal.ui.views.memory.AbstractMemoryViewPane#emptyFolder()
1017     */

1018    protected void emptyFolder() {
1019        super.emptyFolder();
1020        updateToolBarActionsEnablement();
1021        fSelectionProvider.setSelection(AbstractMemoryViewPane.EMPTY);
1022    }
1023
1024    /* (non-Javadoc)
1025     * @see org.eclipse.debug.internal.ui.views.memory.IRenderingViewPane#addMemoryRendering(org.eclipse.debug.internal.ui.views.memory.IMemoryRendering)
1026     */

1027    public void addMemoryRendering(IMemoryRendering rendering) {
1028        
1029        if (rendering == null)
1030            return;
1031
1032        memoryBlockRenderingAdded(rendering);
1033        fRenderingMgr.addMemoryBlockRendering(rendering);
1034        
1035    }
1036
1037    /* (non-Javadoc)
1038     * @see org.eclipse.debug.internal.ui.views.memory.IRenderingViewPane#removeMemoryRendering(org.eclipse.debug.internal.ui.views.memory.IMemoryRendering)
1039     */

1040    public void removeMemoryRendering(IMemoryRendering rendering) {
1041        
1042        if (rendering == null)
1043            return;
1044        
1045        memoryBlockRenderingRemoved(rendering);
1046        
1047        if (fRenderingMgr != null)
1048            fRenderingMgr.removeMemoryBlockRendering(rendering);
1049        
1050    }
1051    
1052    private RenderingViewPane getInstance()
1053    {
1054        return this;
1055    }
1056    
1057    private IMemoryBlock getMemoryBlock(ISelection selection)
1058    {
1059        if (!(selection instanceof IStructuredSelection))
1060            return null;
1061
1062        //only single selection of PICLDebugElements is allowed for this action
1063
if (selection.isEmpty() || ((IStructuredSelection)selection).size() > 1)
1064        {
1065            return null;
1066        }
1067
1068        Object JavaDoc elem = ((IStructuredSelection)selection).getFirstElement();
1069        
1070        if (elem instanceof IMemoryBlock)
1071            return (IMemoryBlock)elem;
1072        else if (elem instanceof IMemoryRendering)
1073            return ((IMemoryRendering)elem).getMemoryBlock();
1074        else
1075            return null;
1076    }
1077    
1078    private void deactivateRendering(IMemoryViewTab viewTab)
1079    {
1080        if (viewTab == null)
1081            return;
1082
1083        if (!viewTab.isDisposed())
1084        {
1085            viewTab.getRendering().deactivated();
1086        }
1087    }
1088
1089    /* (non-Javadoc)
1090     * @see org.eclipse.debug.internal.ui.views.memory.IRenderingViewPane#getMemoryRenderingSite()
1091     */

1092    public IMemoryRenderingSite getMemoryRenderingSite() {
1093        return fRenderingSite;
1094    }
1095
1096    /* (non-Javadoc)
1097     * @see org.eclipse.debug.ui.memory.IMemoryRenderingContainer#getId()
1098     */

1099    public String JavaDoc getId() {
1100        return getPaneId();
1101    }
1102
1103    /* (non-Javadoc)
1104     * @see org.eclipse.debug.ui.memory.IMemoryRenderingContainer#getRenderings()
1105     */

1106    public IMemoryRendering[] getRenderings() {
1107        return fRenderingMgr.getRenderings();
1108    }
1109
1110    /* (non-Javadoc)
1111     * @see org.eclipse.debug.ui.memory.IMemoryRenderingContainer#getActiveRendering()
1112     */

1113    public IMemoryRendering getActiveRendering() {
1114        if (getTopMemoryTab() == null)
1115            return null;
1116        return getTopMemoryTab().getRendering();
1117    }
1118    
1119    /**
1120     * Reset the memory renderings within this view pane.
1121     * @param memoryBlock - reset renderings associated with the given memory block
1122     * @param resetVisible - reset what's currently visible if the parameter is true.
1123     * Otherwise, the view pane will reset all renderings associated with the given
1124     * memory block.
1125     */

1126    public void resetRenderings(IMemoryBlock memoryBlock, boolean resetVisible)
1127    {
1128        // if we only reset what's visible and the view pane is not visible
1129
// do nothing.
1130
if (resetVisible && !isVisible())
1131            return;
1132        
1133        if(resetVisible)
1134        {
1135            IMemoryRendering rendering = getActiveRendering();
1136            if (rendering != null)
1137            {
1138                if (rendering.getMemoryBlock() == memoryBlock)
1139                {
1140                    if (rendering instanceof IResettableMemoryRendering)
1141                    {
1142                        IResettableMemoryRendering resettableRendering = (IResettableMemoryRendering)rendering;
1143                        try {
1144                            resettableRendering.resetRendering();
1145                        } catch (DebugException e) {
1146                            // do not pop up error message
1147
// error message is annoying where there are multiple rendering
1148
// panes and renderings to reset
1149
}
1150                    }
1151                }
1152            }
1153        }
1154        else
1155        {
1156            // get all renderings associated with the given memory block
1157
IMemoryRendering[] renderings = fRenderingMgr.getRenderingsFromMemoryBlock(memoryBlock);
1158            
1159            // back up current synchronization provider
1160
IMemoryRendering originalProvider = null;
1161            IMemoryRenderingSynchronizationService service = getMemoryRenderingSite().getSynchronizationService();
1162            if (service != null)
1163                originalProvider = service.getSynchronizationProvider();
1164            
1165            for (int i=0; i<renderings.length; i++)
1166            {
1167                if (renderings[i] instanceof IResettableMemoryRendering)
1168                {
1169                    try {
1170                        
1171                        // This is done to allow user to select multiple memory monitors and
1172
// reset their renderings.
1173
// In this case, a hidden rendering will not be the sync provider to the sync
1174
// service. When the reset happens, the top visible address and selected
1175
// address is not updated in the sync service. When the rendering later
1176
// becomes visible, the rendering gets the sync info from the sync service
1177
// and will try to sync up with old information, giving user the impression
1178
// that the rendering was never reset. By forcing the rendering that we
1179
// are trying to reset as the synchronization provider, we ensure that
1180
// the rendering is able to update its sync info even though the rendering
1181
// is currently hidden.
1182
if (service != null)
1183                            service.setSynchronizationProvider(renderings[i]);
1184                        ((IResettableMemoryRendering)renderings[i]).resetRendering();
1185                    } catch (DebugException e) {
1186                        // do not pop up error message
1187
// error message is annoying where there are multiple rendering
1188
// panes and renderings to reset
1189
}
1190                }
1191            }
1192            
1193            // restore synchronization provider
1194
if (service != null)
1195                service.setSynchronizationProvider(originalProvider);
1196        }
1197    }
1198    
1199    private boolean isDisposed()
1200    {
1201        return fIsDisposed;
1202    }
1203
1204    public void contextActivated(final ISelection selection) {
1205        
1206        UIJob job = new UIJob("contextActivated"){ //$NON-NLS-1$
1207
public IStatus runInUIThread(IProgressMonitor monitor)
1208            {
1209                if (isDisposed())
1210                    return Status.OK_STATUS;
1211                
1212                IMemoryViewTab lastViewTab = getTopMemoryTab();
1213                
1214                if (MemoryViewUtil.isValidSelection(selection))
1215                {
1216                    if (!(selection instanceof IStructuredSelection))
1217                        return Status.OK_STATUS;
1218
1219                    Object JavaDoc elem = ((IStructuredSelection)selection).getFirstElement();
1220                    
1221                    if (elem instanceof IDebugElement)
1222                    {
1223                        handleDebugElementSelection(lastViewTab, (IDebugElement)elem);
1224                    }
1225                }
1226                else
1227                {
1228                    if (lastViewTab != null)
1229                        lastViewTab.setEnabled(false);
1230                    
1231                    if (fStackLayout.topControl != fEmptyTabFolder)
1232                        emptyFolder();
1233                    
1234                }
1235                return Status.OK_STATUS;
1236            }};
1237        job.setSystem(true);
1238        job.schedule();
1239    }
1240
1241    /**
1242     * @param memory
1243     */

1244    private TabFolder createFolderForMemoryBlock(IMemoryBlock memory) {
1245        TabFolder folder = new TabFolder(fViewPaneCanvas, SWT.NULL);
1246        
1247        fTabFolderForMemoryBlock.put(memory, folder);
1248        fMemoryBlockFromTabFolder.put(folder, memory);
1249        fTabFolderForDebugView.put(getMemoryBlockRetrieval(memory), folder);
1250        
1251        // check renderings, only create if there is no rendering
1252
IMemoryRendering[] renderings = fRenderingMgr.getRenderingsFromMemoryBlock(memory);
1253        if (renderings.length == 0)
1254        {
1255            TabItem newItem = new TabItem(folder, SWT.NULL);
1256            CreateRendering rendering = new CreateRendering(getInstance());
1257            rendering.init(getInstance(), memory);
1258            new MemoryViewTab(newItem, rendering, getInstance());
1259            folder.setSelection(0);
1260        }
1261        
1262        return folder;
1263    }
1264
1265    /* (non-Javadoc)
1266     * @see org.eclipse.debug.internal.ui.contexts.provisional.IDebugContextListener#contextEvent(org.eclipse.debug.internal.ui.contexts.provisional.DebugContextEvent)
1267     */

1268    public void debugContextChanged(DebugContextEvent event) {
1269        if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) {
1270            contextActivated(event.getContext());
1271        }
1272        
1273    }
1274}
1275
Popular Tags