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