KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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
12 package org.eclipse.debug.internal.ui.views.memory.renderings;
13
14 import java.math.BigInteger JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.core.DebugPlugin;
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.IMemoryBlockExtension;
23 import org.eclipse.debug.core.model.IMemoryBlockRetrievalExtension;
24 import org.eclipse.debug.internal.ui.DebugUIMessages;
25 import org.eclipse.debug.internal.ui.DebugUIPlugin;
26 import org.eclipse.debug.internal.ui.views.memory.MemoryViewUtil;
27 import org.eclipse.debug.ui.DebugUITools;
28 import org.eclipse.debug.ui.IDebugUIConstants;
29 import org.eclipse.debug.ui.memory.AbstractMemoryRendering;
30 import org.eclipse.debug.ui.memory.IMemoryRendering;
31 import org.eclipse.debug.ui.memory.IMemoryRenderingType;
32 import org.eclipse.debug.ui.memory.IRepositionableMemoryRendering;
33 import org.eclipse.jface.action.Action;
34 import org.eclipse.jface.window.Window;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.ui.PlatformUI;
37
38
39 /**
40  * Go To Address Action for table rendering
41  *
42  * @since 3.0
43  */

44 public class GoToAddressAction extends Action
45 {
46     private IRepositionableMemoryRendering fRendering;
47     
48     public GoToAddressAction(IRepositionableMemoryRendering rendering)
49     {
50         super(DebugUIMessages.GoToAddressAction_title);
51         setToolTipText(DebugUIMessages.GoToAddressAction_title);
52         
53         fRendering = rendering;
54         
55         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugUIConstants.PLUGIN_ID + ".GoToAddressAction_context"); //$NON-NLS-1$
56
}
57     /* (non-Javadoc)
58      * @see org.eclipse.jface.action.IAction#run()
59      */

60     public void run()
61     {
62         try
63         {
64             Shell shell= DebugUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell();
65         
66             // create dialog to ask for expression/address to block
67
GoToAddressDialog dialog = new GoToAddressDialog(shell);
68             dialog.open();
69         
70             int returnCode = dialog.getReturnCode();
71         
72             if (returnCode == Window.CANCEL)
73             {
74                 return;
75             }
76         
77             // get expression from dialog
78
String JavaDoc expression = dialog.getExpression();
79             
80             expression = parseExpression(expression);
81             
82             doGoToAddress(expression);
83         }
84         // open error in case of any error
85
catch (DebugException e)
86         {
87             MemoryViewUtil.openError(DebugUIMessages.GoToAddressAction_Go_to_address_failed,
88                 DebugUIMessages.GoToAddressAction_Go_to_address_failed, e);
89         }
90         catch (NumberFormatException JavaDoc e1)
91         {
92             MemoryViewUtil.openError(DebugUIMessages.GoToAddressAction_Go_to_address_failed,
93                 DebugUIMessages.GoToAddressAction_Address_is_invalid, null);
94         }
95     }
96     /**
97      * @param expression
98      * @return
99      */

100     public String JavaDoc parseExpression(String JavaDoc expression) {
101         expression = expression.toUpperCase();
102         expression = expression.trim();
103         
104         if (expression.startsWith("0X")) //$NON-NLS-1$
105
{
106             expression = expression.substring(2);
107         }
108         return expression;
109     }
110     /**
111      * @param expression
112      * @throws DebugException
113      */

114     public void doGoToAddress(String JavaDoc expression) throws DebugException, NumberFormatException JavaDoc {
115         // convert expression to address
116
BigInteger JavaDoc address = new BigInteger JavaDoc(expression, 16);
117         
118         // look at this address and figure out if a new memory block should
119
// be opened.
120
IMemoryBlock mb = fRendering.getMemoryBlock();
121         if (mb instanceof IMemoryBlockExtension)
122         {
123             IMemoryBlockExtension mbExt = (IMemoryBlockExtension)mb;
124             BigInteger JavaDoc mbStart = mbExt.getMemoryBlockStartAddress();
125             BigInteger JavaDoc mbEnd = mbExt.getMemoryBlockEndAddress();
126             
127             if (mbStart != null)
128             {
129                 // if trying to go beyond the start address
130
// of the memory block
131
if (address.compareTo(mbStart) < 0)
132                 {
133                     IMemoryBlockRetrievalExtension retrieval = (IMemoryBlockRetrievalExtension)mbExt.getAdapter(IMemoryBlockRetrievalExtension.class);
134                     IDebugTarget dt = mbExt.getDebugTarget();
135                     
136                     if (retrieval == null && dt instanceof IMemoryBlockRetrievalExtension)
137                         retrieval = (IMemoryBlockRetrievalExtension)dt;
138                     
139                     // add a new memory block and then the same rendering as fRendering
140
// in the same container.
141
if (retrieval != null)
142                     {
143                         addNewMemoryBlock(expression, retrieval);
144                             return;
145                     }
146                 }
147             }
148             if (mbEnd != null)
149             {
150                 // if trying to go beyond the end address
151
// of the memory block
152
if (address.compareTo(mbEnd) > 0)
153                 {
154                     IMemoryBlockRetrievalExtension retrieval = (IMemoryBlockRetrievalExtension)mbExt.getAdapter(IMemoryBlockRetrievalExtension.class);
155                     IDebugTarget dt = mbExt.getDebugTarget();
156                     
157                     if (retrieval == null && dt instanceof IMemoryBlockRetrievalExtension)
158                         retrieval = (IMemoryBlockRetrievalExtension)dt;
159                     
160                     // add a new memory block and then the same rendering as fRendering
161
// in the same container.
162
if (retrieval != null)
163                     {
164                         addNewMemoryBlock(expression, retrieval);
165                             return;
166                     }
167                 }
168             }
169         }
170         
171         // go to specified address
172
fRendering.goToAddress(address);
173     }
174     
175     private void addNewMemoryBlock(String JavaDoc expression, IMemoryBlockRetrievalExtension retrieval)
176     {
177         Object JavaDoc elem = DebugUITools.getDebugContext();
178         
179         if (!(elem instanceof IDebugElement))
180             return;
181          
182         try {
183             if (retrieval != null)
184             {
185                 IMemoryBlockExtension mbext = retrieval.getExtendedMemoryBlock(expression, elem);
186                 if (mbext != null)
187                 {
188                     IMemoryBlock[] memArray = new IMemoryBlock[]{mbext};
189                     DebugPlugin.getDefault().getMemoryBlockManager().addMemoryBlocks(memArray);
190                 }
191                 
192                 IMemoryRenderingType renderingType = DebugUITools.getMemoryRenderingManager().getRenderingType(fRendering.getRenderingId());
193                 
194                 if (renderingType != null)
195                 {
196                     IMemoryRendering rendering = renderingType.createRendering();
197                     
198                     if (rendering != null && fRendering instanceof AbstractMemoryRendering)
199                     {
200                         rendering.init(((AbstractMemoryRendering)fRendering).getMemoryRenderingContainer(), mbext);
201                         ((AbstractMemoryRendering)fRendering).getMemoryRenderingContainer().addMemoryRendering(rendering);
202                     }
203                 }
204             }
205         } catch (DebugException e) {
206             MemoryViewUtil.openError(DebugUIMessages.GoToAddressAction_Go_to_address_failed,
207             DebugUIMessages.GoToAddressAction_Go_to_address_failed, e);
208         } catch (CoreException e)
209         {
210             MemoryViewUtil.openError(DebugUIMessages.GoToAddressAction_Go_to_address_failed,
211             DebugUIMessages.GoToAddressAction_Go_to_address_failed, e);
212         }
213     }
214 }
215
Popular Tags