sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.button/src/com/nokia/carbide/cpp/pi/button/BupMapSwitchAction.java
changeset 2 b9ab3b238396
equal deleted inserted replaced
1:1050670c6980 2:b9ab3b238396
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: 
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.carbide.cpp.pi.button;
       
    19 
       
    20 import java.util.Iterator;
       
    21 
       
    22 import org.eclipse.core.resources.IFile;
       
    23 import org.eclipse.core.resources.IResource;
       
    24 import org.eclipse.core.runtime.IAdaptable;
       
    25 import org.eclipse.jface.action.IAction;
       
    26 import org.eclipse.jface.viewers.ISelection;
       
    27 import org.eclipse.jface.viewers.IStructuredSelection;
       
    28 import org.eclipse.ui.IEditorPart;
       
    29 import org.eclipse.ui.IObjectActionDelegate;
       
    30 import org.eclipse.ui.IWorkbenchPart;
       
    31 import org.eclipse.ui.PartInitException;
       
    32 import org.eclipse.ui.PlatformUI;
       
    33 import org.eclipse.ui.ide.IDE;
       
    34 import org.eclipse.ui.ide.ResourceUtil;
       
    35 
       
    36 import com.nokia.carbide.cpp.pi.editors.PIPageEditor;
       
    37 import com.nokia.carbide.cpp.pi.util.GeneralMessages;
       
    38 
       
    39 
       
    40 public class BupMapSwitchAction implements IObjectActionDelegate {
       
    41 	
       
    42 	// IDs defined in plugin.xml
       
    43 	public static final String BUP_MAP_SWITCH_POP_UP_ID = ButtonPlugin.PLUGIN_ID + ".BupMapSwitchAction"; //$NON-NLS-1$
       
    44 	
       
    45 	// private members
       
    46 	private ISelection selection;
       
    47 	
       
    48 	/* (non-Javadoc)
       
    49 	 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
       
    50 	 */
       
    51 	public void setActivePart(IAction arg0, IWorkbenchPart arg1) {
       
    52 	}
       
    53 
       
    54 	/* (non-Javadoc)
       
    55 	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
       
    56 	 */
       
    57 	public void run(IAction arg0) {
       
    58 		if (arg0.getId().equals(BUP_MAP_SWITCH_POP_UP_ID)){
       
    59 			handleBupMapSwitch();
       
    60 		}
       
    61 	}
       
    62 		
       
    63 	private void handleBupMapSwitch() {
       
    64 		if (selection != null && selection instanceof IStructuredSelection) {
       
    65 			Iterator iter = ((IStructuredSelection)selection).iterator();
       
    66 			while (iter.hasNext()) {
       
    67 				Object selectedItem = iter.next();
       
    68 				IResource selectedResource = getResourceFromObject(selectedItem);
       
    69 				if (selectedResource != null && selectedResource instanceof IFile) {
       
    70 					IEditorPart selectedEditorPart = ResourceUtil.findEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), (IFile) selectedResource);
       
    71 					if (selectedEditorPart != null) {
       
    72 						if (selectedEditorPart instanceof PIPageEditor) {
       
    73 							// found an opened editor instance
       
    74 							PIPageEditor selectedPIPageEditor = (PIPageEditor) selectedEditorPart;
       
    75 							PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().activate(selectedPIPageEditor);
       
    76 							ButtonPlugin.getDefault().switchMap();
       
    77 						} else {
       
    78 							GeneralMessages.showErrorMessage(Messages.getString("BupMapSwitchAction.notPIPageeditor")); //$NON-NLS-1$
       
    79 						}
       
    80 					} else {
       
    81 						// NPI file is not opened
       
    82 						try {
       
    83 							IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), (IFile) selectedResource);
       
    84 							// found an opened editor instance
       
    85 							PIPageEditor selectedPIPageEditor = (PIPageEditor) selectedEditorPart;
       
    86 							PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().activate(selectedPIPageEditor);
       
    87 							ButtonPlugin.getDefault().switchMap();
       
    88 						} catch (PartInitException e) {
       
    89 						}
       
    90 					}
       
    91 				}
       
    92 			}
       
    93 		}
       
    94 
       
    95 	}
       
    96 	
       
    97 	/**
       
    98 	 * Return the resource associated with an object.
       
    99 	 * @param element - object in question
       
   100 	 * @return associated resource if there is one
       
   101 	 */
       
   102 	private IResource getResourceFromObject(Object element) {
       
   103 		if (element instanceof IResource) {
       
   104 			return (IResource)element;
       
   105 		} else if (element instanceof IAdaptable) {
       
   106 			return (IResource)((IAdaptable)element).getAdapter(IResource.class);
       
   107 		}
       
   108 		return null;
       
   109 	}
       
   110 
       
   111 	/* (non-Javadoc)
       
   112 	 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
       
   113 	 */
       
   114 	public void selectionChanged(IAction arg0, ISelection arg1) {
       
   115 		this.selection = arg1;
       
   116 	}
       
   117 
       
   118 }