1 /*******************************************************************************
2 * Copyright (c) 2011 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 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.handlers
;
15 import java
.util
.Iterator
;
17 import org
.eclipse
.core
.commands
.AbstractHandler
;
18 import org
.eclipse
.core
.commands
.ExecutionEvent
;
19 import org
.eclipse
.core
.commands
.ExecutionException
;
20 import org
.eclipse
.core
.resources
.IProject
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.core
.runtime
.CoreException
;
23 import org
.eclipse
.jface
.viewers
.ISelection
;
24 import org
.eclipse
.jface
.viewers
.TreeSelection
;
25 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
26 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
30 import org
.eclipse
.swt
.SWT
;
31 import org
.eclipse
.swt
.widgets
.MessageBox
;
32 import org
.eclipse
.ui
.IWorkbenchPage
;
33 import org
.eclipse
.ui
.IWorkbenchPart
;
34 import org
.eclipse
.ui
.IWorkbenchWindow
;
35 import org
.eclipse
.ui
.PlatformUI
;
38 * <b><u>SetTraceTypeHandler</u></b>
41 public class SelectTraceTypeHandler
extends AbstractHandler
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
47 private final String BUNDLE_PARAMETER
= "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.bundle"; //$NON-NLS-1$
48 private final String TYPE_PARAMETER
= "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.type"; //$NON-NLS-1$
49 private final String ICON_PARAMETER
= "org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.select_trace_type.icon"; //$NON-NLS-1$
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
55 private TreeSelection fSelection
= null;
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
62 public boolean isEnabled() {
64 // Check if we are closing down
65 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
70 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
71 IWorkbenchPart part
= page
.getActivePart();
72 ISelection selection
= part
.getSite().getSelectionProvider().getSelection();
74 // Make sure selection contains only traces
76 if (selection
instanceof TreeSelection
) {
77 fSelection
= (TreeSelection
) selection
;
78 @SuppressWarnings("unchecked")
79 Iterator
<Object
> iterator
= fSelection
.iterator();
80 while (iterator
.hasNext()) {
81 Object element
= iterator
.next();
82 if (!(element
instanceof TmfTraceElement
)) {
88 // If we get here, either nothing is selected or everything is a trace
89 return !selection
.isEmpty();
92 // ------------------------------------------------------------------------
94 // ------------------------------------------------------------------------
97 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
99 // Check if we are closing down
100 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
105 for (Object element
: fSelection
.toList()) {
106 TmfTraceElement trace
= (TmfTraceElement
) element
;
107 IResource resource
= trace
.getResource();
108 if (resource
!= null) {
110 // Set the properties for this resource
111 String bundleName
= event
.getParameter(BUNDLE_PARAMETER
);
112 String traceType
= event
.getParameter(TYPE_PARAMETER
);
113 String iconUrl
= event
.getParameter(ICON_PARAMETER
);
114 ok
&= propagateProperties(trace
, bundleName
, traceType
, iconUrl
);
115 } catch (CoreException e
) {
120 ((ITmfProjectModelElement
) fSelection
.getFirstElement()).getProject().refresh();
123 MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT
.ICON_ERROR
);
124 mb
.setText(Messages
.SelectTraceTypeHandler_Title
);
125 mb
.setMessage(Messages
.SelectTraceTypeHandler_InvalidTraceType
);
132 private boolean propagateProperties(TmfTraceElement trace
, String bundleName
, String traceType
, String iconUrl
) throws CoreException
{
134 IResource svResource
= trace
.getResource();
135 String svBundleName
= svResource
.getPersistentProperty(TmfTraceElement
.TRACEBUNDLE
);
136 String svTraceType
= svResource
.getPersistentProperty(TmfTraceElement
.TRACETYPE
);
137 String svIconUrl
= svResource
.getPersistentProperty(TmfTraceElement
.TRACEICON
);
139 setProperties(trace
.getResource(), bundleName
, traceType
, iconUrl
);
140 trace
.refreshTraceType();
141 if (!validateTraceType(trace
)) {
142 setProperties(trace
.getResource(), svBundleName
, svTraceType
, svIconUrl
);
143 trace
.refreshTraceType();
147 trace
.refreshTraceType();
149 if (trace
.getParent() instanceof TmfTraceFolder
) {
150 TmfExperimentFolder experimentFolder
= trace
.getProject().getExperimentsFolder();
151 for (final ITmfProjectModelElement experiment
: experimentFolder
.getChildren()) {
152 for (final ITmfProjectModelElement child
: experiment
.getChildren()) {
153 if (child
instanceof TmfTraceElement
) {
154 TmfTraceElement linkedTrace
= (TmfTraceElement
) child
;
155 if (linkedTrace
.equals(trace
)) {
156 IResource resource
= linkedTrace
.getResource();
157 setProperties(resource
, bundleName
, traceType
, iconUrl
);
158 linkedTrace
.refreshTraceType();
167 private void setProperties(IResource resource
, String bundleName
, String traceType
, String iconUrl
) throws CoreException
{
168 resource
.setPersistentProperty(TmfTraceElement
.TRACEBUNDLE
, bundleName
);
169 resource
.setPersistentProperty(TmfTraceElement
.TRACETYPE
, traceType
);
170 resource
.setPersistentProperty(TmfTraceElement
.TRACEICON
, iconUrl
);
173 private boolean validateTraceType(TmfTraceElement trace
) {
174 IProject project
= trace
.getProject().getResource();
175 ITmfTrace
<?
> tmfTrace
= null;
177 tmfTrace
= trace
.instantiateTrace();
178 return (tmfTrace
!= null && tmfTrace
.validate(project
, trace
.getLocation().getPath()));
180 if (tmfTrace
!= null)