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