Commit | Line | Data |
---|---|---|
1169a815 | 1 | /******************************************************************************* |
c8422608 | 2 | * Copyright (c) 2012, 2013 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 | *******************************************************************************/ | |
12 | package org.eclipse.linuxtools.internal.tmf.ui.project.model; | |
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; | |
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 | ||
1169a815 BH |
44 | @Override |
45 | public IStructuredSelection findSelection(IEditorInput anInput) { | |
46 | IFile file = ResourceUtil.getFile(anInput); | |
47 | if (file != null) { | |
48 | ||
49 | try { | |
50 | // Get the trace type ID | |
51 | String traceTypeId = file.getPersistentProperty(TmfCommonConstants.TRACETYPE); | |
52 | if (traceTypeId == null) { | |
53 | return StructuredSelection.EMPTY; | |
54 | } | |
55 | ||
56 | final TmfNavigatorContentProvider ncp = new TmfNavigatorContentProvider(); | |
57 | ncp.getChildren(file.getProject()); // force the model to be populated | |
58 | final TmfProjectElement project = TmfProjectRegistry.getProject(file.getProject()); | |
59 | ||
60 | // Check for experiments, traces which are folders or traces which are files | |
61 | if (traceTypeId.equals(TmfExperiment.class.getCanonicalName())) { | |
62 | // Case 1: Experiment | |
63 | for (final ITmfProjectModelElement projectElement : project.getExperimentsFolder().getChildren()) { | |
64 | if (projectElement.getName().equals(file.getParent().getName())) { | |
65 | return new StructuredSelection(projectElement); | |
66 | } | |
67 | } | |
68 | } else if (traceTypeId.equals(TmfTrace.class.getCanonicalName())) { | |
69 | // Case 2: Trace that is a folder | |
70 | for (final ITmfProjectModelElement projectElement : project.getTracesFolder().getChildren()) { | |
71 | if (projectElement.getName().equals(file.getParent().getName())) { | |
72 | return new StructuredSelection(projectElement); | |
73 | } | |
74 | } | |
75 | } else { | |
76 | // Case 3: Trace that is a file | |
77 | for (final ITmfProjectModelElement projectElement : project.getTracesFolder().getChildren()) { | |
78 | if (projectElement.getResource().equals(file)) { | |
79 | return new StructuredSelection(projectElement); | |
80 | } | |
81 | } | |
82 | } | |
83 | } catch (CoreException e) { | |
84 | return StructuredSelection.EMPTY; | |
85 | } | |
86 | } | |
87 | return StructuredSelection.EMPTY; | |
88 | } | |
89 | ||
1169a815 BH |
90 | @Override |
91 | public void activateEditor(IWorkbenchPage aPage, IStructuredSelection aSelection) { | |
92 | if (aSelection == null || aSelection.isEmpty()) { | |
93 | return; | |
94 | } | |
95 | ||
96 | IFile file = null; | |
97 | ||
98 | if ((aSelection.getFirstElement() instanceof TmfTraceElement)) { | |
99 | TmfTraceElement traceElement = ((TmfTraceElement)aSelection.getFirstElement()); | |
100 | ||
101 | // If trace is under an experiment, use the original trace from the traces folder | |
102 | traceElement = traceElement.getElementUnderTraceFolder(); | |
81fe3479 | 103 | file = traceElement.getBookmarksFile(); |
1169a815 BH |
104 | } else if ((aSelection.getFirstElement() instanceof TmfExperimentElement)) { |
105 | TmfExperimentElement experimentElement = (TmfExperimentElement) aSelection.getFirstElement(); | |
81fe3479 | 106 | file = experimentElement.getBookmarksFile(); |
1169a815 BH |
107 | } |
108 | ||
109 | if (file != null) { | |
110 | IEditorInput tmpInput = new FileEditorInput(file); | |
111 | IEditorPart localEditor = aPage.findEditor(tmpInput); | |
112 | if (localEditor != null) { | |
113 | // Editor found. | |
1169a815 BH |
114 | aPage.bringToTop(localEditor); |
115 | } else { | |
116 | // Search in references for corresponding editor | |
117 | IEditorReference[] refs = aPage.getEditorReferences(); | |
118 | for (IEditorReference editorReference : refs) { | |
119 | try { | |
120 | if (editorReference.getEditorInput().equals(tmpInput)) { | |
121 | localEditor = editorReference.getEditor(true); | |
122 | if (localEditor != null) { | |
123 | aPage.bringToTop(localEditor); | |
124 | } | |
125 | } | |
126 | } catch (PartInitException e) { | |
127 | // Ignore | |
128 | } | |
129 | } | |
130 | } | |
131 | } | |
132 | } | |
133 | } | |
134 |