| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2012, 2013 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | * Bernd Hufmann - Added handling of streamed traces |
| 12 | **********************************************************************/ |
| 13 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import org.eclipse.core.resources.IFolder; |
| 19 | import org.eclipse.core.resources.IProject; |
| 20 | import org.eclipse.core.resources.ResourcesPlugin; |
| 21 | import org.eclipse.core.runtime.CoreException; |
| 22 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 23 | import org.eclipse.jface.dialogs.Dialog; |
| 24 | import org.eclipse.jface.dialogs.IDialogConstants; |
| 25 | import org.eclipse.jface.dialogs.MessageDialog; |
| 26 | import org.eclipse.jface.viewers.CheckStateChangedEvent; |
| 27 | import org.eclipse.jface.viewers.CheckboxTreeViewer; |
| 28 | import org.eclipse.jface.viewers.ICheckStateListener; |
| 29 | import org.eclipse.jface.viewers.StructuredSelection; |
| 30 | import org.eclipse.jface.window.Window; |
| 31 | import org.eclipse.jface.wizard.WizardDialog; |
| 32 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; |
| 33 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
| 34 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent; |
| 35 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.IRemoteSystemProxy; |
| 36 | import org.eclipse.linuxtools.tmf.core.TmfProjectNature; |
| 37 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; |
| 38 | import org.eclipse.linuxtools.tmf.ui.project.wizards.ImportTraceWizard; |
| 39 | import org.eclipse.rse.services.clientserver.messages.SystemMessageException; |
| 40 | import org.eclipse.rse.subsystems.files.core.servicesubsystem.IFileServiceSubSystem; |
| 41 | import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile; |
| 42 | import org.eclipse.swt.SWT; |
| 43 | import org.eclipse.swt.custom.CCombo; |
| 44 | import org.eclipse.swt.graphics.Point; |
| 45 | import org.eclipse.swt.layout.GridData; |
| 46 | import org.eclipse.swt.layout.GridLayout; |
| 47 | import org.eclipse.swt.widgets.Button; |
| 48 | import org.eclipse.swt.widgets.Composite; |
| 49 | import org.eclipse.swt.widgets.Control; |
| 50 | import org.eclipse.swt.widgets.Group; |
| 51 | import org.eclipse.swt.widgets.Shell; |
| 52 | import org.eclipse.swt.widgets.Text; |
| 53 | import org.eclipse.swt.widgets.Tree; |
| 54 | import org.eclipse.ui.PlatformUI; |
| 55 | import org.eclipse.ui.model.WorkbenchContentProvider; |
| 56 | import org.eclipse.ui.model.WorkbenchLabelProvider; |
| 57 | |
| 58 | /** |
| 59 | * <p> |
| 60 | * Dialog box for collecting trace import information. |
| 61 | * </p> |
| 62 | * |
| 63 | * @author Bernd Hufmann |
| 64 | */ |
| 65 | public class ImportDialog extends Dialog implements IImportDialog { |
| 66 | |
| 67 | // ------------------------------------------------------------------------ |
| 68 | // Constants |
| 69 | // ------------------------------------------------------------------------ |
| 70 | /** The icon file for this dialog box. */ |
| 71 | public static final String IMPORT_ICON_FILE = "icons/elcl16/import_trace.gif"; //$NON-NLS-1$ |
| 72 | |
| 73 | /** Parent directory for UST traces */ |
| 74 | public static final String UST_PARENT_DIRECTORY = "ust"; //$NON-NLS-1$ |
| 75 | |
| 76 | // ------------------------------------------------------------------------ |
| 77 | // Attributes |
| 78 | // ------------------------------------------------------------------------ |
| 79 | /** |
| 80 | * The dialog composite. |
| 81 | */ |
| 82 | private Composite fDialogComposite = null; |
| 83 | /** |
| 84 | * The checkbox tree viewer for selecting available traces |
| 85 | */ |
| 86 | private CheckboxTreeViewer fFolderViewer; |
| 87 | /** |
| 88 | * The combo box for selecting a project. |
| 89 | */ |
| 90 | private CCombo fCombo; |
| 91 | /** |
| 92 | * The overwrite button |
| 93 | */ |
| 94 | private Button fOverwriteButton; |
| 95 | /** |
| 96 | * The button to open import wizard for import locally. |
| 97 | */ |
| 98 | private Button fImportLocallyButton; |
| 99 | /** |
| 100 | * List of available LTTng 2.0 projects |
| 101 | */ |
| 102 | private List<IProject> fProjects; |
| 103 | /** |
| 104 | * The parent where the new node should be added. |
| 105 | */ |
| 106 | private TraceSessionComponent fSession = null; |
| 107 | /** |
| 108 | * List of traces to import |
| 109 | */ |
| 110 | private final List<ImportFileInfo> fTraces = new ArrayList<ImportFileInfo>(); |
| 111 | /** |
| 112 | * Selection index in project combo box. |
| 113 | */ |
| 114 | private int fProjectIndex; |
| 115 | /** |
| 116 | * Flag to indicate that something went wrong when creating the dialog box. |
| 117 | */ |
| 118 | private boolean fIsError = false; |
| 119 | |
| 120 | // ------------------------------------------------------------------------ |
| 121 | // Constructors |
| 122 | // ------------------------------------------------------------------------ |
| 123 | /** |
| 124 | * Constructor |
| 125 | * @param shell - a shell for the display of the dialog |
| 126 | */ |
| 127 | public ImportDialog(Shell shell) { |
| 128 | super(shell); |
| 129 | setShellStyle(SWT.RESIZE); |
| 130 | } |
| 131 | |
| 132 | // ------------------------------------------------------------------------ |
| 133 | // Accessors |
| 134 | // ------------------------------------------------------------------------ |
| 135 | |
| 136 | @Override |
| 137 | public List<ImportFileInfo> getTracePathes() { |
| 138 | List<ImportFileInfo> retList = new ArrayList<ImportFileInfo>(); |
| 139 | retList.addAll(fTraces); |
| 140 | return retList; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public IProject getProject() { |
| 145 | return fProjects.get(fProjectIndex); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void setSession(TraceSessionComponent session) { |
| 150 | fSession = session; |
| 151 | } |
| 152 | |
| 153 | // ------------------------------------------------------------------------ |
| 154 | // Operations |
| 155 | // ------------------------------------------------------------------------ |
| 156 | |
| 157 | @Override |
| 158 | protected void configureShell(Shell newShell) { |
| 159 | super.configureShell(newShell); |
| 160 | newShell.setText(Messages.TraceControl_ImportDialogTitle); |
| 161 | newShell.setImage(Activator.getDefault().loadIcon(IMPORT_ICON_FILE)); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | protected Control createDialogArea(Composite parent) { |
| 166 | |
| 167 | // Main dialog panel |
| 168 | fDialogComposite = new Composite(parent, SWT.NONE); |
| 169 | GridLayout layout = new GridLayout(1, true); |
| 170 | fDialogComposite.setLayout(layout); |
| 171 | fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 172 | |
| 173 | try { |
| 174 | if (fSession.isStreamedTrace()) { |
| 175 | createLocalComposite(); |
| 176 | } else { |
| 177 | createRemoteComposite(); |
| 178 | } |
| 179 | } catch (CoreException e) { |
| 180 | createErrorComposite(parent, e.fillInStackTrace()); |
| 181 | return fDialogComposite; |
| 182 | } catch (SystemMessageException e) { |
| 183 | createErrorComposite(parent, e.fillInStackTrace()); |
| 184 | return fDialogComposite; |
| 185 | } |
| 186 | return fDialogComposite; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | protected void createButtonsForButtonBar(Composite parent) { |
| 191 | createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", true); //$NON-NLS-1$ |
| 192 | fImportLocallyButton = createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$ |
| 193 | if (fSession.isStreamedTrace()) { |
| 194 | fImportLocallyButton.setText("&Next..."); //$NON-NLS-1$ |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | protected void okPressed() { |
| 200 | if (!fIsError) { |
| 201 | |
| 202 | // Validate input data |
| 203 | fTraces.clear(); |
| 204 | |
| 205 | fProjectIndex = fCombo.getSelectionIndex(); |
| 206 | |
| 207 | if (fProjectIndex < 0) { |
| 208 | MessageDialog.openError(getShell(), |
| 209 | Messages.TraceControl_ImportDialogTitle, |
| 210 | Messages.TraceControl_ImportDialogNoProjectSelectedError); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | if (fSession.isStreamedTrace()) { |
| 215 | // For streaming use standard import wizard from TMF because exact location |
| 216 | // is not available (lttng backend limitation) |
| 217 | IProject project = fProjects.get(fCombo.getSelectionIndex()); |
| 218 | ImportTraceWizard wizard = new ImportTraceWizard(); |
| 219 | wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(project)); |
| 220 | WizardDialog dialog = new WizardDialog(getShell(), wizard); |
| 221 | if (dialog.open() == Window.OK) { |
| 222 | super.okPressed(); |
| 223 | } |
| 224 | super.cancelPressed(); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | IProject project = fProjects.get(fProjectIndex); |
| 229 | IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME); |
| 230 | |
| 231 | if (!traceFolder.exists()) { |
| 232 | // Invalid LTTng 2.0 project |
| 233 | MessageDialog.openError(getShell(), |
| 234 | Messages.TraceControl_ImportDialogTitle, |
| 235 | Messages.TraceControl_ImportDialogInvalidTracingProject + " (" + TmfTraceFolder.TRACE_FOLDER_NAME + ")"); //$NON-NLS-1$//$NON-NLS-2$ |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | boolean overwriteAll = fOverwriteButton.getSelection(); |
| 240 | |
| 241 | Object[] checked = fFolderViewer.getCheckedElements(); |
| 242 | for (int i = 0; i < checked.length; i++) { |
| 243 | IRemoteFile file = (IRemoteFile) checked[i]; |
| 244 | |
| 245 | // Only add actual trace directories |
| 246 | if (file.isDirectory() && !UST_PARENT_DIRECTORY.equals(file.getName())) { |
| 247 | |
| 248 | ImportFileInfo info = new ImportFileInfo(file, file.getName(), overwriteAll); |
| 249 | String traceName = info.getLocalTraceName(); |
| 250 | IFolder folder = traceFolder.getFolder(traceName); |
| 251 | |
| 252 | // Verify if trace directory already exists (and not overwrite) |
| 253 | if (folder.exists() && !overwriteAll) { |
| 254 | |
| 255 | // Ask user for overwrite or new name |
| 256 | IImportConfirmationDialog conf = TraceControlDialogFactory.getInstance().getImportConfirmationDialog(); |
| 257 | conf.setTraceName(traceName); |
| 258 | |
| 259 | // Don't add trace to list if dialog was cancelled. |
| 260 | if (conf.open() == Window.OK) { |
| 261 | info.setOverwrite(conf.isOverwrite()); |
| 262 | if (!conf.isOverwrite()) { |
| 263 | info.setLocalTraceName(conf.getNewTraceName()); |
| 264 | } |
| 265 | fTraces.add(info); |
| 266 | } |
| 267 | } else { |
| 268 | fTraces.add(info); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if (fTraces.isEmpty()) { |
| 274 | MessageDialog.openError(getShell(), |
| 275 | Messages.TraceControl_ImportDialogTitle, |
| 276 | Messages.TraceControl_ImportDialogNoTraceSelectedError); |
| 277 | return; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | // validation successful -> call super.okPressed() |
| 282 | super.okPressed(); |
| 283 | } |
| 284 | |
| 285 | // ------------------------------------------------------------------------ |
| 286 | // Helper methods and classes |
| 287 | // ------------------------------------------------------------------------ |
| 288 | |
| 289 | /** |
| 290 | * Helper class for the contents of a folder in a tracing project |
| 291 | * |
| 292 | * @author Bernd Hufmann |
| 293 | */ |
| 294 | public static class FolderContentProvider extends WorkbenchContentProvider { |
| 295 | @Override |
| 296 | public Object[] getChildren(Object o) { |
| 297 | if (o instanceof IRemoteFile) { |
| 298 | IRemoteFile element = (IRemoteFile) o; |
| 299 | // For our purpose, we need folders + files |
| 300 | if (!element.isDirectory()) { |
| 301 | return new Object[0]; |
| 302 | } |
| 303 | } |
| 304 | return super.getChildren(o); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Creates a dialog composite with an error message which can be used |
| 310 | * when an exception occurred during creation time of the dialog box. |
| 311 | * @param parent - a parent composite |
| 312 | * @param e - a error causing exception |
| 313 | */ |
| 314 | private void createErrorComposite(Composite parent, Throwable e) { |
| 315 | fIsError = true; |
| 316 | fDialogComposite.dispose(); |
| 317 | |
| 318 | fDialogComposite = new Composite(parent, SWT.NONE); |
| 319 | GridLayout layout = new GridLayout(1, true); |
| 320 | fDialogComposite.setLayout(layout); |
| 321 | fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 322 | |
| 323 | Text errorText = new Text(fDialogComposite, SWT.MULTI); |
| 324 | StringBuffer error = new StringBuffer(); |
| 325 | error.append(Messages.TraceControl_ImportDialogCreationError); |
| 326 | error.append(System.getProperty("line.separator")); //$NON-NLS-1$ |
| 327 | error.append(System.getProperty("line.separator")); //$NON-NLS-1$ |
| 328 | error.append(e.toString()); |
| 329 | errorText.setText(error.toString()); |
| 330 | errorText.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 331 | } |
| 332 | |
| 333 | private void createRemoteComposite() throws CoreException, SystemMessageException{ |
| 334 | Group contextGroup = new Group(fDialogComposite, SWT.SHADOW_NONE); |
| 335 | contextGroup.setText(Messages.TraceControl_ImportDialogTracesGroupName); |
| 336 | GridLayout layout = new GridLayout(1, true); |
| 337 | contextGroup.setLayout(layout); |
| 338 | contextGroup.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 339 | |
| 340 | IRemoteSystemProxy proxy = fSession.getTargetNode().getRemoteSystemProxy(); |
| 341 | |
| 342 | IFileServiceSubSystem fsss = proxy.getFileServiceSubSystem(); |
| 343 | |
| 344 | IRemoteFile remoteFolder = fsss.getRemoteFileObject(fSession.getSessionPath(), new NullProgressMonitor()); |
| 345 | |
| 346 | fFolderViewer = new CheckboxTreeViewer(contextGroup, SWT.BORDER); |
| 347 | GridData data = new GridData(GridData.FILL_BOTH); |
| 348 | Tree tree = fFolderViewer.getTree(); |
| 349 | tree.setLayoutData(data); |
| 350 | tree.setFont(fDialogComposite.getFont()); |
| 351 | tree.setToolTipText(Messages.TraceControl_ImportDialogTracesTooltip); |
| 352 | |
| 353 | fFolderViewer.setContentProvider(new FolderContentProvider()); |
| 354 | fFolderViewer.setLabelProvider(new WorkbenchLabelProvider()); |
| 355 | |
| 356 | fFolderViewer.addCheckStateListener(new ICheckStateListener() { |
| 357 | @Override |
| 358 | public void checkStateChanged(CheckStateChangedEvent event) { |
| 359 | Object elem = event.getElement(); |
| 360 | if (elem instanceof IRemoteFile) { |
| 361 | IRemoteFile element = (IRemoteFile) elem; |
| 362 | if (!element.isDirectory()) { |
| 363 | // A trick to keep selection of a file in sync with the directory |
| 364 | boolean p = fFolderViewer.getChecked((element.getParentRemoteFile())); |
| 365 | fFolderViewer.setChecked(element, p); |
| 366 | return; |
| 367 | } |
| 368 | fFolderViewer.setSubtreeChecked(event.getElement(), event.getChecked()); |
| 369 | if (!event.getChecked()) { |
| 370 | fFolderViewer.setChecked(element.getParentRemoteFile(), false); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | }); |
| 375 | fFolderViewer.setInput(remoteFolder); |
| 376 | |
| 377 | Group projectGroup = new Group(fDialogComposite, SWT.SHADOW_NONE); |
| 378 | projectGroup.setText(Messages.TraceControl_ImportDialogProjectsGroupName); |
| 379 | layout = new GridLayout(1, true); |
| 380 | projectGroup.setLayout(layout); |
| 381 | projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
| 382 | |
| 383 | fProjects = new ArrayList<IProject>(); |
| 384 | List<String> projectNames = new ArrayList<String>(); |
| 385 | for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) { |
| 386 | if (project.isOpen() && project.hasNature(TmfProjectNature.ID)) { |
| 387 | fProjects.add(project); |
| 388 | projectNames.add(project.getName()); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | fCombo = new CCombo(projectGroup, SWT.READ_ONLY); |
| 393 | fCombo.setToolTipText(Messages.TraceControl_ImportDialogProjectsTooltip); |
| 394 | fCombo.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 1, 1)); |
| 395 | fCombo.setItems(projectNames.toArray(new String[projectNames.size()])); |
| 396 | |
| 397 | Group overrideGroup = new Group(fDialogComposite, SWT.SHADOW_NONE); |
| 398 | layout = new GridLayout(1, true); |
| 399 | overrideGroup.setLayout(layout); |
| 400 | overrideGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
| 401 | |
| 402 | fOverwriteButton = new Button(overrideGroup, SWT.CHECK); |
| 403 | fOverwriteButton.setText(Messages.TraceControl_ImportDialogOverwriteButtonText); |
| 404 | getShell().setMinimumSize(new Point(500, 400)); |
| 405 | } |
| 406 | |
| 407 | private void createLocalComposite() throws CoreException { |
| 408 | |
| 409 | Group projectGroup = new Group(fDialogComposite, SWT.SHADOW_NONE); |
| 410 | projectGroup.setText(Messages.TraceControl_ImportDialogProjectsGroupName); |
| 411 | GridLayout layout = new GridLayout(1, true); |
| 412 | projectGroup.setLayout(layout); |
| 413 | projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
| 414 | |
| 415 | fProjects = new ArrayList<IProject>(); |
| 416 | List<String> projectNames = new ArrayList<String>(); |
| 417 | for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) { |
| 418 | if (project.isOpen() && project.hasNature(TmfProjectNature.ID)) { |
| 419 | fProjects.add(project); |
| 420 | projectNames.add(project.getName()); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | fCombo = new CCombo(projectGroup, SWT.READ_ONLY); |
| 425 | fCombo.setToolTipText(Messages.TraceControl_ImportDialogProjectsTooltip); |
| 426 | fCombo.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 1, 1)); |
| 427 | fCombo.setItems(projectNames.toArray(new String[projectNames.size()])); |
| 428 | |
| 429 | // Group overrideGroup = new Group(fDialogComposite, SWT.SHADOW_NONE); |
| 430 | // layout = new GridLayout(1, true); |
| 431 | // overrideGroup.setLayout(layout); |
| 432 | // overrideGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
| 433 | // |
| 434 | // fOverwriteButton = new Button(overrideGroup, SWT.CHECK); |
| 435 | // fOverwriteButton.setText(Messages.TraceControl_ImportDialogOverwriteButtonText); |
| 436 | |
| 437 | getShell().setMinimumSize(new Point(500, 50)); |
| 438 | } |
| 439 | } |