KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
12 package org.eclipse.debug.internal.ui.views.memory;
13
14 import org.eclipse.debug.core.model.IMemoryBlock;
15 import org.eclipse.debug.internal.ui.DebugUIPlugin;
16 import org.eclipse.debug.ui.IDebugUIConstants;
17 import org.eclipse.debug.ui.memory.IMemoryRendering;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.ui.IViewActionDelegate;
23 import org.eclipse.ui.IViewPart;
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.PartInitException;
26
27 /**
28  * Action for opening a new memory view.
29  */

30 public class NewMemoryViewAction implements IViewActionDelegate {
31
32     private MemoryView fView;
33     public void init(IViewPart view) {
34         if (view instanceof MemoryView)
35             fView = (MemoryView)view;
36     }
37
38     public void run(IAction action) {
39         
40         String JavaDoc secondaryId = MemoryViewIdRegistry.getUniqueSecondaryId(IDebugUIConstants.ID_MEMORY_VIEW);
41         try {
42             IWorkbenchPage page = DebugUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
43             IViewPart newView = page.showView(IDebugUIConstants.ID_MEMORY_VIEW, secondaryId, IWorkbenchPage.VIEW_ACTIVATE);
44             
45             // set initial selection for new view
46
setInitialSelection(newView);
47             setInitialViewSettings(newView);
48             
49         } catch (PartInitException e) {
50             // if view cannot be opened, open error
51
DebugUIPlugin.log(e);
52         }
53     }
54
55     private void setInitialSelection(IViewPart newView) {
56         ISelection selection = fView.getSite().getSelectionProvider().getSelection();
57         if (selection instanceof IStructuredSelection)
58         {
59             IStructuredSelection strucSel = (IStructuredSelection)selection;
60             
61             // return if current selection is empty
62
if (strucSel.isEmpty())
63                 return;
64             
65             Object JavaDoc obj = strucSel.getFirstElement();
66             
67             if (obj == null)
68                 return;
69             
70             if (obj instanceof IMemoryRendering)
71             {
72                 IMemoryBlock memBlock = ((IMemoryRendering)obj).getMemoryBlock();
73                 strucSel = new StructuredSelection(memBlock);
74                 newView.getSite().getSelectionProvider().setSelection(strucSel);
75             }
76             else if (obj instanceof IMemoryBlock)
77             {
78                 newView.getSite().getSelectionProvider().setSelection(strucSel);
79             }
80         }
81     }
82
83     private void setInitialViewSettings(IViewPart newView) {
84         if (fView != null && newView instanceof MemoryView)
85         {
86             MemoryView newMView = (MemoryView)newView;
87             IMemoryViewPane[] viewPanes = fView.getViewPanes();
88             int orientation = fView.getViewPanesOrientation();
89             for (int i=0; i<viewPanes.length; i++)
90             {
91                 // copy view pane visibility
92
newMView.showViewPane(fView.isViewPaneVisible(viewPanes[i].getId()), viewPanes[i].getId());
93             }
94             
95             // do not want to copy renderings as it could be very expensive
96
// create a blank view and let user creates renderings as needed
97

98             // set orientation of new view
99
newMView.setViewPanesOrientation(orientation);
100         }
101     }
102
103     public void selectionChanged(IAction action, ISelection selection) {
104
105     }
106
107 }
108
Popular Tags