1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.model
;
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
.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
;
37 * Implementation of ILinkHelper interface for linking with editor extension for traces and
40 * @author Bernd Hufmann
42 public class TmfEditorLinkHelper
implements ILinkHelper
{
46 * @see org.eclipse.ui.navigator.ILinkHelper#findSelection(org.eclipse.ui.IEditorInput)
49 public IStructuredSelection
findSelection(IEditorInput anInput
) {
50 IFile file
= ResourceUtil
.getFile(anInput
);
54 // Get the trace type ID
55 String traceTypeId
= file
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
56 if (traceTypeId
== null) {
57 return StructuredSelection
.EMPTY
;
60 final TmfNavigatorContentProvider ncp
= new TmfNavigatorContentProvider();
61 ncp
.getChildren(file
.getProject()); // force the model to be populated
62 final TmfProjectElement project
= TmfProjectRegistry
.getProject(file
.getProject());
64 // Check for experiments, traces which are folders or traces which are files
65 if (traceTypeId
.equals(TmfExperiment
.class.getCanonicalName())) {
67 for (final ITmfProjectModelElement projectElement
: project
.getExperimentsFolder().getChildren()) {
68 if (projectElement
.getName().equals(file
.getParent().getName())) {
69 return new StructuredSelection(projectElement
);
72 } else if (traceTypeId
.equals(TmfTrace
.class.getCanonicalName())) {
73 // Case 2: Trace that is a folder
74 for (final ITmfProjectModelElement projectElement
: project
.getTracesFolder().getChildren()) {
75 if (projectElement
.getName().equals(file
.getParent().getName())) {
76 return new StructuredSelection(projectElement
);
80 // Case 3: Trace that is a file
81 for (final ITmfProjectModelElement projectElement
: project
.getTracesFolder().getChildren()) {
82 if (projectElement
.getResource().equals(file
)) {
83 return new StructuredSelection(projectElement
);
87 } catch (CoreException e
) {
88 return StructuredSelection
.EMPTY
;
91 return StructuredSelection
.EMPTY
;
96 * @see org.eclipse.ui.navigator.ILinkHelper#activateEditor(org.eclipse.ui.IWorkbenchPage, org.eclipse.jface.viewers.IStructuredSelection)
99 public void activateEditor(IWorkbenchPage aPage
, IStructuredSelection aSelection
) {
100 if (aSelection
== null || aSelection
.isEmpty()) {
106 if ((aSelection
.getFirstElement() instanceof TmfTraceElement
)) {
107 TmfTraceElement traceElement
= ((TmfTraceElement
)aSelection
.getFirstElement());
109 // If trace is under an experiment, use the original trace from the traces folder
110 traceElement
= traceElement
.getElementUnderTraceFolder();
111 file
= traceElement
.getBookmarksFile();
112 } else if ((aSelection
.getFirstElement() instanceof TmfExperimentElement
)) {
113 TmfExperimentElement experimentElement
= (TmfExperimentElement
) aSelection
.getFirstElement();
114 file
= experimentElement
.getBookmarksFile();
118 IEditorInput tmpInput
= new FileEditorInput(file
);
119 IEditorPart localEditor
= aPage
.findEditor(tmpInput
);
120 if (localEditor
!= null) {
122 aPage
.bringToTop(localEditor
);
124 // Search in references for corresponding editor
125 IEditorReference
[] refs
= aPage
.getEditorReferences();
126 for (IEditorReference editorReference
: refs
) {
128 if (editorReference
.getEditorInput().equals(tmpInput
)) {
129 localEditor
= editorReference
.getEditor(true);
130 if (localEditor
!= null) {
131 aPage
.bringToTop(localEditor
);
134 } catch (PartInitException e
) {