| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2012 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.wizards; |
| 14 | |
| 15 | import java.util.HashMap; |
| 16 | import java.util.Map; |
| 17 | import java.util.Set; |
| 18 | import java.util.Vector; |
| 19 | |
| 20 | import org.eclipse.core.resources.IFile; |
| 21 | import org.eclipse.core.resources.IFolder; |
| 22 | import org.eclipse.core.resources.IResource; |
| 23 | import org.eclipse.core.resources.IWorkspace; |
| 24 | import org.eclipse.core.resources.ResourcesPlugin; |
| 25 | import org.eclipse.core.runtime.CoreException; |
| 26 | import org.eclipse.core.runtime.IPath; |
| 27 | import org.eclipse.core.runtime.QualifiedName; |
| 28 | import org.eclipse.jface.viewers.CheckboxTableViewer; |
| 29 | import org.eclipse.jface.wizard.WizardPage; |
| 30 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
| 31 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
| 32 | import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement; |
| 33 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
| 34 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement; |
| 35 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
| 36 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; |
| 37 | import org.eclipse.linuxtools.tmf.ui.project.model.TraceFolderContentProvider; |
| 38 | import org.eclipse.linuxtools.tmf.ui.project.model.TraceFolderLabelProvider; |
| 39 | import org.eclipse.swt.SWT; |
| 40 | import org.eclipse.swt.layout.FormAttachment; |
| 41 | import org.eclipse.swt.layout.FormData; |
| 42 | import org.eclipse.swt.layout.FormLayout; |
| 43 | import org.eclipse.swt.widgets.Composite; |
| 44 | import org.eclipse.swt.widgets.Table; |
| 45 | import org.eclipse.swt.widgets.TableColumn; |
| 46 | |
| 47 | /** |
| 48 | * Implementation of a wizard page for selecting trace for an experiment. |
| 49 | * <p> |
| 50 | * |
| 51 | * @version 1.0 |
| 52 | * @author Francois Chouinard |
| 53 | */ |
| 54 | public class SelectTracesWizardPage extends WizardPage { |
| 55 | |
| 56 | // ------------------------------------------------------------------------ |
| 57 | // Attributes |
| 58 | // ------------------------------------------------------------------------ |
| 59 | |
| 60 | private final TmfProjectElement fProject; |
| 61 | private final TmfExperimentElement fExperiment; |
| 62 | private Map<String, TmfTraceElement> fPreviousTraces; |
| 63 | private CheckboxTableViewer fCheckboxTableViewer; |
| 64 | |
| 65 | // ------------------------------------------------------------------------ |
| 66 | // Constructor |
| 67 | // ------------------------------------------------------------------------ |
| 68 | /** |
| 69 | * Constructor |
| 70 | * @param project The project model element. |
| 71 | * @param experiment The experiment model experiment. |
| 72 | */ |
| 73 | protected SelectTracesWizardPage(TmfProjectElement project, TmfExperimentElement experiment) { |
| 74 | super(""); //$NON-NLS-1$ |
| 75 | setTitle(Messages.SelectTracesWizardPage_WindowTitle); |
| 76 | setDescription(Messages.SelectTracesWizardPage_Description); |
| 77 | fProject = project; |
| 78 | fExperiment = experiment; |
| 79 | } |
| 80 | |
| 81 | // ------------------------------------------------------------------------ |
| 82 | // Dialog |
| 83 | // ------------------------------------------------------------------------ |
| 84 | /* |
| 85 | * (non-Javadoc) |
| 86 | * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite) |
| 87 | */ |
| 88 | @Override |
| 89 | public void createControl(Composite parent) { |
| 90 | Composite container = new Composite(parent, SWT.NULL); |
| 91 | container.setLayout(new FormLayout()); |
| 92 | setControl(container); |
| 93 | |
| 94 | fCheckboxTableViewer = CheckboxTableViewer.newCheckList(container, SWT.BORDER); |
| 95 | fCheckboxTableViewer.setContentProvider(new TraceFolderContentProvider()); |
| 96 | fCheckboxTableViewer.setLabelProvider(new TraceFolderLabelProvider()); |
| 97 | |
| 98 | final Table table = fCheckboxTableViewer.getTable(); |
| 99 | final FormData formData = new FormData(); |
| 100 | formData.bottom = new FormAttachment(100, 0); |
| 101 | formData.right = new FormAttachment(100, 0); |
| 102 | formData.top = new FormAttachment(0, 0); |
| 103 | formData.left = new FormAttachment(0, 0); |
| 104 | table.setLayoutData(formData); |
| 105 | table.setHeaderVisible(true); |
| 106 | |
| 107 | final TableColumn tableColumn = new TableColumn(table, SWT.NONE); |
| 108 | tableColumn.setWidth(200); |
| 109 | tableColumn.setText(Messages.SelectTracesWizardPage_TraceColumnHeader); |
| 110 | |
| 111 | // Get the list of traces already part of the experiment |
| 112 | fPreviousTraces = new HashMap<String, TmfTraceElement>(); |
| 113 | for (ITmfProjectModelElement child : fExperiment.getChildren()) { |
| 114 | if (child instanceof TmfTraceElement) { |
| 115 | TmfTraceElement trace = (TmfTraceElement) child; |
| 116 | String name = trace.getResource().getName(); |
| 117 | fPreviousTraces.put(name, trace); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Populate the list of traces to choose from |
| 122 | Set<String> keys = fPreviousTraces.keySet(); |
| 123 | TmfTraceFolder traceFolder = fProject.getTracesFolder(); |
| 124 | fCheckboxTableViewer.setInput(traceFolder); |
| 125 | |
| 126 | // Set the checkbox for the traces already included |
| 127 | int index = 0; |
| 128 | Object element = fCheckboxTableViewer.getElementAt(index++); |
| 129 | while (element != null) { |
| 130 | if (element instanceof TmfTraceElement) { |
| 131 | TmfTraceElement trace = (TmfTraceElement) element; |
| 132 | if (keys.contains(trace.getResource().getName())) { |
| 133 | fCheckboxTableViewer.setChecked(element, true); |
| 134 | } |
| 135 | } |
| 136 | element = fCheckboxTableViewer.getElementAt(index++); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Method to finalize the select operation. |
| 142 | * @return <code>true</code> if successful else <code>false</code> |
| 143 | */ |
| 144 | public boolean performFinish() { |
| 145 | |
| 146 | IFolder experiment = fExperiment.getResource(); |
| 147 | |
| 148 | // Add the selected traces to the experiment |
| 149 | Set<String> keys = fPreviousTraces.keySet(); |
| 150 | TmfTraceElement[] traces = getSelection(); |
| 151 | for (TmfTraceElement trace : traces) { |
| 152 | String name = trace.getResource().getName(); |
| 153 | if (keys.contains(name)) { |
| 154 | fPreviousTraces.remove(name); |
| 155 | } else { |
| 156 | IResource resource = trace.getResource(); |
| 157 | IPath location = resource.getLocation(); |
| 158 | createLink(experiment, trace, resource, location); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Remove traces that were unchecked (thus left in fPreviousTraces) |
| 163 | keys = fPreviousTraces.keySet(); |
| 164 | for (String key : keys) { |
| 165 | fExperiment.removeChild(fPreviousTraces.get(key)); |
| 166 | IResource resource = experiment.findMember(key); |
| 167 | try { |
| 168 | resource.delete(true, null); |
| 169 | } catch (CoreException e) { |
| 170 | Activator.getDefault().logError("Error selecting traces for experiment " + experiment.getName(), e); //$NON-NLS-1$ |
| 171 | } |
| 172 | } |
| 173 | fProject.refresh(); |
| 174 | |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Create a link to the actual trace and set the trace type |
| 180 | */ |
| 181 | private static void createLink(IFolder experiment, TmfTraceElement trace, |
| 182 | IResource resource, IPath location) { |
| 183 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| 184 | try { |
| 185 | Map<QualifiedName, String> properties = trace.getResource().getPersistentProperties(); |
| 186 | String bundleName = properties.get(TmfCommonConstants.TRACEBUNDLE); |
| 187 | String traceType = properties.get(TmfCommonConstants.TRACETYPE); |
| 188 | String iconUrl = properties.get(TmfCommonConstants.TRACEICON); |
| 189 | |
| 190 | if (resource instanceof IFolder) { |
| 191 | IFolder folder = experiment.getFolder(trace.getName()); |
| 192 | if (workspace.validateLinkLocation(folder, location).isOK()) { |
| 193 | folder.createLink(location, IResource.REPLACE, null); |
| 194 | setProperties(folder, bundleName, traceType, iconUrl); |
| 195 | |
| 196 | } else { |
| 197 | Activator.getDefault().logError("Error creating link. Invalid trace location " + location); //$NON-NLS-1$ |
| 198 | } |
| 199 | } else { |
| 200 | IFile file = experiment.getFile(trace.getName()); |
| 201 | if (workspace.validateLinkLocation(file, location).isOK()) { |
| 202 | file.createLink(location, IResource.REPLACE, null); |
| 203 | setProperties(file, bundleName, traceType, iconUrl); |
| 204 | } else { |
| 205 | Activator.getDefault().logError("Error creating link. Invalid trace location " + location); //$NON-NLS-1$ |
| 206 | } |
| 207 | } |
| 208 | } catch (CoreException e) { |
| 209 | Activator.getDefault().logError("Error creating link to location " + location, e); //$NON-NLS-1$ |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | private static void setProperties(IResource resource, String bundleName, |
| 214 | String traceType, String iconUrl) throws CoreException { |
| 215 | resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, bundleName); |
| 216 | resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceType); |
| 217 | resource.setPersistentProperty(TmfCommonConstants.TRACEICON, iconUrl); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Get the list of selected traces |
| 222 | */ |
| 223 | private TmfTraceElement[] getSelection() { |
| 224 | Vector<TmfTraceElement> traces = new Vector<TmfTraceElement>(); |
| 225 | Object[] selection = fCheckboxTableViewer.getCheckedElements(); |
| 226 | for (Object sel : selection) { |
| 227 | if (sel instanceof TmfTraceElement) { |
| 228 | traces.add((TmfTraceElement) sel); |
| 229 | } |
| 230 | } |
| 231 | TmfTraceElement[] result = new TmfTraceElement[traces.size()]; |
| 232 | traces.toArray(result); |
| 233 | return result; |
| 234 | } |
| 235 | |
| 236 | } |