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
.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
;
39 * Implementation of ILinkHelper interface for linking with editor extension for traces and
42 * @author Bernd Hufmann
44 public class TmfEditorLinkHelper
implements ILinkHelper
{
48 * @see org.eclipse.ui.navigator.ILinkHelper#findSelection(org.eclipse.ui.IEditorInput)
51 public IStructuredSelection
findSelection(IEditorInput anInput
) {
52 IFile file
= ResourceUtil
.getFile(anInput
);
56 // Get the trace type ID
57 String traceTypeId
= file
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
58 if (traceTypeId
== null) {
59 return StructuredSelection
.EMPTY
;
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());
66 // Check for experiments, traces which are folders or traces which are files
67 if (traceTypeId
.equals(TmfExperiment
.class.getCanonicalName())) {
69 for (final ITmfProjectModelElement projectElement
: project
.getExperimentsFolder().getChildren()) {
70 if (projectElement
.getName().equals(file
.getParent().getName())) {
71 return new StructuredSelection(projectElement
);
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
);
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
);
89 } catch (CoreException e
) {
90 return StructuredSelection
.EMPTY
;
93 return StructuredSelection
.EMPTY
;
98 * @see org.eclipse.ui.navigator.ILinkHelper#activateEditor(org.eclipse.ui.IWorkbenchPage, org.eclipse.jface.viewers.IStructuredSelection)
101 public void activateEditor(IWorkbenchPage aPage
, IStructuredSelection aSelection
) {
102 if (aSelection
== null || aSelection
.isEmpty()) {
108 if ((aSelection
.getFirstElement() instanceof TmfTraceElement
)) {
109 TmfTraceElement traceElement
= ((TmfTraceElement
)aSelection
.getFirstElement());
111 // If trace is under an experiment, use the original trace from the traces folder
112 traceElement
= traceElement
.getElementUnderTraceFolder();
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() + '_');
121 } else if ((aSelection
.getFirstElement() instanceof TmfExperimentElement
)) {
122 TmfExperimentElement experimentElement
= (TmfExperimentElement
) aSelection
.getFirstElement();
123 file
= experimentElement
.getResource().getFile(experimentElement
.getName() + '_');
127 IEditorInput tmpInput
= new FileEditorInput(file
);
128 IEditorPart localEditor
= aPage
.findEditor(tmpInput
);
129 if (localEditor
!= null) {
131 aPage
.activate(localEditor
);
132 aPage
.bringToTop(localEditor
);
134 // Search in references for corresponding editor
135 IEditorReference
[] refs
= aPage
.getEditorReferences();
136 for (IEditorReference editorReference
: refs
) {
138 if (editorReference
.getEditorInput().equals(tmpInput
)) {
139 localEditor
= editorReference
.getEditor(true);
140 if (localEditor
!= null) {
141 aPage
.bringToTop(localEditor
);
144 } catch (PartInitException e
) {