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