KickJava   Java API By Example, From Geeks To Geeks.

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


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.CoreException;
14 import org.eclipse.debug.core.model.IDebugElement;
15 import org.eclipse.debug.core.model.IDebugTarget;
16 import org.eclipse.debug.core.model.IMemoryBlock;
17 import org.eclipse.debug.core.model.IMemoryBlockRetrieval;
18 import org.eclipse.debug.internal.ui.DebugUIMessages;
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.debug.ui.memory.IMemoryRendering;
21 import org.eclipse.debug.ui.memory.IMemoryRenderingContainer;
22 import org.eclipse.debug.ui.memory.IMemoryRenderingType;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.PlatformUI;
25
26 /**
27  * Toolbar "Add Memory Rendering Action" from Memory Rendering Pane
28  */

29 public class AddMemoryRenderingAction extends AddMemoryBlockAction {
30     
31     private IMemoryRenderingContainer fContainer;
32     
33     public AddMemoryRenderingAction(IMemoryRenderingContainer container)
34     {
35         super(DebugUIMessages.AddMemoryRenderingAction_Add_renderings, AS_PUSH_BUTTON, container.getMemoryRenderingSite());
36         setToolTipText(DebugUIMessages.AddMemoryRenderingAction_Add_renderings);
37         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, DebugUIPlugin.getUniqueIdentifier() + ".AddRenderingContextAction_context"); //$NON-NLS-1$
38
fContainer = container;
39     }
40     
41     /* (non-Javadoc)
42      * @see org.eclipse.jface.action.IAction#run()
43      */

44     public void run() {
45         
46         // pop up dialog and ask what renderings to create
47
Shell shell = DebugUIPlugin.getShell();
48         
49         IDebugElement elem = getDebugElement(fCurrentContext);
50         
51         if (elem != null)
52         {
53             AddMemoryRenderingDialog dialog = new AddMemoryRenderingDialog(shell, fSite);
54             dialog.open();
55             
56             // get a list of renderings to create
57
Object JavaDoc[] renderings = dialog.getResult();
58             
59             IMemoryBlock blk = dialog.getMemoryBlock();
60             
61             if (blk == null)
62                 return;
63             
64             // ask for debug target and memory block retrieval
65
IDebugTarget debugTarget = elem.getDebugTarget();
66             IMemoryBlockRetrieval standardMemRetrieval = (IMemoryBlockRetrieval)elem.getAdapter(IMemoryBlockRetrieval.class);
67             
68             if (standardMemRetrieval == null)
69             {
70                 standardMemRetrieval = debugTarget;
71             }
72             
73             if (standardMemRetrieval == null)
74                 return;
75             
76             // add memory renderings to Memory Rendering Manager
77
for (int i=0; i<renderings.length; i++)
78             {
79                 if (renderings[i] instanceof IMemoryRenderingType)
80                 {
81                     try {
82                         IMemoryRendering rendering = ((IMemoryRenderingType)renderings[i]).createRendering();
83                         if (rendering != null)
84                         {
85                             rendering.init(fContainer, blk);
86                             fContainer.addMemoryRendering(rendering);
87                         }
88                     } catch (CoreException e) {
89                         MemoryViewUtil.openError(DebugUIMessages.AddMemoryRenderingAction_Add_rendering_failed, DebugUIMessages.AddMemoryRenderingAction_Unable_to_add_selected_renderings, e);
90                     }
91                 }
92             }
93         }
94     }
95     
96     private IDebugElement getDebugElement(Object JavaDoc elem)
97     {
98         // if not debug element
99
if (!(elem instanceof IDebugElement))
100             return null;
101         return (IDebugElement)elem;
102     }
103 }
104
Popular Tags