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