KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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
14 import java.util.Enumeration JavaDoc;
15 import java.util.Hashtable JavaDoc;
16
17 import org.eclipse.debug.core.IMemoryBlockListener;
18 import org.eclipse.debug.core.model.IMemoryBlock;
19 import org.eclipse.debug.core.model.IMemoryBlockRetrieval;
20 import org.eclipse.debug.internal.ui.DebugUIPlugin;
21 import org.eclipse.debug.ui.DebugUITools;
22 import org.eclipse.debug.ui.contexts.IDebugContextListener;
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.ISelectionChangedListener;
26 import org.eclipse.jface.viewers.ISelectionProvider;
27 import org.eclipse.jface.viewers.SelectionChangedEvent;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.custom.StackLayout;
31 import org.eclipse.swt.events.SelectionListener;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.TabFolder;
37 import org.eclipse.swt.widgets.TabItem;
38 import org.eclipse.ui.ISelectionListener;
39 import org.eclipse.ui.IViewPart;
40 import org.eclipse.ui.IWorkbenchPart;
41
42 public abstract class AbstractMemoryViewPane implements IMemoryBlockListener, ISelectionListener, SelectionListener, IMemoryView, ISelectionChangedListener, IMemoryViewPane, IDebugContextListener{
43     
44     public static final String JavaDoc BEGINNING_POPUP = "popUpBegin"; //$NON-NLS-1$
45
protected static final StructuredSelection EMPTY = new StructuredSelection();
46     
47     protected Composite fViewPaneCanvas;
48     protected StackLayout fStackLayout;
49     protected ViewTabEnablementManager fViewTabEnablementManager;
50     protected TabFolder fEmptyTabFolder;
51     protected Hashtable JavaDoc fTabFolderForDebugView = new Hashtable JavaDoc();
52     protected boolean fVisible;
53     protected Hashtable JavaDoc fRenderingInfoTable;
54     protected IMemoryBlockRetrieval fKey; // store the key for current tab folder
55
protected ViewPaneSelectionProvider fSelectionProvider;
56     protected IViewPart fParent;
57     protected String JavaDoc fPaneId;
58     private Composite fCanvas;
59     protected String JavaDoc fLabel;
60     
61     public AbstractMemoryViewPane(IViewPart parent)
62     {
63         super();
64         fParent = parent;
65         fSelectionProvider = new ViewPaneSelectionProvider();
66     }
67     
68     /**
69      * Create the content of the view pane
70      * @param parent
71      * @return the control of the view pane
72      */

73     public Control createViewPane(Composite parent, String JavaDoc paneId, String JavaDoc label)
74     {
75         fPaneId = paneId;
76         fLabel = label;
77         fCanvas = new Composite(parent, SWT.NONE);
78         GridLayout layout = new GridLayout();
79         layout.makeColumnsEqualWidth = false;
80         layout.numColumns = 1;
81         layout.marginHeight = 0;
82         layout.marginWidth = 0;
83         GridData data = new GridData();
84         data.grabExcessHorizontalSpace = true;
85         data.grabExcessVerticalSpace = true;
86         data.verticalAlignment = SWT.BEGINNING;
87         data.horizontalAlignment = SWT.BEGINNING;
88         fCanvas.setLayout(layout);
89         fCanvas.setLayoutData(data);
90
91         // memory view area
92
Composite memoryViewAreaParent = fCanvas;
93         Composite subCanvas = new Composite(memoryViewAreaParent, SWT.NONE);
94         fViewPaneCanvas = subCanvas;
95         fStackLayout = new StackLayout();
96         GridData memoryAreaData = new GridData();
97         memoryAreaData.grabExcessHorizontalSpace = true;
98         memoryAreaData.grabExcessVerticalSpace = true;
99         memoryAreaData.verticalAlignment = SWT.FILL;
100         memoryAreaData.horizontalAlignment = SWT.FILL;
101         fViewPaneCanvas.setLayout(fStackLayout);
102         fViewPaneCanvas.setLayoutData(memoryAreaData);
103         
104         fViewTabEnablementManager = new ViewTabEnablementManager();
105         
106         fEmptyTabFolder = new TabFolder(fViewPaneCanvas, SWT.NULL);
107         setTabFolder(fEmptyTabFolder);
108         
109         addListeners();
110         
111         Object JavaDoc context = DebugUITools.getDebugContext();
112         if (context != null)
113         {
114             IMemoryBlockRetrieval retrieval = MemoryViewUtil.getMemoryBlockRetrieval(context);
115             if (retrieval != null)
116                 createFolder(retrieval);
117         }
118         
119         fVisible = true;
120         
121         return fCanvas;
122     }
123     
124     protected void addListeners()
125     {
126         MemoryViewUtil.getMemoryBlockManager().addListener(this);
127         fParent.getViewSite().getPage().addSelectionListener(this);
128         DebugUITools.getDebugContextManager().getContextService(fParent.getSite().getWorkbenchWindow()).addDebugContextListener(this);
129     }
130     
131     protected void removeListeners()
132     {
133         MemoryViewUtil.getMemoryBlockManager().removeListener(this);
134         fParent.getViewSite().getPage().removeSelectionListener(this);
135         DebugUITools.getDebugContextManager().getContextService(fParent.getSite().getWorkbenchWindow()).removeDebugContextListener(this);
136         
137         if (fStackLayout.topControl != null)
138         {
139             TabFolder old = (TabFolder)fStackLayout.topControl;
140             
141             if (!old.isDisposed())
142             {
143                 old.removeSelectionListener(this);
144                 old.removeSelectionListener(fViewTabEnablementManager);
145             }
146         }
147     }
148     
149     protected void setTabFolder(TabFolder folder)
150     {
151         if (fStackLayout.topControl != null)
152         {
153             TabFolder old = (TabFolder)fStackLayout.topControl;
154             
155             if (!old.isDisposed())
156             {
157                 old.removeSelectionListener(this);
158                 old.removeSelectionListener(fViewTabEnablementManager);
159             }
160         }
161         
162         fStackLayout.topControl = folder;
163         
164         if (folder.getItemCount() > 0)
165         {
166             TabItem[] selectedItem = folder.getSelection();
167             
168             if (selectedItem.length > 0)
169             {
170                 Object JavaDoc selected = getCurrentSelection();
171                 if (selected != null)
172                 {
173                     fSelectionProvider.setSelection(new StructuredSelection(selected));
174                 }
175                 else
176                 {
177                     fSelectionProvider.setSelection(AbstractMemoryViewPane.EMPTY);
178                 }
179             }
180         }
181         else
182         {
183             fSelectionProvider.setSelection(AbstractMemoryViewPane.EMPTY);
184         }
185         
186         folder.addSelectionListener(this);
187         folder.addSelectionListener(fViewTabEnablementManager);
188     }
189     
190     
191     private void createFolder(IMemoryBlockRetrieval memRetrieval)
192     {
193         //if we've got a tabfolder to go with the IMemoryBlockRetrieval, display it
194
if (fTabFolderForDebugView.containsKey(memRetrieval)) {
195             if (fStackLayout.topControl != (TabFolder)fTabFolderForDebugView.get(memRetrieval)) {
196                 setTabFolder((TabFolder)fTabFolderForDebugView.get(memRetrieval));
197                 fViewPaneCanvas.layout();
198             }
199         } else { //otherwise, add a new one
200
fTabFolderForDebugView.put(memRetrieval, new TabFolder(fViewPaneCanvas, SWT.NULL));
201             setTabFolder((TabFolder)fTabFolderForDebugView.get(memRetrieval));
202             fViewPaneCanvas.layout();
203         }
204     }
205
206     public IMemoryViewTab getTopMemoryTab() {
207         
208         if (fStackLayout.topControl instanceof TabFolder)
209         {
210             TabFolder folder = (TabFolder)fStackLayout.topControl;
211             if (!folder.isDisposed())
212             {
213                 int index = folder.getSelectionIndex();
214                 if (index >= 0) {
215                     TabItem tab = folder.getItem(index);
216                     return (IMemoryViewTab)tab.getData();
217                 }
218             }
219         }
220         return null;
221     }
222     
223     protected void disposeTab(TabItem tabItem)
224     {
225         if (tabItem == null)
226             return;
227         
228         // dispose the tab item in case the view tab has not
229
// cleaned up the tab item
230
if (!tabItem.isDisposed())
231         {
232             tabItem.dispose();
233         }
234     }
235
236     protected void emptyFolder()
237     {
238         setTabFolder(fEmptyTabFolder);
239         if (!fViewPaneCanvas.isDisposed()) {
240             fViewPaneCanvas.layout();
241         }
242     }
243     
244     public void addSelectionListener(ISelectionChangedListener listener)
245     {
246         if (fSelectionProvider == null)
247             fSelectionProvider = new ViewPaneSelectionProvider();
248         
249         fSelectionProvider.addSelectionChangedListener(listener);
250     }
251     
252     public void removeSelctionListener(ISelectionChangedListener listener)
253     {
254         if (fSelectionProvider == null)
255             return;
256         
257         fSelectionProvider.removeSelectionChangedListener(listener);
258     }
259     
260     public ISelectionProvider getSelectionProvider()
261     {
262         return fSelectionProvider;
263     }
264     
265     public void dispose()
266     {
267         removeListeners();
268         
269         // dispose empty folders
270
fEmptyTabFolder.dispose();
271         
272         // dispose all other folders
273
try {
274             
275             if (fTabFolderForDebugView != null) {
276                 Enumeration JavaDoc enumeration = fTabFolderForDebugView.elements();
277                 
278                 while (enumeration.hasMoreElements())
279                 {
280                     TabFolder tabFolder = (TabFolder)enumeration.nextElement();
281                     
282                     if (tabFolder.isDisposed())
283                         continue;
284                     
285                     // if tab folder is not empty, dipose view tabs
286
TabItem[] tabs = tabFolder.getItems();
287                     
288                     for (int i=0; i<tabs.length; i++)
289                     {
290                         disposeTab(tabs[i]);
291                     }
292                     
293                     tabFolder.dispose();
294                 }
295                 
296                 // set to null so that clean up is only done once
297
fTabFolderForDebugView.clear();
298                 fTabFolderForDebugView = null;
299             }
300         } catch (Exception JavaDoc e) {
301             
302             DebugUIPlugin.logErrorMessage("Exception occurred when the Memory View is disposed."); //$NON-NLS-1$
303
}
304     }
305     
306     public void setVisible(boolean visible)
307     {
308         fVisible = visible;
309         
310         IMemoryViewTab currentTab = getTopMemoryTab();
311         if (currentTab != null)
312             currentTab.setEnabled(visible);
313     }
314
315     public void selectionChanged(SelectionChangedEvent event) {
316         ISelection selection = event.getSelection();
317         selectionChanged(fParent,selection);
318         
319         fSelectionProvider.setSelection(selection);
320     }
321     
322     /**
323      * @return the unique identifier of the view pane
324      */

325     public String JavaDoc getPaneId()
326     {
327         return fPaneId;
328     }
329     
330     
331     /* (non-Javadoc)
332      * @see org.eclipse.debug.internal.ui.views.memory.IMemoryViewPane#getControl()
333      */

334     public Control getControl() {
335         return fCanvas;
336     }
337     
338     public boolean isVisible()
339     {
340         return fVisible;
341     }
342     
343     public String JavaDoc getLabel()
344     {
345         return fLabel;
346     }
347     
348     /* (non-Javadoc)
349      * @see org.eclipse.debug.internal.core.memory.IMemoryBlockListener#MemoryBlockAdded(org.eclipse.debug.core.model.IMemoryBlock)
350      */

351     abstract public void memoryBlocksAdded(IMemoryBlock[] memoryBlocks);
352     
353     /* (non-Javadoc)
354      * @see org.eclipse.debug.internal.core.memory.IMemoryBlockListener#MemoryBlockRemoved(org.eclipse.debug.core.model.IMemoryBlock)
355      */

356     abstract public void memoryBlocksRemoved(final IMemoryBlock[] memoryBlocks);
357
358     /* (non-Javadoc)
359      * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
360      */

361     abstract public void selectionChanged(IWorkbenchPart part, ISelection selection);
362     
363     /**
364      * @return current selection from the view pane
365      */

366     abstract public Object JavaDoc getCurrentSelection();
367     
368     /**
369      * retore the view pane based on current selection from the debug view
370      * and the memory blocks and renderings currently exist
371      */

372     abstract public void restoreViewPane();
373     
374     /**
375      * @return actions to be contributed to the view pane's toolbar
376      */

377     abstract public IAction[] getActions();
378
379 }
380
Popular Tags