KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.debug.internal.ui.views.memory;
12
13 import java.util.ArrayList JavaDoc;
14 import org.eclipse.debug.core.model.IMemoryBlock;
15 import org.eclipse.debug.internal.core.memory.IExtendedMemoryBlock;
16 import org.eclipse.debug.internal.core.memory.IMemoryRendering;
17 import org.eclipse.debug.internal.ui.DebugUIPlugin;
18 import org.eclipse.jface.action.MenuManager;
19 import org.eclipse.swt.widgets.TabItem;
20
21 /**
22  * This class is created to help maintain reference counting to a memory block.
23  * Plugins that wish to contribute a new rendering to Memory Rendering View can extend
24  * from this class. When a view tab is enabled, a reference will be addd to a reference
25  * counting object in the synchronizer. When a view tab is disabled, a reference will be removed
26  * to a reference counting object in the synchronizer. Memory Block will be enabled / disabled
27  * based on this reference counting object. If setEnabled is to be overriden, implementor must call
28  * AbstractMemoryViewTab.setEnabled() or handle reference counting by itself.
29  *
30  * @since 3.0
31  */

32 abstract public class AbstractMemoryViewTab implements IMemoryViewTab {
33     
34     protected IMemoryBlock fMemoryBlock;
35     protected TabItem fTabItem;
36     protected MenuManager fMenuMgr;
37     protected IMemoryRendering fRendering;
38     protected String JavaDoc fRenderingId;
39     
40     public AbstractMemoryViewTab(IMemoryBlock newMemory, TabItem newTab, MenuManager menuMgr, IMemoryRendering rendering)
41     {
42         fMemoryBlock = newMemory;
43         fTabItem = newTab;
44         fMenuMgr = menuMgr;
45         fRendering = rendering;
46         fRenderingId = rendering.getRenderingId();
47         
48         fTabItem.setData(this);
49         
50         // Whenever a new view tab is created, enable the tab to ensure
51
// that reference counting and memory block enablement state is
52
// maintained.
53
maintainRefAndEnablement(true);
54     }
55     
56     /* (non-Javadoc)
57      * @see org.eclipse.debug.ui.IMemoryViewTab#setEnabled(boolean)
58      */

59     public void setEnabled(boolean enabled) {
60         maintainRefAndEnablement(enabled);
61     }
62     
63     /**
64      * Maintain memory enabled reference counting and its enablement
65      * If addReference is true, a reference count will be added to the memory block.
66      * The memory block will be enabled if there is more than one object referencing to the
67      * memory block.
68      * If addReference is false, a refernece count will be removed from the memory block
69      * The memory block will be disabled if no object is referencing to the memory block.
70      * @param addReference
71      */

72     protected void maintainRefAndEnablement(boolean addReference)
73     {
74         // if add enabled reference
75
if (addReference)
76         {
77             if (getMemoryBlock() instanceof IExtendedMemoryBlock)
78             {
79                 // add view tab to synchronizer
80
ArrayList JavaDoc references = addReferenceToSynchronizer();
81                 
82                 // if this is the first enabled reference to the memory block
83
// enable the memory block
84
if (references.size() == 1 && !((IExtendedMemoryBlock)getMemoryBlock()).isEnabled() )
85                     ((IExtendedMemoryBlock)getMemoryBlock()).enable();
86             }
87         }
88         // if remove enabled reference
89
else if (!addReference){
90             if (getMemoryBlock() instanceof IExtendedMemoryBlock)
91             {
92                 ArrayList JavaDoc references = removeReferenceFromSynchronizer();
93                 
94                 if (references == null)
95                     return;
96                 
97                 // if there is not more enabled reference to the memory block
98
// disable the memory block
99
if (references.size() == 0 && ((IExtendedMemoryBlock)getMemoryBlock()).isEnabled())
100                     ((IExtendedMemoryBlock)getMemoryBlock()).disable();
101             }
102         }
103     }
104     
105     /**
106      * Multiple view tabs can reference to the same memory block.
107      * Use this property to keep track of all references that are enabled.
108      * (i.e. requiring change events from the memory block.)
109      * When a view tab is created/enabled, a reference will be added to the synchronizer.
110      * When a view tab is disposed/disabled, the reference will be removed from the synchronizer.
111      * The view tab examines this references array and will only enable
112      * a memory block if there is at least one enabled reference to the memory
113      * block. If there is no enabled reference to the memory block, the
114      * memory block should be disabled.
115      * @return the reference object
116      */

117     protected ArrayList JavaDoc addReferenceToSynchronizer() {
118
119         ArrayList JavaDoc references = (ArrayList JavaDoc)DebugUIPlugin.getDefault().getMemoryBlockViewSynchronizer().getSynchronizedProperty(getMemoryBlock(), IMemoryViewConstants.PROPERTY_ENABLED_REFERENCES);
120         
121         // first reference count
122
if (references == null)
123         {
124             references = new ArrayList JavaDoc();
125         }
126         
127         // add the reference to the reference counting object
128
if (!references.contains(this))
129         {
130             references.add(this);
131         }
132         
133         DebugUIPlugin.getDefault().getMemoryBlockViewSynchronizer().setSynchronizedProperty(getMemoryBlock(), IMemoryViewConstants.PROPERTY_ENABLED_REFERENCES, references);
134         return references;
135     }
136     
137     /**
138      * @return the reference object, null if the reference object does not exisit in the synchronizer
139      */

140     protected ArrayList JavaDoc removeReferenceFromSynchronizer()
141     {
142         ArrayList JavaDoc references = (ArrayList JavaDoc)DebugUIPlugin.getDefault().getMemoryBlockViewSynchronizer().getSynchronizedProperty(getMemoryBlock(), IMemoryViewConstants.PROPERTY_ENABLED_REFERENCES);
143         
144         // do not create a new reference object if it does not exist
145
// the memory block may have been deleted
146
if (references == null)
147         {
148             return null;
149         }
150         
151         // remove the reference from the reference counting object
152
if (references.contains(this))
153         {
154             references.remove(this);
155         }
156         
157         DebugUIPlugin.getDefault().getMemoryBlockViewSynchronizer().setSynchronizedProperty(getMemoryBlock(), IMemoryViewConstants.PROPERTY_ENABLED_REFERENCES, references);
158         return references;
159     }
160
161     /* (non-Javadoc)
162      * @see org.eclipse.debug.ui.IMemoryViewTab#dispose()
163      */

164     public void dispose() {
165         
166         // whenever a view tab is disposed, disable view tab to ensure that
167
// reference counting and memory block enablement state is maintained.
168
maintainRefAndEnablement(false);
169     }
170
171     /* (non-Javadoc)
172      * @see org.eclipse.debug.ui.IMemoryViewTab#getMemoryBlock()
173      */

174     public IMemoryBlock getMemoryBlock() {
175         return fMemoryBlock;
176     }
177
178     /* (non-Javadoc)
179      * @see org.eclipse.debug.ui.IMemoryViewTab#getRenderingId()
180      */

181     public String JavaDoc getRenderingId() {
182         return fRendering.getRenderingId();
183     }
184     
185     /* (non-Javadoc)
186      * @see org.eclipse.debug.ui.IMemoryViewTab#getRendering()
187      */

188     public IMemoryRendering getRendering()
189     {
190         return fRendering;
191     }
192
193 }
194
Popular Tags