KickJava   Java API By Example, From Geeks To Geeks.

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


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.renderings;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.eclipse.debug.core.DebugException;
19 import org.eclipse.debug.core.model.IDebugElement;
20 import org.eclipse.debug.core.model.IDebugTarget;
21 import org.eclipse.debug.core.model.IMemoryBlock;
22 import org.eclipse.debug.core.model.IMemoryBlockRetrieval;
23 import org.eclipse.debug.internal.ui.DebugUIMessages;
24 import org.eclipse.debug.internal.ui.DebugUIPlugin;
25 import org.eclipse.debug.internal.ui.views.memory.MemoryViewUtil;
26 import org.eclipse.debug.ui.DebugUITools;
27 import org.eclipse.debug.ui.memory.AbstractMemoryRendering;
28 import org.eclipse.debug.ui.memory.IMemoryRendering;
29 import org.eclipse.debug.ui.memory.IMemoryRenderingBindingsListener;
30 import org.eclipse.debug.ui.memory.IMemoryRenderingContainer;
31 import org.eclipse.debug.ui.memory.IMemoryRenderingType;
32 import org.eclipse.jface.util.PropertyChangeEvent;
33 import org.eclipse.jface.viewers.DoubleClickEvent;
34 import org.eclipse.jface.viewers.IBasicPropertyConstants;
35 import org.eclipse.jface.viewers.IDoubleClickListener;
36 import org.eclipse.jface.viewers.ILabelProvider;
37 import org.eclipse.jface.viewers.ILabelProviderListener;
38 import org.eclipse.jface.viewers.ISelection;
39 import org.eclipse.jface.viewers.IStructuredContentProvider;
40 import org.eclipse.jface.viewers.IStructuredSelection;
41 import org.eclipse.jface.viewers.ListViewer;
42 import org.eclipse.jface.viewers.Viewer;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.events.SelectionEvent;
45 import org.eclipse.swt.events.SelectionListener;
46 import org.eclipse.swt.graphics.Image;
47 import org.eclipse.swt.layout.GridData;
48 import org.eclipse.swt.layout.GridLayout;
49 import org.eclipse.swt.widgets.Button;
50 import org.eclipse.swt.widgets.Composite;
51 import org.eclipse.swt.widgets.Control;
52 import org.eclipse.swt.widgets.Label;
53 import org.eclipse.ui.progress.WorkbenchJob;
54
55 /**
56  * The rendering to allow users to create a rendering.
57  * @since 3.1
58  */

59 public class CreateRendering extends AbstractMemoryRendering implements IMemoryRenderingBindingsListener {
60
61     private ListViewer fViewer;
62     private Label fMemoryBlockLabel;
63     private IMemoryRenderingContainer fContainer;
64     private Composite fCanvas;
65     private String JavaDoc fLabel;
66     
67     public CreateRendering(IMemoryRenderingContainer container)
68     {
69         super("org.eclipse.debug.internal.ui.views.createrendering"); //$NON-NLS-1$
70
fContainer = container;
71     }
72
73     class MemoryRenderingLabelProvider implements ILabelProvider
74     {
75         
76         /* (non-Javadoc)
77          * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
78          */

79         public Image getImage(Object JavaDoc element) {
80             return null;
81         }
82
83         /* (non-Javadoc)
84          * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
85          */

86         public String JavaDoc getText(Object JavaDoc element) {
87             if (element instanceof IMemoryRenderingType)
88             {
89                 String JavaDoc label = ((IMemoryRenderingType)element).getLabel();
90                 return label;
91             }
92             return element.toString();
93         }
94
95         /* (non-Javadoc)
96          * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
97          */

98         public void addListener(ILabelProviderListener listener) {
99         }
100
101         /* (non-Javadoc)
102          * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
103          */

104         public void dispose() {
105         }
106
107         /* (non-Javadoc)
108          * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
109          */

110         public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
111             return false;
112         }
113
114         /* (non-Javadoc)
115          * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
116          */

117         public void removeListener(ILabelProviderListener listener) {
118         }
119
120     }
121     
122     class MemoryRenderingContentProvider implements IStructuredContentProvider
123     {
124
125         /* (non-Javadoc)
126          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
127          */

128         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
129             IMemoryRenderingType[] renderings = DebugUITools.getMemoryRenderingManager().getRenderingTypes((IMemoryBlock)inputElement);
130             return renderings;
131         }
132
133         /* (non-Javadoc)
134          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
135          */

136         public void dispose() {
137             
138         }
139
140         /* (non-Javadoc)
141          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
142          */

143         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
144         }
145         
146     }
147     
148     /* (non-Javadoc)
149      * @see org.eclipse.debug.ui.memory.IMemoryRendering#createControl(org.eclipse.swt.widgets.Composite)
150      */

151     public Control createControl(Composite parent) {
152         fCanvas = new Composite(parent, SWT.NONE);
153         GridLayout compositeLayout = new GridLayout();
154         compositeLayout.numColumns = 2;
155         compositeLayout.makeColumnsEqualWidth = false;
156         fCanvas.setLayout(compositeLayout);
157         
158         GridData comositeSpec= new GridData();
159         comositeSpec.grabExcessVerticalSpace= true;
160         comositeSpec.grabExcessHorizontalSpace= true;
161         comositeSpec.horizontalAlignment= GridData.FILL;
162         comositeSpec.verticalAlignment= GridData.CENTER;
163         fCanvas.setLayoutData(comositeSpec);
164         
165         fMemoryBlockLabel = new Label(fCanvas, SWT.BORDER);
166         
167         String JavaDoc memoryBlockLabel = " "; //$NON-NLS-1$
168
memoryBlockLabel = getLabel();
169         
170         fMemoryBlockLabel.setText(" " + DebugUIMessages.CreateRenderingTab_Memory_monitor + memoryBlockLabel + " "); //$NON-NLS-1$ //$NON-NLS-2$
171
GridData textLayout = new GridData();
172         textLayout.verticalAlignment=GridData.CENTER;
173         textLayout.horizontalAlignment=GridData.BEGINNING;
174         fMemoryBlockLabel.setLayoutData(textLayout);
175
176         Label renderingLabel = new Label(fCanvas, SWT.NONE);
177         renderingLabel.setText(DebugUIMessages.CreateRenderingTab_Select_renderings_to_create);
178         GridData renderingLayout = new GridData();
179         renderingLayout.horizontalAlignment = GridData.BEGINNING;
180         renderingLayout.verticalAlignment = GridData.CENTER;
181         renderingLayout.horizontalSpan = 2;
182         renderingLabel.setLayoutData(renderingLayout);
183         
184         fViewer = new ListViewer(fCanvas);
185         fViewer.setContentProvider(new MemoryRenderingContentProvider());
186         fViewer.setLabelProvider(new MemoryRenderingLabelProvider());
187         fViewer.setInput(getMemoryBlock());
188         
189         if (fViewer.getElementAt(0) != null)
190         {
191             fViewer.getList().select(0);
192         }
193         
194         GridData listLayout = new GridData(GridData.FILL_BOTH);
195         listLayout.horizontalSpan = 1;
196         fViewer.getControl().setLayoutData(listLayout);
197         
198         fViewer.addDoubleClickListener(new IDoubleClickListener (){
199
200             public void doubleClick(DoubleClickEvent event) {
201                 addRenderings();
202             }});
203         
204         Button addButton = new Button(fCanvas, SWT.NONE);
205         addButton.setText(DebugUIMessages.CreateRenderingTab_Add_renderings);
206         GridData buttonLayout = new GridData();
207         buttonLayout.horizontalAlignment = GridData.BEGINNING;
208         buttonLayout.verticalAlignment = GridData.BEGINNING;
209         addButton.setLayoutData(buttonLayout);
210         
211         addButton.addSelectionListener(new SelectionListener() {
212
213             public void widgetSelected(SelectionEvent e) {
214                 addRenderings();
215             }
216
217             public void widgetDefaultSelected(SelectionEvent e) {
218                 addRenderings();
219             }});
220         
221         DebugUITools.getMemoryRenderingManager().addListener(this);
222         
223         return fCanvas;
224     }
225     
226     private void addRenderings()
227     {
228         ISelection selection = fViewer.getSelection();
229         Object JavaDoc[] renderings = null;
230         
231         if (selection instanceof IStructuredSelection)
232         {
233             IStructuredSelection strucSelection = (IStructuredSelection)selection;
234             
235             renderings = strucSelection.toArray();
236         }
237         
238         if (renderings == null)
239         {
240             Status stat = new Status(
241                     IStatus.ERROR,DebugUIPlugin.getUniqueIdentifier(),
242                     DebugException.INTERNAL_ERROR, DebugUIMessages.CreateRenderingTab_0, null);
243             DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), DebugUIMessages.CreateRenderingTab_1, DebugUIMessages.CreateRenderingTab_2, stat); //
244
return;
245         }
246                                         
247         // ask for debug target and memory block retrieval
248
IDebugTarget debugTarget = ((IDebugElement)getMemoryBlock()).getDebugTarget();
249         IMemoryBlockRetrieval standardMemRetrieval = (IMemoryBlockRetrieval)((IDebugElement)getMemoryBlock()).getAdapter(IMemoryBlockRetrieval.class);
250         
251         if (standardMemRetrieval == null)
252         {
253             standardMemRetrieval = debugTarget;
254         }
255         
256         if (standardMemRetrieval == null)
257             return;
258         
259         
260         // make a copy of the container, may be diposed when a rendering is added
261
IMemoryRenderingContainer container = fContainer;
262         // add memory renderings to Memory Rendering Manager
263
for (int i=0; i<renderings.length; i++)
264         {
265             if (renderings[i] instanceof IMemoryRenderingType)
266             {
267                 try {
268                     IMemoryRendering rendering = ((IMemoryRenderingType)renderings[i]).createRendering();
269                     if (rendering != null)
270                     {
271                         rendering.init(container, getMemoryBlock());
272                         container.addMemoryRendering(rendering);
273                     }
274                 } catch (CoreException e) {
275                     
276                     MemoryViewUtil.openError(DebugUIMessages.CreateRendering_0, DebugUIMessages.CreateRendering_1, e); //
277
}
278             }
279         }
280     }
281
282     /* (non-Javadoc)
283      * @see org.eclipse.debug.ui.memory.IMemoryRendering#dispose()
284      */

285     public void dispose() {
286         fViewer = null;
287         fCanvas = null;
288         fContainer = null;
289         fMemoryBlockLabel = null;
290         DebugUITools.getMemoryRenderingManager().removeListener(this);
291     }
292
293     /* (non-Javadoc)
294      * @see org.eclipse.debug.internal.ui.memory.AbstractMemoryRendering#getControl()
295      */

296     public Control getControl() {
297         return fCanvas;
298     }
299     public void memoryRenderingBindingsChanged() {
300         if (fViewer != null)
301             fViewer.refresh();
302     }
303
304     public String JavaDoc getLabel() {
305         if (fLabel == null)
306         {
307             fLabel = DebugUIMessages.CreateRendering_2;
308             updateRenderingLabel();
309         }
310         
311         return fLabel;
312     }
313     
314     protected void updateRenderingLabel()
315     {
316         Job job = new Job("Update Rendering Label"){ //$NON-NLS-1$
317

318             protected IStatus run(IProgressMonitor monitor) {
319                 fLabel = CreateRendering.super.getLabel();
320                 firePropertyChangedEvent(new PropertyChangeEvent(CreateRendering.this, IBasicPropertyConstants.P_TEXT, null, fLabel));
321                 
322                 WorkbenchJob wbJob = new WorkbenchJob("Create Rendering Update Label"){ //$NON-NLS-1$
323

324                     public IStatus runInUIThread(IProgressMonitor wbMonitor) {
325                         if (fMemoryBlockLabel != null)
326                         {
327                             fMemoryBlockLabel.setText(" " + DebugUIMessages.CreateRenderingTab_Memory_monitor + fLabel + " "); //$NON-NLS-1$//$NON-NLS-2$
328
fMemoryBlockLabel.getParent().layout();
329                         }
330                         return Status.OK_STATUS;
331                     }};
332                 wbJob.setSystem(true);
333                 wbJob.schedule();
334                 return Status.OK_STATUS;
335             }};
336         job.setSystem(true);
337         job.schedule();
338     }
339 }
340
Popular Tags