KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.eclipse.core.runtime.IAdaptable;
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.DebugPlugin;
20 import org.eclipse.debug.core.IMemoryBlockListener;
21 import org.eclipse.debug.core.model.IDebugElement;
22 import org.eclipse.debug.core.model.IMemoryBlock;
23 import org.eclipse.debug.core.model.IMemoryBlockExtension;
24 import org.eclipse.debug.internal.ui.DebugUIMessages;
25 import org.eclipse.debug.internal.ui.DebugUIPlugin;
26 import org.eclipse.debug.ui.DebugUITools;
27 import org.eclipse.debug.ui.IDebugUIConstants;
28 import org.eclipse.debug.ui.memory.IMemoryRendering;
29 import org.eclipse.debug.ui.memory.IMemoryRenderingBindingsListener;
30 import org.eclipse.debug.ui.memory.IMemoryRenderingSite;
31 import org.eclipse.debug.ui.memory.IMemoryRenderingType;
32 import org.eclipse.jface.dialogs.IDialogConstants;
33 import org.eclipse.jface.viewers.DoubleClickEvent;
34 import org.eclipse.jface.viewers.IDoubleClickListener;
35 import org.eclipse.jface.viewers.ILabelDecorator;
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.ISelectionChangedListener;
40 import org.eclipse.jface.viewers.ISelectionProvider;
41 import org.eclipse.jface.viewers.IStructuredContentProvider;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43 import org.eclipse.jface.viewers.ListViewer;
44 import org.eclipse.jface.viewers.SelectionChangedEvent;
45 import org.eclipse.jface.viewers.Viewer;
46 import org.eclipse.swt.SWT;
47 import org.eclipse.swt.events.SelectionAdapter;
48 import org.eclipse.swt.events.SelectionEvent;
49 import org.eclipse.swt.events.SelectionListener;
50 import org.eclipse.swt.graphics.Image;
51 import org.eclipse.swt.layout.GridData;
52 import org.eclipse.swt.layout.GridLayout;
53 import org.eclipse.swt.widgets.Button;
54 import org.eclipse.swt.widgets.Combo;
55 import org.eclipse.swt.widgets.Composite;
56 import org.eclipse.swt.widgets.Control;
57 import org.eclipse.swt.widgets.Label;
58 import org.eclipse.swt.widgets.Shell;
59 import org.eclipse.ui.PlatformUI;
60 import org.eclipse.ui.dialogs.SelectionDialog;
61 import org.eclipse.ui.progress.UIJob;
62 import org.eclipse.ui.progress.WorkbenchJob;
63
64 /**
65  * Dialog allowing user to add a memory rendering
66  */

67 public class AddMemoryRenderingDialog extends SelectionDialog {
68     
69     private IMemoryBlock[] fMemoryBlocks;
70     private Combo memoryBlock;
71     private ListViewer fViewer;
72     private IMemoryBlock fSelectedMemoryBlock;
73     private Button addNew;
74     
75     private ISelectionChangedListener fSelectionChangedListener;
76     private SelectionListener fSelectionListener;
77     private SelectionAdapter fAddNewSelectionAdapter;
78     private IMemoryRenderingSite fSite;
79     
80     private IMemoryBlockListener fMemoryBlockListener = new IMemoryBlockListener(){
81         
82         public void memoryBlocksAdded(final IMemoryBlock[] memory)
83         {
84             if (memory.length > 0)
85             {
86                 IMemoryBlock currentBlock = getMemoryBlockToSelect(memory[0]);
87                 if (currentBlock == null)
88                 {
89                     addNew();
90                 }
91                 else
92                 {
93                     populateDialog(currentBlock);
94                 }
95             }
96         }
97         public void memoryBlocksRemoved(IMemoryBlock[] memory)
98         {
99         }
100     };
101     
102     private IMemoryRenderingBindingsListener fBindingListener = new IMemoryRenderingBindingsListener()
103     {
104         public void memoryRenderingBindingsChanged() {
105             UIJob job = new UIJob("refresh"){ //$NON-NLS-1$
106

107                 public IStatus runInUIThread(IProgressMonitor monitor) {
108                     fViewer.refresh();
109                     return Status.OK_STATUS;
110                 }
111             };
112             job.setSystem(true);
113             job.schedule();
114         }
115         
116     };
117
118     class MemoryRenderingLabelProvider implements ILabelProvider
119     {
120         
121         /* (non-Javadoc)
122          * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
123          */

124         public Image getImage(Object JavaDoc element) {
125             return null;
126         }
127
128         /* (non-Javadoc)
129          * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
130          */

131         public String JavaDoc getText(Object JavaDoc element) {
132             if (element instanceof IMemoryRenderingType)
133             {
134                 String JavaDoc label = ((IMemoryRenderingType)element).getLabel();
135                 return label;
136             }
137             return element.toString();
138         }
139
140         /* (non-Javadoc)
141          * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
142          */

143         public void addListener(ILabelProviderListener listener) {
144         }
145
146         /* (non-Javadoc)
147          * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
148          */

149         public void dispose() {
150         }
151
152         /* (non-Javadoc)
153          * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
154          */

155         public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
156             return false;
157         }
158
159         /* (non-Javadoc)
160          * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
161          */

162         public void removeListener(ILabelProviderListener listener) {
163         }
164
165     }
166     
167     class MemoryRenderingContentProvider implements IStructuredContentProvider
168     {
169
170         /* (non-Javadoc)
171          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
172          */

173         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
174             IMemoryRenderingType[] renderings = DebugUITools.getMemoryRenderingManager().getRenderingTypes((IMemoryBlock)inputElement);
175             return renderings;
176         }
177
178         /* (non-Javadoc)
179          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
180          */

181         public void dispose() {
182         }
183
184         /* (non-Javadoc)
185          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
186          */

187         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
188         }
189         
190     }
191
192     
193     /* (non-Javadoc)
194      * @see org.eclipse.jface.window.Window#close()
195      */

196     public boolean close() {
197         
198         fViewer.removeSelectionChangedListener(fSelectionChangedListener);
199         memoryBlock.removeSelectionListener(fSelectionListener);
200         addNew.removeSelectionListener(fAddNewSelectionAdapter);
201         DebugPlugin.getDefault().getMemoryBlockManager().removeListener(fMemoryBlockListener);
202         DebugUITools.getMemoryRenderingManager().removeListener(fBindingListener);
203         
204         return super.close();
205     }
206
207     /* (non-Javadoc)
208      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
209      */

210     protected void createButtonsForButtonBar(Composite parent) {
211         super.createButtonsForButtonBar(parent);
212         getButton(IDialogConstants.OK_ID).setEnabled(false);
213     }
214
215     /* (non-Javadoc)
216      * @see org.eclipse.ui.dialogs.SelectionDialog#getResult()
217      */

218     public Object JavaDoc[] getResult() {
219         
220         Object JavaDoc[] results = super.getResult();
221         
222         if (results != null)
223         {
224             Object JavaDoc[] renderings = ((IStructuredSelection)results[0]).toArray();
225             return renderings;
226         }
227         return new Object JavaDoc[0];
228     }
229
230     /* (non-Javadoc)
231      * @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
232      */

233     protected void cancelPressed() {
234         
235         setResult(null);
236         
237         super.cancelPressed();
238     }
239
240     /* (non-Javadoc)
241      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
242      */

243     protected void okPressed() {
244         
245         ISelection select = fViewer.getSelection();
246         setSelectionResult(new Object JavaDoc[]{select});
247         
248         super.okPressed();
249     }
250
251     /* (non-Javadoc)
252      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
253      */

254     protected Control createDialogArea(Composite parent) {
255
256         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, DebugUIPlugin.getUniqueIdentifier() + ".AddMemoryRenderingDialog_context"); //$NON-NLS-1$
257
Composite composite = new Composite(parent, SWT.NONE);
258         GridLayout compositeLayout = new GridLayout();
259         compositeLayout.numColumns = 3;
260         compositeLayout.makeColumnsEqualWidth = true;
261         composite.setLayout(compositeLayout);
262         
263         GridData comositeSpec= new GridData();
264         comositeSpec.grabExcessVerticalSpace= true;
265         comositeSpec.grabExcessHorizontalSpace= true;
266         comositeSpec.horizontalAlignment= GridData.FILL;
267         comositeSpec.verticalAlignment= GridData.CENTER;
268         composite.setLayoutData(comositeSpec);
269         
270         Label textLabel = new Label(composite, SWT.NONE);
271         textLabel.setText(DebugUIMessages.AddMemoryRenderingDialog_Memory_Monitor);
272         GridData textLayout = new GridData();
273         textLayout.verticalAlignment=GridData.CENTER;
274         textLayout.horizontalAlignment=GridData.BEGINNING;
275         textLabel.setLayoutData(textLayout);
276         
277         memoryBlock = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
278         GridData spec= new GridData(GridData.FILL_HORIZONTAL);
279         spec.grabExcessVerticalSpace= false;
280         spec.grabExcessHorizontalSpace= false;
281         spec.horizontalAlignment= GridData.FILL;
282         spec.verticalAlignment= GridData.FILL;
283         spec.horizontalSpan = 4;
284         memoryBlock.setLayoutData(spec);
285         
286         Label filler = new Label(composite, SWT.NONE);
287         filler.setText(" "); //$NON-NLS-1$
288
GridData fillerData = new GridData(GridData.FILL_HORIZONTAL);
289         fillerData.horizontalSpan = 2;
290         filler.setLayoutData(fillerData);
291         
292         addNew = new Button(composite, SWT.NONE);
293         addNew.setText(DebugUIMessages.AddMemoryRenderingDialog_Add_New);
294         GridData specButton= new GridData();
295         specButton.horizontalAlignment= GridData.END;
296         specButton.verticalAlignment= GridData.CENTER;
297         addNew.setLayoutData(specButton);
298         
299         fAddNewSelectionAdapter = new SelectionAdapter() {
300
301             public void widgetSelected(SelectionEvent e) {
302                 RetargetAddMemoryBlockAction action = new RetargetAddMemoryBlockAction(fSite, false);
303                 action.run();
304                 action.dispose();
305             }};
306         
307         addNew.addSelectionListener(fAddNewSelectionAdapter);
308         
309         fSelectionListener = new SelectionListener(){
310
311             public void widgetSelected(SelectionEvent e) {
312                 
313                 int idx = memoryBlock.getSelectionIndex();
314                 
315                 // avoid null pointer exception
316
if (fMemoryBlocks == null)
317                     return;
318                 
319                 fSelectedMemoryBlock = fMemoryBlocks[idx];
320                 
321                 fViewer.setInput(fSelectedMemoryBlock);
322                 
323             }
324
325             public void widgetDefaultSelected(SelectionEvent e) {
326             }};
327         
328         memoryBlock.addSelectionListener(fSelectionListener);
329         
330         Label renderingLabel = new Label(composite, SWT.NONE);
331         renderingLabel.setText(DebugUIMessages.AddMemoryRenderingDialog_Memory_renderings);
332         GridData renderingLayout = new GridData();
333         renderingLayout.horizontalAlignment = GridData.BEGINNING;
334         renderingLayout.verticalAlignment = GridData.CENTER;
335         renderingLayout.horizontalSpan = 3;
336         renderingLabel.setLayoutData(renderingLayout);
337         
338         fViewer = new ListViewer(composite);
339         fViewer.setContentProvider(new MemoryRenderingContentProvider());
340         fViewer.setLabelProvider(new MemoryRenderingLabelProvider());
341         
342         GridData listLayout = new GridData(GridData.FILL_BOTH);
343         listLayout.horizontalSpan = 3;
344         listLayout.heightHint =140;
345         fViewer.getControl().setLayoutData(listLayout);
346         
347         fViewer.addDoubleClickListener(new IDoubleClickListener (){
348
349             public void doubleClick(DoubleClickEvent event) {
350                 okPressed();
351             }});
352         
353         IMemoryBlock currentBlock = getMemoryBlockToSelect(null);
354         if (currentBlock == null)
355         {
356             addNew();
357         }
358         else
359         {
360             populateDialog(currentBlock);
361         }
362         
363         
364         fSelectionChangedListener = new ISelectionChangedListener() {
365
366             public void selectionChanged(SelectionChangedEvent event) {
367                 ISelection selection = fViewer.getSelection();
368                 
369                 if (selection.isEmpty())
370                 {
371                     getButton(IDialogConstants.OK_ID).setEnabled(false);
372                 }
373                 else
374                 {
375                     getButton(IDialogConstants.OK_ID).setEnabled(true);
376                 }
377             }};
378         
379         fViewer.addSelectionChangedListener(fSelectionChangedListener);
380         
381         DebugPlugin.getDefault().getMemoryBlockManager().addListener(fMemoryBlockListener);
382         DebugUITools.getMemoryRenderingManager().addListener(fBindingListener);
383         
384         return composite;
385     }
386
387     public AddMemoryRenderingDialog(Shell parent, IMemoryRenderingSite site) {
388         super(parent);
389         super.setTitle(DebugUIMessages.AddMemoryRenderingDialog_Add_memory_rendering);
390         setShellStyle(getShellStyle() | SWT.RESIZE);
391         fSite = site;
392     }
393     
394     
395     private void doPopulateDialog(Combo combo, ListViewer viewer, String JavaDoc[] labels, int selectionIdx, IMemoryBlock currentBlock)
396     {
397         // clean up
398
combo.removeAll();
399         
400         for (int i=0; i<labels.length; i++)
401         {
402             combo.add(labels[i]);
403         }
404
405         combo.select(selectionIdx);
406         fSelectedMemoryBlock = currentBlock;
407         
408         viewer.setInput(currentBlock);
409     }
410     
411     private IMemoryBlock getMemoryBlockToSelect(IMemoryBlock lastAdded)
412     {
413         IMemoryBlock currentBlock = null;
414         
415         if (lastAdded != null)
416             currentBlock = lastAdded;
417         else
418         {
419             // take Memory View's selection if possible
420
ISelectionProvider selectionProvider = fSite.getSite().getSelectionProvider();
421             ISelection selection = null;
422             
423             if (selectionProvider != null)
424                 selection = selectionProvider.getSelection();
425             else // otherwise, take selection from selection service
426
selection = DebugUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(IDebugUIConstants.ID_MEMORY_VIEW);
427             
428             IDebugElement element = getMemoryBlock(selection);
429             
430             if (!(element instanceof IMemoryBlock))
431             {
432                 
433                 IAdaptable context = DebugUITools.getDebugContext();
434                 
435                 if (context != null)
436                 {
437                     IDebugElement debugElement = (IDebugElement)context.getAdapter(IDebugElement.class);
438                     
439                     if (debugElement != null)
440                     {
441                         IMemoryBlock[] blocks = MemoryViewUtil.getMemoryBlockManager().getMemoryBlocks(debugElement.getDebugTarget());
442                         
443                         if (blocks.length > 0)
444                             currentBlock = blocks[0];
445                     }
446                 }
447             }
448             else
449             {
450                 currentBlock = (IMemoryBlock)element;
451             }
452         }
453         return currentBlock;
454     }
455     
456     private String JavaDoc[] getLabels(IMemoryBlock[] memoryBlocks)
457     {
458         String JavaDoc[] labels = new String JavaDoc[memoryBlocks.length];
459         for (int i=0; i<memoryBlocks.length; i++)
460         {
461             String JavaDoc text = ""; //$NON-NLS-1$
462
if (memoryBlocks[i] instanceof IMemoryBlockExtension)
463             {
464                 try {
465                     text = ((IMemoryBlockExtension)memoryBlocks[i]).getExpression();
466                     
467                     if (text == null)
468                         text = DebugUIMessages.AddMemoryRenderingDialog_Unknown;
469                     
470                     if (((IMemoryBlockExtension)memoryBlocks[i]).getBigBaseAddress() != null)
471                     {
472                         text += " : 0x"; //$NON-NLS-1$
473
text += ((IMemoryBlockExtension)memoryBlocks[i]).getBigBaseAddress().toString(16);
474                     }
475                 } catch (DebugException e) {
476                     long address = memoryBlocks[i].getStartAddress();
477                     text = Long.toHexString(address);
478                 }
479             }
480             else
481             {
482                 long address = memoryBlocks[i].getStartAddress();
483                 text = Long.toHexString(address);
484             }
485             
486             // ask decorator to decorate to ensure consistent label
487
ILabelDecorator decorator = (ILabelDecorator)fMemoryBlocks[i].getAdapter(ILabelDecorator.class);
488             if (decorator != null)
489                 text = decorator.decorateText(text, fMemoryBlocks[i]);
490             
491             labels[i] = text;
492         }
493         return labels;
494     }
495     
496     private IMemoryBlock getMemoryBlock(ISelection selection)
497     {
498         if (!(selection instanceof IStructuredSelection))
499             return null;
500
501         //only single selection of PICLDebugElements is allowed for this action
502
if (selection.isEmpty() || ((IStructuredSelection)selection).size() > 1)
503         {
504             return null;
505         }
506
507         Object JavaDoc elem = ((IStructuredSelection)selection).getFirstElement();
508         
509         if (elem instanceof IMemoryBlock)
510             return (IMemoryBlock)elem;
511         else if (elem instanceof IMemoryRendering)
512             return ((IMemoryRendering)elem).getMemoryBlock();
513         else
514             return null;
515     }
516     
517     public IMemoryBlock getMemoryBlock()
518     {
519         return fSelectedMemoryBlock;
520     }
521     
522     /**
523      * @param currentBlock
524      */

525     private void populateDialog(IMemoryBlock currentBlock) {
526         final IMemoryBlock selectMB = currentBlock;
527         Job job = new Job("Populate dialog") //$NON-NLS-1$
528
{
529             protected IStatus run(IProgressMonitor monitor) {
530                 fMemoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(selectMB.getDebugTarget());
531                 int selectionIdx = 0;
532                 for (int i=0; i<fMemoryBlocks.length; i++)
533                 {
534                     if (fMemoryBlocks[i] == selectMB)
535                     {
536                         selectionIdx = i;
537                         break;
538                     }
539                 }
540                 
541                 final String JavaDoc[] labels = getLabels(fMemoryBlocks);
542                 final int idx = selectionIdx;
543                 final IMemoryBlock selectedBlk = selectMB;
544                 WorkbenchJob wbJob = new WorkbenchJob("populate dialog"){ //$NON-NLS-1$
545

546                     public IStatus runInUIThread(IProgressMonitor wbMonitor) {
547                         doPopulateDialog(memoryBlock, fViewer, labels, idx, selectedBlk);
548                         return Status.OK_STATUS;
549                     }};
550                     wbJob.setSystem(true);
551                     wbJob.schedule();
552                 return Status.OK_STATUS;
553             }};
554         job.setSystem(true);
555         job.schedule();
556     }
557
558     /**
559      *
560      */

561     private void addNew() {
562         WorkbenchJob job = new WorkbenchJob("populate dialog"){ //$NON-NLS-1$
563

564             public IStatus runInUIThread(IProgressMonitor monitor) {
565                 memoryBlock.add(DebugUIMessages.AddMemoryRenderingDialog_Add_New);
566                 memoryBlock.select(0);
567                 return Status.OK_STATUS;
568             }};
569         job.setSystem(true);
570         job.schedule();
571     }
572 }
573
Popular Tags