| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2013 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Francois Chouinard - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
| 14 | |
| 15 | import org.eclipse.core.commands.AbstractHandler; |
| 16 | import org.eclipse.core.commands.ExecutionEvent; |
| 17 | import org.eclipse.core.commands.ExecutionException; |
| 18 | import org.eclipse.core.resources.IResource; |
| 19 | import org.eclipse.core.runtime.CoreException; |
| 20 | import org.eclipse.jface.viewers.ISelection; |
| 21 | import org.eclipse.jface.viewers.TreeSelection; |
| 22 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
| 23 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
| 24 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder; |
| 25 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; |
| 26 | import org.eclipse.ui.IWorkbenchPage; |
| 27 | import org.eclipse.ui.IWorkbenchPart; |
| 28 | import org.eclipse.ui.IWorkbenchWindow; |
| 29 | import org.eclipse.ui.PlatformUI; |
| 30 | |
| 31 | /** |
| 32 | * <b><u>RefreshHandler</u></b> |
| 33 | * <p> |
| 34 | * TODO: Handle multiple selections |
| 35 | */ |
| 36 | public class RefreshHandler extends AbstractHandler { |
| 37 | |
| 38 | // ------------------------------------------------------------------------ |
| 39 | // Execution |
| 40 | // ------------------------------------------------------------------------ |
| 41 | |
| 42 | @Override |
| 43 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 44 | |
| 45 | // Check if we are closing down |
| 46 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 47 | if (window == null) { |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | // Get the selection |
| 52 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
| 53 | IWorkbenchPart part = page.getActivePart(); |
| 54 | if (part == null) { |
| 55 | return false; |
| 56 | } |
| 57 | ISelection selection = part.getSite().getSelectionProvider().getSelection(); |
| 58 | if (selection instanceof TreeSelection) { |
| 59 | TreeSelection treeSelection = (TreeSelection) selection; |
| 60 | Object element = treeSelection.getFirstElement(); |
| 61 | IResource resource = null; |
| 62 | if (element instanceof TmfTraceFolder) { |
| 63 | TmfTraceFolder folder = (TmfTraceFolder) element; |
| 64 | resource = folder.getResource(); |
| 65 | } |
| 66 | else if (element instanceof TmfExperimentFolder) { |
| 67 | TmfExperimentFolder folder = (TmfExperimentFolder) element; |
| 68 | resource = folder.getResource(); |
| 69 | } |
| 70 | else if (element instanceof TmfExperimentElement) { |
| 71 | TmfExperimentElement folder = (TmfExperimentElement) element; |
| 72 | resource = folder.getResource(); |
| 73 | } |
| 74 | try { |
| 75 | if (resource != null) { |
| 76 | resource.refreshLocal(IResource.DEPTH_INFINITE, null); |
| 77 | } |
| 78 | } catch (CoreException e) { |
| 79 | Activator.getDefault().logError("Error refreshing projects", e); //$NON-NLS-1$ |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | } |