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