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