| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | package org.eclipse.linuxtools.internal.tmf.ui.project.model; |
| 13 | |
| 14 | import org.eclipse.core.resources.IFile; |
| 15 | import org.eclipse.core.runtime.CoreException; |
| 16 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 17 | import org.eclipse.jface.viewers.StructuredSelection; |
| 18 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
| 19 | import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment; |
| 20 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; |
| 21 | import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement; |
| 22 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
| 23 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfNavigatorContentProvider; |
| 24 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement; |
| 25 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry; |
| 26 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
| 27 | import org.eclipse.ui.IEditorInput; |
| 28 | import org.eclipse.ui.IEditorPart; |
| 29 | import org.eclipse.ui.IEditorReference; |
| 30 | import org.eclipse.ui.IWorkbenchPage; |
| 31 | import org.eclipse.ui.PartInitException; |
| 32 | import org.eclipse.ui.ide.ResourceUtil; |
| 33 | import org.eclipse.ui.navigator.ILinkHelper; |
| 34 | import org.eclipse.ui.part.FileEditorInput; |
| 35 | |
| 36 | /** |
| 37 | * Implementation of ILinkHelper interface for linking with editor extension for traces and |
| 38 | * experiments. |
| 39 | * |
| 40 | * @author Bernd Hufmann |
| 41 | */ |
| 42 | public class TmfEditorLinkHelper implements ILinkHelper { |
| 43 | |
| 44 | /* |
| 45 | * (non-Javadoc) |
| 46 | * @see org.eclipse.ui.navigator.ILinkHelper#findSelection(org.eclipse.ui.IEditorInput) |
| 47 | */ |
| 48 | @Override |
| 49 | public IStructuredSelection findSelection(IEditorInput anInput) { |
| 50 | IFile file = ResourceUtil.getFile(anInput); |
| 51 | if (file != null) { |
| 52 | |
| 53 | try { |
| 54 | // Get the trace type ID |
| 55 | String traceTypeId = file.getPersistentProperty(TmfCommonConstants.TRACETYPE); |
| 56 | if (traceTypeId == null) { |
| 57 | return StructuredSelection.EMPTY; |
| 58 | } |
| 59 | |
| 60 | final TmfNavigatorContentProvider ncp = new TmfNavigatorContentProvider(); |
| 61 | ncp.getChildren(file.getProject()); // force the model to be populated |
| 62 | final TmfProjectElement project = TmfProjectRegistry.getProject(file.getProject()); |
| 63 | |
| 64 | // Check for experiments, traces which are folders or traces which are files |
| 65 | if (traceTypeId.equals(TmfExperiment.class.getCanonicalName())) { |
| 66 | // Case 1: Experiment |
| 67 | for (final ITmfProjectModelElement projectElement : project.getExperimentsFolder().getChildren()) { |
| 68 | if (projectElement.getName().equals(file.getParent().getName())) { |
| 69 | return new StructuredSelection(projectElement); |
| 70 | } |
| 71 | } |
| 72 | } else if (traceTypeId.equals(TmfTrace.class.getCanonicalName())) { |
| 73 | // Case 2: Trace that is a folder |
| 74 | for (final ITmfProjectModelElement projectElement : project.getTracesFolder().getChildren()) { |
| 75 | if (projectElement.getName().equals(file.getParent().getName())) { |
| 76 | return new StructuredSelection(projectElement); |
| 77 | } |
| 78 | } |
| 79 | } else { |
| 80 | // Case 3: Trace that is a file |
| 81 | for (final ITmfProjectModelElement projectElement : project.getTracesFolder().getChildren()) { |
| 82 | if (projectElement.getResource().equals(file)) { |
| 83 | return new StructuredSelection(projectElement); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } catch (CoreException e) { |
| 88 | return StructuredSelection.EMPTY; |
| 89 | } |
| 90 | } |
| 91 | return StructuredSelection.EMPTY; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * (non-Javadoc) |
| 96 | * @see org.eclipse.ui.navigator.ILinkHelper#activateEditor(org.eclipse.ui.IWorkbenchPage, org.eclipse.jface.viewers.IStructuredSelection) |
| 97 | */ |
| 98 | @Override |
| 99 | public void activateEditor(IWorkbenchPage aPage, IStructuredSelection aSelection) { |
| 100 | if (aSelection == null || aSelection.isEmpty()) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | IFile file = null; |
| 105 | |
| 106 | if ((aSelection.getFirstElement() instanceof TmfTraceElement)) { |
| 107 | TmfTraceElement traceElement = ((TmfTraceElement)aSelection.getFirstElement()); |
| 108 | |
| 109 | // If trace is under an experiment, use the original trace from the traces folder |
| 110 | traceElement = traceElement.getElementUnderTraceFolder(); |
| 111 | file = traceElement.getBookmarksFile(); |
| 112 | } else if ((aSelection.getFirstElement() instanceof TmfExperimentElement)) { |
| 113 | TmfExperimentElement experimentElement = (TmfExperimentElement) aSelection.getFirstElement(); |
| 114 | file = experimentElement.getBookmarksFile(); |
| 115 | } |
| 116 | |
| 117 | if (file != null) { |
| 118 | IEditorInput tmpInput = new FileEditorInput(file); |
| 119 | IEditorPart localEditor = aPage.findEditor(tmpInput); |
| 120 | if (localEditor != null) { |
| 121 | // Editor found. |
| 122 | aPage.bringToTop(localEditor); |
| 123 | } else { |
| 124 | // Search in references for corresponding editor |
| 125 | IEditorReference[] refs = aPage.getEditorReferences(); |
| 126 | for (IEditorReference editorReference : refs) { |
| 127 | try { |
| 128 | if (editorReference.getEditorInput().equals(tmpInput)) { |
| 129 | localEditor = editorReference.getEditor(true); |
| 130 | if (localEditor != null) { |
| 131 | aPage.bringToTop(localEditor); |
| 132 | } |
| 133 | } |
| 134 | } catch (PartInitException e) { |
| 135 | // Ignore |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |