KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.math.BigInteger JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.IMemoryBlockManager;
23 import org.eclipse.debug.core.model.IDebugElement;
24 import org.eclipse.debug.core.model.IDebugTarget;
25 import org.eclipse.debug.core.model.IMemoryBlockRetrieval;
26 import org.eclipse.debug.internal.ui.DebugUIPlugin;
27 import org.eclipse.jface.dialogs.MessageDialog;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.progress.UIJob;
33
34 /**
35  * Util class for Memory View
36  *
37  * @since 3.0
38  */

39 public class MemoryViewUtil {
40     
41     public static final int[] ignoreKeyEvents =
42     {
43         SWT.ARROW_UP,
44         SWT.ARROW_DOWN,
45         SWT.ARROW_LEFT,
46         SWT.ARROW_RIGHT,
47         SWT.PAGE_UP,
48         SWT.PAGE_DOWN,
49         SWT.HOME,
50         SWT.END,
51         SWT.INSERT,
52         SWT.F1,
53         SWT.F2,
54         SWT.F3,
55         SWT.F4,
56         SWT.F5,
57         SWT.F6,
58         SWT.F7,
59         SWT.F8,
60         SWT.F9,
61         SWT.F10,
62         SWT.F11,
63         SWT.F12,
64         SWT.F13,
65         SWT.F14,
66         SWT.F15,
67         SWT.HELP,
68         SWT.CAPS_LOCK,
69         SWT.NUM_LOCK,
70         SWT.SCROLL_LOCK,
71         SWT.PAUSE,
72         SWT.BREAK,
73         SWT.PRINT_SCREEN,
74         SWT.ESC,
75         SWT.CTRL,
76         SWT.ALT,
77         SWT.SHIFT
78     };
79     
80     public static ArrayList JavaDoc MEMORY_BLOCKS_HISTORY = new ArrayList JavaDoc();
81     
82     /**
83      * @param selection
84      * @return true if the given selection is valid for creating a memory block
85      */

86     static public boolean isValidSelection(ISelection selection) {
87     
88         if (!(selection instanceof IStructuredSelection))
89             return false;
90     
91         //only single selection is allowed for this action
92
if (selection.isEmpty() || ((IStructuredSelection)selection).size() > 1)
93         {
94             return false;
95         }
96     
97         Object JavaDoc elem = ((IStructuredSelection)selection).getFirstElement();
98     
99         return isValidContext(elem);
100     }
101
102
103     /**
104      * @param elem
105      * @return
106      */

107     public static boolean isValidContext(Object JavaDoc elem) {
108         // if not debug element
109
if (!(elem instanceof IDebugElement))
110             return false;
111     
112         IDebugTarget debugTarget = ((IDebugElement)elem).getDebugTarget();
113         IMemoryBlockRetrieval memRetrieval =(IMemoryBlockRetrieval) ((IDebugElement)elem).getAdapter(IMemoryBlockRetrieval.class);
114         
115         if (memRetrieval == null)
116         {
117             // if debug element returns null from getAdapter, assume its debug target is going to retrieve memory blocks
118
memRetrieval = debugTarget;
119         }
120         
121         if (debugTarget == null)
122             return false;
123         
124         // not valid if the debug target is already terminated
125
if (debugTarget.isTerminated() || debugTarget.isDisconnected())
126             return false;
127         
128         if (memRetrieval.supportsStorageRetrieval()) {
129             return true;
130         }
131         
132         return false;
133     }
134
135     
136     /**
137      * Helper function to open an error dialog.
138      * @param title
139      * @param message
140      * @param e
141      */

142     static public void openError (final String JavaDoc title, final String JavaDoc message, final Exception JavaDoc e)
143     {
144         UIJob uiJob = new UIJob("open error"){ //$NON-NLS-1$
145

146             public IStatus runInUIThread(IProgressMonitor monitor) {
147 // open error for the exception
148
String JavaDoc detail = ""; //$NON-NLS-1$
149
if (e != null)
150                     detail = e.getMessage();
151                 
152                 Shell shell = DebugUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell();
153                 
154                 MessageDialog.openError(
155                     shell,
156                     title,
157                     message + "\n" + detail); //$NON-NLS-1$
158
return Status.OK_STATUS;
159             }};
160         uiJob.setSystem(true);
161         uiJob.schedule();
162     }
163     
164     static IMemoryBlockManager getMemoryBlockManager()
165     {
166         return DebugPlugin.getDefault().getMemoryBlockManager();
167     }
168     
169     static public boolean isLinuxGTK()
170     {
171         String JavaDoc ws = Platform.getWS();
172         return ws.equals(Platform.WS_GTK);
173     }
174     
175     /**
176      * Checks to see if the event is valid for activating
177      * cell editing in a view tab
178      * @param event
179      * @return true if the edit event is valid for activating the cell editor
180      */

181     public static boolean isValidEditEvent(int event) {
182         for (int i = 0; i < MemoryViewUtil.ignoreKeyEvents.length; i++) {
183             if (event == MemoryViewUtil.ignoreKeyEvents[i])
184                 return false;
185         }
186         return true;
187     }
188     
189     public static BigInteger JavaDoc alignToBoundary(BigInteger JavaDoc integer, int numberOfUnitsPerLine)
190     {
191         BigInteger JavaDoc[] result = integer.divideAndRemainder(BigInteger.valueOf(numberOfUnitsPerLine));
192         integer = integer.subtract(result[1]);
193         return integer;
194     }
195     
196     public static void addHistory(String JavaDoc expression)
197     {
198         if (!MEMORY_BLOCKS_HISTORY.contains(expression))
199             MEMORY_BLOCKS_HISTORY.add(0, expression);
200         
201         if (MEMORY_BLOCKS_HISTORY.size() > 5)
202             MEMORY_BLOCKS_HISTORY.remove(MEMORY_BLOCKS_HISTORY.size()-1);
203     }
204     
205     public static String JavaDoc[] getHistory()
206     {
207         return (String JavaDoc[])MEMORY_BLOCKS_HISTORY.toArray(new String JavaDoc[MEMORY_BLOCKS_HISTORY.size()]);
208     }
209     
210     public static IMemoryBlockRetrieval getMemoryBlockRetrieval(Object JavaDoc object)
211     {
212         IMemoryBlockRetrieval retrieval = null;
213
214         if (object instanceof IAdaptable)
215         {
216             IAdaptable adaptable = (IAdaptable)object;
217             retrieval = (IMemoryBlockRetrieval)adaptable.getAdapter(IMemoryBlockRetrieval.class);
218         }
219         
220         if (retrieval == null && object instanceof IDebugElement)
221         {
222             IDebugElement de = (IDebugElement)object;
223             retrieval = de.getDebugTarget();
224         }
225         
226         return retrieval;
227     }
228 }
229
Popular Tags