Commit | Line | Data |
---|---|---|
12c155f5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011 Ericsson | |
6256d8ad | 3 | * |
12c155f5 FC |
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 | |
6256d8ad | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
d34665f9 | 13 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
12c155f5 FC |
14 | |
15 | import java.util.Iterator; | |
16 | ||
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; | |
1595249b | 24 | import org.eclipse.jface.viewers.ISelectionProvider; |
12c155f5 | 25 | import org.eclipse.jface.viewers.TreeSelection; |
8fd82db5 | 26 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
e12ecd30 | 27 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
6c13869b | 28 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
12c155f5 FC |
29 | import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement; |
30 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder; | |
31 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
32 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
33 | import org.eclipse.swt.SWT; | |
34 | import org.eclipse.swt.widgets.MessageBox; | |
35 | import org.eclipse.ui.IWorkbenchPage; | |
36 | import org.eclipse.ui.IWorkbenchPart; | |
37 | import org.eclipse.ui.IWorkbenchWindow; | |
38 | import org.eclipse.ui.PlatformUI; | |
39 | ||
40 | /** | |
41 | * <b><u>SetTraceTypeHandler</u></b> | |
42 | * <p> | |
43 | */ | |
44 | public class SelectTraceTypeHandler extends AbstractHandler { | |
45 | ||
46 | // ------------------------------------------------------------------------ | |
47 | // Constants | |
48 | // ------------------------------------------------------------------------ | |
49 | ||
37d150dd FC |
50 | private static final String BUNDLE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.bundle"; //$NON-NLS-1$ |
51 | private static final String TYPE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.type"; //$NON-NLS-1$ | |
52 | private static final String ICON_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.icon"; //$NON-NLS-1$ | |
12c155f5 FC |
53 | |
54 | // ------------------------------------------------------------------------ | |
55 | // Attributes | |
56 | // ------------------------------------------------------------------------ | |
57 | ||
58 | private TreeSelection fSelection = null; | |
59 | ||
60 | // ------------------------------------------------------------------------ | |
61 | // Validation | |
62 | // ------------------------------------------------------------------------ | |
63 | ||
64 | @Override | |
65 | public boolean isEnabled() { | |
66 | ||
67 | // Check if we are closing down | |
68 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
6256d8ad | 69 | if (window == null) { |
12c155f5 | 70 | return false; |
6256d8ad | 71 | } |
12c155f5 FC |
72 | |
73 | // Get the selection | |
74 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
75 | IWorkbenchPart part = page.getActivePart(); | |
1595249b | 76 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
6256d8ad | 77 | if (selectionProvider == null) { |
1595249b | 78 | return false; |
6256d8ad | 79 | } |
1595249b | 80 | ISelection selection = selectionProvider.getSelection(); |
12c155f5 FC |
81 | |
82 | // Make sure selection contains only traces | |
83 | fSelection = null; | |
84 | if (selection instanceof TreeSelection) { | |
85 | fSelection = (TreeSelection) selection; | |
12c155f5 FC |
86 | Iterator<Object> iterator = fSelection.iterator(); |
87 | while (iterator.hasNext()) { | |
88 | Object element = iterator.next(); | |
89 | if (!(element instanceof TmfTraceElement)) { | |
90 | return false; | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | // If we get here, either nothing is selected or everything is a trace | |
96 | return !selection.isEmpty(); | |
97 | } | |
98 | ||
99 | // ------------------------------------------------------------------------ | |
100 | // Execution | |
101 | // ------------------------------------------------------------------------ | |
102 | ||
103 | @Override | |
104 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
105 | ||
106 | // Check if we are closing down | |
107 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
6256d8ad | 108 | if (window == null) { |
12c155f5 | 109 | return null; |
6256d8ad | 110 | } |
12c155f5 FC |
111 | |
112 | boolean ok = true; | |
113 | for (Object element : fSelection.toList()) { | |
114 | TmfTraceElement trace = (TmfTraceElement) element; | |
e12ecd30 | 115 | trace = trace.getElementUnderTraceFolder(); |
12c155f5 FC |
116 | IResource resource = trace.getResource(); |
117 | if (resource != null) { | |
118 | try { | |
119 | // Set the properties for this resource | |
120 | String bundleName = event.getParameter(BUNDLE_PARAMETER); | |
121 | String traceType = event.getParameter(TYPE_PARAMETER); | |
122 | String iconUrl = event.getParameter(ICON_PARAMETER); | |
123 | ok &= propagateProperties(trace, bundleName, traceType, iconUrl); | |
124 | } catch (CoreException e) { | |
8fd82db5 | 125 | Activator.getDefault().logError("Error selecting trace type for trace" + trace.getName(), e); //$NON-NLS-1$ |
12c155f5 FC |
126 | } |
127 | } | |
128 | } | |
129 | ((ITmfProjectModelElement) fSelection.getFirstElement()).getProject().refresh(); | |
130 | ||
131 | if (!ok) { | |
132 | MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.ICON_ERROR); | |
133 | mb.setText(Messages.SelectTraceTypeHandler_Title); | |
134 | mb.setMessage(Messages.SelectTraceTypeHandler_InvalidTraceType); | |
135 | mb.open(); | |
136 | } | |
137 | ||
138 | return null; | |
139 | } | |
140 | ||
abbdd66a AM |
141 | private static boolean propagateProperties(TmfTraceElement trace, |
142 | String bundleName, String traceType, String iconUrl) | |
143 | throws CoreException { | |
12c155f5 FC |
144 | |
145 | IResource svResource = trace.getResource(); | |
e12ecd30 BH |
146 | String svBundleName = svResource.getPersistentProperty(TmfCommonConstants.TRACEBUNDLE); |
147 | String svTraceType = svResource.getPersistentProperty(TmfCommonConstants.TRACETYPE); | |
148 | String svIconUrl = svResource.getPersistentProperty(TmfCommonConstants.TRACEICON); | |
12c155f5 FC |
149 | |
150 | setProperties(trace.getResource(), bundleName, traceType, iconUrl); | |
151 | trace.refreshTraceType(); | |
152 | if (!validateTraceType(trace)) { | |
153 | setProperties(trace.getResource(), svBundleName, svTraceType, svIconUrl); | |
154 | trace.refreshTraceType(); | |
155 | return false; | |
156 | } | |
157 | ||
158 | trace.refreshTraceType(); | |
159 | ||
160 | if (trace.getParent() instanceof TmfTraceFolder) { | |
161 | TmfExperimentFolder experimentFolder = trace.getProject().getExperimentsFolder(); | |
162 | for (final ITmfProjectModelElement experiment : experimentFolder.getChildren()) { | |
163 | for (final ITmfProjectModelElement child : experiment.getChildren()) { | |
164 | if (child instanceof TmfTraceElement) { | |
165 | TmfTraceElement linkedTrace = (TmfTraceElement) child; | |
166 | if (linkedTrace.equals(trace)) { | |
167 | IResource resource = linkedTrace.getResource(); | |
168 | setProperties(resource, bundleName, traceType, iconUrl); | |
169 | linkedTrace.refreshTraceType(); | |
170 | } | |
171 | } | |
172 | } | |
173 | } | |
174 | } | |
c50b1d3b | 175 | |
12c155f5 FC |
176 | return true; |
177 | } | |
178 | ||
abbdd66a AM |
179 | private static void setProperties(IResource resource, String bundleName, |
180 | String traceType, String iconUrl) throws CoreException { | |
e12ecd30 BH |
181 | resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, bundleName); |
182 | resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceType); | |
183 | resource.setPersistentProperty(TmfCommonConstants.TRACEICON, iconUrl); | |
12c155f5 FC |
184 | } |
185 | ||
abbdd66a | 186 | private static boolean validateTraceType(TmfTraceElement trace) { |
12c155f5 | 187 | IProject project = trace.getProject().getResource(); |
6256d8ad | 188 | ITmfTrace tmfTrace = null; |
12c155f5 FC |
189 | try { |
190 | tmfTrace = trace.instantiateTrace(); | |
191 | return (tmfTrace != null && tmfTrace.validate(project, trace.getLocation().getPath())); | |
192 | } finally { | |
6256d8ad | 193 | if (tmfTrace != null) { |
12c155f5 | 194 | tmfTrace.dispose(); |
6256d8ad | 195 | } |
12c155f5 FC |
196 | } |
197 | } | |
198 | ||
199 | } |