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