552123ea3cb8b96c59607d95385d51fceabdda96
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / model / TmfEditorLinkHelper.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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.TmfExperimentElement;
22 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
23 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry;
24 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
25 import org.eclipse.ui.IEditorInput;
26 import org.eclipse.ui.IEditorPart;
27 import org.eclipse.ui.IEditorReference;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.PartInitException;
30 import org.eclipse.ui.ide.ResourceUtil;
31 import org.eclipse.ui.navigator.ILinkHelper;
32 import 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 */
40 public class TmfEditorLinkHelper implements ILinkHelper {
41
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
54 final TmfProjectElement project = TmfProjectRegistry.getProject(file.getProject(), true);
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
59 for (final TmfExperimentElement experimentElement : project.getExperimentsFolder().getExperiments()) {
60 if (experimentElement.getResource().equals(file.getParent())) {
61 return new StructuredSelection(experimentElement);
62 }
63 }
64 } else if (traceTypeId.equals(TmfTrace.class.getCanonicalName())) {
65 // Case 2: Trace that is a folder
66 for (final TmfTraceElement traceElement : project.getTracesFolder().getTraces()) {
67 if (traceElement.getResource().equals(file.getParent())) {
68 return new StructuredSelection(traceElement);
69 }
70 }
71 } else {
72 // Case 3: Trace that is a file
73 for (final TmfTraceElement traceElement : project.getTracesFolder().getTraces()) {
74 if (traceElement.getResource().equals(file)) {
75 return new StructuredSelection(traceElement);
76 }
77 }
78 }
79 } catch (CoreException e) {
80 return StructuredSelection.EMPTY;
81 }
82 }
83 return StructuredSelection.EMPTY;
84 }
85
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();
99 file = traceElement.getBookmarksFile();
100 } else if ((aSelection.getFirstElement() instanceof TmfExperimentElement)) {
101 TmfExperimentElement experimentElement = (TmfExperimentElement) aSelection.getFirstElement();
102 file = experimentElement.getBookmarksFile();
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.
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.033364 seconds and 5 git commands to generate.